openaire-library/connect/communityGuard/isCommunity.guard.ts

38 lines
1.1 KiB
TypeScript

import { Injectable } from '@angular/core';
import {
Router,
CanActivate,
ActivatedRouteSnapshot,
RouterStateSnapshot,
CanLoad, Route, UrlSegment
} from '@angular/router';
import {Observable} from 'rxjs';
import {ConnectHelper} from '../connectHelper';
import {properties} from "../../../../environments/environment";
@Injectable()
export class IsCommunity implements CanActivate, CanLoad {
constructor(private router: Router) {
}
check(community: string): Observable<boolean> | boolean {
if (community && community !== 'undefined') {
return true;
} else {
this.router.navigate(['error']);
return false;
}
}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
return this.check((route.queryParams['communityId']) ? route.queryParams['communityId'] : ConnectHelper.getCommunityFromDomain(properties.domain));
}
canLoad(route: Route, segments: UrlSegment[]): Observable<boolean> | Promise<boolean> | boolean {
const path = '/' + route.path + document.location.search;
return this.check(ConnectHelper.getCommunityFromPath(path));
}
}