From 1f5f461ec0f5f091f497e8b8013dfad5a07ac47f Mon Sep 17 00:00:00 2001 From: Lin Wang Date: Tue, 23 Jul 2024 11:19:01 +0800 Subject: [PATCH] [Workspace]Hide create workspace button for non dashboard admin (#7357) (#7382) * Hide create workspace for non dashboard admin * Changeset file for PR #7357 created/updated * Add more cases and update snapshot * Update match rule --------- Signed-off-by: Lin Wang Co-authored-by: opensearch-changeset-bot[bot] <154024398+opensearch-changeset-bot[bot]@users.noreply.github.com> --- changelogs/fragments/7357.yml | 2 ++ .../__snapshots__/index.test.tsx.snap | 19 --------------- .../components/workspace_list/index.test.tsx | 21 ++++++++++++++++- .../components/workspace_list/index.tsx | 23 ++++++++++++------- .../workspace_menu/workspace_menu.test.tsx | 17 +++++++++++++- .../workspace_menu/workspace_menu.tsx | 7 ++++-- 6 files changed, 58 insertions(+), 31 deletions(-) create mode 100644 changelogs/fragments/7357.yml diff --git a/changelogs/fragments/7357.yml b/changelogs/fragments/7357.yml new file mode 100644 index 000000000000..801e0d0fd8e6 --- /dev/null +++ b/changelogs/fragments/7357.yml @@ -0,0 +1,2 @@ +feat: +- [Workspace]Hide create workspace button for non dashboard admin ([#7357](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/7357)) \ No newline at end of file diff --git a/src/plugins/workspace/public/components/workspace_list/__snapshots__/index.test.tsx.snap b/src/plugins/workspace/public/components/workspace_list/__snapshots__/index.test.tsx.snap index 375e320e6305..32283810c139 100644 --- a/src/plugins/workspace/public/components/workspace_list/__snapshots__/index.test.tsx.snap +++ b/src/plugins/workspace/public/components/workspace_list/__snapshots__/index.test.tsx.snap @@ -81,25 +81,6 @@ exports[`WorkspaceList should render title and table normally 1`] = ` -
- -
{ expect(queryByText('name1')).not.toBeInTheDocument(); expect(getByText('name6')).toBeInTheDocument(); }); + + it('should display create workspace button for dashboard admin', async () => { + const { getByText } = render(getWrapWorkspaceListInContext([], true)); + + expect(getByText('Create workspace')).toBeInTheDocument(); + }); + + it('should hide create workspace button for non dashboard admin', async () => { + const { queryByText } = render(getWrapWorkspaceListInContext([], false)); + + expect(queryByText('Create workspace')).toBeNull(); + }); }); diff --git a/src/plugins/workspace/public/components/workspace_list/index.tsx b/src/plugins/workspace/public/components/workspace_list/index.tsx index 3a9342cf68f6..0d608a918473 100644 --- a/src/plugins/workspace/public/components/workspace_list/index.tsx +++ b/src/plugins/workspace/public/components/workspace_list/index.tsx @@ -26,7 +26,7 @@ import { WORKSPACE_CREATE_APP_ID } from '../../../common/constants'; import { cleanWorkspaceId } from '../../../../../core/public'; import { DeleteWorkspaceModal } from '../delete_workspace_modal'; -import { getFirstUseCaseOfFeatureConfigs, getUseCaseFromFeatureConfig } from '../../utils'; +import { getFirstUseCaseOfFeatureConfigs } from '../../utils'; import { WorkspaceUseCase } from '../../types'; const WORKSPACE_LIST_PAGE_DESCRIPTION = i18n.translate('workspace.list.description', { @@ -43,6 +43,7 @@ export const WorkspaceList = ({ registeredUseCases$ }: WorkspaceListProps) => { services: { workspaces, application, http }, } = useOpenSearchDashboards(); const registeredUseCases = useObservable(registeredUseCases$); + const isDashboardAdmin = application?.capabilities?.dashboards?.isDashboardAdmin; const initialSortField = 'name'; const initialSortDirection = 'asc'; @@ -172,13 +173,19 @@ export const WorkspaceList = ({ registeredUseCases$ }: WorkspaceListProps) => { incremental: true, }, toolsRight: [ - - Create workspace - , + ...(isDashboardAdmin + ? [ + + {i18n.translate('workspace.workspaceList.buttons.createWorkspace', { + defaultMessage: 'Create workspace', + })} + , + ] + : []), ], }; diff --git a/src/plugins/workspace/public/components/workspace_menu/workspace_menu.test.tsx b/src/plugins/workspace/public/components/workspace_menu/workspace_menu.test.tsx index 68ed1c67359f..6b1779a8f847 100644 --- a/src/plugins/workspace/public/components/workspace_menu/workspace_menu.test.tsx +++ b/src/plugins/workspace/public/components/workspace_menu/workspace_menu.test.tsx @@ -62,7 +62,7 @@ describe('', () => { it('should display a list of workspaces in the dropdown', () => { coreStartMock.workspaces.workspaceList$.next([ { id: 'workspace-1', name: 'workspace 1', features: [] }, - { id: 'workspace-2', name: 'workspace 2', features: [] }, + { id: 'workspace-2', name: 'workspace 2' }, ]); render(); @@ -170,4 +170,19 @@ describe('', () => { fireEvent.click(screen.getByText(/View all/i)); expect(coreStartMock.application.navigateToApp).toHaveBeenCalledWith('workspace_list'); }); + + it('should hide create workspace button for non dashboard admin', () => { + coreStartMock.application.capabilities = { + ...coreStartMock.application.capabilities, + dashboards: { + ...coreStartMock.application.capabilities.dashboards, + isDashboardAdmin: false, + }, + }; + render(); + + fireEvent.click(screen.getByTestId('workspace-select-button')); + expect(screen.getByText(/View all/i)).toBeInTheDocument(); + expect(screen.queryByText(/create workspaces/i)).toBeNull(); + }); }); diff --git a/src/plugins/workspace/public/components/workspace_menu/workspace_menu.tsx b/src/plugins/workspace/public/components/workspace_menu/workspace_menu.tsx index 05fe58d352a8..df276d8c43bb 100644 --- a/src/plugins/workspace/public/components/workspace_menu/workspace_menu.tsx +++ b/src/plugins/workspace/public/components/workspace_menu/workspace_menu.tsx @@ -72,7 +72,7 @@ export const WorkspaceMenu = ({ coreStart, registeredUseCases$ }: Props) => { const [isPopoverOpen, setPopover] = useState(false); const currentWorkspace = useObservable(coreStart.workspaces.currentWorkspace$, null); const workspaceList = useObservable(coreStart.workspaces.workspaceList$, []); - const isDashboardAdmin = !!coreStart.application.capabilities.dashboards; + const isDashboardAdmin = coreStart.application.capabilities?.dashboards?.isDashboardAdmin; const availableUseCases = useObservable(registeredUseCases$, []); const filteredWorkspaceList = useMemo(() => { @@ -90,7 +90,10 @@ export const WorkspaceMenu = ({ coreStart, registeredUseCases$ }: Props) => { const currentWorkspaceName = currentWorkspace?.name ?? defaultHeaderName; const getUseCase = (workspace: WorkspaceObject) => { - const useCaseId = getFirstUseCaseOfFeatureConfigs(workspace?.features!); + if (!workspace.features) { + return; + } + const useCaseId = getFirstUseCaseOfFeatureConfigs(workspace.features); return availableUseCases.find((useCase) => useCase.id === useCaseId); };