Project

General

Profile

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

    
14
@Injectable()
15
export class ConnectCommunityGuard 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.isCommunityType(community).pipe(take(1), tap(isCommunity => {
24
      if (!isCommunity) {
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
}
(2-2/5)