openaire-library/login/claimsCuratorGuard.guard.ts

43 lines
1.4 KiB
TypeScript

import { Injectable } from '@angular/core';
import {Router, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, CanLoad, Route} from '@angular/router';
import {Observable} from 'rxjs/Observable';
import {Session} from './utils/helper.class';
import {LoginErrorCodes} from './utils/guardHelper.class';
@Injectable()
export class ClaimsCuratorGuard implements CanActivate, CanLoad {
constructor(private router: Router) {}
check(path: string) : boolean{
//var user;
//var role = route.data['role']
let loggedIn = false;
let isAuthorized = false;
let errorCode = LoginErrorCodes.NOT_LOGIN;
if(Session.isLoggedIn()){
loggedIn = true;
if(Session.isClaimsCurator() || Session.isPortalAdministrator()){
isAuthorized = true;
}
else {
errorCode = LoginErrorCodes.NOT_ADMIN;
}
}
if(!loggedIn || !isAuthorized){
// this.guardHelper.redirect("/user-info",errorCode,state.url);
this.router.navigate(['/user-info'], {queryParams: {"errorCode": errorCode, "redirectUrl": path}});
}
return loggedIn && isAuthorized;
}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
return this.check(state.url);
}
canLoad(route: Route): Observable<boolean> | Promise<boolean> | boolean {
return this.check('/' + route.path);
}
}