openaire-library/error/isRouteEnabled.guard.ts

80 lines
2.7 KiB
TypeScript

import {Observable, Subscription} from 'rxjs';
import {tap, take} from 'rxjs/operators';
import { Injectable } from '@angular/core';
import {
Router,
CanActivate,
ActivatedRouteSnapshot,
RouterStateSnapshot,
Data
} from '@angular/router';
import { ConfigurationService } from '../utils/configuration/configuration.service';
import {ConnectHelper} from '../connect/connectHelper';
import {properties} from "../../../environments/environment";
@Injectable()
export class IsRouteEnabled implements CanActivate {
sub: Subscription = null;
constructor(private router: Router,
private config: ConfigurationService) {}
// 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;
// }
check(data: Data, community: string, path: string): Observable<boolean> | boolean {
const customRedirect = data['redirect'];
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;
}
return this.config.isPageEnabledByState(properties, community, '/'+path).pipe(take(1),tap((enabled) => {
if(!enabled){
this.router.navigate([redirect], {queryParams: {'page': path}});
}
}));
}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
return this.check(route.data, route.queryParams['communityId'], state.url);
}
}