1
|
import {Injectable} from '@angular/core';
|
2
|
import {
|
3
|
Router,
|
4
|
CanActivate,
|
5
|
ActivatedRouteSnapshot,
|
6
|
RouterStateSnapshot,
|
7
|
CanLoad,
|
8
|
Route,
|
9
|
UrlSegment
|
10
|
} from '@angular/router';
|
11
|
import {Observable} from 'rxjs';
|
12
|
import {Session} from './utils/helper.class';
|
13
|
import {LoginErrorCodes} from './utils/guardHelper.class';
|
14
|
import {UserManagementService} from "../services/user-management.service";
|
15
|
import {EnvironmentSpecificService} from "../utils/properties/environment-specific.service";
|
16
|
import {filter, map, mergeMap} from "rxjs/operators";
|
17
|
|
18
|
@Injectable()
|
19
|
export class AdminLoginGuard implements CanActivate{
|
20
|
|
21
|
constructor(private router: Router,
|
22
|
private userManagementService: UserManagementService,
|
23
|
private propertiesService: EnvironmentSpecificService) {
|
24
|
}
|
25
|
|
26
|
check(path: string): Observable<boolean> {
|
27
|
let errorCode = LoginErrorCodes.NOT_LOGIN;
|
28
|
const obs = this.propertiesService.subscribeEnvironment().pipe(mergeMap(properties => {
|
29
|
return this.userManagementService.getUserInfo(properties['userInfoUrl']).pipe(map( user => {
|
30
|
if(user) {
|
31
|
errorCode = LoginErrorCodes.NOT_ADMIN;
|
32
|
}
|
33
|
return Session.isPortalAdministrator(user);
|
34
|
}));
|
35
|
}));
|
36
|
obs.pipe(filter( isAdmin => !isAdmin)).subscribe( () => {
|
37
|
this.router.navigate(['/user-info'], {
|
38
|
queryParams: {
|
39
|
'errorCode': errorCode,
|
40
|
'redirectUrl': path
|
41
|
}
|
42
|
});
|
43
|
});
|
44
|
return obs;
|
45
|
}
|
46
|
|
47
|
/* check(path: string): boolean {
|
48
|
let loggedIn = false;
|
49
|
let isAdmin = false;
|
50
|
let errorCode = LoginErrorCodes.NOT_LOGIN;
|
51
|
if (Session.isLoggedIn()) {
|
52
|
loggedIn = true;
|
53
|
isAdmin = Session.isPortalAdministrator();
|
54
|
if (!isAdmin) {
|
55
|
errorCode = LoginErrorCodes.NOT_ADMIN;
|
56
|
}
|
57
|
}
|
58
|
if (!loggedIn || !isAdmin) {
|
59
|
this.router.navigate(['/user-info'], {
|
60
|
queryParams: {
|
61
|
'errorCode': errorCode,
|
62
|
'redirectUrl': path
|
63
|
}
|
64
|
});
|
65
|
}
|
66
|
return loggedIn && isAdmin;
|
67
|
}*/
|
68
|
|
69
|
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
|
70
|
return this.check(state.url);
|
71
|
}
|
72
|
|
73
|
}
|