-
Notifications
You must be signed in to change notification settings - Fork 22
Disable new organization button if user doesn't have write on the silo #1244
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,12 +2,15 @@ import { useMemo } from 'react' | |
| import { Link, useLocation, useNavigate } from 'react-router-dom' | ||
|
|
||
| import type { Organization } from '@oxide/api' | ||
| import { canWrite } from '@oxide/api' | ||
| import { useEffectiveSiloRole } from '@oxide/api' | ||
| import { apiQueryClient } from '@oxide/api' | ||
| import { useApiQueryClient } from '@oxide/api' | ||
| import { useApiMutation, useApiQuery } from '@oxide/api' | ||
| import type { MenuAction } from '@oxide/table' | ||
| import { DateCell, linkCell, useQueryTable } from '@oxide/table' | ||
| import { | ||
| Button, | ||
| EmptyMessage, | ||
| Folder24Icon, | ||
| PageHeader, | ||
|
|
@@ -33,7 +36,10 @@ const EmptyState = () => ( | |
| ) | ||
|
|
||
| OrgsPage.loader = async () => { | ||
| await apiQueryClient.prefetchQuery('organizationList', { query: { limit: 10 } }) | ||
| await Promise.all([ | ||
| apiQueryClient.prefetchQuery('organizationList', { query: { limit: 10 } }), | ||
| apiQueryClient.prefetchQuery('policyView', {}), | ||
| ]) | ||
| } | ||
|
|
||
| interface OrgsPageProps { | ||
|
|
@@ -47,6 +53,8 @@ export default function OrgsPage({ modal }: OrgsPageProps) { | |
| const { Table, Column } = useQueryTable('organizationList', {}) | ||
| const queryClient = useApiQueryClient() | ||
|
|
||
| const siloRole = useEffectiveSiloRole() | ||
|
|
||
| const { data: orgs } = useApiQuery('organizationList', { | ||
| query: { limit: 10 }, // to have same params as QueryTable | ||
| }) | ||
|
|
@@ -92,9 +100,18 @@ export default function OrgsPage({ modal }: OrgsPageProps) { | |
| <PageTitle icon={<Folder24Icon />}>Organizations</PageTitle> | ||
| </PageHeader> | ||
| <TableActions> | ||
| <Link to={pb.orgNew()} className={buttonStyle({ variant: 'default', size: 'sm' })}> | ||
| New Organization | ||
| </Link> | ||
| {canWrite(siloRole) ? ( | ||
| <Link | ||
| to={pb.orgNew()} | ||
| className={buttonStyle({ variant: 'default', size: 'sm' })} | ||
| > | ||
| New Organization | ||
| </Link> | ||
| ) : ( | ||
| <Button size="sm" disabled> | ||
| New Organization | ||
| </Button> | ||
| )} | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Clearly this is too noisy — we'd want to wrap this up into a disable-able custom link component with a
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Disabled links are a bit of an odd pattern. We'd want to change the pointer and ensure we had |
||
| </TableActions> | ||
| <Table emptyState={<EmptyState />} makeActions={makeActions}> | ||
| <Column accessor="name" cell={linkCell((orgName) => pb.projects({ orgName }))} /> | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -157,3 +157,21 @@ export function userRoleFromPolicies( | |
| .map((ra) => ra.roleName) | ||
| return getEffectiveRole(myRoles) || null | ||
| } | ||
|
|
||
| // Not having pop-in when the requests resolve depends on the right prefetches | ||
| // having happened. This is probably too fragile. We need to explore this. | ||
| // https://tkdodo.eu/blog/react-query-meets-react-router#a-typescript-tip | ||
| export function useEffectiveSiloRole() { | ||
| const { data: me } = useApiQuery('sessionMe', {}) | ||
| const { data: myGroups } = useApiQuery('sessionMeGroups', {}) | ||
| const { data: siloPolicy } = useApiQuery('policyView', {}) | ||
| return me && myGroups && siloPolicy | ||
| ? userRoleFromPolicies(me, myGroups.items, [siloPolicy]) | ||
| : null | ||
| } | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. all of these requests are prefetched and therefore never |
||
|
|
||
| const readRoles = new Set<RoleKey>(['admin', 'collaborator', 'viewer']) | ||
| const writeRoles = new Set<RoleKey>(['admin', 'collaborator']) | ||
|
|
||
| export const canRead = (role: RoleKey | null) => (role ? readRoles.has(role) : false) | ||
| export const canWrite = (role: RoleKey | null) => (role ? writeRoles.has(role) : false) | ||
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.
prefetch silo policy
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.
Worth a comment?