2017-12-19 13:53:46 +01:00
|
|
|
import { Injectable } from '@angular/core';
|
2019-01-25 12:18:38 +01:00
|
|
|
import {Router, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, CanLoad, Route} from '@angular/router';
|
2017-12-19 13:53:46 +01:00
|
|
|
import {Observable} from 'rxjs/Observable';
|
|
|
|
import {Session} from './utils/helper.class';
|
2018-11-01 18:20:05 +01:00
|
|
|
import {LoginErrorCodes} from './utils/guardHelper.class';
|
2018-02-05 14:14:59 +01:00
|
|
|
|
2017-12-19 13:53:46 +01:00
|
|
|
@Injectable()
|
2019-01-25 12:18:38 +01:00
|
|
|
export class AdminLoginGuard implements CanActivate, CanLoad {
|
2017-12-19 13:53:46 +01:00
|
|
|
|
|
|
|
constructor(private router: Router) {}
|
|
|
|
|
2019-01-29 14:03:31 +01:00
|
|
|
check(path: string) : boolean{
|
2019-01-25 12:18:38 +01:00
|
|
|
let loggedIn = false;
|
|
|
|
let isAdmin = false;
|
|
|
|
let errorCode = LoginErrorCodes.NOT_LOGIN;
|
2017-12-19 13:53:46 +01:00
|
|
|
if(Session.isLoggedIn()){
|
|
|
|
loggedIn = true;
|
2019-01-25 12:18:38 +01:00
|
|
|
isAdmin = Session.isPortalAdministrator();
|
|
|
|
if(!isAdmin){
|
|
|
|
errorCode = LoginErrorCodes.NOT_ADMIN;
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
|
|
|
}
|
2019-01-25 12:18:38 +01:00
|
|
|
if(!loggedIn || !isAdmin) {
|
2019-01-29 14:03:31 +01:00
|
|
|
this.router.navigate(['/user-info'], {queryParams: {"errorCode": errorCode, "redirectUrl": path}});
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
2019-01-25 12:18:38 +01:00
|
|
|
return loggedIn && isAdmin;
|
|
|
|
}
|
|
|
|
|
|
|
|
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
|
2019-01-29 14:03:31 +01:00
|
|
|
return this.check(state.url);
|
2019-01-25 12:18:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
canLoad(route: Route): Observable<boolean> | Promise<boolean> | boolean {
|
2019-01-29 14:03:31 +01:00
|
|
|
return this.check('/' + route.path);
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
|
|
|
}
|