Project

General

Profile

1
import { Injectable } from '@angular/core';
2
import {
3
  Router,
4
  CanActivate,
5
  ActivatedRouteSnapshot,
6
  RouterStateSnapshot,
7
  CanLoad,
8
  Route
9
} from '@angular/router';
10
import {Observable} from 'rxjs/Observable';
11
import {Session} from '../../login/utils/helper.class';
12
import {LoginErrorCodes} from '../../login/utils/guardHelper.class';
13
import {CommunityService} from '../community/community.service';
14
import { EnvironmentSpecificService} from '../../utils/properties/environment-specific.service';
15
import {ConnectHelper} from '../connectHelper';
16

    
17
@Injectable()
18
export class ConnectRIGuard implements  CanActivate, CanLoad {
19

    
20
  constructor(private router: Router,
21
              private communityService: CommunityService,
22
              private propertiesService: EnvironmentSpecificService) {
23
  }
24

    
25
  check(community: string, path: string): Observable<boolean> | boolean {
26
    let errorCode = LoginErrorCodes.NOT_LOGIN;
27
    if (Session.isLoggedIn()) {
28
      if (Session.isPortalAdministrator()) {
29
        return true;
30
      } else {
31
        const obs = this.propertiesService.subscribeEnvironment().mergeMap(properties => {
32
          return this.communityService.iscommunityRI(properties, properties['communityAPI'] + community);
33
        });
34
        obs.filter(enabled => !enabled)
35
            .subscribe(() => this.router.navigate(['/user-info'], {
36
              queryParams: {
37
                'errorCode': errorCode,
38
                'redirectUrl': path
39
              }
40
            }));
41
        return obs;
42
      }
43
    } else {
44
      errorCode = LoginErrorCodes.NOT_LOGIN;
45
      this.router.navigate(['/user-info'], {queryParams: {'errorCode': errorCode, 'redirectUrl': path}});
46
      return false;
47
    }
48
  }
49

    
50
  canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
51
    return this.check(route.queryParams['communityId'], state.url);
52
  }
53

    
54
  canLoad(route: Route): Observable<boolean> | Promise<boolean> | boolean {
55
    const path = '/' + route.path + document.location.search;
56
    return this.check(ConnectHelper.getCommunityFromPath(path), path);
57
  }
58
}
(4-4/6)