2021-04-01 16:24:54 +02:00
|
|
|
import {Observable} from 'rxjs';
|
|
|
|
import {take, tap} from 'rxjs/operators';
|
|
|
|
import {Injectable} from '@angular/core';
|
2021-07-14 13:19:57 +02:00
|
|
|
import {ActivatedRouteSnapshot, CanActivate, Data, Router, RouterStateSnapshot, UrlTree} from '@angular/router';
|
2021-04-01 16:24:54 +02:00
|
|
|
import {ConfigurationService} from '../utils/configuration/configuration.service';
|
2018-04-16 13:49:35 +02:00
|
|
|
import {ConnectHelper} from '../connect/connectHelper';
|
2020-07-13 00:19:30 +02:00
|
|
|
import {properties} from "../../../environments/environment";
|
2017-12-19 13:53:46 +01:00
|
|
|
|
|
|
|
@Injectable()
|
2019-06-03 15:20:36 +02:00
|
|
|
export class IsRouteEnabled implements CanActivate {
|
2021-04-01 16:24:54 +02:00
|
|
|
|
2019-02-26 16:35:09 +01:00
|
|
|
constructor(private router: Router,
|
2021-07-14 13:19:57 +02:00
|
|
|
private config: ConfigurationService) {
|
|
|
|
}
|
|
|
|
|
2021-04-01 16:24:54 +02:00
|
|
|
check(data: Data, path: string): Observable<boolean> | boolean {
|
2019-02-27 11:39:13 +01:00
|
|
|
const customRedirect = data['redirect'];
|
2020-07-13 00:19:30 +02:00
|
|
|
const redirect = customRedirect ? customRedirect : '/error';
|
2021-07-14 13:19:57 +02:00
|
|
|
let community = ConnectHelper.getCommunityFromDomain(properties.domain);
|
2021-04-01 16:24:54 +02:00
|
|
|
if (!community && data['community']) { // for openaire or connect
|
2020-07-13 00:19:30 +02:00
|
|
|
community = data['community'];
|
|
|
|
}
|
|
|
|
if (!community) {
|
|
|
|
community = properties.adminToolsCommunity;
|
|
|
|
}
|
2021-07-14 13:19:57 +02:00
|
|
|
return this.config.isPageEnabledByState(properties, community, '/' + path).pipe(take(1), tap((enabled) => {
|
|
|
|
if (!enabled) {
|
2020-11-11 15:43:13 +01:00
|
|
|
this.router.navigate([redirect], {queryParams: {'page': path}});
|
|
|
|
}
|
|
|
|
}));
|
2021-07-14 13:19:57 +02:00
|
|
|
|
2019-02-26 14:57:04 +01:00
|
|
|
}
|
2021-07-14 13:19:57 +02:00
|
|
|
|
|
|
|
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
|
2021-04-01 16:24:54 +02:00
|
|
|
return this.check(route.data, state.url);
|
2019-02-26 14:57:04 +01:00
|
|
|
}
|
2021-07-14 13:19:57 +02:00
|
|
|
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|