import { Injectable } from '@angular/core'; import { Router, CanActivate, CanLoad, ActivatedRouteSnapshot, RouterStateSnapshot, Route, Data } 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, CanLoad { constructor(private router: Router, private config: ConfigurationService, private propertiesService: EnvironmentSpecificService) {} check(data: Data, community: string, path: string): Observable | boolean { const customRedirect = data['redirect']; if (!community && data['community']) { // for openaire community = data['community']; } if (!community && typeof document !== 'undefined') { community = ConnectHelper.getCommunityFromDomain(document.location.hostname); } if (community) { const redirect = customRedirect ? customRedirect : '/error'; const obs = this.propertiesService.subscribeEnvironment().map(res => res['adminToolsAPIURL']).mergeMap(url => { return this.config.isPageEnabled(url, community, '/' + path.split('?')[0].substring(1)); }); obs.filter(enabled => !enabled) .subscribe(() => this.router.navigate([redirect], {queryParams: {'page': path}})); return obs; } return true; } canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable | boolean { return this.check(route.data, route.queryParams['communityId'], state.url); } canLoad(route: Route): Observable | Promise | boolean { const path = '/' + route; return this.check(route.data, ConnectHelper.getCommunityFromPath(path), path); } }