openaire-library/error/isRouteEnabled.guard.ts

36 lines
1.7 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 { EnvironmentSpecificService} from '../utils/properties/environment-specific.service';
import {ConnectHelper} from '../connect/connectHelper';
@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){
community = ConnectHelper.getCommunityFromDomain(document.location.hostname);
}
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;
}
}