From c51859c82735c22f17dbc79c8cd03b6e3f4de140 Mon Sep 17 00:00:00 2001
From: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
Date: Fri, 27 Sep 2024 06:23:13 +1000
Subject: [PATCH] [8.x] [Spaces] Filtering out features that do not have space
scope (#194157) (#194212)
# Backport
This will backport the following commits from `main` to `8.x`:
- [[Spaces] Filtering out features that do not have space scope
(#194157)](https://github.com/elastic/kibana/pull/194157)
### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)
Co-authored-by: Elena Shostak <165678770+elena-shostak@users.noreply.github.com>
---
.../create_space/create_space_page.test.tsx | 58 +++++++++++++++++++
.../create_space/create_space_page.tsx | 11 +++-
2 files changed, 67 insertions(+), 2 deletions(-)
diff --git a/x-pack/plugins/spaces/public/management/create_space/create_space_page.test.tsx b/x-pack/plugins/spaces/public/management/create_space/create_space_page.test.tsx
index 4c8617ff007b8..757e882bf741c 100644
--- a/x-pack/plugins/spaces/public/management/create_space/create_space_page.test.tsx
+++ b/x-pack/plugins/spaces/public/management/create_space/create_space_page.test.tsx
@@ -14,6 +14,7 @@ import { act } from 'react-dom/test-utils';
import { DEFAULT_APP_CATEGORIES } from '@kbn/core/public';
import { notificationServiceMock, scopedHistoryMock } from '@kbn/core/public/mocks';
+import { KibanaFeatureScope } from '@kbn/features-plugin/common';
import { KibanaFeature } from '@kbn/features-plugin/public';
import { featuresPluginMock } from '@kbn/features-plugin/public/mocks';
import { findTestSubject, mountWithIntl } from '@kbn/test-jest-helpers';
@@ -48,6 +49,15 @@ featuresStart.getFeatures.mockResolvedValue([
app: [],
category: DEFAULT_APP_CATEGORIES.kibana,
privileges: null,
+ scope: [KibanaFeatureScope.Spaces, KibanaFeatureScope.Security],
+ }),
+ new KibanaFeature({
+ id: 'feature-2',
+ name: 'feature 2',
+ app: [],
+ category: DEFAULT_APP_CATEGORIES.kibana,
+ privileges: null,
+ scope: [KibanaFeatureScope.Security],
}),
]);
@@ -641,6 +651,54 @@ describe('ManageSpacePage', () => {
expect(spacesManager.updateSpace).toHaveBeenCalledTimes(1);
});
+
+ it('shows only features with space scope', async () => {
+ const spacesManager = spacesManagerMock.create();
+ spacesManager.getSpace = jest.fn().mockResolvedValue({
+ id: 'my-space',
+ name: 'Existing Space',
+ description: 'hey an existing space',
+ color: '#aabbcc',
+ initials: 'AB',
+ disabledFeatures: [],
+ });
+ spacesManager.getActiveSpace = jest.fn().mockResolvedValue(space);
+
+ const wrapper = mountWithIntl(
+
+ );
+
+ await waitFor(() => {
+ wrapper.update();
+ expect(spacesManager.getSpace).toHaveBeenCalledWith('my-space');
+ });
+
+ expect(wrapper.state('features')).toEqual([
+ new KibanaFeature({
+ id: 'feature-1',
+ name: 'feature 1',
+ app: [],
+ category: DEFAULT_APP_CATEGORIES.kibana,
+ privileges: null,
+ scope: [KibanaFeatureScope.Spaces, KibanaFeatureScope.Security],
+ }),
+ ]);
+ });
});
function updateSpace(
diff --git a/x-pack/plugins/spaces/public/management/create_space/create_space_page.tsx b/x-pack/plugins/spaces/public/management/create_space/create_space_page.tsx
index e8204a53fe345..ed0e52edd6c4b 100644
--- a/x-pack/plugins/spaces/public/management/create_space/create_space_page.tsx
+++ b/x-pack/plugins/spaces/public/management/create_space/create_space_page.tsx
@@ -22,6 +22,7 @@ import React, { Component } from 'react';
import type { Capabilities, NotificationsStart, ScopedHistory } from '@kbn/core/public';
import { SectionLoading } from '@kbn/es-ui-shared-plugin/public';
+import { KibanaFeatureScope } from '@kbn/features-plugin/common';
import type { FeaturesPluginStart, KibanaFeature } from '@kbn/features-plugin/public';
import { i18n } from '@kbn/i18n';
import { FormattedMessage } from '@kbn/i18n-react';
@@ -91,6 +92,10 @@ export class CreateSpacePage extends Component {
};
}
+ private filterSpaceFeatures = (features: KibanaFeature[]) => {
+ return features.filter((feature) => feature.scope?.includes(KibanaFeatureScope.Spaces));
+ };
+
public async componentDidMount() {
if (!this.props.capabilities.spaces.manage) {
return;
@@ -103,7 +108,8 @@ export class CreateSpacePage extends Component {
await this.loadSpace(spaceId, getFeatures());
} else {
const features = await getFeatures();
- this.setState({ isLoading: false, features });
+
+ this.setState({ isLoading: false, features: this.filterSpaceFeatures(features) });
}
} catch (e) {
notifications.toasts.addError(e, {
@@ -410,6 +416,7 @@ export class CreateSpacePage extends Component {
spacesManager.getSpace(spaceId),
featuresPromise,
]);
+
if (space) {
if (onLoadSpace) {
onLoadSpace(space);
@@ -426,7 +433,7 @@ export class CreateSpacePage extends Component {
!!space.initials && getSpaceInitials({ name: space.name }) !== space.initials,
customAvatarColor: !!space.color && getSpaceColor({ name: space.name }) !== space.color,
},
- features,
+ features: this.filterSpaceFeatures(features),
originalSpace: space,
isLoading: false,
});