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

73 lines
3.1 KiB
TypeScript

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 {
//console.log(state.url);
var user;
var loggedIn = false;
var isAdmin = false;
var errorCode = LoginErrorCodes.NOT_LOGIN;
let community = (route.queryParams["communityId"]);
if(Session.isLoggedIn()){
loggedIn = true;
if(!Session.isLoggedIn()){
loggedIn = false;
errorCode = LoginErrorCodes.NOT_VALID;
this.router.navigate(['/user-info'], { queryParams: { "errorCode": errorCode, "redirectUrl": state.url } });
return false;
}else if(Session.isPortalAdministrator() || Session.isCommunityCurator()) {
//console.log("is Admin");
isAdmin = true;
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;
}
}
/*
constructor(private route: ActivatedRoute,private router: Router, private config: ConfigurationService) {}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
let customRedirect = route.data['redirect'];
let api = route.data['api'];
let community = route.data['community']
if(!community){
community = (route.queryParams["communityId"])?route.queryParams["communityId"]:route.queryParams["community"];
}
if(community){
let isEnabled = this.config.isPageEnabled(api, community,"/"+state.url.split("?")[0].substring(1));
let redirect = !!customRedirect ? customRedirect : '/error';
isEnabled.filter(enabled => !enabled)
.subscribe(() => this.router.navigate([redirect], { queryParams: { "page": state.url } }));
return isEnabled;
}
return true;
}
*/
}