1
|
import {map, filter, mergeMap, take} from 'rxjs/operators';
|
2
|
import {Injectable} from '@angular/core';
|
3
|
import {
|
4
|
Router,
|
5
|
CanActivate,
|
6
|
ActivatedRouteSnapshot,
|
7
|
RouterStateSnapshot,
|
8
|
CanLoad, Route, UrlSegment
|
9
|
} from '@angular/router';
|
10
|
import {Observable, of, Subscription} from 'rxjs';
|
11
|
import {Session} from '../../login/utils/helper.class';
|
12
|
import {LoginErrorCodes} from '../../login/utils/guardHelper.class';
|
13
|
import {CommunityService} from '../community/community.service';
|
14
|
import {EnvironmentSpecificService} from '../../utils/properties/environment-specific.service';
|
15
|
import {ConnectHelper} from '../connectHelper';
|
16
|
import {StringUtils} from '../../utils/string-utils.class';
|
17
|
import {UserManagementService} from "../../services/user-management.service";
|
18
|
import {properties} from "../../../../environments/environment";
|
19
|
|
20
|
@Injectable()
|
21
|
export class ConnectAdminLoginGuard implements CanActivate, CanLoad {
|
22
|
sub: Subscription = null;
|
23
|
|
24
|
constructor(private router: Router,
|
25
|
private communityService: CommunityService,
|
26
|
private propertiesService: EnvironmentSpecificService,
|
27
|
private userManagementService: UserManagementService) {
|
28
|
}
|
29
|
|
30
|
check(community: string, path: string): Observable<boolean> | boolean {
|
31
|
let errorCode = LoginErrorCodes.NOT_LOGIN;
|
32
|
let email = null;
|
33
|
const authorized = this.userManagementService.getUserInfo(false).pipe(take(1),map(user => {
|
34
|
if (user) {
|
35
|
email = user.email;
|
36
|
if (Session.isPortalAdministrator(user) || Session.isCommunityCurator(user) || Session.isManager('community', community, user)) {
|
37
|
return of(true);
|
38
|
} else {
|
39
|
errorCode = LoginErrorCodes.NOT_ADMIN;
|
40
|
return this.communityService.isCommunityManagerByState(properties, properties['communityAPI'] + community,
|
41
|
email).pipe(take(1));
|
42
|
}
|
43
|
} else {
|
44
|
return of(false);
|
45
|
}
|
46
|
}), mergeMap( authorized => {
|
47
|
return authorized;
|
48
|
}));
|
49
|
//}));
|
50
|
this.sub = authorized.pipe(filter(authorized => !authorized)).subscribe(() => {
|
51
|
this.router.navigate(['/user-info'], {
|
52
|
queryParams: {
|
53
|
'errorCode': errorCode,
|
54
|
'redirectUrl': path
|
55
|
}
|
56
|
})});
|
57
|
return authorized;
|
58
|
}
|
59
|
|
60
|
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
|
61
|
return this.check(route.queryParams['communityId'], state.url);
|
62
|
}
|
63
|
|
64
|
canLoad(route: Route, segments: UrlSegment[]): Observable<boolean> | Promise<boolean> | boolean {
|
65
|
const path = StringUtils.URLSegmentsToPath(segments) + document.location.search;
|
66
|
return this.check(ConnectHelper.getCommunityFromPath(path), path);
|
67
|
}
|
68
|
|
69
|
canDeactivate() {
|
70
|
if(this.sub) {
|
71
|
this.sub.unsubscribe();
|
72
|
}
|
73
|
return true;
|
74
|
}
|
75
|
}
|