openaire-library/login/claimsCuratorGuard.guard.ts

50 lines
1.6 KiB
TypeScript
Raw Normal View History

import { Injectable } from '@angular/core';
import { Router,CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot} 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 {
constructor(private router: Router) {}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
var user;
var role = route.data['role']
var loggedIn = false;
var isAuthorized = false;
var errorCode = LoginErrorCodes.NOT_LOGGIN;
if(Session.isLoggedIn()){
loggedIn = true;
if(!Session.isLoggedIn()){
loggedIn = false;
errorCode = LoginErrorCodes.NOT_VALID;
}else {
if(Session.isClaimsCurator() || Session.isPortalAdministrator()){
isAuthorized =true;
}
if(!Session.isClaimsCurator() && !Session.isPortalAdministrator()){
errorCode = LoginErrorCodes.NOT_ADMIN;
}
}
}else{
errorCode =LoginErrorCodes.NOT_LOGGIN;
}
if(!loggedIn){
// this.guardHelper.redirect("/user-info",errorCode,state.url);
this.router.navigate(['/user-info'], { queryParams: { "errorCode": errorCode, "redirectUrl": state.url } });
return false;
}else if(!isAuthorized){
// this.guardHelper.redirect("/user-info",errorCode,state.url);
this.router.navigate(['/user-info'], { queryParams: { "errorCode": errorCode, "redirectUrl": state.url } });
return false;
}else{
return true;
}
}
}