Project

General

Profile

1
import {map, filter, mergeMap, take} from 'rxjs/operators';
2
import {Injectable} from '@angular/core';
3
import {
4
  Router,
5
  CanActivate,
6
  ActivatedRouteSnapshot,
7
  RouterStateSnapshot,
8
  CanLoad, Route, UrlSegment, CanActivateChild, UrlTree
9
} from '@angular/router';
10
import {Observable, of, Subscription} from 'rxjs';
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
import {StringUtils} from '../../utils/string-utils.class';
17
import {UserManagementService} from "../../services/user-management.service";
18
import {properties} from "../../../../environments/environment";
19

    
20
@Injectable()
21
export class ConnectAdminLoginGuard implements CanActivate, CanActivateChild {
22
  
23
  constructor(private router: Router,
24
              private propertiesService: EnvironmentSpecificService,
25
              private userManagementService: UserManagementService) {
26
  }
27
  
28
  check(community: string, path: string): Observable<boolean> | boolean {
29
    let errorCode = LoginErrorCodes.NOT_LOGIN;
30
    const authorized = this.userManagementService.getUserInfo(false).pipe(take(1), map(user => {
31
      if (user) {
32
        if (Session.isPortalAdministrator(user) || Session.isCommunityCurator(user) || Session.isManager('community', community, user)) {
33
          return of(true);
34
        }
35
      }
36
      return of(false);
37
    }), mergeMap(authorized => {
38
      return authorized;
39
    }));
40
    authorized.pipe(filter(authorized => !authorized)).subscribe(() => {
41
      this.router.navigate(['/user-info'], {
42
        queryParams: {
43
          'errorCode': errorCode,
44
          'redirectUrl': path
45
        }
46
      })
47
    });
48
    return authorized;
49
  }
50
  
51
  canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
52
    return this.check(route.params['community'], state.url);
53
  }
54
  
55
  canActivateChild(childRoute: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
56
    return this.check(childRoute.params['community'], state.url);
57
  }
58
}
(3-3/8)