openaire-library/login/adminLoginGuard.guard.ts

48 lines
1.1 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';
@Injectable()
export class AdminLoginGuard implements CanActivate{
constructor(private router: Router) {
}
check(path: string): boolean {
let loggedIn = false;
let isAdmin = false;
let errorCode = LoginErrorCodes.NOT_LOGIN;
if (Session.isLoggedIn()) {
loggedIn = true;
isAdmin = Session.isPortalAdministrator();
if (!isAdmin) {
errorCode = LoginErrorCodes.NOT_ADMIN;
}
}
if (!loggedIn || !isAdmin) {
this.router.navigate(['/user-info'], {
queryParams: {
'errorCode': errorCode,
'redirectUrl': path
}
});
}
return loggedIn && isAdmin;
}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
return this.check(state.url);
}
}