Skip to content

Commit

Permalink
[Workspace]Hide create workspace button for non dashboard admin (#7357)…
Browse files Browse the repository at this point in the history
… (#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 <[email protected]>
Co-authored-by: opensearch-changeset-bot[bot] <154024398+opensearch-changeset-bot[bot]@users.noreply.github.com>
  • Loading branch information
wanglam and opensearch-changeset-bot[bot] authored Jul 23, 2024
1 parent 0429e8a commit 1f5f461
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 31 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/7357.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
feat:
- [Workspace]Hide create workspace button for non dashboard admin ([#7357](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/7357))

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,16 @@ function getWrapWorkspaceListInContext(
{ id: 'id1', name: 'name1', features: ['use-case-all'] },
{ id: 'id2', name: 'name2' },
{ id: 'id3', name: 'name3', features: ['use-case-observability'] },
]
],
isDashboardAdmin = true
) {
const coreStartMock = coreMock.createStart();
coreStartMock.application.capabilities = {
...coreStartMock.application.capabilities,
dashboards: {
isDashboardAdmin,
},
};

const services = {
...coreStartMock,
Expand Down Expand Up @@ -135,4 +142,16 @@ describe('WorkspaceList', () => {
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();
});
});
23 changes: 15 additions & 8 deletions src/plugins/workspace/public/components/workspace_list/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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', {
Expand All @@ -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';
Expand Down Expand Up @@ -172,13 +173,19 @@ export const WorkspaceList = ({ registeredUseCases$ }: WorkspaceListProps) => {
incremental: true,
},
toolsRight: [
<EuiButton
href={workspaceCreateUrl}
key="create_workspace"
data-test-subj="workspaceList-create-workspace"
>
Create workspace
</EuiButton>,
...(isDashboardAdmin
? [
<EuiButton
href={workspaceCreateUrl}
key="create_workspace"
data-test-subj="workspaceList-create-workspace"
>
{i18n.translate('workspace.workspaceList.buttons.createWorkspace', {
defaultMessage: 'Create workspace',
})}
</EuiButton>,
]
: []),
],
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ describe('<WorkspaceMenu />', () => {
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(<WorkspaceMenuCreatorComponent />);
Expand Down Expand Up @@ -170,4 +170,19 @@ describe('<WorkspaceMenu />', () => {
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(<WorkspaceMenuCreatorComponent />);

fireEvent.click(screen.getByTestId('workspace-select-button'));
expect(screen.getByText(/View all/i)).toBeInTheDocument();
expect(screen.queryByText(/create workspaces/i)).toBeNull();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand All @@ -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);
};

Expand Down

0 comments on commit 1f5f461

Please sign in to comment.