Project

General

Profile

1
import {Injectable} from '@angular/core';
2
import {ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot} from '@angular/router';
3
import {Observable} from 'rxjs';
4
import {filter, map, mergeMap, take} from "rxjs/operators";
5
import {UserManagementService} from "../../services/user-management.service";
6
import {EnvironmentSpecificService} from "../../utils/properties/environment-specific.service";
7
import {Session} from "../../login/utils/helper.class";
8

    
9
@Injectable()
10
export class IsCommunityOrAdmin implements CanActivate {
11

    
12
  constructor(private router: Router,
13
              private userManagementService: UserManagementService,
14
              private propertiesService: EnvironmentSpecificService) {
15
  }
16
  /*
17
  //TODO add login guard and simplify this method
18
    check(community: string): Observable<boolean> | boolean {
19
    return this.userManagementService.getUserInfo(false).pipe(take(1),map(user => {
20
      if (community && community !== 'undefined' || Session.isPortalAdministrator(user)) {
21
        return true;
22
      } else {
23
        this.router.navigate(['/errorCommunity']);
24
        return false;
25
      }
26
    }));
27
  }
28
   */
29
  check(community: string): Observable<boolean> | boolean {
30
    if (community && community !== 'undefined') {
31
      return true;
32
    } else {
33
      const obs = this.propertiesService.subscribeEnvironment().pipe(mergeMap(properties => {
34
        return this.userManagementService.getUserInfo(false).pipe(map(user => {
35
          return Session.isPortalAdministrator(user);
36
        }));
37
      }));
38
      obs.pipe(filter( isAdmin => !isAdmin)).subscribe( () => {
39
        this.router.navigate(['/errorCommunity']);
40
      });
41
      return obs;
42
    }
43
  }
44
/*
45
  check(community: string): Observable<boolean> | boolean {
46
    if(Session.isLoggedIn() && Session.isPortalAdministrator()) {
47
      return true;
48
    }
49
    else if (community && community !== 'undefined') {
50
      return true;
51
    } else {
52
      this.router.navigate(['errorcommunity']);
53
      return false;
54
    }
55
  }*/
56

    
57
  canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
58
    return this.check(route.queryParams['communityId']);
59
  }
60

    
61
}
62

    
(8-8/8)