-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathAddUsers.tsx
More file actions
414 lines (378 loc) · 13.6 KB
/
AddUsers.tsx
File metadata and controls
414 lines (378 loc) · 13.6 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
412
413
414
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 UserAddIcon from '@mui/icons-material/PersonAdd';
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 {
FormContainer,
SelectElement,
AutocompleteElement,
DatePickerElement,
TextFieldElement,
} from 'react-hook-form-mui';
import {
useGetUsers,
usePutGroupMembersById,
PutGroupMembersByIdError,
PutGroupMembersByIdVariables,
} from '../../api/apiComponents';
import {PolymorphicGroup, GroupMember, OktaUser, RoleGroup, OktaGroup, AppGroup} from '../../api/apiSchemas';
import {canManageGroup, isAccessAdmin} from '../../authorization';
import {
displayUserName,
minTagTime,
minTagTimeGroups,
ownerCantAddSelf,
ownerCantAddSelfGroups,
requiredReason,
requiredReasonGroups,
} from '../../helpers';
import accessConfig, {requireReasons} from '../../config/accessConfig';
dayjs.extend(IsSameOrBefore);
interface AddUsersButtonProps {
owner: boolean;
setOpen(open: boolean): any;
}
function AddUsersButton(props: AddUsersButtonProps) {
return (
<Button variant="contained" onClick={() => props.setOpen(true)} endIcon={<UserAddIcon />}>
{'Add ' + (props.owner ? 'Owners' : 'Members')}
</Button>
);
}
interface AddUsersDialogProps {
currentUser: OktaUser;
owner: boolean;
group: PolymorphicGroup;
setOpen(open: boolean): any;
}
interface AddUsersForm {
until?: string;
customUntil?: string;
reason?: string;
}
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 AddUsersDialog(props: AddUsersDialogProps) {
const navigate = useNavigate();
const [until, setUntil] = React.useState(accessConfig.DEFAULT_ACCESS_TIME);
const [userSearchInput, setUserSearchInput] = React.useState('');
const [users, setUsers] = React.useState<Array<OktaUser>>([]);
const [requestError, setRequestError] = React.useState('');
const [submitting, setSubmitting] = React.useState(false);
// in seconds
let timeLimit = minTagTime(
props.group.active_group_tags ? props.group.active_group_tags.map((tagMap) => tagMap.active_tag!) : [],
props.owner,
);
let reason = requiredReason(
props.group.active_group_tags ? props.group.active_group_tags.map((tagMap) => tagMap.active_tag!) : [],
props.owner,
);
let disallow_owner_add = ownerCantAddSelf(
props.group.active_group_tags ? props.group.active_group_tags.map((tagMap) => tagMap.active_tag!) : [],
props.owner,
);
if (props.group.type == 'role_group' && !props.owner) {
const active_groups_owners = (props.group as RoleGroup).active_role_associated_group_owner_mappings?.reduce(
(out, curr) => {
curr.active_group ? out.push(curr.active_group) : null;
return out;
},
new Array<OktaGroup | AppGroup>(),
);
const active_groups_members = (props.group as RoleGroup).active_role_associated_group_member_mappings?.reduce(
(out, curr) => {
curr.active_group ? out.push(curr.active_group) : null;
return out;
},
new Array<OktaGroup | AppGroup>(),
);
reason =
reason ||
requiredReasonGroups(active_groups_members ?? [], false) ||
requiredReasonGroups(active_groups_owners ?? [], true);
disallow_owner_add =
disallow_owner_add ||
ownerCantAddSelfGroups(active_groups_members ?? [], false) ||
ownerCantAddSelfGroups(active_groups_owners ?? [], true);
}
let labels = null;
let timeLimitUntil = null;
if (!(timeLimit == null)) {
const filteredUntil = Object.keys(UNTIL_JUST_NUMERIC_ID_TO_LABELS)
.filter((key) => Number(key) <= timeLimit!)
.reduce(
(obj, key) => {
obj[key] = UNTIL_JUST_NUMERIC_ID_TO_LABELS[key];
return obj;
},
{} as Record<string, string>,
);
timeLimitUntil =
timeLimit >= Number(accessConfig.DEFAULT_ACCESS_TIME)
? accessConfig.DEFAULT_ACCESS_TIME
: Object.keys(filteredUntil).at(-1)!;
labels = Object.entries(Object.assign({}, filteredUntil, {custom: 'Custom'})).map(([id, label], index) => ({
id: id,
label: label,
}));
}
const complete = (
completedUsersChange: GroupMember | undefined,
error: PutGroupMembersByIdError | null,
variables: PutGroupMembersByIdVariables,
context: any,
) => {
setSubmitting(false);
if (error != null) {
setRequestError(error.payload.toString());
} else {
props.setOpen(true);
navigate(0);
}
};
const putGroupUsers = usePutGroupMembersById({
onSettled: complete,
});
const {data: userSearchData} = useGetUsers({
queryParams: {
page: 0,
per_page: 10,
q: userSearchInput,
},
});
const userSearchOptions = userSearchData?.results ?? [];
const submit = (usersForm: AddUsersForm) => {
setSubmitting(true);
const groupUsers: GroupMember = {
members_to_add: [],
members_to_remove: [],
owners_to_add: [],
owners_to_remove: [],
};
if (props.owner) {
groupUsers.owners_to_add = users.map((user) => user.id);
} else {
groupUsers.members_to_add = users.map((user) => user.id);
}
switch (usersForm.until) {
case 'indefinite':
break;
case 'custom':
groupUsers.users_added_ending_at = (usersForm.customUntil as unknown as Dayjs).format(RFC822_FORMAT);
break;
default:
groupUsers.users_added_ending_at = dayjs()
.add(parseInt(usersForm.until ?? '0', 10), 'seconds')
.format(RFC822_FORMAT);
break;
}
groupUsers.created_reason = usersForm.reason ?? '';
putGroupUsers.mutate({
body: groupUsers,
pathParams: {groupId: props.group?.id ?? ''},
});
};
const removeUserFromList = (userId: string) => {
setUsers(users.filter((user) => user.id != userId));
};
const addUsersText = props.owner ? 'Owners' : 'Members';
return (
<Dialog open fullWidth onClose={() => props.setOpen(false)}>
<FormContainer<AddUsersForm>
defaultValues={timeLimit ? {until: timeLimitUntil!} : {until: accessConfig.DEFAULT_ACCESS_TIME}}
onSuccess={(formData) => submit(formData)}>
<DialogTitle>Add {addUsersText}</DialogTitle>
<DialogContent>
<Typography variant="subtitle1" color="text.accent">
{timeLimit
? (props.owner ? 'Ownership of ' : 'Membership to ') +
'this group is limited to ' +
Math.floor(timeLimit / 86400) +
' days.'
: null}
</Typography>
<Typography variant="subtitle1" color="text.accent">
{disallow_owner_add && !isAccessAdmin(props.currentUser)
? 'Owners may not add themselves as ' + (props.owner ? 'owners' : 'members') + ' of this group.'
: null}
</Typography>
{requestError != '' ? <Alert severity="error">{requestError}</Alert> : null}
<FormControl size="small" margin="normal" fullWidth>
<SelectElement
label="For how long?"
name="until"
options={labels ?? UNTIL_OPTIONS}
onChange={(value) => setUntil(value)}
required
/>
</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 || reason}
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 ' + addUsersText + ' to Add'}
name="user"
options={userSearchOptions}
autocompleteProps={{
getOptionLabel: (option) => displayUserName(option),
isOptionEqualToValue: (option, value) => option.id == value.id,
filterOptions: (options) =>
options.filter((option) => {
const userIds = users.map((user) => user.id);
return (
!userIds.includes(option.id) &&
(disallow_owner_add && !isAccessAdmin(props.currentUser)
? !(option.id == props.currentUser.id)
: true)
);
}),
onInputChange: (event, newInputValue, reason) => {
if (reason != 'reset') {
setUserSearchInput(newInputValue);
}
},
onChange: (event, value) => {
if (value != null) {
setUsers([value, ...users]);
setUserSearchInput('');
}
},
inputValue: userSearchInput,
renderOption: (props, option, state) => {
return (
<li {...props}>
<Grid container alignItems="center">
<Grid item>
<Box>{displayUserName(option)}</Box>
<Typography variant="body2" color="text.secondary">
{option.email.toLowerCase()}
</Typography>
</Grid>
</Grid>
</li>
);
},
}}
/>
</FormControl>
<FormControl fullWidth sx={{marginTop: '5px'}}>
<InputLabel shrink={users.length > 0}>{addUsersText} to Add</InputLabel>
<List
sx={{
overflow: 'auto',
minHeight: 200,
maxHeight: 400,
backgroundColor: (theme) =>
theme.palette.mode === 'light' ? theme.palette.grey[100] : theme.palette.grey[900],
}}
dense={true}>
{users.map((user) => (
<React.Fragment key={user.id}>
<ListItem
sx={{py: 0}}
secondaryAction={
<IconButton edge="end" aria-label="delete" onClick={() => removeUserFromList(user.id)}>
<DeleteIcon />
</IconButton>
}>
<ListItemText primary={displayUserName(user)} secondary={user.email.toLowerCase()} />
</ListItem>
<Divider />
</React.Fragment>
))}
</List>
</FormControl>
</DialogContent>
<DialogActions>
<Button onClick={() => props.setOpen(false)}>Cancel</Button>
<Button type="submit" disabled={submitting || users.length === 0}>
{submitting ? <CircularProgress size={24} /> : 'Add'}
</Button>
</DialogActions>
</FormContainer>
</Dialog>
);
}
interface AddUsersProps {
currentUser: OktaUser;
group: PolymorphicGroup;
owner?: boolean;
}
export default function AddUsers(props: AddUsersProps) {
const [open, setOpen] = React.useState(false);
if (
props.group.deleted_at != null ||
!canManageGroup(props.currentUser, props.group) ||
props.group?.is_managed == false
) {
return null;
}
return (
<>
<AddUsersButton setOpen={setOpen} owner={props.owner ?? false} />
{open ? (
<AddUsersDialog
currentUser={props.currentUser}
group={props.group}
owner={props.owner ?? false}
setOpen={setOpen}
/>
) : null}
</>
);
}
AddUsers.defaultProps = {
owner: false,
};