From 23cde97cb3244cd7928b672136864d705e509699 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Tue, 22 Oct 2024 17:47:47 +0000 Subject: [PATCH] [Workspace] Fix non-workspace admin update observability:defaultDashboard (#2223) * Fix non-workspace admin update observability:defaultDashboard Signed-off-by: yubonluo * optimize the code Signed-off-by: yubonluo * optimize the code Signed-off-by: yubonluo * optimize the code Signed-off-by: yubonluo --------- Signed-off-by: yubonluo (cherry picked from commit f631248f006fa89adb41a836b4d8e07568977cbb) Signed-off-by: github-actions[bot] --- public/components/overview/home.tsx | 50 +++++++++++++++++++---------- public/framework/core_refs.ts | 2 ++ public/plugin.tsx | 1 + 3 files changed, 36 insertions(+), 17 deletions(-) diff --git a/public/components/overview/home.tsx b/public/components/overview/home.tsx index aa50a01e7..66256500a 100644 --- a/public/components/overview/home.tsx +++ b/public/components/overview/home.tsx @@ -11,7 +11,7 @@ import { EuiButtonEmpty, EuiToolTip, } from '@elastic/eui'; -import React, { ReactNode, useEffect, useState } from 'react'; +import React, { ReactNode, useEffect, useMemo, useState } from 'react'; import { HashRouter, Route, Switch } from 'react-router-dom'; import { useObservable } from 'react-use'; import { EMPTY } from 'rxjs'; @@ -52,6 +52,18 @@ export const Home = () => { const [isPopoverOpen, setIsPopoverOpen] = useState(false); const [showGetStarted, setShowGetStarted] = useState(null); // Initial null state + const canUpdateUiSetting = useMemo(() => { + const capabilities = coreRefs.application?.capabilities; + + // When workspace enabled, only workspace owner/OSD admin can update observability:defaultDashboard. + if (capabilities?.workspaces?.enabled) { + const isCurrentWorkspaceOwner = coreRefs.workspaces?.currentWorkspace$.getValue()?.owner; + const isDashboardAdmin = capabilities?.dashboards?.isDashboardAdmin !== false; + return isCurrentWorkspaceOwner || isDashboardAdmin; + } + return true; + }, [coreRefs.workspaces?.currentWorkspace$, coreRefs.application?.capabilities]); + ObsDashboardStateManager.showFlyout$.next(() => () => setIsFlyoutVisible(true)); const loadHomePage = () => { @@ -95,14 +107,16 @@ export const Home = () => { }); ObsDashboardStateManager.isDashboardSelected$.next(true); } else { - setObservabilityDashboardsId(null); - ObsDashboardStateManager.dashboardState$.next({ - startDate: '', - endDate: '', - dashboardTitle: '', - dashboardId: '', - }); - ObsDashboardStateManager.isDashboardSelected$.next(false); + if (canUpdateUiSetting) { + setObservabilityDashboardsId(null); + ObsDashboardStateManager.dashboardState$.next({ + startDate: '', + endDate: '', + dashboardTitle: '', + dashboardId: '', + }); + ObsDashboardStateManager.isDashboardSelected$.next(false); + } } }) .catch((error) => { @@ -316,14 +330,16 @@ export const Home = () => { }); ObsDashboardStateManager.isDashboardSelected$.next(true); } else { - setObservabilityDashboardsId(null); - ObsDashboardStateManager.dashboardState$.next({ - startDate: '', - endDate: '', - dashboardTitle: '', - dashboardId: '', - }); - ObsDashboardStateManager.isDashboardSelected$.next(false); + if (canUpdateUiSetting) { + setObservabilityDashboardsId(null); + ObsDashboardStateManager.dashboardState$.next({ + startDate: '', + endDate: '', + dashboardTitle: '', + dashboardId: '', + }); + ObsDashboardStateManager.isDashboardSelected$.next(false); + } } }) .catch((error) => { diff --git a/public/framework/core_refs.ts b/public/framework/core_refs.ts index 41cd5c344..a8142c97b 100644 --- a/public/framework/core_refs.ts +++ b/public/framework/core_refs.ts @@ -12,6 +12,7 @@ import { IToasts, OverlayStart, SavedObjectsClientContract, + WorkspacesStart, } from '../../../../src/core/public'; import { DashboardStart } from '../../../../src/plugins/dashboard/public'; import { DataSourceStart } from '../../../../src/plugins/data/public/data_sources/datasource_services/types'; @@ -40,6 +41,7 @@ class CoreRefs { public dataSources?: DataSourceStart; public navigation?: NavigationPublicPluginStart; public contentManagement?: ContentManagementPluginStart; + public workspaces?: WorkspacesStart; private constructor() { // ... } diff --git a/public/plugin.tsx b/public/plugin.tsx index 402ceceeb..61928fa60 100644 --- a/public/plugin.tsx +++ b/public/plugin.tsx @@ -459,6 +459,7 @@ export class ObservabilityPlugin coreRefs.dataSource = startDeps.dataSource; coreRefs.navigation = startDeps.navigation; coreRefs.contentManagement = startDeps.contentManagement; + coreRefs.workspaces = core.workspaces; // redirect trace URL based on new navigation if (window.location.pathname.includes(observabilityTracesID)) {