openaire-library/connect/communityGuard/connectRIGuard.guard.ts

73 lines
2.9 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 ConnectRIGuard 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_LOGGIN;
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()) {
isAdmin = true;
return true;
}else {
let obs = this.propertiesService.subscribeEnvironment().mergeMap(properties => {
return this.communityService.iscommunityRI(properties, properties["communityAPI"]+community)});
obs.filter(enabled => !enabled)
.subscribe(() => this.router.navigate(['/user-info'], { queryParams: { "errorCode": errorCode, "redirectUrl": state.url } }));
return obs;
}
}else{
errorCode =LoginErrorCodes.NOT_LOGGIN;
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;
}
*/
}