52 lines
1.6 KiB
TypeScript
52 lines
1.6 KiB
TypeScript
import {Injectable} from '@angular/core';
|
|
import {
|
|
Router,
|
|
CanActivate,
|
|
ActivatedRouteSnapshot,
|
|
RouterStateSnapshot,
|
|
CanLoad,
|
|
Route,
|
|
UrlSegment
|
|
} from '@angular/router';
|
|
import {Observable} from 'rxjs';
|
|
import {Session} from './utils/helper.class';
|
|
import {LoginErrorCodes} from './utils/guardHelper.class';
|
|
import {filter, map, mergeMap} from "rxjs/operators";
|
|
import {EnvironmentSpecificService} from "../utils/properties/environment-specific.service";
|
|
import {UserManagementService} from "../services/user-management.service";
|
|
|
|
@Injectable()
|
|
export class ClaimsCuratorGuard implements CanActivate {
|
|
|
|
constructor(private router: Router,
|
|
private userManagementService: UserManagementService,
|
|
private propertiesService: EnvironmentSpecificService) {
|
|
}
|
|
|
|
check(path: string): Observable<boolean> |boolean {
|
|
let errorCode = LoginErrorCodes.NOT_LOGIN;
|
|
const obs = this.propertiesService.subscribeEnvironment().pipe(mergeMap(properties => {
|
|
return this.userManagementService.getUserInfo(properties['userInfoUrl']).pipe(map( user => {
|
|
if(user) {
|
|
errorCode = LoginErrorCodes.NOT_ADMIN;
|
|
}
|
|
return Session.isClaimsCurator(user) || Session.isPortalAdministrator(user);
|
|
}));
|
|
}));
|
|
obs.pipe(filter( isAdmin => !isAdmin)).subscribe( () => {
|
|
this.router.navigate(['/user-info'], {
|
|
queryParams: {
|
|
'errorCode': errorCode,
|
|
'redirectUrl': path
|
|
}
|
|
});
|
|
});
|
|
return obs;
|
|
}
|
|
|
|
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
|
|
return this.check(state.url);
|
|
}
|
|
|
|
}
|