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';
|
2019-09-13 09:01:19 +02:00
|
|
|
import {filter, map, mergeMap} from "rxjs/operators";
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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 => {
|
|
|
|
return this.userManagementService.getUserInfo(properties['userInfoUrl']).pipe(map(user => {
|
|
|
|
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
|
|
|
|