From 09209d4c7bb00afab9b1aff8c4611a15d0cb664d Mon Sep 17 00:00:00 2001 From: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> Date: Sat, 5 Oct 2024 04:04:49 +1000 Subject: [PATCH] =?UTF-8?q?[8.x]=20Disable=20=E2=80=9CAssign=20roles=20to?= =?UTF-8?q?=20space=E2=80=9D=20button=20if=20no=20privileges=20are=20selec?= =?UTF-8?q?ted=20(#194874)=20(#195118)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # Backport This will backport the following commits from `main` to `8.x`: - [Disable “Assign roles to space” button if no privileges are selected (#194874)](https://github.com/elastic/kibana/pull/194874) ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sqren/backport) Co-authored-by: Tim Sullivan --- .../space_assign_role_privilege_form.test.tsx | 26 +++++++++++++++++++ .../space_assign_role_privilege_form.tsx | 25 ++++++++++++++++-- 2 files changed, 49 insertions(+), 2 deletions(-) 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 (