1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, CanActivate, CanActivateChild, Router } from '@angular/router';
import { of as observableOf } from 'rxjs';
import { catchError, map } from 'rxjs/operators';
import { MgrModuleService } from '~/app/shared/api/mgr-module.service';
import { Icons } from '~/app/shared/enum/icons.enum';
/**
* This service checks if a route can be activated by executing a
* REST API call to '/ui-api/<uiApiPath>/status'. If the returned response
* states that the module is not available, then the user is redirected
* to the specified <redirectTo> URL path.
*
* A controller implementing this endpoint should return an object of
* the following form:
* {'available': true|false, 'message': null|string}.
*
* The configuration of this guard should look like this:
* const routes: Routes = [
* {
* path: 'rgw/bucket',
* component: RgwBucketListComponent,
* canActivate: [AuthGuardService, ModuleStatusGuardService],
* data: {
* moduleStatusGuardConfig: {
* uiApiPath: 'rgw',
* redirectTo: 'rgw/501'
* }
* }
* },
* ...
*/
@Injectable({
providedIn: 'root'
})
export class ModuleStatusGuardService implements CanActivate, CanActivateChild {
// TODO: Hotfix - remove ALLOWLIST'ing when a generic ErrorComponent is implemented
static readonly ALLOWLIST: string[] = ['501'];
constructor(
private http: HttpClient,
private router: Router,
private mgrModuleService: MgrModuleService
) {}
canActivate(route: ActivatedRouteSnapshot) {
return this.doCheck(route);
}
canActivateChild(childRoute: ActivatedRouteSnapshot) {
return this.doCheck(childRoute);
}
private doCheck(route: ActivatedRouteSnapshot) {
if (route.url.length > 0 && ModuleStatusGuardService.ALLOWLIST.includes(route.url[0].path)) {
return observableOf(true);
}
const config = route.data['moduleStatusGuardConfig'];
let backendCheck = false;
if (config.backend) {
this.mgrModuleService.getConfig('orchestrator').subscribe(
(resp) => {
backendCheck = config.backend === resp['orchestrator'];
},
() => {
this.router.navigate([config.redirectTo]);
return observableOf(false);
}
);
}
return this.http.get(`ui-api/${config.uiApiPath}/status`).pipe(
map((resp: any) => {
if (!resp.available && !backendCheck) {
this.router.navigate([config.redirectTo || ''], {
state: {
header: config.header,
message: resp.message,
section: config.section,
section_info: config.section_info,
button_name: config.button_name,
button_route: config.button_route,
button_title: config.button_title,
uiConfig: config.uiConfig,
uiApiPath: config.uiApiPath,
icon: Icons.wrench,
component: config.component
}
});
}
return resp.available;
}),
catchError(() => {
this.router.navigate([config.redirectTo]);
return observableOf(false);
})
);
}
}
|