2019-06-03 15:20:36 +02:00
|
|
|
import {Injectable} from '@angular/core';
|
|
|
|
import {
|
|
|
|
Router,
|
|
|
|
CanActivate,
|
|
|
|
ActivatedRouteSnapshot,
|
|
|
|
RouterStateSnapshot,
|
|
|
|
CanLoad,
|
|
|
|
Route,
|
|
|
|
UrlSegment
|
|
|
|
} from '@angular/router';
|
|
|
|
import {Observable} from 'rxjs';
|
2017-12-19 13:53:46 +01:00
|
|
|
import {Session} from './utils/helper.class';
|
2018-11-01 18:20:05 +01:00
|
|
|
import {LoginErrorCodes} from './utils/guardHelper.class';
|
2019-09-13 09:01:19 +02:00
|
|
|
import {UserManagementService} from "../services/user-management.service";
|
|
|
|
import {EnvironmentSpecificService} from "../utils/properties/environment-specific.service";
|
|
|
|
import {filter, map, mergeMap} from "rxjs/operators";
|
2018-02-05 14:14:59 +01:00
|
|
|
|
2017-12-19 13:53:46 +01:00
|
|
|
@Injectable()
|
2019-06-03 15:20:36 +02:00
|
|
|
export class AdminLoginGuard implements CanActivate{
|
2017-12-19 13:53:46 +01:00
|
|
|
|
2019-09-13 09:01:19 +02:00
|
|
|
constructor(private router: Router,
|
|
|
|
private userManagementService: UserManagementService,
|
|
|
|
private propertiesService: EnvironmentSpecificService) {
|
2019-02-26 16:25:52 +01:00
|
|
|
}
|
2017-12-19 13:53:46 +01:00
|
|
|
|
2019-09-13 09:01:19 +02:00
|
|
|
check(path: string): Observable<boolean> {
|
|
|
|
let errorCode = LoginErrorCodes.NOT_LOGIN;
|
|
|
|
const obs = this.propertiesService.subscribeEnvironment().pipe(mergeMap(properties => {
|
|
|
|
return this.userManagementService.getUserInfo(properties['userInfoUrl']).pipe(map( user => {
|
|
|
|
if(user) {
|
|
|
|
errorCode = LoginErrorCodes.NOT_ADMIN;
|
|
|
|
}
|
|
|
|
return Session.isPortalAdministrator(user);
|
|
|
|
}));
|
|
|
|
}));
|
|
|
|
obs.pipe(filter( isAdmin => !isAdmin)).subscribe( () => {
|
|
|
|
this.router.navigate(['/user-info'], {
|
|
|
|
queryParams: {
|
|
|
|
'errorCode': errorCode,
|
|
|
|
'redirectUrl': path
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
return obs;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check(path: string): boolean {
|
2019-01-25 12:18:38 +01:00
|
|
|
let loggedIn = false;
|
|
|
|
let isAdmin = false;
|
|
|
|
let errorCode = LoginErrorCodes.NOT_LOGIN;
|
2019-02-26 16:25:52 +01:00
|
|
|
if (Session.isLoggedIn()) {
|
2017-12-19 13:53:46 +01:00
|
|
|
loggedIn = true;
|
2019-01-25 12:18:38 +01:00
|
|
|
isAdmin = Session.isPortalAdministrator();
|
2019-02-26 16:25:52 +01:00
|
|
|
if (!isAdmin) {
|
2019-01-25 12:18:38 +01:00
|
|
|
errorCode = LoginErrorCodes.NOT_ADMIN;
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
|
|
|
}
|
2019-02-26 16:25:52 +01:00
|
|
|
if (!loggedIn || !isAdmin) {
|
2019-06-03 15:20:36 +02: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;
|
2019-09-13 09:01:19 +02:00
|
|
|
}*/
|
2019-01-25 12:18:38 +01:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|