1
|
import {filter, map, mergeMap, take} from 'rxjs/operators';
|
2
|
import {Injectable} from '@angular/core';
|
3
|
import {
|
4
|
ActivatedRouteSnapshot,
|
5
|
CanActivate,
|
6
|
CanActivateChild,
|
7
|
Router,
|
8
|
RouterStateSnapshot,
|
9
|
UrlTree
|
10
|
} from '@angular/router';
|
11
|
import {Observable, of} from 'rxjs';
|
12
|
import {LoginErrorCodes} from '../../login/utils/guardHelper.class';
|
13
|
import {properties} from "../../../../environments/environment";
|
14
|
import {ConnectHelper} from "../connectHelper";
|
15
|
import {Session} from "../../login/utils/helper.class";
|
16
|
import {UserManagementService} from "../../services/user-management.service";
|
17
|
|
18
|
@Injectable()
|
19
|
export class ConnectSubscriberGuard implements CanActivate, CanActivateChild {
|
20
|
|
21
|
constructor(private router: Router,
|
22
|
private userManagementService: UserManagementService) {
|
23
|
}
|
24
|
|
25
|
check(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {
|
26
|
let errorCode = LoginErrorCodes.NOT_SUBSCRIBER;
|
27
|
let community;
|
28
|
if(properties.isDashboard) {
|
29
|
community = route.params['community'];
|
30
|
} else {
|
31
|
community = ConnectHelper.getCommunityFromDomain(properties.domain);
|
32
|
}
|
33
|
const authorized = this.userManagementService.getUserInfo(false).pipe(take(1), map(user => {
|
34
|
if (user) {
|
35
|
if (Session.isSubscribedTo('community', community, user)) {
|
36
|
return of(true);
|
37
|
}
|
38
|
}
|
39
|
return of(false);
|
40
|
}), mergeMap(authorized => {
|
41
|
return authorized;
|
42
|
}));
|
43
|
authorized.pipe(filter(authorized => !authorized)).subscribe(() => {
|
44
|
this.router.navigate(['/user-info'], {
|
45
|
queryParams: {
|
46
|
'errorCode': errorCode,
|
47
|
'redirectUrl': state.url
|
48
|
}
|
49
|
})
|
50
|
});
|
51
|
return authorized;
|
52
|
}
|
53
|
|
54
|
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
|
55
|
return this.check(route, state);
|
56
|
}
|
57
|
|
58
|
canActivateChild(childRoute: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
|
59
|
return this.check(childRoute, state);
|
60
|
}
|
61
|
}
|