forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcapabilities_switcher.ts
134 lines (115 loc) · 4.47 KB
/
capabilities_switcher.ts
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import _ from 'lodash';
import type { Capabilities, CapabilitiesSwitcher, CoreSetup, Logger } from 'src/core/server';
import type { KibanaFeature } from '../../../features/server';
import type { Space } from '../../common';
import type { PluginsStart } from '../plugin';
import type { SpacesServiceStart } from '../spaces_service';
export function setupCapabilitiesSwitcher(
core: CoreSetup<PluginsStart>,
getSpacesService: () => SpacesServiceStart,
logger: Logger
): CapabilitiesSwitcher {
return async (request, capabilities, useDefaultCapabilities) => {
const isAuthRequiredOrOptional = !request.route.options.authRequired;
const shouldNotToggleCapabilities = isAuthRequiredOrOptional || useDefaultCapabilities;
if (shouldNotToggleCapabilities) {
return capabilities;
}
try {
const [activeSpace, [, { features }]] = await Promise.all([
getSpacesService().getActiveSpace(request),
core.getStartServices(),
]);
const registeredFeatures = features.getKibanaFeatures();
// try to retrieve capabilities for authenticated or "maybe authenticated" users
return toggleCapabilities(registeredFeatures, capabilities, activeSpace);
} catch (e) {
logger.debug(`Error toggling capabilities for request to ${request.url.pathname}: ${e}`);
return capabilities;
}
};
}
function toggleCapabilities(
features: KibanaFeature[],
capabilities: Capabilities,
activeSpace: Space
) {
const clonedCapabilities = _.cloneDeep(capabilities);
toggleDisabledFeatures(features, clonedCapabilities, activeSpace);
return clonedCapabilities;
}
function toggleDisabledFeatures(
features: KibanaFeature[],
capabilities: Capabilities,
activeSpace: Space
) {
const disabledFeatureKeys = activeSpace.disabledFeatures;
const [enabledFeatures, disabledFeatures] = features.reduce(
(acc, feature) => {
if (disabledFeatureKeys.includes(feature.id)) {
return [acc[0], [...acc[1], feature]];
}
return [[...acc[0], feature], acc[1]];
},
[[], []] as [KibanaFeature[], KibanaFeature[]]
);
const navLinks = capabilities.navLinks;
const catalogueEntries = capabilities.catalogue;
const managementItems = capabilities.management;
const enabledAppEntries = new Set(enabledFeatures.flatMap((ef) => ef.app ?? []));
const enabledCatalogueEntries = new Set(enabledFeatures.flatMap((ef) => ef.catalogue ?? []));
const enabledManagementEntries = enabledFeatures.reduce((acc, feature) => {
const sections = Object.entries(feature.management ?? {});
sections.forEach((section) => {
if (!acc.has(section[0])) {
acc.set(section[0], []);
}
acc.get(section[0])!.push(...section[1]);
});
return acc;
}, new Map<string, string[]>());
for (const feature of disabledFeatures) {
// Disable associated navLink, if one exists
feature.app.forEach((app) => {
if (navLinks.hasOwnProperty(app) && !enabledAppEntries.has(app)) {
navLinks[app] = false;
}
});
// Disable associated catalogue entries
const privilegeCatalogueEntries = feature.catalogue || [];
privilegeCatalogueEntries.forEach((catalogueEntryId) => {
if (!enabledCatalogueEntries.has(catalogueEntryId)) {
catalogueEntries[catalogueEntryId] = false;
}
});
// Disable associated management items
const privilegeManagementSections = feature.management || {};
Object.entries(privilegeManagementSections).forEach(([sectionId, sectionItems]) => {
sectionItems.forEach((item) => {
const enabledManagementEntriesSection = enabledManagementEntries.get(sectionId);
if (
managementItems.hasOwnProperty(sectionId) &&
managementItems[sectionId].hasOwnProperty(item)
) {
const isEnabledElsewhere = (enabledManagementEntriesSection ?? []).includes(item);
if (!isEnabledElsewhere) {
managementItems[sectionId][item] = false;
}
}
});
});
// Disable "sub features" that match the disabled feature
if (capabilities.hasOwnProperty(feature.id)) {
const capability = capabilities[feature.id];
Object.keys(capability).forEach((featureKey) => {
capability[featureKey] = false;
});
}
}
}