Revision 54906
Added by Konstantinos Triantafyllou about 5 years ago
isRouteEnabled.guard.ts | ||
---|---|---|
1 | 1 |
import { Injectable } from '@angular/core'; |
2 |
import {ActivatedRoute, Router,CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot} from '@angular/router'; |
|
2 |
import { |
|
3 |
ActivatedRoute, |
|
4 |
Router, |
|
5 |
CanActivate, |
|
6 |
CanLoad, |
|
7 |
ActivatedRouteSnapshot, |
|
8 |
RouterStateSnapshot, |
|
9 |
Route |
|
10 |
} from '@angular/router'; |
|
3 | 11 |
import {Observable} from 'rxjs/Observable'; |
4 | 12 |
import 'rxjs/add/operator/filter'; |
5 | 13 |
import { ConfigurationService } from '../utils/configuration/configuration.service'; |
... | ... | |
7 | 15 |
import {ConnectHelper} from '../connect/connectHelper'; |
8 | 16 |
|
9 | 17 |
@Injectable() |
10 |
export class IsRouteEnabled implements CanActivate { |
|
18 |
export class IsRouteEnabled implements CanActivate, CanLoad {
|
|
11 | 19 |
|
12 | 20 |
constructor(private route: ActivatedRoute,private router: Router, private config: ConfigurationService, private propertiesService:EnvironmentSpecificService ) {} |
13 | 21 |
|
22 |
check(path: string): Observable<boolean> | boolean { |
|
23 |
let customRedirect = this.route.data['redirect']; |
|
24 |
let community = this.route.queryParams["communityId"]; |
|
25 |
if(!community && this.route.data['community']){ // for openaire |
|
26 |
community = this.route.data['community']; |
|
27 |
} |
|
28 |
if(!community && typeof document != 'undefined'){ |
|
29 |
community = ConnectHelper.getCommunityFromDomain(document.location.hostname); |
|
30 |
} |
|
31 |
if(community){ |
|
32 |
let redirect = customRedirect ? customRedirect : '/error'; |
|
33 |
let obs = this.propertiesService.subscribeEnvironment().map(res=>res["adminToolsAPIURL"]).mergeMap(url => { |
|
34 |
return this.config.isPageEnabled(url, community,"/" + path.split("?")[0].substring(1))}); |
|
35 |
obs.filter(enabled => !enabled) |
|
36 |
.subscribe(() => this.router.navigate([redirect], { queryParams: { "page": path } })); |
|
37 |
return obs; |
|
38 |
} |
|
39 |
return true; |
|
40 |
} |
|
41 |
|
|
14 | 42 |
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean { |
43 |
return this.check(state.url); |
|
44 |
} |
|
15 | 45 |
|
16 |
let customRedirect = route.data['redirect']; |
|
17 |
let community = route.queryParams["communityId"]; |
|
18 |
if(!community && route.data['community']){ // for openaire |
|
19 |
let community = route.data['community'] |
|
20 |
} |
|
21 |
if(!community && typeof document != 'undefined'){ |
|
22 |
community = ConnectHelper.getCommunityFromDomain(document.location.hostname); |
|
23 |
} |
|
24 |
|
|
25 |
if(community){ |
|
26 |
let redirect = !!customRedirect ? customRedirect : '/error'; |
|
27 |
let obs = this.propertiesService.subscribeEnvironment().map(res=>res["adminToolsAPIURL"]).mergeMap(url => { |
|
28 |
return this.config.isPageEnabled(url, community,"/"+state.url.split("?")[0].substring(1))}); |
|
29 |
obs.filter(enabled => !enabled) |
|
30 |
.subscribe(() => this.router.navigate([redirect], { queryParams: { "page": state.url } })); |
|
31 |
return obs; |
|
32 |
} |
|
33 |
return true; |
|
34 |
} |
|
46 |
canLoad(route: Route): Observable<boolean> | Promise<boolean> | boolean { |
|
47 |
return this.check('/' + route.path); |
|
48 |
} |
|
35 | 49 |
} |
Also available in: Unified diff
[Trunk|Library]: All guards implement CanLoad. Need to be tested! CanActivate is now enabled