2019-06-03 15:20:36 +02:00
|
|
|
|
2019-09-13 09:01:19 +02:00
|
|
|
import {filter, map, mergeMap} from 'rxjs/operators';
|
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';
|
2019-09-13 09:01:19 +02:00
|
|
|
import {Observable, of} from 'rxjs';
|
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';
|
2019-09-13 09:01:19 +02:00
|
|
|
import {UserManagementService} from "../../services/user-management.service";
|
2018-04-16 13:49:35 +02:00
|
|
|
|
|
|
|
@Injectable()
|
2019-06-03 15:20:36 +02:00
|
|
|
export class ConnectSubscriberGuard implements CanActivate {
|
2019-02-26 14:57:04 +01:00
|
|
|
constructor(private router: Router,
|
|
|
|
private communityService: CommunityService,
|
2019-09-13 09:01:19 +02:00
|
|
|
private userManagementService: UserManagementService,
|
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-09-13 11:38:21 +02:00
|
|
|
let errorCode = LoginErrorCodes.NOT_LOGIN;
|
2019-09-13 09:01:19 +02:00
|
|
|
let email = null;
|
|
|
|
const subscribed = this.propertiesService.subscribeEnvironment().pipe(map(res => res), mergeMap(properties => {
|
2019-11-08 12:01:50 +01:00
|
|
|
return typeof document !== 'undefined' && this.userManagementService.getUserInfo(properties.userInfoUrl).pipe(map(user => {
|
2019-09-13 09:01:19 +02:00
|
|
|
if (user) {
|
2019-09-13 11:38:21 +02:00
|
|
|
errorCode = LoginErrorCodes.NOT_SUBSCRIBER;
|
2019-09-13 09:01:19 +02:00
|
|
|
email = user.email;
|
|
|
|
let communityDomain = ConnectHelper.getCommunityFromDomain(properties.domain);
|
|
|
|
if(communityDomain) {
|
|
|
|
community = communityDomain;
|
|
|
|
}
|
|
|
|
return this.communityService.isSubscribedToCommunity( community, email, properties["adminToolsAPIURL"])
|
|
|
|
} else {
|
|
|
|
return of(false);
|
2019-03-01 12:29:52 +01:00
|
|
|
}
|
2019-09-13 09:01:19 +02:00
|
|
|
}), mergeMap( authorized => {
|
|
|
|
return authorized;
|
2019-06-03 15:20:36 +02:00
|
|
|
}));
|
2019-09-13 09:01:19 +02:00
|
|
|
}));
|
|
|
|
subscribed.pipe(filter(subscribed => !subscribed)).subscribe(() => {
|
|
|
|
this.router.navigate(['/user-info'], {
|
|
|
|
queryParams: {
|
|
|
|
'errorCode': errorCode,
|
|
|
|
'redirectUrl': path
|
|
|
|
}
|
|
|
|
})});
|
|
|
|
return subscribed;
|
2019-02-26 16:25:52 +01:00
|
|
|
}
|
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
|
|
|
}
|
2018-04-16 13:49:35 +02:00
|
|
|
|
|
|
|
}
|