Project

General

Profile

1
import {Observable, Subscription} from 'rxjs';
2
import {tap, take} from 'rxjs/operators';
3
import { Injectable } from '@angular/core';
4
import {
5
  Router,
6
  CanActivate,
7
  ActivatedRouteSnapshot,
8
  RouterStateSnapshot,
9
  Data
10
} from '@angular/router';
11

    
12
import { ConfigurationService } from '../utils/configuration/configuration.service';
13
import {ConnectHelper} from '../connect/connectHelper';
14
import {properties} from "../../../environments/environment";
15

    
16
@Injectable()
17
export class IsRouteEnabled implements CanActivate {
18
  sub: Subscription = null;
19

    
20
  constructor(private router: Router,
21
              private config: ConfigurationService) {}
22

    
23
  // check(data: Data, community: string, path: string): Observable<boolean> | boolean {
24
  //   const customRedirect = data['redirect'];
25
  //
26
  //     const redirect = customRedirect ? customRedirect : '/error';
27
  //     const obs = this.propertiesService.subscribeEnvironment().pipe(map(res => {
28
  //       let communityDomain = null;
29
  //       //if (!community) {
30
  //         communityDomain  = ConnectHelper.getCommunityFromDomain(res.domain);
31
  //       //}
32
  //       if(communityDomain) {
33
  //         community = communityDomain;
34
  //       } else if (!community && data['community']) { // for openaire or connect
35
  //         community = data['community'];
36
  //       }
37
  //      return  res;
38
  //     }),mergeMap(prop => {
39
  //       if (!community) {
40
  //         community = prop.adminToolsCommunity;
41
  //       }
42
  //       return this.config.isPageEnabled(prop, community, '/' + path);
43
  //     }),);
44
  //   console.log("check isRouteEnabled.guard : call isPageEnabled");
45
  //
46
  //   obs.pipe(filter(enabled => !enabled))
47
  //         .subscribe(() => this.router.navigate([redirect], {queryParams: {'page': path}}));
48
  //     return obs;
49
  // }
50

    
51
  check(data: Data, community: string, path: string): Observable<boolean> | boolean {
52
    const customRedirect = data['redirect'];
53

    
54
    const redirect = customRedirect ? customRedirect : '/error';
55
    let communityDomain  = ConnectHelper.getCommunityFromDomain(properties.domain);
56

    
57
    if(communityDomain) {
58
      community = communityDomain;
59
    } else if (!community && data['community']) { // for openaire or connect
60
      community = data['community'];
61
    }
62

    
63
    if (!community) {
64
      community = properties.adminToolsCommunity;
65
    }
66

    
67
    return this.config.isPageEnabledByState(properties, community, '/'+path).pipe(take(1),tap((enabled) => {
68
      if(!enabled){
69
        this.router.navigate([redirect], {queryParams: {'page': path}});
70
      }
71
    }));
72

    
73
  }
74

    
75
  canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
76
    return this.check(route.data, route.queryParams['communityId'], state.url);
77
  }
78

    
79
}
(3-3/3)