openaire-library/connect/communityGuard/connectAdminLoginGuard.guar...

37 lines
1.7 KiB
TypeScript
Raw Normal View History

import { Injectable } from '@angular/core';
import { Router,CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot} from '@angular/router';
import {Observable} from 'rxjs/Observable';
import {Session} from '../../login/utils/helper.class';
import {LoginErrorCodes} from '../../login/utils/guardHelper.class';
import {CommunityService} from '../community/community.service';
import { EnvironmentSpecificService} from '../../utils/properties/environment-specific.service';
import { mergeMap } from 'rxjs/operators';
@Injectable()
export class ConnectAdminLoginGuard implements CanActivate {
constructor(private router: Router, private communityService: CommunityService, private propertiesService:EnvironmentSpecificService ) {}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
let errorCode = LoginErrorCodes.NOT_LOGIN;
let community = (route.queryParams["communityId"]);
if(Session.isLoggedIn()){
if(Session.isPortalAdministrator() || Session.isCommunityCurator()) {
return true;
} else {
let obs = this.propertiesService.subscribeEnvironment().map(res=>res).mergeMap(properties => {
return this.communityService.iscommunityManager(properties, properties["communityAPI"]+community,Session.getUserEmail())});
obs.filter(enabled => !enabled)
.subscribe(() => this.router.navigate(['/user-info'], { queryParams: { "errorCode": errorCode, "redirectUrl": state.url } }));
return obs;
}
} else{
errorCode =LoginErrorCodes.NOT_LOGIN;
this.router.navigate(['/user-info'], { queryParams: { "errorCode": errorCode, "redirectUrl": state.url } });
return false;
}
}
}