2020-11-11 15:43:13 +01:00
|
|
|
import {take, tap} from 'rxjs/operators';
|
|
|
|
import {Injectable} from '@angular/core';
|
2019-03-07 16:43:54 +01:00
|
|
|
import {
|
|
|
|
Router,
|
|
|
|
CanActivate,
|
|
|
|
ActivatedRouteSnapshot,
|
2021-03-02 16:52:09 +01:00
|
|
|
RouterStateSnapshot, UrlTree, CanActivateChild,
|
|
|
|
|
2019-03-07 16:43:54 +01:00
|
|
|
} from '@angular/router';
|
2020-07-17 01:08:10 +02:00
|
|
|
import {Observable, Subscription} from 'rxjs';
|
2019-03-07 16:43:54 +01:00
|
|
|
import {CommunityService} from '../community/community.service';
|
2020-07-17 01:08:10 +02:00
|
|
|
import {properties} from "../../../../environments/environment";
|
2019-03-07 16:43:54 +01:00
|
|
|
|
|
|
|
@Injectable()
|
2021-03-02 16:52:09 +01:00
|
|
|
export class ConnectCommunityGuard implements CanActivate, CanActivateChild {
|
2020-07-17 01:08:10 +02:00
|
|
|
sub: Subscription = null;
|
2019-03-07 16:43:54 +01:00
|
|
|
|
|
|
|
constructor(private router: Router,
|
2020-08-14 11:49:35 +02:00
|
|
|
private communityService: CommunityService) {
|
2019-03-07 16:43:54 +01:00
|
|
|
}
|
|
|
|
|
2021-03-02 16:52:09 +01:00
|
|
|
check(community: string, url: string): Observable<boolean> | boolean {
|
2021-02-19 18:50:34 +01:00
|
|
|
return this.communityService.isCommunityType(community).pipe(take(1), tap(isCommunity => {
|
2020-11-11 15:43:13 +01:00
|
|
|
if (!isCommunity) {
|
2021-03-02 16:52:09 +01:00
|
|
|
this.router.navigate(['/error'], {queryParams: {'page': url}});
|
2020-08-14 11:49:35 +02:00
|
|
|
}
|
|
|
|
}));
|
2019-03-07 16:43:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
|
2021-04-01 16:24:54 +02:00
|
|
|
let community = route.params['community'];
|
|
|
|
return community && this.check(community, state.url);
|
2021-03-02 16:52:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
canActivateChild(childRoute: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
|
|
|
|
let community = childRoute.params['community'];
|
|
|
|
return community && this.check(community, state.url);
|
2019-03-07 16:43:54 +01:00
|
|
|
}
|
|
|
|
}
|