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

rbac: fix bug with selecting permissions in PermissionList #50033

Merged
merged 3 commits into from
Mar 27, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
add unique id for checkbox in permission list
  • Loading branch information
BolajiOlajide committed Mar 27, 2023
commit 19286fba0f0a98d5b6161f798bdfeb4aeef410e0
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export const CreateRoleModal: React.FunctionComponent<React.PropsWithChildren<Cr
afterCreate,
allPermissions,
}) => {
const labelId = 'addRole'
const labelId = 'createRole'

const [createRole, { loading, error }] = useCreateRole(afterCreate)
const onSubmit = (values: CreateRoleModalFormValues): SubmissionResult => {
Expand Down Expand Up @@ -97,6 +97,7 @@ export const CreateRoleModal: React.FunctionComponent<React.PropsWithChildren<Cr
isChecked={isChecked}
onBlur={onBlur}
onChange={onChange}
roleName={nameInput.input.value}
/>
{error && !loading && <ErrorAlert error={error} className="my-2" />}

Expand Down
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a story with multiple roles/should we make one?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Roles Management page story already exists for this.

The storybook link for this PR wasn't generated (my guess because I didn't add a new storybook, only modified the existing one).

CleanShot 2023-03-27 at 21 52 49@2x

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ooh this is perfect, thanks for confirming this for me! 😄

Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ const isChecked = (role: typeof roleWithAllPermissions): ((value: string) => boo
return (value: string): boolean => rolePermissions[value]
}

const roleName = 'TEST-ROLE'

export const NoPermissions: Story = () => (
<WebStory>
{() => (
Expand All @@ -36,6 +38,7 @@ export const NoPermissions: Story = () => (
onChange={noop}
onBlur={noop}
isChecked={isChecked(roleWithNoPermission)}
roleName={roleName}
/>
</MockedTestProvider>
)}
Expand All @@ -53,6 +56,7 @@ export const OnePermissionAssigned: Story = () => (
onChange={noop}
onBlur={noop}
isChecked={isChecked(roleWithOnePermission)}
roleName={roleName}
/>
</MockedTestProvider>
)}
Expand All @@ -70,6 +74,7 @@ export const AllPermissionsAssigned: Story = () => (
onChange={noop}
onBlur={noop}
isChecked={isChecked(roleWithAllPermissions)}
roleName={roleName}
/>
</MockedTestProvider>
)}
Expand Down
15 changes: 11 additions & 4 deletions client/web/src/enterprise/rbac/components/Permissions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ interface PermissionListProps {
onChange?: (event: ChangeEvent<HTMLInputElement>) => void
onBlur?: FocusEventHandler<HTMLInputElement>
disabled?: boolean
roleName: string
}

export const PermissionsList: React.FunctionComponent<React.PropsWithChildren<PermissionListProps>> = ({
Expand All @@ -22,6 +23,7 @@ export const PermissionsList: React.FunctionComponent<React.PropsWithChildren<Pe
onChange,
onBlur,
disabled,
roleName,
}) => (
<>
{allNamespaces.map(namespace => {
Expand All @@ -31,12 +33,17 @@ export const PermissionsList: React.FunctionComponent<React.PropsWithChildren<Pe
<Text className="font-weight-bold">{prettifyNamespace(namespace)}</Text>
<Grid columnCount={4}>
{namespacePermissions.map(permission => {
// The checkbox component keeps its own state and because we reuse this component when rendering
// multiple roles on a pege, we have to ensure the `id` and `key` are unique across all instances
// rendered on a page.
const key = `${permission.id}-${roleName}`

// This is a hack to disable the BatchChangesReadPermission
// from the UI for now until it's fully implemented.
if (permission.displayName === BatchChangesReadPermission) {
return (
<Checkbox
key={permission.id}
key={key}
label={
<>
{prettifyAction(permission.action)}
Expand All @@ -49,7 +56,7 @@ export const PermissionsList: React.FunctionComponent<React.PropsWithChildren<Pe
</Tooltip>
</>
}
id={permission.displayName}
id={key}
checked={isChecked(permission.id)}
value={permission.id}
disabled={true}
Expand All @@ -58,9 +65,9 @@ export const PermissionsList: React.FunctionComponent<React.PropsWithChildren<Pe
}
return (
<Checkbox
key={permission.id}
key={key}
label={prettifyAction(permission.action)}
id={permission.displayName}
id={key}
checked={isChecked(permission.id)}
value={permission.id}
onChange={onChange}
Expand Down
3 changes: 2 additions & 1 deletion client/web/src/enterprise/rbac/components/RoleNode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ const ModifiableRoleNode: React.FunctionComponent<RoleNodeProps> = ({ node, refe
isChecked={isChecked}
onBlur={onBlur}
onChange={onChange}
roleName={node.name}
/>
<LoaderButton
alwaysShowLabel={true}
Expand Down Expand Up @@ -258,7 +259,7 @@ const LockedRoleNode: React.FunctionComponent<Pick<RoleNodeProps, 'node' | 'allP
</div>

<CollapsePanel className={styles.roleNodePermissions} forcedRender={false}>
<PermissionsList allPermissions={allPermissions} isChecked={isChecked} disabled={true} />
<PermissionsList allPermissions={allPermissions} isChecked={isChecked} disabled={true} roleName={node.name} />
</CollapsePanel>
</Collapse>
</li>
Expand Down