Skip to content

Commit

Permalink
[8.x] Disable “Assign roles to space” button if no privileges are sel…
Browse files Browse the repository at this point in the history
…ected (#194874) (#195118)

# Backport

This will backport the following commits from `main` to `8.x`:
- [Disable “Assign roles to space” button if no privileges are selected
(#194874)](#194874)

<!--- Backport version: 9.4.3 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Tim
Sullivan","email":"[email protected]"},"sourceCommit":{"committedDate":"2024-10-04T16:10:17Z","message":"Disable
“Assign roles to space” button if no privileges are selected
(#194874)\n\n## Summary\r\n\r\nDisable “Assign roles to space” button if
no base privileges or features\r\nare
selected\r\n\r\n**Before**\r\n\r\n\r\nhttps://github.com/user-attachments/assets/4efa921b-6cc4-4496-99f7-0ee79dd318be\r\n\r\n**After**\r\n\r\n\r\nhttps://github.com/user-attachments/assets/fc8fe809-f03a-4f0b-a961-328596c9b2c7\r\n\r\n###
Checklist\r\n\r\nDelete any items that are not applicable to this
PR.\r\n\r\n- [x] [Unit or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common scenarios\r\n- [x] [Flaky
Test\r\nRunner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1)
was\r\nused on any tests
changed","sha":"381c430db53fe82db454da4589803bb9cf447dd5","branchLabelMapping":{"^v9.0.0$":"main","^v8.16.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["Team:Security","release_note:skip","v9.0.0","v8.16.0","backport:version"],"title":"Disable
“Assign roles to space” button if no privileges are
selected","number":194874,"url":"https://github.com/elastic/kibana/pull/194874","mergeCommit":{"message":"Disable
“Assign roles to space” button if no privileges are selected
(#194874)\n\n## Summary\r\n\r\nDisable “Assign roles to space” button if
no base privileges or features\r\nare
selected\r\n\r\n**Before**\r\n\r\n\r\nhttps://github.com/user-attachments/assets/4efa921b-6cc4-4496-99f7-0ee79dd318be\r\n\r\n**After**\r\n\r\n\r\nhttps://github.com/user-attachments/assets/fc8fe809-f03a-4f0b-a961-328596c9b2c7\r\n\r\n###
Checklist\r\n\r\nDelete any items that are not applicable to this
PR.\r\n\r\n- [x] [Unit or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common scenarios\r\n- [x] [Flaky
Test\r\nRunner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1)
was\r\nused on any tests
changed","sha":"381c430db53fe82db454da4589803bb9cf447dd5"}},"sourceBranch":"main","suggestedTargetBranches":["8.x"],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/194874","number":194874,"mergeCommit":{"message":"Disable
“Assign roles to space” button if no privileges are selected
(#194874)\n\n## Summary\r\n\r\nDisable “Assign roles to space” button if
no base privileges or features\r\nare
selected\r\n\r\n**Before**\r\n\r\n\r\nhttps://github.com/user-attachments/assets/4efa921b-6cc4-4496-99f7-0ee79dd318be\r\n\r\n**After**\r\n\r\n\r\nhttps://github.com/user-attachments/assets/fc8fe809-f03a-4f0b-a961-328596c9b2c7\r\n\r\n###
Checklist\r\n\r\nDelete any items that are not applicable to this
PR.\r\n\r\n- [x] [Unit or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common scenarios\r\n- [x] [Flaky
Test\r\nRunner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1)
was\r\nused on any tests
changed","sha":"381c430db53fe82db454da4589803bb9cf447dd5"}},{"branch":"8.x","label":"v8.16.0","branchLabelMappingKey":"^v8.16.0$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->

Co-authored-by: Tim Sullivan <[email protected]>
  • Loading branch information
kibanamachine and tsullivan authored Oct 4, 2024
1 parent 8ab1994 commit 09209d4
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -543,11 +543,32 @@ export const PrivilegesRolesForm: FC<PrivilegesRolesFormProps> = (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 (
<EuiButton
fill
disabled={!selectedRoles.length}
disabled={!canSave()}
isLoading={assigningToRole}
onClick={() => assignRolesToSpace()}
data-test-subj={`space-${
Expand All @@ -563,7 +584,7 @@ export const PrivilegesRolesForm: FC<PrivilegesRolesFormProps> = (props) => {
})}
</EuiButton>
);
}, [assignRolesToSpace, assigningToRole, selectedRoles.length]);
}, [assignRolesToSpace, assigningToRole, canSave]);

return (
<React.Fragment>
Expand Down

0 comments on commit 09209d4

Please sign in to comment.