Project

General

Profile

1 51526 argiro.kok
import { Injectable } from '@angular/core';
2 54906 k.triantaf
import {
3
  Router,
4
  CanActivate,
5
  ActivatedRouteSnapshot,
6
  RouterStateSnapshot,
7
  CanLoad,
8 54915 k.triantaf
  Route
9 54906 k.triantaf
} from '@angular/router';
10 54912 k.triantaf
import {Observable} from 'rxjs/Observable';
11 51526 argiro.kok
import {Session} from '../../login/utils/helper.class';
12 53612 konstantin
import {LoginErrorCodes} from '../../login/utils/guardHelper.class';
13 51526 argiro.kok
import {CommunityService} from '../community/community.service';
14
import { EnvironmentSpecificService} from '../../utils/properties/environment-specific.service';
15 54915 k.triantaf
import {ConnectHelper} from '../connectHelper';
16 51526 argiro.kok
17
@Injectable()
18 54906 k.triantaf
export class ConnectRIGuard implements  CanActivate, CanLoad {
19 51526 argiro.kok
20 54912 k.triantaf
  constructor(private router: Router,
21
              private communityService: CommunityService,
22
              private propertiesService: EnvironmentSpecificService) {
23
  }
24 54906 k.triantaf
25 54915 k.triantaf
  check(community: string, path: string): Observable<boolean> | boolean {
26 54903 k.triantaf
    let errorCode = LoginErrorCodes.NOT_LOGIN;
27 54912 k.triantaf
    if (Session.isLoggedIn()) {
28
      if (Session.isPortalAdministrator()) {
29 51526 argiro.kok
        return true;
30 54903 k.triantaf
      } else {
31 54912 k.triantaf
        const obs = this.propertiesService.subscribeEnvironment().mergeMap(properties => {
32
          return this.communityService.iscommunityRI(properties, properties['communityAPI'] + community);
33 54906 k.triantaf
        });
34 51526 argiro.kok
        obs.filter(enabled => !enabled)
35 54906 k.triantaf
            .subscribe(() => this.router.navigate(['/user-info'], {
36
              queryParams: {
37 54912 k.triantaf
                'errorCode': errorCode,
38
                'redirectUrl': path
39 54906 k.triantaf
              }
40
            }));
41 51526 argiro.kok
        return obs;
42 54906 k.triantaf
      }
43 54912 k.triantaf
    } else {
44 54903 k.triantaf
      errorCode = LoginErrorCodes.NOT_LOGIN;
45 54912 k.triantaf
      this.router.navigate(['/user-info'], {queryParams: {'errorCode': errorCode, 'redirectUrl': path}});
46 51526 argiro.kok
      return false;
47
    }
48 54906 k.triantaf
  }
49 51526 argiro.kok
50 54906 k.triantaf
  canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
51 54915 k.triantaf
    return this.check(route.queryParams['communityId'], state.url);
52 54906 k.triantaf
  }
53 51526 argiro.kok
54 54906 k.triantaf
  canLoad(route: Route): Observable<boolean> | Promise<boolean> | boolean {
55 54960 k.triantaf
    const path = '/' + route.path + document.location.search;
56 54915 k.triantaf
    return this.check(ConnectHelper.getCommunityFromPath(path), path);
57 51526 argiro.kok
  }
58
}