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

46 lines
2.0 KiB
TypeScript

import { Injectable } from '@angular/core';
import { Router,CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot} from '@angular/router';
import {Observable} from 'rxjs/Observable';
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 { mergeMap } from 'rxjs/operators';
import {ConnectHelper} from '../connectHelper';
@Injectable()
export class ConnectSubscriberGuard implements CanActivate {
constructor(private router: Router, private communityService: CommunityService, private propertiesService:EnvironmentSpecificService ) {}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
//console.log(state.url);
var user;
var authorized = false;
var errorCode = LoginErrorCodes.NOT_SUBSCRIBER;
let community = (route.queryParams["communityId"]);
if(!community && (typeof document !== 'undefined')){
community = ConnectHelper.getCommunityFromDomain(document.location.hostname);
}
if(!Session.isLoggedIn()){
errorCode = LoginErrorCodes.NOT_LOGIN;
this.router.navigate(['/user-info'], { queryParams: { "errorCode": errorCode, "redirectUrl": state.url } });
return false;
}
// if(Session.isPortalAdministrator() || Session.isCommunityCurator() || Session.isClaimsCurator()){
// authorized = true;
// return true;
//
// }else {
let obs = this.propertiesService.subscribeEnvironment().mergeMap(properties => {
return this.communityService.isSubscribedToCommunity( community, Session.getUserEmail(), properties["adminToolsAPIURL"])});
obs.filter(enabled => !enabled)
.subscribe(() => this.router.navigate(['/user-info'], { queryParams: { "errorCode": errorCode, "redirectUrl": state.url } }));
return obs;
// }
}
}