Repository navigation
Expand file tree
/
Copy pathSiloAccessPage.tsx
More file actions
223 lines (202 loc) · 6.64 KB
/
Copy pathSiloAccessPage.tsx
File metadata and controls
223 lines (202 loc) · 6.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, you can obtain one at https://mozilla.org/MPL/2.0/.
*
* Copyright Oxide Computer Company
*/
import { createColumnHelper, getCoreRowModel, useReactTable } from '@tanstack/react-table'
import { useMemo, useState } from 'react'
import {
api,
byGroupThenName,
deleteRole,
displayUserName,
q,
queryClient,
useApiMutation,
usePrefetchedQuery,
useUserRows,
type IdentityType,
type RoleKey,
type UserName,
} from '@oxide/api'
import { Access16Icon, Access24Icon } from '@oxide/design-system/icons/react'
import { Badge } from '@oxide/design-system/ui'
import { DocsPopover } from '~/components/DocsPopover'
import { HL } from '~/components/HL'
import {
SiloAccessAddUserSideModal,
SiloAccessEditUserSideModal,
} from '~/forms/silo-access'
import { useCurrentUser } from '~/hooks/use-current-user'
import { useQuickActions } from '~/hooks/use-quick-actions'
import { confirmDelete } from '~/stores/confirm-delete'
import { addToast } from '~/stores/toast'
import { getActionsCol } from '~/table/columns/action-col'
import { Table } from '~/table/Table'
import { CreateButton } from '~/ui/lib/CreateButton'
import { EmptyMessage } from '~/ui/lib/EmptyMessage'
import { PageHeader, PageTitle } from '~/ui/lib/PageHeader'
import { TableActions, TableEmptyBox } from '~/ui/lib/Table'
import { identityTypeLabel, roleColor } from '~/util/access'
import { groupBy } from '~/util/array'
import { docLinks } from '~/util/links'
const EmptyState = ({ onClick }: { onClick: () => void }) => (
<TableEmptyBox>
<EmptyMessage
icon={<Access24Icon />}
title="No authorized users"
body="Give permission to view, edit, or administer this silo"
buttonText="Add user or group"
onClick={onClick}
/>
</TableEmptyBox>
)
const policyView = q(api.policyView, {})
const userList = q(api.userList, {})
const groupList = q(api.groupList, {})
export async function clientLoader() {
await Promise.all([
queryClient.prefetchQuery(policyView),
// used to resolve user names
queryClient.prefetchQuery(userList),
queryClient.prefetchQuery(groupList),
])
return null
}
export const handle = { crumb: 'Silo Access' }
type UserRow = {
id: string
identityType: IdentityType
name: UserName
siloRole: RoleKey | undefined
}
const colHelper = createColumnHelper<UserRow>()
export default function SiloAccessPage() {
const [addModalOpen, setAddModalOpen] = useState(false)
const [editingUserRow, setEditingUserRow] = useState<UserRow | null>(null)
const { me } = useCurrentUser()
const { data: siloPolicy } = usePrefetchedQuery(policyView)
const siloRows = useUserRows(siloPolicy.roleAssignments, 'silo')
const rows = useMemo(() => {
return groupBy(siloRows, (u) => u.id)
.map(([userId, userAssignments]) => {
const siloRole = userAssignments.find((a) => a.roleSource === 'silo')?.roleName
const { name, identityType } = userAssignments[0]
const row: UserRow = {
id: userId,
identityType,
name,
siloRole,
}
return row
})
.sort(byGroupThenName)
}, [siloRows])
const { mutateAsync: updatePolicy } = useApiMutation(api.policyUpdate, {
onSuccess: () => {
queryClient.invalidateEndpoint('policyView')
addToast({ content: 'Access removed' })
},
// TODO: handle 403
})
// TODO: checkboxes and bulk delete? not sure
// TODO: disable delete on permissions you can't delete
const columns = useMemo(
() => [
colHelper.accessor('name', {
header: 'Name',
cell: (info) => displayUserName(info.getValue()),
}),
colHelper.accessor('identityType', {
header: 'Type',
cell: (info) => identityTypeLabel[info.getValue()],
}),
colHelper.accessor('siloRole', {
header: 'Role',
cell: (info) => {
const role = info.getValue()
return role ? <Badge color={roleColor[role]}>silo.{role}</Badge> : null
},
}),
// TODO: tooltips on disabled elements explaining why
getActionsCol((row: UserRow) => [
{
label: 'Change role',
onActivate: () => setEditingUserRow(row),
disabled: !row.siloRole && "You don't have permission to change this user's role",
},
// TODO: only show if you have permission to do this
{
label: 'Delete',
onActivate: confirmDelete({
doDelete: () => updatePolicy({ body: deleteRole(row.id, siloPolicy) }),
label: (
<span>
the <HL>{row.siloRole}</HL> role for <HL>{displayUserName(row.name)}</HL>
</span>
),
resourceKind: 'role assignment',
extraContent:
row.id === me.id ? 'This will remove your own silo access.' : undefined,
}),
disabled: !row.siloRole && "You don't have permission to delete this user",
},
]),
],
[siloPolicy, updatePolicy, me]
)
const tableInstance = useReactTable({
columns,
data: rows,
getCoreRowModel: getCoreRowModel(),
})
useQuickActions(
() => [
{
value: 'Add user or group',
navGroup: 'Actions',
action: () => setAddModalOpen(true),
},
],
[]
)
return (
<>
<PageHeader>
<PageTitle icon={<Access24Icon />}>Silo Access</PageTitle>
<DocsPopover
heading="access"
icon={<Access16Icon />}
summary="Roles determine who can view, edit, or administer this silo and the projects within it. If a user or group has both a silo and project role, the stronger role takes precedence."
links={[docLinks.keyConceptsIam, docLinks.access]}
/>
</PageHeader>
<TableActions>
<CreateButton onClick={() => setAddModalOpen(true)}>Add user or group</CreateButton>
</TableActions>
{addModalOpen && (
<SiloAccessAddUserSideModal
onDismiss={() => setAddModalOpen(false)}
policy={siloPolicy}
/>
)}
{editingUserRow?.siloRole && (
<SiloAccessEditUserSideModal
onDismiss={() => setEditingUserRow(null)}
policy={siloPolicy}
name={displayUserName(editingUserRow.name)}
identityId={editingUserRow.id}
identityType={editingUserRow.identityType}
defaultValues={{ roleName: editingUserRow.siloRole }}
/>
)}
{rows.length === 0 ? (
<EmptyState onClick={() => setAddModalOpen(true)} />
) : (
<Table table={tableInstance} />
)}
</>
)
}