-
Notifications
You must be signed in to change notification settings - Fork 15
Roles page for orgs #948
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
Merged
Merged
Roles page for orgs #948
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1ff67bc
roles page for orgs
david-crespo a33ccdb
share user row aggregation logic
david-crespo 3a4071e
extract useUsersNotInPolicy
david-crespo 43b7f8a
add orgs e2e test, get dropdown items directly from role order
david-crespo 48743be
comments for organization
david-crespo b48b92a
use absolute paths to work around RR bug
david-crespo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| import * as Yup from 'yup' | ||
|
|
||
| import type { OrganizationRole, OrganizationRolePolicy } from '@oxide/api' | ||
| import { orgRoles, useUsersNotInPolicy } from '@oxide/api' | ||
| import { setUserRole } from '@oxide/api' | ||
| import { useApiQueryClient } from '@oxide/api' | ||
| import { useApiMutation } from '@oxide/api' | ||
| import { capitalize } from '@oxide/util' | ||
|
|
||
| import { Form, ListboxField, SideModalForm } from 'app/components/form' | ||
| import { useParams } from 'app/hooks' | ||
|
|
||
| import type { CreateSideModalFormProps, EditSideModalFormProps } from '.' | ||
|
|
||
| type AddUserValues = { | ||
| userId: string | ||
| roleName: OrganizationRole | '' | ||
| } | ||
|
|
||
| const initialValues: AddUserValues = { | ||
| userId: '', | ||
| roleName: '', | ||
| } | ||
|
|
||
| const roleItems = orgRoles.map((role) => ({ value: role, label: capitalize(role) })) | ||
|
|
||
| type AddRoleModalProps = CreateSideModalFormProps<AddUserValues, OrganizationRolePolicy> & { | ||
| policy: OrganizationRolePolicy | ||
| } | ||
|
|
||
| export function OrgAccessAddUserSideModal({ | ||
| onSubmit, | ||
| onSuccess, | ||
| onDismiss, | ||
| policy, | ||
| ...props | ||
| }: AddRoleModalProps) { | ||
| const orgParams = useParams('orgName') | ||
|
|
||
| const users = useUsersNotInPolicy(policy) | ||
| const userItems = users.map((u) => ({ value: u.id, label: u.name })) | ||
|
|
||
| const queryClient = useApiQueryClient() | ||
| const updatePolicy = useApiMutation('organizationPutPolicy', { | ||
| onSuccess: (data) => { | ||
| queryClient.invalidateQueries('organizationGetPolicy', orgParams) | ||
| onSuccess?.(data) | ||
| onDismiss() | ||
| }, | ||
| }) | ||
|
|
||
| return ( | ||
| <SideModalForm | ||
| onDismiss={onDismiss} | ||
| title="Add user to organization" | ||
| id="org-access-add-user" | ||
| initialValues={initialValues} | ||
| onSubmit={ | ||
| onSubmit || | ||
| (({ userId, roleName }) => { | ||
| // can't happen because roleName is validated not to be '', but TS | ||
| // wants to be sure | ||
| if (roleName === '') return | ||
|
|
||
| updatePolicy.mutate({ | ||
| ...orgParams, | ||
| body: setUserRole(userId, roleName, policy), | ||
| }) | ||
| }) | ||
| } | ||
| validationSchema={Yup.object({ | ||
| userId: Yup.string().required(), | ||
| roleName: Yup.string().required(), | ||
| })} | ||
| submitDisabled={updatePolicy.isLoading} | ||
| error={updatePolicy.error?.error as Error | undefined} | ||
| {...props} | ||
| > | ||
| <ListboxField id="userId" name="userId" items={userItems} label="User" required /> | ||
| <ListboxField id="roleName" name="roleName" label="Role" items={roleItems} required /> | ||
| <Form.Submit>Add user</Form.Submit> | ||
| </SideModalForm> | ||
| ) | ||
| } | ||
|
|
||
| type EditUserValues = { | ||
| roleName: OrganizationRole | ||
| } | ||
|
|
||
| type EditRoleModalProps = EditSideModalFormProps<EditUserValues, OrganizationRolePolicy> & { | ||
| userId: string | ||
| policy: OrganizationRolePolicy | ||
| } | ||
|
|
||
| export function OrgAccessEditUserSideModal({ | ||
| onSubmit, | ||
| onSuccess, | ||
| onDismiss, | ||
| userId, | ||
| policy, | ||
| ...props | ||
| }: EditRoleModalProps) { | ||
| const orgParams = useParams('orgName') | ||
|
|
||
| const queryClient = useApiQueryClient() | ||
| const updatePolicy = useApiMutation('organizationPutPolicy', { | ||
| onSuccess: (data) => { | ||
| queryClient.invalidateQueries('organizationGetPolicy', orgParams) | ||
| onSuccess?.(data) | ||
| onDismiss() | ||
| }, | ||
| }) | ||
|
|
||
| return ( | ||
| <SideModalForm | ||
| onDismiss={onDismiss} | ||
| // TODO: show user name in header or SOMEWHERE | ||
| title="Change user role" | ||
| id="org-access-edit-user" | ||
| onSubmit={ | ||
| onSubmit || | ||
| (({ roleName }) => { | ||
| updatePolicy.mutate({ | ||
| ...orgParams, | ||
| body: setUserRole(userId, roleName, policy), | ||
| }) | ||
| }) | ||
| } | ||
| submitDisabled={updatePolicy.isLoading || !policy} | ||
| error={updatePolicy.error?.error as Error | undefined} | ||
| {...props} | ||
| > | ||
| <ListboxField id="roleName" name="roleName" label="Role" items={roleItems} required /> | ||
| <Form.Submit>Update role</Form.Submit> | ||
| </SideModalForm> | ||
| ) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| import { getCoreRowModel, useTableInstance } from '@tanstack/react-table' | ||
| import { useMemo, useState } from 'react' | ||
|
|
||
| import { | ||
| orgRoleOrder, | ||
| setUserRole, | ||
| useApiMutation, | ||
| useApiQueryClient, | ||
| useUserAccessRows, | ||
| } from '@oxide/api' | ||
| import type { OrganizationRole, UserAccessRow } from '@oxide/api' | ||
| import { useApiQuery } from '@oxide/api' | ||
| import { Table, createTable, getActionsCol } from '@oxide/table' | ||
| import { Access24Icon, Badge, Button, PageHeader, PageTitle, TableActions } from '@oxide/ui' | ||
|
|
||
| import { OrgAccessAddUserSideModal, OrgAccessEditUserSideModal } from 'app/forms/org-access' | ||
| import { useParams } from 'app/hooks' | ||
|
|
||
| type UserRow = UserAccessRow<OrganizationRole> | ||
|
|
||
| const table = createTable().setRowType<UserRow>() | ||
|
|
||
| export const OrgAccessPage = () => { | ||
| const [addModalOpen, setAddModalOpen] = useState(false) | ||
| const [editingUserRow, setEditingUserRow] = useState<UserRow | null>(null) | ||
| const orgParams = useParams('orgName') | ||
| const { data: policy } = useApiQuery('organizationGetPolicy', orgParams) | ||
|
|
||
| const rows = useUserAccessRows(policy, orgRoleOrder) | ||
|
|
||
| const queryClient = useApiQueryClient() | ||
| const updatePolicy = useApiMutation('organizationPutPolicy', { | ||
| onSuccess: () => queryClient.invalidateQueries('organizationGetPolicy', orgParams), | ||
| // TODO: handle 403 | ||
| }) | ||
|
|
||
| // TODO: checkboxes and bulk delete? not sure | ||
| // TODO: disable delete on permissions you can't delete | ||
|
|
||
| const columns = useMemo( | ||
| () => [ | ||
| table.createDataColumn('id', { header: 'ID' }), | ||
| table.createDataColumn('name', { header: 'Name' }), | ||
| table.createDataColumn('roleName', { | ||
| header: 'Role', | ||
| cell: (info) => <Badge color="neutral">{info.getValue()}</Badge>, | ||
| }), | ||
| table.createDisplayColumn( | ||
| getActionsCol((row) => [ | ||
| { | ||
| label: 'Change role', | ||
| onActivate: () => setEditingUserRow(row), | ||
| }, | ||
| // TODO: only show if you have permission to do this | ||
| { | ||
| label: 'Delete', | ||
| onActivate() { | ||
| // TODO: confirm delete | ||
| updatePolicy.mutate({ | ||
| ...orgParams, | ||
| // we know policy is there, otherwise there's no row to display | ||
| body: setUserRole(row.id, null, policy!), | ||
| }) | ||
| }, | ||
| }, | ||
| ]) | ||
| ), | ||
| ], | ||
| [policy, orgParams, updatePolicy] | ||
| ) | ||
|
|
||
| const tableInstance = useTableInstance(table, { | ||
| columns, | ||
| data: rows, | ||
| getCoreRowModel: getCoreRowModel(), | ||
| }) | ||
|
|
||
| return ( | ||
| <> | ||
| <PageHeader> | ||
| <PageTitle icon={<Access24Icon />}>Access & IAM</PageTitle> | ||
| </PageHeader> | ||
|
|
||
| <TableActions> | ||
| <Button size="xs" variant="secondary" onClick={() => setAddModalOpen(true)}> | ||
| Add user to organization | ||
| </Button> | ||
| </TableActions> | ||
| {policy && ( | ||
| <OrgAccessAddUserSideModal | ||
| isOpen={addModalOpen} | ||
| onDismiss={() => setAddModalOpen(false)} | ||
| onSuccess={() => setAddModalOpen(false)} | ||
| policy={policy} | ||
| /> | ||
| )} | ||
| {policy && editingUserRow && ( | ||
| <OrgAccessEditUserSideModal | ||
| isOpen={!!editingUserRow} | ||
| onDismiss={() => setEditingUserRow(null)} | ||
| onSuccess={() => setEditingUserRow(null)} | ||
| policy={policy} | ||
| userId={editingUserRow.id} | ||
| initialValues={{ roleName: editingUserRow.roleName }} | ||
| /> | ||
| )} | ||
| <Table table={tableInstance} /> | ||
| </> | ||
| ) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unfortunately the paths here are kind of messed up and I think they depend on my RR fix and another prerelease, which we've been waiting on longer than I expected