1
|
import {Injectable} from '@angular/core';
|
2
|
import {ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot} from '@angular/router';
|
3
|
import {Observable} from 'rxjs/Observable';
|
4
|
|
5
|
import {map, take, tap} from "rxjs/operators";
|
6
|
import {UserManagementService} from "../openaireLibrary/services/user-management.service";
|
7
|
import {LoginErrorCodes} from "../openaireLibrary/login/utils/guardHelper.class";
|
8
|
import {Session} from "../openaireLibrary/login/utils/helper.class";
|
9
|
import {StakeholderService} from "../openaireLibrary/monitor/services/stakeholder.service";
|
10
|
import {zip} from "rxjs";
|
11
|
|
12
|
|
13
|
@Injectable()
|
14
|
export class AdminDashboardGuard implements CanActivate {
|
15
|
|
16
|
constructor(private router: Router,
|
17
|
private stakeholderService: StakeholderService,
|
18
|
private userManagementService: UserManagementService) {
|
19
|
}
|
20
|
|
21
|
check(path: string, alias: string): Observable<boolean> | boolean {
|
22
|
if (Session.isLoggedIn()) {
|
23
|
return zip(
|
24
|
this.userManagementService.getUserInfo(false)
|
25
|
,this.stakeholderService.getStakeholder(alias)
|
26
|
).pipe(take(1),map(res =>{
|
27
|
return res[0] && res[1] && (Session.isPortalAdministrator(res[0]) ||
|
28
|
Session.isCurator(res[1].type, res[0]) || Session.isManager(res[1].type, res[1].alias, res[0]));
|
29
|
}),tap(authorized => {
|
30
|
if(!authorized){
|
31
|
this.router.navigate(['/user-info'], {queryParams: {'errorCode': LoginErrorCodes.NOT_ADMIN, 'redirectUrl':path}});
|
32
|
}
|
33
|
}));
|
34
|
|
35
|
} else {
|
36
|
this.router.navigate(['/user-info'], {queryParams: {'errorCode': LoginErrorCodes.NOT_LOGIN, 'redirectUrl':path}});
|
37
|
return false;
|
38
|
}
|
39
|
}
|
40
|
|
41
|
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
|
42
|
return this.check(state.url, route.params.stakeholder);
|
43
|
}
|
44
|
}
|