41 lines
1.2 KiB
TypeScript
41 lines
1.2 KiB
TypeScript
import {Injectable} from '@angular/core';
|
|
import {ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot} from '@angular/router';
|
|
import {Observable} from 'rxjs';
|
|
import {Session} from './utils/helper.class';
|
|
import {LoginErrorCodes} from './utils/guardHelper.class';
|
|
import {UserManagementService} from "../services/user-management.service";
|
|
import {map, tap} from "rxjs/operators";
|
|
|
|
@Injectable()
|
|
export class AdminLoginGuard implements CanActivate {
|
|
|
|
constructor(private router: Router,
|
|
private userManagementService: UserManagementService) {
|
|
}
|
|
|
|
check(path: string): Observable<boolean> {
|
|
let errorCode = LoginErrorCodes.NOT_LOGIN;
|
|
return this.userManagementService.getUserInfo(false).pipe(map(user => {
|
|
if (user) {
|
|
errorCode = LoginErrorCodes.NOT_ADMIN;
|
|
}
|
|
return Session.isPortalAdministrator(user);
|
|
}),tap(isAdmin => {
|
|
if(!isAdmin) {
|
|
this.router.navigate(['/user-info'], {
|
|
queryParams: {
|
|
'errorCode': errorCode,
|
|
'redirectUrl': path
|
|
}
|
|
});
|
|
}
|
|
}));
|
|
|
|
}
|
|
|
|
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
|
|
return this.check(state.url);
|
|
}
|
|
|
|
}
|