2021-02-19 18:50:34 +01:00
|
|
|
import {filter, map, mergeMap, take} from 'rxjs/operators';
|
2020-08-06 22:01:37 +02:00
|
|
|
import {Injectable} from '@angular/core';
|
2021-04-01 16:24:54 +02:00
|
|
|
import {
|
|
|
|
ActivatedRouteSnapshot,
|
|
|
|
CanActivate,
|
|
|
|
CanActivateChild,
|
|
|
|
Router,
|
|
|
|
RouterStateSnapshot,
|
|
|
|
UrlTree
|
|
|
|
} from '@angular/router';
|
2021-02-19 18:50:34 +01:00
|
|
|
import {Observable, of} from 'rxjs';
|
2018-11-01 18:20:05 +01:00
|
|
|
import {LoginErrorCodes} from '../../login/utils/guardHelper.class';
|
2020-08-06 22:01:37 +02:00
|
|
|
import {properties} from "../../../../environments/environment";
|
2021-01-12 17:18:00 +01:00
|
|
|
import {ConnectHelper} from "../connectHelper";
|
2021-02-19 18:50:34 +01:00
|
|
|
import {Session} from "../../login/utils/helper.class";
|
|
|
|
import {UserManagementService} from "../../services/user-management.service";
|
2018-04-16 13:49:35 +02:00
|
|
|
|
|
|
|
@Injectable()
|
2021-04-01 16:24:54 +02:00
|
|
|
export class ConnectSubscriberGuard implements CanActivate, CanActivateChild {
|
2021-02-19 18:50:34 +01:00
|
|
|
|
2019-02-26 14:57:04 +01:00
|
|
|
constructor(private router: Router,
|
2021-02-19 18:50:34 +01:00
|
|
|
private userManagementService: UserManagementService) {
|
2020-08-06 22:01:37 +02:00
|
|
|
}
|
2021-02-19 18:50:34 +01:00
|
|
|
|
2021-04-01 16:24:54 +02:00
|
|
|
check(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {
|
2020-11-12 16:41:03 +01:00
|
|
|
let errorCode = LoginErrorCodes.NOT_SUBSCRIBER;
|
2021-04-01 16:24:54 +02:00
|
|
|
let community;
|
|
|
|
if(properties.isDashboard) {
|
|
|
|
community = route.params['community'];
|
|
|
|
} else {
|
|
|
|
community = ConnectHelper.getCommunityFromDomain(properties.domain);
|
2021-01-12 17:18:00 +01:00
|
|
|
}
|
2021-02-19 18:50:34 +01:00
|
|
|
const authorized = this.userManagementService.getUserInfo(false).pipe(take(1), map(user => {
|
|
|
|
if (user) {
|
|
|
|
if (Session.isSubscribedTo('community', community, user)) {
|
|
|
|
return of(true);
|
|
|
|
}
|
2020-08-06 22:01:37 +02:00
|
|
|
}
|
2021-02-19 18:50:34 +01:00
|
|
|
return of(false);
|
|
|
|
}), mergeMap(authorized => {
|
|
|
|
return authorized;
|
2020-08-06 22:01:37 +02:00
|
|
|
}));
|
2021-02-19 18:50:34 +01:00
|
|
|
authorized.pipe(filter(authorized => !authorized)).subscribe(() => {
|
|
|
|
this.router.navigate(['/user-info'], {
|
|
|
|
queryParams: {
|
|
|
|
'errorCode': errorCode,
|
2021-04-01 16:24:54 +02:00
|
|
|
'redirectUrl': state.url
|
2021-02-19 18:50:34 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
});
|
|
|
|
return authorized;
|
2020-08-06 22:01:37 +02:00
|
|
|
}
|
|
|
|
|
2018-04-16 13:49:35 +02:00
|
|
|
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
|
2021-04-01 16:24:54 +02:00
|
|
|
return this.check(route, state);
|
2019-02-26 16:25:52 +01:00
|
|
|
}
|
2020-08-06 22:01:37 +02:00
|
|
|
|
2021-04-01 16:24:54 +02:00
|
|
|
canActivateChild(childRoute: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
|
|
|
|
return this.check(childRoute, state);
|
|
|
|
}
|
2018-04-16 13:49:35 +02:00
|
|
|
}
|