1
|
import {Injectable} from '@angular/core';
|
2
|
import {ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot} from '@angular/router';
|
3
|
import {Observable} from 'rxjs';
|
4
|
import {Session} from './utils/helper.class';
|
5
|
import {LoginErrorCodes} from './utils/guardHelper.class';
|
6
|
import {map, tap} from "rxjs/operators";
|
7
|
import {UserManagementService} from "../services/user-management.service";
|
8
|
|
9
|
@Injectable()
|
10
|
export class ClaimsCuratorGuard implements CanActivate {
|
11
|
|
12
|
constructor(private router: Router,
|
13
|
private userManagementService: UserManagementService) {
|
14
|
}
|
15
|
|
16
|
check(path: string): Observable<boolean> | boolean {
|
17
|
let errorCode = LoginErrorCodes.NOT_LOGIN;
|
18
|
return this.userManagementService.getUserInfo(false).pipe(map(user => {
|
19
|
if (user) {
|
20
|
errorCode = LoginErrorCodes.NOT_ADMIN;
|
21
|
}
|
22
|
return Session.isClaimsCurator(user) || Session.isPortalAdministrator(user);
|
23
|
}),tap(isAdmin => {
|
24
|
if(!isAdmin) {
|
25
|
this.router.navigate(['/user-info'], {
|
26
|
queryParams: {
|
27
|
'errorCode': errorCode,
|
28
|
'redirectUrl': path
|
29
|
}
|
30
|
});
|
31
|
}
|
32
|
}));
|
33
|
}
|
34
|
|
35
|
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
|
36
|
return this.check(state.url);
|
37
|
}
|
38
|
|
39
|
}
|