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

48 lines
2.0 KiB
TypeScript

import {filter, mergeMap} from 'rxjs/operators';
import { Injectable } from '@angular/core';
import {Router, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, CanLoad, Route} from '@angular/router';
import {Observable} from 'rxjs';
import {Session} from '../../login/utils/helper.class';
import {LoginErrorCodes} from '../../login/utils/guardHelper.class';
import {CommunityService} from '../community/community.service';
import { EnvironmentSpecificService} from '../../utils/properties/environment-specific.service';
import {ConnectHelper} from '../connectHelper';
@Injectable()
export class ConnectSubscriberGuard implements CanActivate {
constructor(private router: Router,
private communityService: CommunityService,
private propertiesService: EnvironmentSpecificService) {}
check(community: string, path: string): Observable<boolean> | boolean {
let errorCode = LoginErrorCodes.NOT_SUBSCRIBER;
if (!Session.isLoggedIn()) {
errorCode = LoginErrorCodes.NOT_LOGIN;
this.router.navigate(['/user-info'], {queryParams: {'errorCode': errorCode, 'redirectUrl': path, communityId:community}});
return false;
} else {
const obs = this.propertiesService.subscribeEnvironment().pipe(mergeMap(properties => {
if(!community){
community = ConnectHelper.getCommunityFromDomain(properties.domain);
}
return this.communityService.isSubscribedToCommunity( community, Session.getUserEmail(), properties["adminToolsAPIURL"])
}));
obs.pipe(filter(enabled => !enabled))
.subscribe(() => this.router.navigate(['/user-info'], {
queryParams: {
'errorCode': errorCode,
'redirectUrl': path,
communityId:community
}
}));
return obs;
}
}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
return this.check(route.queryParams['communityId'], state.url);
}
}