1
|
import {Injectable} from '@angular/core';
|
2
|
import {ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot} from '@angular/router';
|
3
|
import {Observable} from 'rxjs/Observable';
|
4
|
import {Session} from './utils/helper.class';
|
5
|
import {LoginErrorCodes} from './utils/guardHelper.class';
|
6
|
import {catchError, filter, map, mergeMap} from "rxjs/operators";
|
7
|
import {UserManagementService} from "../services/user-management.service";
|
8
|
import {UserRegistryService} from "../services/user-registry.service";
|
9
|
import {of} from "rxjs";
|
10
|
import {properties} from "../../../environments/environment";
|
11
|
import {ConnectHelper} from "../connect/connectHelper";
|
12
|
|
13
|
@Injectable()
|
14
|
export class VerificationGuard implements CanActivate {
|
15
|
|
16
|
constructor(private router: Router,
|
17
|
private userRegistryService: UserRegistryService,
|
18
|
private userManagementService: UserManagementService) {
|
19
|
}
|
20
|
|
21
|
check(path: string, id: string, type: string, entity: string): Observable<boolean> | boolean {
|
22
|
if (Session.isLoggedIn()) {
|
23
|
return this.userManagementService.getUserInfo(false).pipe(map(user => {
|
24
|
if(user) {
|
25
|
if(id) {
|
26
|
return this.userRegistryService.getInvitation(id).pipe(map(invitation => {
|
27
|
if(invitation.type !== type || invitation.entity !== entity) {
|
28
|
this.router.navigate(['/error'], {queryParams: {'page': path}});
|
29
|
return false;
|
30
|
} else {
|
31
|
return true;
|
32
|
}
|
33
|
}), catchError(error => {
|
34
|
if(error.status === 404) {
|
35
|
this.router.navigate(['/error'], {queryParams: {'page': path}});
|
36
|
} else {
|
37
|
this.router.navigate(['/user-info'], {queryParams: {'errorCode': LoginErrorCodes.NOT_AUTHORIZED, 'redirectUrl': path}});
|
38
|
}
|
39
|
return of(false);
|
40
|
}));
|
41
|
} else {
|
42
|
this.router.navigate(['/error'], {queryParams: {'page': path}});
|
43
|
return of(false);
|
44
|
}
|
45
|
} else {
|
46
|
this.router.navigate(['/user-info'], {queryParams: {'errorCode': LoginErrorCodes.NOT_LOGIN, 'redirectUrl': path}});
|
47
|
return of(false);
|
48
|
}
|
49
|
}), mergeMap(authorized => {
|
50
|
return authorized;
|
51
|
}));
|
52
|
} else {
|
53
|
this.router.navigate(['/user-info'], {queryParams: {'errorCode': LoginErrorCodes.NOT_LOGIN,'redirectUrl': path}});
|
54
|
return false;
|
55
|
}
|
56
|
}
|
57
|
|
58
|
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
|
59
|
if(properties.dashboard === "connect" && properties.environment === 'development') {
|
60
|
let communityId = ConnectHelper.getCommunityFromDomain(properties.domain);
|
61
|
communityId = (communityId)?communityId:route.queryParams['communityId'];
|
62
|
return this.check(state.url, route.params.id, 'community', communityId);
|
63
|
} else {
|
64
|
return false;
|
65
|
}
|
66
|
}
|
67
|
}
|