2018-04-16 13:49:35 +02:00
|
|
|
import { Injectable } from '@angular/core';
|
2019-02-26 16:25:52 +01:00
|
|
|
import {Router, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, CanLoad, Route} from '@angular/router';
|
|
|
|
import {Observable} from 'rxjs/Observable';
|
2018-04-16 13:49:35 +02:00
|
|
|
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()
|
2019-02-27 11:39:13 +01:00
|
|
|
export class ConnectSubscriberGuard implements CanActivate, CanLoad {
|
2019-02-26 14:57:04 +01:00
|
|
|
constructor(private router: Router,
|
|
|
|
private communityService: CommunityService,
|
2019-02-26 16:25:52 +01:00
|
|
|
private propertiesService: EnvironmentSpecificService) {}
|
|
|
|
|
2019-02-27 11:39:13 +01:00
|
|
|
check(community: string, path: string): Observable<boolean> | boolean {
|
2019-02-26 16:25:52 +01:00
|
|
|
let errorCode = LoginErrorCodes.NOT_SUBSCRIBER;
|
|
|
|
if (!Session.isLoggedIn()) {
|
|
|
|
errorCode = LoginErrorCodes.NOT_LOGIN;
|
|
|
|
this.router.navigate(['/user-info'], {queryParams: {'errorCode': errorCode, 'redirectUrl': path}});
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
const obs = this.propertiesService.subscribeEnvironment().mergeMap(properties => {
|
2019-03-01 12:29:52 +01:00
|
|
|
if(!community){
|
|
|
|
community = ConnectHelper.getCommunityFromDomain(properties.domain);
|
|
|
|
}
|
|
|
|
return this.communityService.isSubscribedToCommunity( community, Session.getUserEmail(), properties["adminToolsAPIURL"])
|
2019-02-26 16:25:52 +01:00
|
|
|
});
|
|
|
|
obs.filter(enabled => !enabled)
|
|
|
|
.subscribe(() => this.router.navigate(['/user-info'], {
|
|
|
|
queryParams: {
|
|
|
|
'errorCode': errorCode,
|
|
|
|
'redirectUrl': path
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
return obs;
|
|
|
|
}
|
|
|
|
}
|
2018-04-16 13:49:35 +02:00
|
|
|
|
|
|
|
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
|
2019-02-27 11:39:13 +01:00
|
|
|
return this.check(route.queryParams['communityId'], state.url);
|
2019-02-26 16:25:52 +01:00
|
|
|
}
|
|
|
|
canLoad(route: Route): Observable<boolean> | Promise<boolean> | boolean {
|
2019-02-27 11:39:13 +01:00
|
|
|
const path = '/' + route.path;
|
|
|
|
return this.check(ConnectHelper.getCommunityFromPath(path), path);
|
2019-03-01 12:29:52 +01:00
|
|
|
|
2018-04-16 13:49:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|