import {tap} from 'rxjs/operators'; import { Injectable } from '@angular/core'; import { Router, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, CanLoad, Route, UrlSegment } from '@angular/router'; import {Observable, Subscription} from 'rxjs'; import {CommunityService} from '../community/community.service'; import { EnvironmentSpecificService} from '../../utils/properties/environment-specific.service'; import {ConnectHelper} from '../connectHelper'; import {properties} from "../../../../environments/environment"; @Injectable() export class ConnectRIGuard implements CanActivate, CanLoad { sub: Subscription = null; constructor(private router: Router, private communityService: CommunityService, private propertiesService: EnvironmentSpecificService) { } check(community: string): Observable | boolean { return this.communityService.isRIType(community).pipe(tap(authorized => { if (!authorized) { this.router.navigate(['errorcommunity']); } })); } canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable | boolean { let community = route.params['community']?route.params['community']:route.queryParams['communityId']; return community && this.check(community); } canLoad(route: Route, segments: UrlSegment[]): Observable | Promise | boolean { const path = '/' + route.path + document.location.search; return this.check(ConnectHelper.getCommunityFromPath(path)); } canDeactivate() { if(this.sub) { this.sub.unsubscribe(); } return true; } }