[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
This commit is contained in:
k.triantafyllou 2019-02-26 15:35:09 +00:00
parent 75533e17bf
commit bc9801aeb0
1 changed files with 19 additions and 17 deletions

View File

@ -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> | 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> | 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> | boolean {
return this.check(state.url);
return this.check(route, state.url);
}
canLoad(route: Route): Observable<boolean> | Promise<boolean> | boolean {
return this.check('/' + route.path);
return this.check(null, '/' + route.path);
}
}