53 lines
1.8 KiB
TypeScript
53 lines
1.8 KiB
TypeScript
|
|
import {of as observableOf, Observable} from 'rxjs';
|
|
|
|
import {map, filter, mergeMap} from 'rxjs/operators';
|
|
import { Injectable } from '@angular/core';
|
|
import {
|
|
Router,
|
|
CanActivate,
|
|
CanLoad,
|
|
ActivatedRouteSnapshot,
|
|
RouterStateSnapshot,
|
|
Route, Data
|
|
} from '@angular/router';
|
|
|
|
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 {
|
|
|
|
constructor(private router: Router,
|
|
private config: ConfigurationService,
|
|
private propertiesService: EnvironmentSpecificService) {}
|
|
|
|
check(data: Data, community: string, path: string): Observable<boolean> | boolean {
|
|
const customRedirect = data['redirect'];
|
|
|
|
const redirect = customRedirect ? customRedirect : '/error';
|
|
const obs = this.propertiesService.subscribeEnvironment().pipe(map(res => {
|
|
if (!community) {
|
|
community = ConnectHelper.getCommunityFromDomain(res.domain);
|
|
}
|
|
if (!community && data['community']) { // for openaire or connect
|
|
community = data['community'];
|
|
}
|
|
return res;
|
|
}),mergeMap(prop => {
|
|
if (!community) { // no community to check - return true
|
|
return observableOf(true);
|
|
}
|
|
return this.config.isPageEnabled(prop, community, '/' + path.split('?')[0].substring(1));
|
|
}),);
|
|
obs.pipe(filter(enabled => !enabled))
|
|
.subscribe(() => this.router.navigate([redirect], {queryParams: {'page': path}}));
|
|
return obs;
|
|
}
|
|
|
|
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
|
|
return this.check(route.data, route.queryParams['communityId'], state.url);
|
|
}
|
|
}
|