From c1b954518d82251d1450802e471addb40b7d4d96 Mon Sep 17 00:00:00 2001 From: Lin Wang Date: Wed, 4 Dec 2024 11:41:36 +0800 Subject: [PATCH] Skip ui setting update for non workspace admin Signed-off-by: Lin Wang --- .../opensearch_dashboards_services.ts | 2 ++ .../public/application/sample_data_client.js | 20 ++++++++++++++----- src/plugins/home/public/plugin.ts | 1 + 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/plugins/home/public/application/opensearch_dashboards_services.ts b/src/plugins/home/public/application/opensearch_dashboards_services.ts index 1107e46ecf2e..eb4b085d86ae 100644 --- a/src/plugins/home/public/application/opensearch_dashboards_services.ts +++ b/src/plugins/home/public/application/opensearch_dashboards_services.ts @@ -37,6 +37,7 @@ import { SavedObjectsClientContract, IUiSettingsClient, ApplicationStart, + WorkspacesSetup, } from 'opensearch-dashboards/public'; import { UiStatsMetricType } from '@osd/analytics'; import { TelemetryPluginStart } from '../../../telemetry/public'; @@ -77,6 +78,7 @@ export interface HomeOpenSearchDashboardsServices { }; dataSource?: DataSourcePluginStart; sectionTypes: SectionTypeService; + workspaces?: WorkspacesSetup; } let services: HomeOpenSearchDashboardsServices | null = null; diff --git a/src/plugins/home/public/application/sample_data_client.js b/src/plugins/home/public/application/sample_data_client.js index 51a387675219..b2adaf44cf81 100644 --- a/src/plugins/home/public/application/sample_data_client.js +++ b/src/plugins/home/public/application/sample_data_client.js @@ -41,16 +41,26 @@ export async function listSampleDataSets(dataSourceId) { return await getServices().http.get(sampleDataUrl, { query }); } -const isWorkspaceEnabled = () => { - const workspaces = getServices().application.capabilities.workspaces; - return !!(workspaces && workspaces.enabled); +const canUpdateUISetting = () => { + const { + application: { capabilities }, + workspaces, + } = getServices(); + if ( + capabilities.workspaces && + capabilities.workspaces.enabled && + capabilities.workspaces.permissionEnabled + ) { + return !!workspaces?.currentWorkspace$.getValue()?.owner; + } + return true; }; export async function installSampleDataSet(id, sampleDataDefaultIndex, dataSourceId) { const query = buildQuery(dataSourceId); await getServices().http.post(`${sampleDataUrl}/${id}`, { query }); - if (!isWorkspaceEnabled() && getServices().uiSettings.isDefault('defaultIndex')) { + if (canUpdateUISetting() && getServices().uiSettings.isDefault('defaultIndex')) { getServices().uiSettings.set('defaultIndex', sampleDataDefaultIndex); } @@ -64,7 +74,7 @@ export async function uninstallSampleDataSet(id, sampleDataDefaultIndex, dataSou const uiSettings = getServices().uiSettings; if ( - !isWorkspaceEnabled() && + canUpdateUISetting() && !uiSettings.isDefault('defaultIndex') && uiSettings.get('defaultIndex') === sampleDataDefaultIndex ) { diff --git a/src/plugins/home/public/plugin.ts b/src/plugins/home/public/plugin.ts index 435c7d4d3b9f..6d9771c724ef 100644 --- a/src/plugins/home/public/plugin.ts +++ b/src/plugins/home/public/plugin.ts @@ -156,6 +156,7 @@ export class HomePublicPlugin injectedMetadata: coreStart.injectedMetadata, dataSource, sectionTypes: this.sectionTypeService, + workspaces: core.workspaces, ...homeOpenSearchDashboardsServices, }); };