[Library | Trunk]: Fix is Community guard

git-svn-id: https://svn.driver.research-infrastructures.eu/driver/dnet40/modules/uoa-services-library/trunk/ng-openaire-library/src/app@60684 d315682c-612b-4755-9ff5-7f18f6832af3
This commit is contained in:
k.triantafyllou 2021-03-22 12:00:38 +00:00
parent 4e5ccfa88b
commit 90fc34f24f
1 changed files with 29 additions and 11 deletions

View File

@ -4,22 +4,41 @@ import {
CanActivate, CanActivate,
ActivatedRouteSnapshot, ActivatedRouteSnapshot,
RouterStateSnapshot, RouterStateSnapshot,
CanLoad, Route, UrlSegment CanLoad, Route, UrlSegment, CanActivateChild, UrlTree
} from '@angular/router'; } from '@angular/router';
import {Observable} from 'rxjs'; import {Observable} from 'rxjs';
import {ConnectHelper} from '../connectHelper'; import {ConnectHelper} from '../connectHelper';
import {properties} from "../../../../environments/environment"; import {properties} from "../../../../environments/environment";
import {CommunityService} from "../community/community.service";
import {map} from "rxjs/operators";
@Injectable() @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> | boolean { check(route: ActivatedRouteSnapshot): Observable<boolean> | boolean {
if (community && community !== 'undefined') { let community;
return true; 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 { } else {
this.router.navigate(['error']); this.router.navigate(['error']);
return false; return false;
@ -27,11 +46,10 @@ export class IsCommunity implements CanActivate, CanLoad {
} }
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean { canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
return this.check((route.queryParams['communityId']) ? route.queryParams['communityId'] : ConnectHelper.getCommunityFromDomain(properties.domain)); return this.check(route);
} }
canLoad(route: Route, segments: UrlSegment[]): Observable<boolean> | Promise<boolean> | boolean { canActivateChild(childRoute: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
const path = '/' + route.path + document.location.search; return this.check(childRoute);
return this.check(ConnectHelper.getCommunityFromPath(path));
} }
} }