1
|
import { Injectable } from '@angular/core';
|
2
|
import {
|
3
|
CanActivate,
|
4
|
ActivatedRouteSnapshot,
|
5
|
RouterStateSnapshot,
|
6
|
UrlTree, Router
|
7
|
} from '@angular/router';
|
8
|
import { Observable } from 'rxjs';
|
9
|
import { AuthService } from '../services/auth.service';
|
10
|
|
11
|
@Injectable({
|
12
|
providedIn: 'root'
|
13
|
})
|
14
|
export class RoleGuard implements CanActivate {
|
15
|
|
16
|
constructor(
|
17
|
private authService: AuthService,
|
18
|
private router: Router,
|
19
|
) {}
|
20
|
|
21
|
canActivate(
|
22
|
routeSnapshot: ActivatedRouteSnapshot,
|
23
|
state: RouterStateSnapshot
|
24
|
): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
|
25
|
const expectedRole: string[] = routeSnapshot.data.expectedRole;
|
26
|
if (expectedRole.includes(this.authService.getUser().userInfo?.rightsId.toString())) {
|
27
|
console.log('You have enough rights!');
|
28
|
return true;
|
29
|
}
|
30
|
console.log('You are not allowed to be here!');
|
31
|
this.router.navigate(['']);
|
32
|
return false;
|
33
|
}
|
34
|
}
|