Project

General

Profile

1
import {Observable} from 'rxjs';
2
import {take, tap} from 'rxjs/operators';
3
import {Injectable} from '@angular/core';
4
import {ActivatedRouteSnapshot, CanActivate, Data, Router, RouterStateSnapshot, UrlTree} from '@angular/router';
5
import {ConfigurationService} from '../utils/configuration/configuration.service';
6
import {ConnectHelper} from '../connect/connectHelper';
7
import {properties} from "../../../environments/environment";
8

    
9
@Injectable()
10
export class IsRouteEnabled implements CanActivate {
11
  
12
  constructor(private router: Router,
13
              private config: ConfigurationService) {
14
  }
15
  
16
  check(data: Data, path: string): Observable<boolean> | boolean {
17
    const customRedirect = data['redirect'];
18
    const redirect = customRedirect ? customRedirect : '/error';
19
    let community = ConnectHelper.getCommunityFromDomain(properties.domain);
20
    if (!community && data['community']) { // for openaire or connect
21
      community = data['community'];
22
    }
23
    if (!community) {
24
      community = properties.adminToolsCommunity;
25
    }
26
    return this.config.isPageEnabledByState(properties, community, '/' + path).pipe(take(1), tap((enabled) => {
27
      if (!enabled) {
28
        this.router.navigate([redirect], {queryParams: {'page': path}});
29
      }
30
    }));
31
    
32
  }
33
  
34
  canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
35
    return this.check(route.data, state.url);
36
  }
37
  
38
}
(3-3/3)