1 |
2ddd52c9
|
hlavja
|
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 |
2c5da396
|
hlavja
|
if (expectedRole.includes(this.authService.getUser().userInfo.rights.toString())) {
|
27 |
|
|
console.log('You have enough rights!');
|
28 |
2ddd52c9
|
hlavja
|
return true;
|
29 |
|
|
}
|
30 |
2c5da396
|
hlavja
|
console.log('You are not allowed to be here!');
|
31 |
2ddd52c9
|
hlavja
|
this.router.navigate(['']);
|
32 |
|
|
return false;
|
33 |
|
|
}
|
34 |
|
|
}
|