1
|
import {Injectable} from '@angular/core';
|
2
|
import {ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot} from '@angular/router';
|
3
|
import {Observable} from 'rxjs';
|
4
|
import {filter, map, mergeMap} from "rxjs/operators";
|
5
|
import {UserManagementService} from "../../services/user-management.service";
|
6
|
import {EnvironmentSpecificService} from "../../utils/properties/environment-specific.service";
|
7
|
import {Session} from "../../login/utils/helper.class";
|
8
|
|
9
|
@Injectable()
|
10
|
export class IsCommunityOrAdmin implements CanActivate {
|
11
|
|
12
|
constructor(private router: Router,
|
13
|
private userManagementService: UserManagementService,
|
14
|
private propertiesService: EnvironmentSpecificService) {
|
15
|
}
|
16
|
|
17
|
check(community: string): Observable<boolean> | boolean {
|
18
|
if (community && community !== 'undefined') {
|
19
|
return true;
|
20
|
} else {
|
21
|
const obs = this.propertiesService.subscribeEnvironment().pipe(mergeMap(properties => {
|
22
|
return this.userManagementService.getUserInfo(properties['userInfoUrl']).pipe(map(user => {
|
23
|
return Session.isPortalAdministrator(user);
|
24
|
}));
|
25
|
}));
|
26
|
obs.pipe(filter( isAdmin => !isAdmin)).subscribe( () => {
|
27
|
this.router.navigate(['/errorCommunity']);
|
28
|
});
|
29
|
return obs;
|
30
|
}
|
31
|
}
|
32
|
/*
|
33
|
check(community: string): Observable<boolean> | boolean {
|
34
|
if(Session.isLoggedIn() && Session.isPortalAdministrator()) {
|
35
|
return true;
|
36
|
}
|
37
|
else if (community && community !== 'undefined') {
|
38
|
return true;
|
39
|
} else {
|
40
|
this.router.navigate(['errorcommunity']);
|
41
|
return false;
|
42
|
}
|
43
|
}*/
|
44
|
|
45
|
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
|
46
|
return this.check(route.queryParams['communityId']);
|
47
|
}
|
48
|
|
49
|
}
|
50
|
|