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

57 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';
@Injectable()
export class ConnectAdminLoginGuard implements CanActivate, CanLoad{
constructor(private router: Router,
private communityService: CommunityService,
private propertiesService: EnvironmentSpecificService) {}
check(route: ActivatedRouteSnapshot, path: string): Observable<boolean> | boolean {
let errorCode = LoginErrorCodes.NOT_LOGIN;
const community = route.queryParams['communityId'];
if (Session.isLoggedIn()) {
if (Session.isPortalAdministrator() || Session.isCommunityCurator()) {
return true;
} else {
const 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(route, state.url);
}
canLoad(route: Route): Observable<boolean> | Promise<boolean> | boolean {
return this.check(null, '/' + route.path);
}
}