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

41 lines
1.4 KiB
TypeScript

import { Injectable } from '@angular/core';
import {
Router,
CanActivate,
ActivatedRouteSnapshot,
RouterStateSnapshot,
CanLoad,
Route
} from '@angular/router';
import {Observable} from 'rxjs/Observable';
import {CommunityService} from '../community/community.service';
import { EnvironmentSpecificService} from '../../utils/properties/environment-specific.service';
import {ConnectHelper} from '../connectHelper';
@Injectable()
export class ConnectRIGuard implements CanActivate, CanLoad {
constructor(private router: Router,
private communityService: CommunityService,
private propertiesService: EnvironmentSpecificService) {
}
check(community: string): Observable<boolean> | boolean {
const obs = this.propertiesService.subscribeEnvironment().mergeMap(properties => {
return this.communityService.isRIType(properties, properties['communityAPI'] + community);
});
obs.filter(enabled => !enabled)
.subscribe(() => this.router.navigate(['errorcommunity']));
return obs;
}
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));
}
}