Project

General

Profile

1
import {tap} from 'rxjs/operators';
2
import {Injectable} from '@angular/core';
3
import {
4
  ActivatedRouteSnapshot,
5
  CanActivate,
6
  CanActivateChild,
7
  Router,
8
  RouterStateSnapshot,
9
  UrlTree
10
} from '@angular/router';
11
import {Observable, Subscription} from 'rxjs';
12
import {CommunityService} from '../community/community.service';
13

    
14
@Injectable()
15
export class ConnectRIGuard implements CanActivate, CanActivateChild {
16
  sub: Subscription = null;
17
  
18
  constructor(private router: Router,
19
              private communityService: CommunityService) {
20
  }
21
  
22
  check(community: string, url: string): Observable<boolean> | boolean {
23
    return this.communityService.isRIType(community).pipe(tap(authorized => {
24
      if (!authorized) {
25
        this.router.navigate(['/error'], {queryParams: {'page': url}});
26
      }
27
    }));
28
  }
29
  
30
  canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
31
    let community = route.params['community'];
32
    return community && this.check(community, state.url);
33
  }
34
  
35
  canActivateChild(childRoute: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
36
    let community = childRoute.params['community'];
37
    return community && this.check(community, state.url);
38
  }
39
}
(3-3/5)