From bc9801aeb0739ffb9a3b26fffe206d24fd653998 Mon Sep 17 00:00:00 2001 From: "k.triantafyllou" Date: Tue, 26 Feb 2019 15:35:09 +0000 Subject: [PATCH] [Trunk|Library]: isRouteEnabled git-svn-id: https://svn.driver.research-infrastructures.eu/driver/dnet40/modules/uoa-services-library/trunk/ng-openaire-library/src/app@54913 d315682c-612b-4755-9ff5-7f18f6832af3 --- error/isRouteEnabled.guard.ts | 36 ++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) 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); } }