2019-09-13 09:01:19 +02:00
|
|
|
import {Injectable} from '@angular/core';
|
|
|
|
import {ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot} from '@angular/router';
|
2019-07-12 15:16:30 +02:00
|
|
|
import {Observable} from 'rxjs';
|
2020-11-12 16:41:03 +01:00
|
|
|
import {filter, map, mergeMap, take} from "rxjs/operators";
|
2019-09-13 09:01:19 +02:00
|
|
|
import {UserManagementService} from "../../services/user-management.service";
|
|
|
|
import {EnvironmentSpecificService} from "../../utils/properties/environment-specific.service";
|
|
|
|
import {Session} from "../../login/utils/helper.class";
|
2019-07-12 15:16:30 +02:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class IsCommunityOrAdmin implements CanActivate {
|
|
|
|
|
2019-09-13 09:01:19 +02:00
|
|
|
constructor(private router: Router,
|
|
|
|
private userManagementService: UserManagementService,
|
|
|
|
private propertiesService: EnvironmentSpecificService) {
|
2019-07-12 15:16:30 +02:00
|
|
|
}
|
2020-11-12 16:41:03 +01:00
|
|
|
/*
|
|
|
|
//TODO add login guard and simplify this method
|
|
|
|
check(community: string): Observable<boolean> | boolean {
|
|
|
|
return this.userManagementService.getUserInfo(false).pipe(take(1),map(user => {
|
|
|
|
if (community && community !== 'undefined' || Session.isPortalAdministrator(user)) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
this.router.navigate(['/errorCommunity']);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
*/
|
2019-09-13 09:01:19 +02:00
|
|
|
check(community: string): Observable<boolean> | boolean {
|
|
|
|
if (community && community !== 'undefined') {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
const obs = this.propertiesService.subscribeEnvironment().pipe(mergeMap(properties => {
|
2020-07-02 17:02:14 +02:00
|
|
|
return this.userManagementService.getUserInfo(false).pipe(map(user => {
|
2019-09-13 09:01:19 +02:00
|
|
|
return Session.isPortalAdministrator(user);
|
|
|
|
}));
|
|
|
|
}));
|
|
|
|
obs.pipe(filter( isAdmin => !isAdmin)).subscribe( () => {
|
|
|
|
this.router.navigate(['/errorCommunity']);
|
|
|
|
});
|
|
|
|
return obs;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
2019-07-12 15:16:30 +02:00
|
|
|
check(community: string): Observable<boolean> | boolean {
|
|
|
|
if(Session.isLoggedIn() && Session.isPortalAdministrator()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (community && community !== 'undefined') {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
this.router.navigate(['errorcommunity']);
|
|
|
|
return false;
|
|
|
|
}
|
2019-09-13 09:01:19 +02:00
|
|
|
}*/
|
2019-07-12 15:16:30 +02:00
|
|
|
|
|
|
|
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
|
|
|
|
return this.check(route.queryParams['communityId']);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2019-09-13 09:01:19 +02:00
|
|
|
|