2019-06-03 15:20:36 +02:00
|
|
|
|
|
|
|
import {of as observableOf, Observable} from 'rxjs';
|
|
|
|
|
|
|
|
import {map, filter, mergeMap} from 'rxjs/operators';
|
2017-12-19 13:53:46 +01:00
|
|
|
import { Injectable } from '@angular/core';
|
2019-02-26 14:57:04 +01:00
|
|
|
import {
|
|
|
|
Router,
|
|
|
|
CanActivate,
|
|
|
|
CanLoad,
|
|
|
|
ActivatedRouteSnapshot,
|
|
|
|
RouterStateSnapshot,
|
2019-02-27 11:39:13 +01:00
|
|
|
Route, Data
|
2019-02-26 14:57:04 +01:00
|
|
|
} from '@angular/router';
|
2019-06-03 15:20:36 +02:00
|
|
|
|
2017-12-19 13:53:46 +01:00
|
|
|
import { ConfigurationService } from '../utils/configuration/configuration.service';
|
2018-03-19 15:33:08 +01:00
|
|
|
import { EnvironmentSpecificService} from '../utils/properties/environment-specific.service';
|
2018-04-16 13:49:35 +02:00
|
|
|
import {ConnectHelper} from '../connect/connectHelper';
|
2017-12-19 13:53:46 +01:00
|
|
|
|
|
|
|
@Injectable()
|
2019-06-03 15:20:36 +02:00
|
|
|
export class IsRouteEnabled implements CanActivate {
|
2017-12-19 13:53:46 +01:00
|
|
|
|
2019-02-26 16:35:09 +01:00
|
|
|
constructor(private router: Router,
|
|
|
|
private config: ConfigurationService,
|
2019-02-27 11:39:13 +01:00
|
|
|
private propertiesService: EnvironmentSpecificService) {}
|
2017-12-19 13:53:46 +01:00
|
|
|
|
2019-02-27 11:39:13 +01:00
|
|
|
check(data: Data, community: string, path: string): Observable<boolean> | boolean {
|
|
|
|
const customRedirect = data['redirect'];
|
2019-06-24 13:05:24 +02:00
|
|
|
|
2019-02-26 16:35:09 +01:00
|
|
|
const redirect = customRedirect ? customRedirect : '/error';
|
2019-06-03 15:20:36 +02:00
|
|
|
const obs = this.propertiesService.subscribeEnvironment().pipe(map(res => {
|
2019-06-27 15:28:47 +02:00
|
|
|
let communityDomain = null;
|
2019-07-03 12:14:24 +02:00
|
|
|
//if (!community) {
|
2019-06-27 15:28:47 +02:00
|
|
|
communityDomain = ConnectHelper.getCommunityFromDomain(res.domain);
|
2019-07-03 12:14:24 +02:00
|
|
|
//}
|
2019-06-27 15:28:47 +02:00
|
|
|
if(communityDomain) {
|
|
|
|
community = communityDomain;
|
|
|
|
} else if (!community && data['community']) { // for openaire or connect
|
2019-06-24 13:05:24 +02:00
|
|
|
community = data['community'];
|
|
|
|
}
|
2019-06-05 15:55:35 +02:00
|
|
|
return res;
|
|
|
|
}),mergeMap(prop => {
|
2019-07-18 18:02:01 +02:00
|
|
|
if (!community) {
|
|
|
|
community = prop.adminToolsCommunity;
|
2019-03-01 12:29:52 +01:00
|
|
|
}
|
2020-04-07 13:15:28 +02:00
|
|
|
return this.config.isPageEnabled(prop, community, '/' + path);
|
2019-06-03 15:20:36 +02:00
|
|
|
}),);
|
|
|
|
obs.pipe(filter(enabled => !enabled))
|
2019-02-26 16:35:09 +01:00
|
|
|
.subscribe(() => this.router.navigate([redirect], {queryParams: {'page': path}}));
|
2019-02-26 14:57:04 +01:00
|
|
|
return obs;
|
|
|
|
}
|
2018-03-13 17:02:22 +01:00
|
|
|
|
2019-02-26 14:57:04 +01:00
|
|
|
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
|
2019-02-27 11:39:13 +01:00
|
|
|
return this.check(route.data, route.queryParams['communityId'], state.url);
|
2019-02-26 14:57:04 +01:00
|
|
|
}
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|