openaire-library/error/isRouteEnabled.guard.ts

34 lines
1.6 KiB
TypeScript

import { Injectable } from '@angular/core';
import {ActivatedRoute, Router,CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot} from '@angular/router';
import {Observable} from 'rxjs/Observable';
import 'rxjs/add/operator/filter';
import { ConfigurationService } from '../utils/configuration/configuration.service';
import{EnvProperties} from '../utils/properties/env-properties';
import { EnvironmentSpecificService} from '../utils/properties/environment-specific.service';
@Injectable()
export class IsRouteEnabled implements CanActivate {
constructor(private route: ActivatedRoute,private router: Router, private config: ConfigurationService, private propertiesService:EnvironmentSpecificService ) {}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
let customRedirect = route.data['redirect'];
let community = route.queryParams["communityId"];
if(!community && route.data['community']){ // for openaire
let community = route.data['community']
}
if(community){
let redirect = !!customRedirect ? customRedirect : '/error';
let obs = this.propertiesService.subscribeEnvironment().map(res=>res["adminToolsAPIURL"]).mergeMap(url => {
return this.config.isPageEnabled(url, community,"/"+state.url.split("?")[0].substring(1))});
obs.filter(enabled => !enabled)
.subscribe(() => this.router.navigate([redirect], { queryParams: { "page": state.url } }));
return obs;
}
return true;
}
}