2018-04-16 13:49:35 +02:00
|
|
|
import { Injectable } from '@angular/core';
|
2019-02-26 14:57:04 +01:00
|
|
|
import {Router, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, ActivatedRoute} from '@angular/router';
|
2018-04-16 13:49:35 +02:00
|
|
|
import {Observable} from 'rxjs/Observable';
|
|
|
|
import {Session} from '../../login/utils/helper.class';
|
2018-11-01 18:20:05 +01:00
|
|
|
import {LoginErrorCodes} from '../../login/utils/guardHelper.class';
|
2018-04-16 13:49:35 +02:00
|
|
|
import {CommunityService} from '../community/community.service';
|
|
|
|
import { EnvironmentSpecificService} from '../../utils/properties/environment-specific.service';
|
|
|
|
import {ConnectHelper} from '../connectHelper';
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class ConnectSubscriberGuard implements CanActivate {
|
2019-02-26 14:57:04 +01:00
|
|
|
constructor(private router: Router,
|
|
|
|
private communityService: CommunityService,
|
|
|
|
private propertiesService:EnvironmentSpecificService,
|
|
|
|
private route: ActivatedRoute) {}
|
2018-04-16 13:49:35 +02:00
|
|
|
|
|
|
|
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
|
2019-02-26 14:57:04 +01:00
|
|
|
let errorCode = LoginErrorCodes.NOT_SUBSCRIBER;
|
|
|
|
let community = this.route.queryParams["communityId"];
|
2019-02-15 16:03:48 +01:00
|
|
|
if(!community && (typeof document !== 'undefined')){
|
2018-04-16 13:49:35 +02:00
|
|
|
community = ConnectHelper.getCommunityFromDomain(document.location.hostname);
|
|
|
|
}
|
|
|
|
if(!Session.isLoggedIn()){
|
2019-01-25 12:18:38 +01:00
|
|
|
errorCode = LoginErrorCodes.NOT_LOGIN;
|
2018-04-16 13:49:35 +02:00
|
|
|
this.router.navigate(['/user-info'], { queryParams: { "errorCode": errorCode, "redirectUrl": state.url } });
|
|
|
|
return false;
|
|
|
|
}
|
2019-02-26 14:57:04 +01:00
|
|
|
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;
|
|
|
|
}
|
2018-04-16 13:49:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|