2019-06-03 15:20:36 +02:00
|
|
|
|
2020-07-17 01:08:10 +02:00
|
|
|
import {of as observableOf, Observable, Subject, Subscription} from 'rxjs';
|
2019-06-03 15:20:36 +02:00
|
|
|
|
2020-07-13 00:19:30 +02:00
|
|
|
import {map, filter, mergeMap, tap} 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';
|
2020-07-13 00:19:30 +02:00
|
|
|
import {properties} from "../../../environments/environment";
|
|
|
|
import {LoginErrorCodes} from "../login/utils/guardHelper.class";
|
|
|
|
import {of} from 'rxjs';
|
|
|
|
import {Page} from "../utils/entities/adminTool/page";
|
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,
|
|
|
|
private config: ConfigurationService,
|
2019-02-27 11:39:13 +01:00
|
|
|
private propertiesService: EnvironmentSpecificService) {}
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
const obs =
|
|
|
|
//this.config.isPageEnabled(properties, community, '/' + path);
|
|
|
|
this.config.isPageEnabledByState(properties, community, '/'+path);
|
2020-07-17 01:08:10 +02:00
|
|
|
this.sub = obs
|
2020-07-15 12:36:03 +02:00
|
|
|
//.pipe(tap((enabled) => console.log("aaa: "+enabled)))
|
2020-07-13 00:19:30 +02:00
|
|
|
.pipe(filter(enabled => !enabled))
|
|
|
|
.subscribe(() => {
|
|
|
|
this.router.navigate([redirect], {queryParams: {'page': path}});
|
|
|
|
});
|
|
|
|
|
|
|
|
return obs;
|
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
|
|
|
|
|
|
|
canDeactivate() {
|
|
|
|
if(this.sub) {
|
|
|
|
this.sub.unsubscribe();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|