1
|
import {Injectable} from '@angular/core';
|
2
|
import {
|
3
|
ActivatedRouteSnapshot,
|
4
|
CanActivate,
|
5
|
CanActivateChild,
|
6
|
Router,
|
7
|
RouterStateSnapshot,
|
8
|
UrlTree
|
9
|
} from '@angular/router';
|
10
|
import {Observable} from 'rxjs';
|
11
|
import {Session} from './utils/helper.class';
|
12
|
import {LoginErrorCodes} from './utils/guardHelper.class';
|
13
|
import {UserManagementService} from "../services/user-management.service";
|
14
|
import {map, tap} from "rxjs/operators";
|
15
|
|
16
|
@Injectable()
|
17
|
export class AdminLoginGuard implements CanActivate, CanActivateChild {
|
18
|
|
19
|
constructor(private router: Router,
|
20
|
private userManagementService: UserManagementService) {
|
21
|
}
|
22
|
|
23
|
check(path: string): Observable<boolean> {
|
24
|
let errorCode = LoginErrorCodes.NOT_LOGIN;
|
25
|
return this.userManagementService.getUserInfo(false).pipe(map(user => {
|
26
|
if (user) {
|
27
|
errorCode = LoginErrorCodes.NOT_ADMIN;
|
28
|
}
|
29
|
return Session.isPortalAdministrator(user);
|
30
|
}),tap(isAdmin => {
|
31
|
if(!isAdmin) {
|
32
|
this.router.navigate(['/user-info'], {
|
33
|
queryParams: {
|
34
|
'errorCode': errorCode,
|
35
|
'redirectUrl': path
|
36
|
}
|
37
|
});
|
38
|
}
|
39
|
}));
|
40
|
|
41
|
}
|
42
|
|
43
|
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
|
44
|
return this.check(state.url);
|
45
|
}
|
46
|
|
47
|
canActivateChild(childRoute: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
|
48
|
return this.check(state.url);
|
49
|
}
|
50
|
|
51
|
}
|