diff --git a/connect/communityGuard/isCommunity.guard.ts b/connect/communityGuard/isCommunity.guard.ts index ec67699b..3441713a 100644 --- a/connect/communityGuard/isCommunity.guard.ts +++ b/connect/communityGuard/isCommunity.guard.ts @@ -4,22 +4,41 @@ import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, - CanLoad, Route, UrlSegment + CanLoad, Route, UrlSegment, CanActivateChild, UrlTree } from '@angular/router'; import {Observable} from 'rxjs'; import {ConnectHelper} from '../connectHelper'; import {properties} from "../../../../environments/environment"; +import {CommunityService} from "../community/community.service"; +import {map} from "rxjs/operators"; @Injectable() -export class IsCommunity implements CanActivate, CanLoad { +export class IsCommunity implements CanActivate, CanActivateChild { - constructor(private router: Router) { + constructor(private router: Router, + private communityService: CommunityService) { } - check(community: string): Observable | boolean { - if (community && community !== 'undefined') { - return true; + check(route: ActivatedRouteSnapshot): Observable | boolean { + let community; + if(properties.isDashboard) { + community = route.params['community']; + } else { + community = route.queryParams['communityId']; + if(!community) { + community = ConnectHelper.getCommunityFromDomain(properties.domain); + } + } + if (community) { + return this.communityService.getCommunity(community).pipe(map(community => { + if(community) { + return true; + } else { + this.router.navigate(['error']); + return false; + } + })); } else { this.router.navigate(['error']); return false; @@ -27,11 +46,10 @@ export class IsCommunity implements CanActivate, CanLoad { } canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable | boolean { - return this.check((route.queryParams['communityId']) ? route.queryParams['communityId'] : ConnectHelper.getCommunityFromDomain(properties.domain)); + return this.check(route); } - - canLoad(route: Route, segments: UrlSegment[]): Observable | Promise | boolean { - const path = '/' + route.path + document.location.search; - return this.check(ConnectHelper.getCommunityFromPath(path)); + + canActivateChild(childRoute: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable | Promise | boolean | UrlTree { + return this.check(childRoute); } }