openaire-library/connect/communityGuard/isCommunityOrAdmin.ts

63 lines
2.1 KiB
TypeScript
Raw Normal View History

import {Injectable} from '@angular/core';
import {ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot} from '@angular/router';
import {Observable} from 'rxjs';
import {filter, map, mergeMap, take} 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";
@Injectable()
export class IsCommunityOrAdmin implements CanActivate {
constructor(private router: Router,
private userManagementService: UserManagementService,
private propertiesService: EnvironmentSpecificService) {
}
/*
//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;
}
}));
}
*/
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(false).pipe(map(user => {
return Session.isPortalAdministrator(user);
}));
}));
obs.pipe(filter( isAdmin => !isAdmin)).subscribe( () => {
this.router.navigate(['/errorCommunity']);
});
return obs;
}
}
/*
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;
}
}*/
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
return this.check(route.queryParams['communityId']);
}
}