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

37 lines
1015 B
TypeScript

import { Injectable } from '@angular/core';
import {
Router,
CanActivate,
ActivatedRouteSnapshot,
RouterStateSnapshot,
CanLoad, Route
} from '@angular/router';
import {Observable} from 'rxjs/Observable';
import 'rxjs/add/operator/filter';
import {ConnectHelper} from '../connectHelper';
@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(['errorcommunity']);
return false;
}
}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
return this.check(route.queryParams['communityId']);
}
canLoad(route: Route): Observable<boolean> | Promise<boolean> | boolean {
const path = '/' + route.path + document.location.search;
return this.check(ConnectHelper.getCommunityFromPath(path));
}
}