diff --git a/x-pack/plugins/spaces/public/management/edit_space/roles/component/space_assign_role_privilege_form.test.tsx b/x-pack/plugins/spaces/public/management/edit_space/roles/component/space_assign_role_privilege_form.test.tsx index 9150f0c211adb..a7cc54820774d 100644 --- a/x-pack/plugins/spaces/public/management/edit_space/roles/component/space_assign_role_privilege_form.test.tsx +++ b/x-pack/plugins/spaces/public/management/edit_space/roles/component/space_assign_role_privilege_form.test.tsx @@ -160,6 +160,32 @@ describe('PrivilegesRolesForm', () => { expect(screen.getByTestId('space-assign-role-create-roles-privilege-button')).toBeDisabled(); }); + it('renders with the assign roles button disabled when no base privileges or feature privileges are selected', async () => { + getRolesSpy.mockResolvedValue([]); + getAllKibanaPrivilegeSpy.mockResolvedValue(createRawKibanaPrivileges(kibanaFeatures)); + + const roles: Role[] = [ + createRole('test_role_1', [{ base: [], feature: {}, spaces: [space.id] }]), + ]; + + renderPrivilegeRolesForm({ + preSelectedRoles: roles, + }); + + await waitFor(() => null); + + expect(screen.getByTestId(`${FEATURE_PRIVILEGES_READ}-privilege-button`)).toHaveAttribute( + 'aria-pressed', + String(false) + ); + + expect( + screen.getByTestId('space-assign-role-privilege-customization-form') + ).toBeInTheDocument(); + + expect(screen.getByTestId('space-update-role-create-roles-privilege-button')).toBeDisabled(); + }); + it('preselects the privilege of the selected role when one is provided', async () => { getRolesSpy.mockResolvedValue([]); getAllKibanaPrivilegeSpy.mockResolvedValue(createRawKibanaPrivileges(kibanaFeatures)); diff --git a/x-pack/plugins/spaces/public/management/edit_space/roles/component/space_assign_role_privilege_form.tsx b/x-pack/plugins/spaces/public/management/edit_space/roles/component/space_assign_role_privilege_form.tsx index 276efb7f92526..23a7383a01a06 100644 --- a/x-pack/plugins/spaces/public/management/edit_space/roles/component/space_assign_role_privilege_form.tsx +++ b/x-pack/plugins/spaces/public/management/edit_space/roles/component/space_assign_role_privilege_form.tsx @@ -543,11 +543,32 @@ export const PrivilegesRolesForm: FC = (props) => { ); }; + const canSave = useCallback(() => { + if (selectedRoles.length === 0) { + return false; + } + + const form = roleCustomizationAnchor.value.kibana[roleCustomizationAnchor.privilegeIndex] ?? {}; + const formBase = form.base ?? []; + const formFeature = form.feature ?? {}; + + // ensure that the form has base privileges or has selected features that are valid + if ( + formBase.length === 0 && + (Object.keys(formFeature).length === 0 || + Object.values(formFeature).every((privileges) => privileges.length === 0)) + ) { + return false; + } + + return true; + }, [selectedRoles, roleCustomizationAnchor]); + const getSaveButton = useCallback(() => { return ( assignRolesToSpace()} data-test-subj={`space-${ @@ -563,7 +584,7 @@ export const PrivilegesRolesForm: FC = (props) => { })} ); - }, [assignRolesToSpace, assigningToRole, selectedRoles.length]); + }, [assignRolesToSpace, assigningToRole, canSave]); return (