diff --git a/error/isRouteEnabled.guard.ts b/error/isRouteEnabled.guard.ts index b230016d..b31c9bbc 100644 --- a/error/isRouteEnabled.guard.ts +++ b/error/isRouteEnabled.guard.ts @@ -1,6 +1,5 @@ import { Injectable } from '@angular/core'; import { - ActivatedRoute, Router, CanActivate, CanLoad, @@ -8,7 +7,7 @@ import { RouterStateSnapshot, Route } from '@angular/router'; -import {Observable} from 'rxjs/Observable'; +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'; @@ -17,33 +16,36 @@ import {ConnectHelper} from '../connect/connectHelper'; @Injectable() export class IsRouteEnabled implements CanActivate, CanLoad { - constructor(private route: ActivatedRoute,private router: Router, private config: ConfigurationService, private propertiesService:EnvironmentSpecificService ) {} + constructor(private router: Router, + private config: ConfigurationService, + private propertiesService: EnvironmentSpecificService ) {} - check(path: string): Observable | boolean { - let customRedirect = this.route.data['redirect']; - let community = this.route.queryParams["communityId"]; - if(!community && this.route.data['community']){ // for openaire - community = this.route.data['community']; + check(route: ActivatedRouteSnapshot, path: string): Observable | boolean { + const customRedirect = route.data['redirect']; + let community = route.queryParams['communityId']; + if (!community && route.data['community']) { // for openaire + community = route.data['community']; } - if(!community && typeof document != 'undefined'){ - community = ConnectHelper.getCommunityFromDomain(document.location.hostname); + if (!community && typeof document !== 'undefined') { + 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,"/" + path.split("?")[0].substring(1))}); + 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 } })); + .subscribe(() => this.router.navigate([redirect], {queryParams: {'page': path}})); return obs; } return true; } canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable | boolean { - return this.check(state.url); + return this.check(route, state.url); } canLoad(route: Route): Observable | Promise | boolean { - return this.check('/' + route.path); + return this.check(null, '/' + route.path); } }