openaire-library/login/adminLoginGuard.guard.ts

46 lines
1.5 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 {ErrorCodes} from './utils/guardHelper.class';
@Injectable()
export class AdminLoginGuard implements CanActivate {
constructor(private router: Router) {}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
var user;
var loggedIn = false;
var isAdmin = false;
var errorCode = ErrorCodes.NOT_LOGGIN;
if(Session.isLoggedIn()){
loggedIn = true;
if(!Session.isLoggedIn()){
loggedIn = false;
errorCode = ErrorCodes.NOT_VALID;
}else {
isAdmin = Session.isPortalAdministrator();
if(!isAdmin){
errorCode = ErrorCodes.NOT_ADMIN;
}
}
}else{
errorCode =ErrorCodes.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(!isAdmin){
// this.guardHelper.redirect("/user-info",errorCode,state.url);
this.router.navigate(['/user-info'], { queryParams: { "errorCode": errorCode, "redirectUrl": state.url } });
return false;
}else{
return true;
}
}
}