1
1
import type { Interpolation , Theme } from "@emotion/react" ;
2
2
import Checkbox from "@mui/material/Checkbox" ;
3
+ import FormControlLabel from "@mui/material/FormControlLabel" ;
3
4
import Table from "@mui/material/Table" ;
4
5
import TableBody from "@mui/material/TableBody" ;
5
6
import TableCell from "@mui/material/TableCell" ;
6
7
import TableContainer from "@mui/material/TableContainer" ;
8
+ import TableHead from "@mui/material/TableHead" ;
7
9
import TableRow from "@mui/material/TableRow" ;
8
10
import TextField from "@mui/material/TextField" ;
9
11
import type { useFormik } from "formik" ;
@@ -103,10 +105,28 @@ const ResourceActionComparator = (
103
105
p . resource_type === resource &&
104
106
( p . action . toString ( ) === "*" || p . action === action ) ;
105
107
108
+ const DEFAULT_RESOURCES = [
109
+ "audit_log" ,
110
+ "group" ,
111
+ "template" ,
112
+ "organization_member" ,
113
+ "provisioner_daemon" ,
114
+ "workspace" ,
115
+ ] ;
116
+
117
+ const resources = new Set ( DEFAULT_RESOURCES ) ;
118
+
119
+ const filteredRBACResourceActions = Object . fromEntries (
120
+ Object . entries ( RBACResourceActions ) . filter ( ( [ resource ] ) =>
121
+ resources . has ( resource ) ,
122
+ ) ,
123
+ ) ;
124
+
106
125
const ActionCheckboxes : FC < ActionCheckboxesProps > = ( { permissions, form } ) => {
107
126
const [ checkedActions , setCheckActions ] = useState ( permissions ) ;
127
+ const [ showAllResources , setShowAllResources ] = useState ( false ) ;
108
128
109
- const handleCheckChange = async (
129
+ const handleActionCheckChange = async (
110
130
e : ChangeEvent < HTMLInputElement > ,
111
131
form : ReturnType < typeof useFormik < Role > > & { values : Role } ,
112
132
) => {
@@ -130,50 +150,86 @@ const ActionCheckboxes: FC<ActionCheckboxesProps> = ({ permissions, form }) => {
130
150
await form . setFieldValue ( "organization_permissions" , newPermissions ) ;
131
151
} ;
132
152
153
+ const resourceActions = showAllResources
154
+ ? RBACResourceActions
155
+ : filteredRBACResourceActions ;
156
+
133
157
return (
134
- < TableContainer >
135
- < Table >
136
- < TableBody >
137
- { Object . entries ( RBACResourceActions ) . map ( ( [ resourceKey , value ] ) => {
138
- return (
139
- < TableRow key = { resourceKey } >
140
- < TableCell >
141
- < li key = { resourceKey } css = { styles . checkBoxes } >
142
- { resourceKey }
143
- < ul css = { styles . checkBoxes } >
144
- { Object . entries ( value ) . map ( ( [ actionKey , value ] ) => {
145
- return (
146
- < li key = { actionKey } >
147
- < span css = { styles . actionText } >
148
- < Checkbox
149
- name = { `${ resourceKey } :${ actionKey } ` }
150
- checked = {
151
- checkedActions ?. some ( ( p ) =>
152
- ResourceActionComparator (
153
- p ,
154
- resourceKey ,
155
- actionKey ,
156
- ) ,
157
- ) || false
158
- }
159
- onChange = { ( e ) => handleCheckChange ( e , form ) }
160
- />
161
- { actionKey }
162
- </ span > { " " }
163
- –{ " " }
164
- < span css = { styles . actionDescription } > { value } </ span >
165
- </ li >
166
- ) ;
167
- } ) }
168
- </ ul >
169
- </ li >
170
- </ TableCell >
171
- </ TableRow >
172
- ) ;
173
- } ) }
174
- </ TableBody >
175
- </ Table >
176
- </ TableContainer >
158
+ < >
159
+ < TableContainer >
160
+ < Table >
161
+ < TableHead >
162
+ < TableRow >
163
+ < TableCell
164
+ align = "right"
165
+ sx = { { paddingTop : 0.4 , paddingBottom : 0.4 } }
166
+ >
167
+ < FormControlLabel
168
+ sx = { { marginRight : 1 } }
169
+ control = {
170
+ < Checkbox
171
+ size = "small"
172
+ id = "show_all_permissions"
173
+ name = "show_all_permissions"
174
+ checked = { showAllResources }
175
+ onChange = { ( e ) =>
176
+ setShowAllResources ( e . currentTarget . checked )
177
+ }
178
+ />
179
+ }
180
+ label = {
181
+ < span style = { { fontSize : 12 } } > Show all permissions</ span >
182
+ }
183
+ />
184
+ </ TableCell >
185
+ </ TableRow >
186
+ </ TableHead >
187
+ < TableBody >
188
+ { Object . entries ( resourceActions ) . map ( ( [ resourceKey , value ] ) => {
189
+ return (
190
+ < TableRow key = { resourceKey } >
191
+ < TableCell sx = { { paddingLeft : 2 } } >
192
+ < li key = { resourceKey } css = { styles . checkBoxes } >
193
+ { resourceKey }
194
+ < ul css = { styles . checkBoxes } >
195
+ { Object . entries ( value ) . map ( ( [ actionKey , value ] ) => {
196
+ return (
197
+ < li key = { actionKey } >
198
+ < span css = { styles . actionText } >
199
+ < Checkbox
200
+ name = { `${ resourceKey } :${ actionKey } ` }
201
+ checked = {
202
+ checkedActions ?. some ( ( p ) =>
203
+ ResourceActionComparator (
204
+ p ,
205
+ resourceKey ,
206
+ actionKey ,
207
+ ) ,
208
+ ) || false
209
+ }
210
+ onChange = { ( e ) =>
211
+ handleActionCheckChange ( e , form )
212
+ }
213
+ />
214
+ { actionKey }
215
+ </ span > { " " }
216
+ –{ " " }
217
+ < span css = { styles . actionDescription } >
218
+ { value }
219
+ </ span >
220
+ </ li >
221
+ ) ;
222
+ } ) }
223
+ </ ul >
224
+ </ li >
225
+ </ TableCell >
226
+ </ TableRow >
227
+ ) ;
228
+ } ) }
229
+ </ TableBody >
230
+ </ Table >
231
+ </ TableContainer >
232
+ </ >
177
233
) ;
178
234
} ;
179
235
0 commit comments