Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Workspace]Hide create workspace button for non dashboard admin #7357

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
});
});
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: [
<EuiSmallButton
href={workspaceCreateUrl}
key="create_workspace"
data-test-subj="workspaceList-create-workspace"
>
Create workspace
</EuiSmallButton>,
...(isDashboardAdmin
? [
<EuiSmallButton
href={workspaceCreateUrl}
key="create_workspace"
data-test-subj="workspaceList-create-workspace"
>
{i18n.translate('workspace.workspaceList.buttons.createWorkspace', {
defaultMessage: 'Create workspace',
})}
</EuiSmallButton>,
]
: []),
],
};

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
Loading