openaire-library/connect/communityGuard/isCommunityOrAdmin.ts

51 lines
1.7 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} 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) {
}
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']);
}
}