2020-11-11 15:43:13 +01:00
|
|
|
import {Observable, Subscription} from 'rxjs';
|
|
|
|
import {tap, take} 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,
|
|
|
|
ActivatedRouteSnapshot,
|
|
|
|
RouterStateSnapshot,
|
2020-11-11 15:43:13 +01:00
|
|
|
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-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 {
|
2020-07-17 01:08:10 +02:00
|
|
|
sub: Subscription = null;
|
2017-12-19 13:53:46 +01:00
|
|
|
|
2019-02-26 16:35:09 +01:00
|
|
|
constructor(private router: Router,
|
2020-11-11 15:43:13 +01:00
|
|
|
private config: ConfigurationService) {}
|
2017-12-19 13:53:46 +01:00
|
|
|
|
2020-07-13 00:19:30 +02:00
|
|
|
// 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 => {
|
|
|
|
// let communityDomain = null;
|
|
|
|
// //if (!community) {
|
|
|
|
// communityDomain = ConnectHelper.getCommunityFromDomain(res.domain);
|
|
|
|
// //}
|
|
|
|
// if(communityDomain) {
|
|
|
|
// community = communityDomain;
|
|
|
|
// } else if (!community && data['community']) { // for openaire or connect
|
|
|
|
// community = data['community'];
|
|
|
|
// }
|
|
|
|
// return res;
|
|
|
|
// }),mergeMap(prop => {
|
|
|
|
// if (!community) {
|
|
|
|
// community = prop.adminToolsCommunity;
|
|
|
|
// }
|
|
|
|
// return this.config.isPageEnabled(prop, community, '/' + path);
|
|
|
|
// }),);
|
|
|
|
// console.log("check isRouteEnabled.guard : call isPageEnabled");
|
|
|
|
//
|
|
|
|
// obs.pipe(filter(enabled => !enabled))
|
|
|
|
// .subscribe(() => this.router.navigate([redirect], {queryParams: {'page': path}}));
|
|
|
|
// return obs;
|
|
|
|
// }
|
|
|
|
|
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
|
|
|
|
2020-07-13 00:19:30 +02:00
|
|
|
const redirect = customRedirect ? customRedirect : '/error';
|
|
|
|
let communityDomain = ConnectHelper.getCommunityFromDomain(properties.domain);
|
|
|
|
|
|
|
|
if(communityDomain) {
|
|
|
|
community = communityDomain;
|
|
|
|
} else if (!community && data['community']) { // for openaire or connect
|
|
|
|
community = data['community'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!community) {
|
|
|
|
community = properties.adminToolsCommunity;
|
|
|
|
}
|
|
|
|
|
2020-11-11 15:43:13 +01:00
|
|
|
return this.config.isPageEnabledByState(properties, community, '/'+path).pipe(take(1),tap((enabled) => {
|
|
|
|
if(!enabled){
|
|
|
|
this.router.navigate([redirect], {queryParams: {'page': path}});
|
|
|
|
}
|
|
|
|
}));
|
2020-07-13 00:19:30 +02:00
|
|
|
|
2019-02-26 14:57:04 +01:00
|
|
|
}
|
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
|
|
|
}
|
2020-07-17 01:08:10 +02:00
|
|
|
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|