Project

General

Profile

1

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

    
18
@Injectable()
19
export class ConnectAdminLoginGuard implements CanActivate, CanLoad {
20
  constructor(private router: Router,
21
              private communityService: CommunityService,
22
              private propertiesService: EnvironmentSpecificService) {}
23

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

    
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, segments: UrlSegment[]): Observable<boolean> | Promise<boolean> | boolean {
55
    const path = '/' + route.path + document.location.search;
56
    return this.check(ConnectHelper.getCommunityFromPath(path), path);
57
  }
58
}
(3-3/7)