-
Notifications
You must be signed in to change notification settings - Fork 1
/
data-explorer-guard.ts
40 lines (37 loc) · 1.51 KB
/
data-explorer-guard.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, Router } from '@angular/router';
import { Observable } from "rxjs";
import { map } from "rxjs/operators";
import { Injectable } from '@angular/core';
import { AlertService } from '../../services/alert.service';
import { AuthenticationService } from 'src/app/services/authentication.service';
import { RoleEnum } from '../../models/enums/role.enum';
@Injectable({
providedIn: 'root'
})
export class DataExplorerGuard implements CanActivate {
constructor(private router: Router, private alertService: AlertService, private authenticationService: AuthenticationService) {
}
canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
if (!this.authenticationService.isCurrentUserNullOrUndefined()) {
if (this.authenticationService.isCurrentUserInRole([RoleEnum.Admin, RoleEnum.DataExplorer])) {
return true;
} else {
this.alertService.pushNotFoundUnauthorizedAlert();
this.router.navigate(["/"]);
return false;
}
}
return this.authenticationService.currentUserSetObservable
.pipe(
map(x => {
if (x.Role.RoleID == RoleEnum.Admin || x.Role.RoleID == RoleEnum.DataExplorer) {
return true;
} else {
this.alertService.pushNotFoundUnauthorizedAlert();
this.router.navigate(["/"]);
return false;
}
})
);
}
}