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

59 lines
2.1 KiB
TypeScript
Raw Normal View History

import { Injectable } from '@angular/core';
import {
Router,
CanActivate,
ActivatedRouteSnapshot,
RouterStateSnapshot,
ActivatedRoute,
CanLoad, Route
} 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, CanLoad{
constructor(private router: Router,
private communityService: CommunityService,
private propertiesService:EnvironmentSpecificService,
private route: ActivatedRoute) {}
check(path: string): Observable<boolean> | boolean {
let errorCode = LoginErrorCodes.NOT_LOGIN;
let community = this.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": path
}
}));
return obs;
}
} else {
errorCode = LoginErrorCodes.NOT_LOGIN;
this.router.navigate(['/user-info'], {queryParams: {"errorCode": errorCode, "redirectUrl": path}});
return false;
}
}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
return this.check(state.url);
}
canLoad(route: Route): Observable<boolean> | Promise<boolean> | boolean {
return this.check('/' + route.path);
}
}