-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy pathAddGroups.tsx
More file actions
411 lines (377 loc) · 14 KB
/
Copy pathAddGroups.tsx
File metadata and controls
411 lines (377 loc) · 14 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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
import * as React from 'react';
import dayjs, {Dayjs} from 'dayjs';
import IsSameOrBefore from 'dayjs/plugin/isSameOrBefore';
import {useNavigate} from 'react-router-dom';
import Button from '@mui/material/Button';
import Dialog from '@mui/material/Dialog';
import DialogActions from '@mui/material/DialogActions';
import DialogContent from '@mui/material/DialogContent';
import DialogTitle from '@mui/material/DialogTitle';
import GroupAddIcon from '@mui/icons-material/GroupAdd';
import Alert from '@mui/material/Alert';
import FormControl from '@mui/material/FormControl';
import Grid from '@mui/material/Grid';
import Typography from '@mui/material/Typography';
import Box from '@mui/material/Box';
import Divider from '@mui/material/Divider';
import List from '@mui/material/List';
import ListItem from '@mui/material/ListItem';
import ListItemText from '@mui/material/ListItemText';
import IconButton from '@mui/material/IconButton';
import InputLabel from '@mui/material/InputLabel';
import DeleteIcon from '@mui/icons-material/Close';
import CircularProgress from '@mui/material/CircularProgress';
import Select from '@mui/material/Select';
import MenuItem from '@mui/material/MenuItem';
import {
FormContainer,
SelectElement,
AutocompleteElement,
DatePickerElement,
TextFieldElement,
} from 'react-hook-form-mui';
import {
useGetGroups,
usePutRoleMembersById,
PutRoleMembersByIdError,
PutRoleMembersByIdVariables,
} from '../../api/apiComponents';
import {PolymorphicGroup, AppGroup, OktaGroup, RoleMember, OktaUser} from '../../api/apiSchemas';
import {isAccessAdmin, isGroupOwner} from '../../authorization';
import {minTagTimeGroups, requiredReasonGroups, ownerCantAddSelf} from '../../helpers';
import {useCurrentUser} from '../../authentication';
import {group} from 'console';
import accessConfig, {requireReasons} from '../../config/accessConfig';
dayjs.extend(IsSameOrBefore);
interface AddGroupsButtonProps {
owner: boolean;
setOpen(open: boolean): any;
}
function AddGroupsButton(props: AddGroupsButtonProps) {
return (
<Button variant="contained" onClick={() => props.setOpen(true)} endIcon={<GroupAddIcon />}>
{'Add ' + (props.owner ? 'Owner Groups' : 'Groups')}
</Button>
);
}
interface AddGroupsDialogProps {
currentUser: OktaUser;
owner: boolean;
group: PolymorphicGroup;
setOpen(open: boolean): any;
}
interface AddGroupsForm {
customUntil?: string;
reason?: string;
}
const GROUP_TYPE_ID_TO_LABELS: Record<string, string> = {
okta_group: 'Group',
app_group: 'App Group',
role_group: 'Role',
} as const;
const RFC822_FORMAT = 'ddd, DD MMM YYYY HH:mm:ss ZZ';
const UNTIL_ID_TO_LABELS: Record<string, string> = accessConfig.ACCESS_TIME_LABELS;
const UNTIL_JUST_NUMERIC_ID_TO_LABELS: Record<string, string> = Object.fromEntries(
Object.entries(UNTIL_ID_TO_LABELS).filter(([key]) => !isNaN(Number(key))),
);
const UNTIL_OPTIONS = Object.entries(UNTIL_ID_TO_LABELS).map(([id, label], index) => ({id: id, label: label}));
function AddGroupsDialog(props: AddGroupsDialogProps) {
const navigate = useNavigate();
const currentUser = useCurrentUser();
const disallowedGroups = (
currentUser.active_group_ownerships?.reduce((out, curr) => {
curr != null && curr.active_group != null ? out.push(curr.active_group) : null;
return out;
}, new Array<OktaGroup>()) ?? []
)
.filter((group) =>
ownerCantAddSelf(
group.active_group_tags?.map((tagMap) => tagMap.active_tag!),
props.owner,
),
)
.map((group) => group.id!);
const currUserRoleGroupMember =
props.group.active_user_memberships?.map((membership) => membership.active_user!.id).includes(currentUser.id) ??
false;
const [until, setUntil] = React.useState(accessConfig.DEFAULT_ACCESS_TIME);
const [groupSearchInput, setGroupSearchInput] = React.useState('');
const [groups, setGroups] = React.useState<Array<OktaGroup | AppGroup>>([]);
const [requestError, setRequestError] = React.useState('');
const [submitting, setSubmitting] = React.useState(false);
const [labels, setLabels] = React.useState<Array<Record<string, string>>>(UNTIL_OPTIONS);
const [timeLimit, setTimeLimit] = React.useState<number | null>(null);
const [requiredReason, setRequiredReason] = React.useState<boolean>(false);
const complete = (
completedUsersChange: RoleMember | undefined,
error: PutRoleMembersByIdError | null,
variables: PutRoleMembersByIdVariables,
context: any,
) => {
setSubmitting(false);
if (error != null) {
setRequestError(error.payload.toString());
} else {
props.setOpen(false);
navigate(0);
}
};
const putGroupUsers = usePutRoleMembersById({
onSettled: complete,
});
const {data: groupSearchData} = useGetGroups({
queryParams: {
page: 0,
per_page: 10,
managed: true,
q: groupSearchInput,
},
});
const groupSearchOptions = groupSearchData?.results ?? [];
const updateUntil = (time: number | null) => {
setTimeLimit(time);
if (!(time == null)) {
const filteredUntil = Object.keys(UNTIL_JUST_NUMERIC_ID_TO_LABELS)
.filter((key) => Number(key) <= time!)
.reduce(
(obj, key) => {
obj[key] = UNTIL_JUST_NUMERIC_ID_TO_LABELS[key];
return obj;
},
{} as Record<string, string>,
);
setUntil(Object.keys(filteredUntil).at(-1)!);
setLabels(
Object.entries(Object.assign({}, filteredUntil, {custom: 'Custom'})).map(([id, label], index) => ({
id: id,
label: label,
})),
);
} else {
setLabels(UNTIL_OPTIONS);
}
};
const submit = (groupsForm: AddGroupsForm) => {
setSubmitting(true);
const roleMembers: RoleMember = {
groups_to_add: [],
groups_to_remove: [],
owner_groups_to_add: [],
owner_groups_to_remove: [],
};
if (props.owner) {
roleMembers.owner_groups_to_add = groups.map((group) => group?.id ?? '');
} else {
roleMembers.groups_to_add = groups.map((group) => group?.id ?? '');
}
switch (until) {
case 'indefinite':
break;
case 'custom':
roleMembers.groups_added_ending_at = (groupsForm.customUntil as unknown as Dayjs).format(RFC822_FORMAT);
break;
default:
roleMembers.groups_added_ending_at = dayjs()
.add(parseInt(until ?? '0', 10), 'seconds')
.format(RFC822_FORMAT);
break;
}
roleMembers.created_reason = groupsForm.reason ?? '';
putGroupUsers.mutate({
body: roleMembers,
pathParams: {roleId: props.group?.id ?? ''},
});
};
const removeGroupFromList = (groupId: string) => {
setGroups(groups.filter((group) => group.id != groupId));
};
const addGroupsText = props.owner ? 'Owner Groups' : 'Member Groups';
return (
<Dialog open fullWidth onClose={() => props.setOpen(false)}>
<FormContainer<AddGroupsForm> onSuccess={(formData) => submit(formData)}>
<DialogTitle>Add {addGroupsText}</DialogTitle>
<DialogContent>
<Typography variant="subtitle1" color="text.accent">
{timeLimit
? (props.owner ? 'Ownership of ' : 'Membership to ') +
'one or more selected groups is limited to ' +
Math.floor(timeLimit / 86400) +
' days.'
: null}
</Typography>
<Typography variant="subtitle1" color="text.accent">
{disallowedGroups.length != 0 && !isAccessAdmin(currentUser)
? // this case will never be hit as-is since admins are exempt from owner add constraints
// leaving in in case we change the visibility of this dialog in the future
'Some groups may not be added due to group tag constraints.'
: null}
</Typography>
{requestError != '' ? <Alert severity="error">{requestError}</Alert> : null}
<FormControl size="small" margin="normal" fullWidth>
<InputLabel>For how long?</InputLabel>
<Select
label="For how long?"
name="until"
value={until}
onChange={(event) => setUntil(event.target.value)}
required>
{labels.map((label) => {
return (
<MenuItem key={label.id} value={label.id}>
{label.label}
</MenuItem>
);
})}
</Select>
</FormControl>
{until == 'custom' ? (
<FormControl margin="normal" fullWidth required>
<DatePickerElement
label="Custom End Date"
name="customUntil"
shouldDisableDate={(date: Dayjs) => date.isSameOrBefore(dayjs(), 'day')}
maxDate={timeLimit ? dayjs().add(timeLimit, 'second') : null}
required
/>
</FormControl>
) : null}
<FormControl fullWidth sx={{margin: '2px 0'}}>
<TextFieldElement
label="Why? (provide a reason)"
name="reason"
multiline
rows={4}
required={requireReasons || requiredReason}
validation={{maxLength: 1024}}
parseError={(error) => {
if (error?.message != '') {
return error?.message ?? '';
}
if (error.type == 'maxLength') {
return 'Reason can be at most 1024 characters in length';
}
return '';
}}
/>
</FormControl>
<FormControl fullWidth sx={{margin: '8px 0'}}>
<AutocompleteElement
label={'Search for ' + addGroupsText + ' to Add'}
name="group"
options={groupSearchOptions}
autocompleteProps={{
getOptionLabel: (option) => option.name,
isOptionEqualToValue: (option, value) => option.id == value.id,
filterOptions: (options) =>
options.filter(
(option) =>
option.type != 'role_group' &&
option.is_managed == true &&
(!groups.map((group) => group.id).includes(option.id) ||
(currUserRoleGroupMember && !isAccessAdmin(currentUser)
? !disallowedGroups.includes(option.id!)
: false)),
),
onInputChange: (event, newInputValue, reason) => {
if (reason != 'reset') {
setGroupSearchInput(newInputValue);
}
},
onChange: (event, value) => {
if (value != null) {
const allGroups = [value, ...groups];
setGroups(allGroups);
setGroupSearchInput('');
updateUntil(minTagTimeGroups(allGroups, props.owner));
setRequiredReason(requiredReasonGroups(allGroups, props.owner));
} else {
updateUntil(null);
setRequiredReason(false);
}
},
inputValue: groupSearchInput,
renderOption: (props, option, state) => {
return (
<li {...props}>
<Grid container alignItems="center">
<Grid item>
<Box>{option.name}</Box>
<Typography variant="body2" color="text.secondary">
{GROUP_TYPE_ID_TO_LABELS[option.type]}
</Typography>
</Grid>
</Grid>
</li>
);
},
}}
/>
</FormControl>
<FormControl fullWidth sx={{marginTop: '5px'}}>
<InputLabel shrink={groups.length > 0}>{addGroupsText} to Add</InputLabel>
<List
sx={{
overflow: 'auto',
minHeight: 300,
maxHeight: 600,
backgroundColor: (theme) =>
theme.palette.mode === 'light' ? theme.palette.grey[100] : theme.palette.grey[900],
}}
dense={true}>
{groups.map((group) => (
<React.Fragment key={group.id}>
<ListItem
sx={{py: 0}}
secondaryAction={
<IconButton edge="end" aria-label="delete" onClick={() => removeGroupFromList(group?.id ?? '')}>
<DeleteIcon />
</IconButton>
}>
<ListItemText
primary={group?.name ?? ''}
secondary={GROUP_TYPE_ID_TO_LABELS[group?.type ?? 'okta_group']}
/>
</ListItem>
<Divider />
</React.Fragment>
))}
</List>
</FormControl>
</DialogContent>
<DialogActions>
<Button onClick={() => props.setOpen(false)}>Cancel</Button>
<Button type="submit" disabled={submitting || groups.length == 0}>
{submitting ? <CircularProgress size={24} /> : 'Add'}
</Button>
</DialogActions>
</FormContainer>
</Dialog>
);
}
interface AddGroupsProps {
currentUser: OktaUser;
group: PolymorphicGroup;
owner?: boolean;
}
export default function AddGroups(props: AddGroupsProps) {
const [open, setOpen] = React.useState(false);
if (props.group.deleted_at != null || !isAccessAdmin(props.currentUser)) {
return null;
}
return (
<>
<AddGroupsButton setOpen={setOpen} owner={props.owner ?? false} />
{open ? (
<AddGroupsDialog
currentUser={props.currentUser}
group={props.group}
owner={props.owner ?? false}
setOpen={setOpen}
/>
) : null}
</>
);
}
AddGroups.defaultProps = {
owner: false,
};