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