kibana/x-pack/plugins/spaces/server/lib/toggle_ui_capabilities.ts
Larry Gregory 9c2fa892ca
[7.x] Spaces - Client NP Migration, Phase 1 (#40856) (#53113)
* Spaces - Client NP Migration, Phase 1 (#40856)

* shimming NP for spaces client-side plugin

* refresh active space in nav control when updated

* fix advanced settings screen

* allow npStart from unauthed routes

* use NP for deriving space management url

* remove security's usage of SpacesManager

* remove usages of ui/capabilities

* fix tests

* implement NP plugin interface

* remove hack in favor of convention in migration guide

* shim feature catalogue registration

* streamline nav control, and handle async loading more gracefully

* adding opaqueId

* fixes from merge

* fix merge from master

* fixing merge from master

* move _active_space route to NP

* moving to the NP feature catalogue registry

* moving setup to setup phase

* optimizing active space retrieval

* reverting test isolation change

* Apply suggestions from code review

Co-Authored-By: Aleh Zasypkin <aleh.zasypkin@gmail.com>

* removing unnecessary PluginInitializerContext

* updating advanced settings subtitle

* using NP anonymousPaths service

* additional nav_control_popover cleanup

* additional cleanup

* testing out onActiveSpaceChange$ property

* make the linter happy

* make the type checker happy

* fixing types

* fix merge from master

* spaces LP init should run on all pages, not just the kibana app

* address nits

* fix infra/logs, and the spaces disabled scenario

* fix typescript errors

* revert changes to infra plugin

* reintroducing activeSpace injected var for legacy plugins

* fixing react deprecation warning and unhandled promise rejection

* restore activeSpace default var

* spaces does not need to check its own enabled status

* fix from merge

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>

* fix backport merge
2019-12-16 12:13:05 -05:00

71 lines
2.3 KiB
TypeScript

/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import _ from 'lodash';
import { UICapabilities } from 'ui/capabilities';
import { Feature } from '../../../../plugins/features/server';
import { Space } from '../../common/model/space';
export function toggleUICapabilities(
features: Feature[],
capabilities: UICapabilities,
activeSpace: Space
) {
const clonedCapabilities = _.cloneDeep(capabilities);
toggleDisabledFeatures(features, clonedCapabilities, activeSpace);
return clonedCapabilities;
}
function toggleDisabledFeatures(
features: Feature[],
capabilities: UICapabilities,
activeSpace: Space
) {
const disabledFeatureKeys = activeSpace.disabledFeatures;
const disabledFeatures = disabledFeatureKeys
.map(key => features.find(feature => feature.id === key))
.filter(feature => typeof feature !== 'undefined') as Feature[];
const navLinks = capabilities.navLinks;
const catalogueEntries = capabilities.catalogue;
const managementItems = capabilities.management;
for (const feature of disabledFeatures) {
// Disable associated navLink, if one exists
if (feature.navLinkId && navLinks.hasOwnProperty(feature.navLinkId)) {
navLinks[feature.navLinkId] = false;
}
// Disable associated catalogue entries
const privilegeCatalogueEntries = feature.catalogue || [];
privilegeCatalogueEntries.forEach(catalogueEntryId => {
catalogueEntries[catalogueEntryId] = false;
});
// Disable associated management items
const privilegeManagementSections = feature.management || {};
Object.entries(privilegeManagementSections).forEach(([sectionId, sectionItems]) => {
sectionItems.forEach(item => {
if (
managementItems.hasOwnProperty(sectionId) &&
managementItems[sectionId].hasOwnProperty(item)
) {
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;
});
}
}
}