Revision 59825
Added by Argiro Kokogiannaki over 3 years ago
connectSubscriber.guard.ts | ||
---|---|---|
1 |
import {filter, map, mergeMap, take, tap} from 'rxjs/operators';
|
|
1 |
import {take, tap} from 'rxjs/operators'; |
|
2 | 2 |
import {Injectable} from '@angular/core'; |
3 |
import {Router, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, CanLoad, Route} from '@angular/router'; |
|
4 |
import {Observable, of} from 'rxjs'; |
|
5 |
import {Session} from '../../login/utils/helper.class'; |
|
3 |
import {Router, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot} from '@angular/router'; |
|
4 |
import {Observable} from 'rxjs'; |
|
6 | 5 |
import {LoginErrorCodes} from '../../login/utils/guardHelper.class'; |
7 |
import {CommunityService} from '../community/community.service'; |
|
8 |
import {ConnectHelper} from '../connectHelper'; |
|
9 |
import {UserManagementService} from "../../services/user-management.service"; |
|
10 | 6 |
import {SubscribeService} from "../../utils/subscribe/subscribe.service"; |
11 | 7 |
import {properties} from "../../../../environments/environment"; |
12 | 8 |
|
13 | 9 |
@Injectable() |
14 | 10 |
export class ConnectSubscriberGuard implements CanActivate { |
15 | 11 |
constructor(private router: Router, |
16 |
private communityService: CommunityService, |
|
17 |
private subscribeService: SubscribeService, |
|
18 |
private userManagementService: UserManagementService) { |
|
12 |
private subscribeService: SubscribeService) { |
|
19 | 13 |
} |
20 |
|
|
21 |
check(community: string, path: string): Observable<boolean> | boolean { |
|
22 |
let errorCode = LoginErrorCodes.NOT_LOGIN; |
|
23 |
let email = null; |
|
24 |
const subscribed = this.userManagementService.getUserInfo(false).pipe(take(1),map(user => { |
|
25 |
if (user) { |
|
26 |
errorCode = LoginErrorCodes.NOT_SUBSCRIBER; |
|
27 |
email = user.email; |
|
28 |
let communityDomain = ConnectHelper.getCommunityFromDomain(properties.domain); |
|
29 |
if (communityDomain) { |
|
30 |
community = communityDomain; |
|
31 |
} |
|
32 |
if (Session.isSubscribedTo('community', community, user)) { |
|
33 |
return of(true); |
|
34 |
} |
|
35 |
return this.subscribeService.isSubscribedToCommunity(properties, community).pipe(take(1)); |
|
36 |
} else { |
|
37 |
return of(false); |
|
14 |
|
|
15 |
check(community: string, path: string): Observable<boolean> { |
|
16 |
let errorCode = LoginErrorCodes.NOT_SUBSCRIBER; |
|
17 |
return this.subscribeService.isSubscribedToCommunity(properties, community).pipe(take(1), tap(subscribed =>{ |
|
18 |
if(!subscribed){ |
|
19 |
this.router.navigate(['/user-info'], { |
|
20 |
queryParams: { |
|
21 |
'errorCode': errorCode, |
|
22 |
'redirectUrl': path |
|
23 |
} |
|
24 |
}); |
|
38 | 25 |
} |
39 |
}), mergeMap(authorized => { |
|
40 |
return authorized; |
|
41 | 26 |
})); |
42 |
subscribed.pipe(filter(subscribed => !subscribed)).subscribe(() => { |
|
43 |
this.router.navigate(['/user-info'], { |
|
44 |
queryParams: { |
|
45 |
'errorCode': errorCode, |
|
46 |
'redirectUrl': path |
|
47 |
} |
|
48 |
}) |
|
49 |
}); |
|
50 |
return subscribed; |
|
51 | 27 |
} |
52 | 28 |
|
53 | 29 |
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean { |
Also available in: Unified diff
[Library|Trunk]
code clean up - Connect related:
-remove unused imports, code, files (Old search pages for results, dataproviders, map search page, etc)
-remove Freeguard from modules
-unsubscribe all subscriptions
- remove old properties json files