2019-06-03 15:20:36 +02:00
|
|
|
import {Injectable} from '@angular/core';
|
2023-08-01 13:29:27 +02:00
|
|
|
import { ActivatedRouteSnapshot, Data, Router, RouterStateSnapshot, UrlTree } from '@angular/router';
|
2019-06-03 15:20:36 +02:00
|
|
|
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";
|
2020-11-11 15:43:13 +01:00
|
|
|
import {map, tap} from "rxjs/operators";
|
2018-02-05 14:14:59 +01:00
|
|
|
|
2023-06-26 10:31:22 +02:00
|
|
|
@Injectable({
|
|
|
|
providedIn: 'root'
|
|
|
|
})
|
2023-08-01 13:29:27 +02:00
|
|
|
export class AdminLoginGuard {
|
2020-01-29 11:20:53 +01:00
|
|
|
|
2019-09-13 09:01:19 +02:00
|
|
|
constructor(private router: Router,
|
2020-01-29 11:20:53 +01:00
|
|
|
private userManagementService: UserManagementService) {
|
2019-02-26 16:25:52 +01:00
|
|
|
}
|
2020-01-29 11:20:53 +01:00
|
|
|
|
2022-07-27 11:34:10 +02:00
|
|
|
check(data: Data, path: string): Observable<boolean> {
|
2019-09-13 09:01:19 +02:00
|
|
|
let errorCode = LoginErrorCodes.NOT_LOGIN;
|
2022-09-08 15:43:59 +02:00
|
|
|
return this.userManagementService.getUserInfo().pipe(map(user => {
|
2020-01-29 11:20:53 +01:00
|
|
|
if (user) {
|
|
|
|
errorCode = LoginErrorCodes.NOT_ADMIN;
|
|
|
|
}
|
2022-07-27 11:34:10 +02:00
|
|
|
return Session.isPortalAdministrator(user) ||
|
|
|
|
(data.monitorCurator && Session.isMonitorCurator(user)) ||
|
2022-07-29 11:15:54 +02:00
|
|
|
(data.communityCurator && Session.isCommunityCurator(user)) ||
|
|
|
|
(data.monitorManager && Session.isKindOfMonitorManager(user)) ||
|
|
|
|
(data.communityManager && Session.isKindOfCommunityManager(user))
|
2020-11-11 15:43:13 +01:00
|
|
|
}),tap(isAdmin => {
|
|
|
|
if(!isAdmin) {
|
|
|
|
this.router.navigate(['/user-info'], {
|
|
|
|
queryParams: {
|
|
|
|
'errorCode': errorCode,
|
|
|
|
'redirectUrl': path
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2019-09-13 09:01:19 +02:00
|
|
|
}));
|
2020-11-11 15:43:13 +01:00
|
|
|
|
2019-09-13 09:01:19 +02:00
|
|
|
}
|
2020-01-29 11:20:53 +01:00
|
|
|
|
2021-07-14 13:19:57 +02:00
|
|
|
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
|
2022-07-27 11:34:10 +02:00
|
|
|
return this.check(route.data, state.url);
|
2019-01-25 12:18:38 +01:00
|
|
|
}
|
2020-01-29 11:20:53 +01:00
|
|
|
|
2021-03-01 19:19:53 +01:00
|
|
|
canActivateChild(childRoute: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
|
2022-07-27 11:34:10 +02:00
|
|
|
return this.check(childRoute.data, state.url);
|
2021-03-01 19:19:53 +01:00
|
|
|
}
|
|
|
|
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|