forked from KelvinTegelaar/CIPP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCippUserActions.jsx
More file actions
794 lines (761 loc) · 24.1 KB
/
CippUserActions.jsx
File metadata and controls
794 lines (761 loc) · 24.1 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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
import { useEffect } from "react";
import { EyeIcon, MagnifyingGlassIcon, TrashIcon } from "@heroicons/react/24/outline";
import {
Archive,
Clear,
CloudDone,
Edit,
Email,
ForwardToInbox,
GroupAdd,
LockClock,
LockPerson,
LockReset,
MeetingRoom,
Password,
PersonOff,
PhonelinkLock,
PhonelinkSetup,
Shortcut,
EditAttributes,
CloudSync,
} from "@mui/icons-material";
import { getCippLicenseTranslation } from "../../utils/get-cipp-license-translation";
import { useSettings } from "../../hooks/use-settings.js";
import { usePermissions } from "../../hooks/use-permissions";
import { Tooltip, Box, Divider, Typography } from "@mui/material";
import CippFormComponent from "./CippFormComponent";
import { CippFormCondition } from "./CippFormCondition";
import { useWatch } from "react-hook-form";
// Separate component for Manage Licenses form to avoid hook issues
const ManageLicensesForm = ({ formControl, tenant }) => {
const licenseOperation = useWatch({
control: formControl.control,
name: "LicenseOperation",
});
const removeAllLicenses = useWatch({
control: formControl.control,
name: "RemoveAllLicenses",
});
const replaceAllLicenses = useWatch({
control: formControl.control,
name: "ReplaceAllLicenses",
});
// Handle both string values and object values with .value property
const licenseOpValue = licenseOperation?.value || licenseOperation;
const isRemoveOperation = licenseOpValue === "Remove";
const isReplaceOperation = licenseOpValue === "Replace";
const showLicensesToRemove = isRemoveOperation && !removeAllLicenses;
const showLicensesToReplace = isReplaceOperation && !replaceAllLicenses;
// Clear fields when operation changes to prevent stale data submission
useEffect(() => {
if (licenseOpValue) {
// Clear all license-related fields when switching operations
if (licenseOpValue === "Add") {
// Clear Remove/Replace specific fields
formControl.setValue("RemoveAllLicenses", false);
formControl.setValue("ReplaceAllLicenses", false);
formControl.setValue("LicensesToRemove", []);
formControl.setValue("LicensesToReplace", []);
} else if (licenseOpValue === "Remove") {
// Clear Add/Replace specific fields
formControl.setValue("ReplaceAllLicenses", false);
formControl.setValue("LicensesToReplace", []);
formControl.setValue("Licenses", []);
} else if (licenseOpValue === "Replace") {
// Clear Remove specific fields
formControl.setValue("RemoveAllLicenses", false);
formControl.setValue("LicensesToRemove", []);
}
}
}, [licenseOpValue, formControl]);
// Clear LicensesToReplace when ReplaceAllLicenses is toggled
useEffect(() => {
if (isReplaceOperation && replaceAllLicenses) {
formControl.setValue("LicensesToReplace", []);
}
}, [replaceAllLicenses, isReplaceOperation, formControl]);
return (
<>
<CippFormComponent
type="radio"
name="LicenseOperation"
label="License Operation"
formControl={formControl}
options={[
{ label: "Add Licenses", value: "Add" },
{ label: "Remove Licenses", value: "Remove" },
{ label: "Replace Licenses", value: "Replace" },
]}
validators={{ required: "Please select a license operation" }}
/>
{isRemoveOperation && (
<CippFormComponent
type="switch"
name="RemoveAllLicenses"
label="Remove All Existing Licenses"
formControl={formControl}
/>
)}
{isReplaceOperation && (
<CippFormComponent
type="switch"
name="ReplaceAllLicenses"
label="Replace All Existing Licenses"
formControl={formControl}
/>
)}
{showLicensesToRemove && (
<CippFormComponent
type="autoComplete"
name="LicensesToRemove"
label="Select Licenses to Remove"
multiple={true}
creatable={false}
formControl={formControl}
validators={{ required: "Please select at least one license to remove" }}
api={{
url: "/api/ListLicenses",
labelField: (option) => option.displayName || option.skuPartNumber,
valueField: "skuId",
queryKey: `ListLicenses-${tenant}`,
}}
/>
)}
{showLicensesToReplace && (
<CippFormComponent
type="autoComplete"
name="LicensesToReplace"
label="Select Licenses to Replace"
multiple={true}
creatable={false}
formControl={formControl}
validators={{ required: "Please select at least one license to replace" }}
api={{
url: "/api/ListLicenses",
labelField: (option) => option.displayName || option.skuPartNumber,
valueField: "skuId",
queryKey: `ListLicenses-${tenant}`,
}}
/>
)}
{(licenseOpValue === "Add" || isReplaceOperation) && (
<CippFormComponent
type="autoComplete"
name="Licenses"
label={isReplaceOperation ? "Select New Licenses" : "Select Licenses"}
multiple={true}
creatable={false}
formControl={formControl}
validators={{ required: "Please select at least one license" }}
api={{
url: "/api/ListLicenses",
labelField: (option) =>
`${option.displayName || option.skuPartNumber} (${
option.availableUnits || 0
} available)`,
valueField: "skuId",
queryKey: `ListLicenses-Available-${tenant}`,
}}
/>
)}
</>
);
};
// Separate component for Out of Office form to avoid hook issues
const OutOfOfficeForm = ({ formControl }) => {
// Watch the Auto Reply State value
const autoReplyState = useWatch({
control: formControl.control,
name: "AutoReplyState",
});
// Calculate if date fields should be disabled
const areDateFieldsDisabled = autoReplyState?.value !== "Scheduled";
return (
<>
<CippFormComponent
type="autoComplete"
name="AutoReplyState"
label="Auto Reply State"
multiple={false}
formControl={formControl}
creatable={false}
options={[
{ label: "Enabled", value: "Enabled" },
{ label: "Disabled", value: "Disabled" },
{ label: "Scheduled", value: "Scheduled" },
]}
/>
<Tooltip
title={
areDateFieldsDisabled
? "Scheduling is only available when Auto Reply State is set to Scheduled"
: ""
}
placement="bottom"
>
<Box>
<CippFormComponent
type="datePicker"
label="Start Date/Time"
name="StartTime"
formControl={formControl}
disabled={areDateFieldsDisabled}
/>
</Box>
</Tooltip>
<Tooltip
title={
areDateFieldsDisabled
? "Scheduling is only available when Auto Reply State is set to Scheduled"
: ""
}
placement="bottom"
>
<Box>
<CippFormComponent
type="datePicker"
label="End Date/Time"
name="EndTime"
formControl={formControl}
disabled={areDateFieldsDisabled}
/>
</Box>
</Tooltip>
<CippFormComponent
type="richText"
label="Internal Message"
name="InternalMessage"
formControl={formControl}
multiline
rows={4}
/>
<CippFormComponent
type="richText"
label="External Message"
name="ExternalMessage"
formControl={formControl}
multiline
rows={4}
/>
{!areDateFieldsDisabled && (
<>
<Divider sx={{ my: 1 }} />
<Typography variant="subtitle2">Calendar Options</Typography>
<CippFormComponent
type="switch"
name="CreateOOFEvent"
label="Block my calendar for this period"
formControl={formControl}
/>
<CippFormCondition
formControl={formControl}
field="CreateOOFEvent"
compareType="is"
compareValue={true}
>
<CippFormComponent
type="textField"
name="OOFEventSubject"
label="Calendar Event Subject"
formControl={formControl}
/>
</CippFormCondition>
<CippFormComponent
type="switch"
name="AutoDeclineFutureRequestsWhenOOF"
label="Automatically decline new invitations during this period"
formControl={formControl}
/>
<CippFormComponent
type="switch"
name="DeclineEventsForScheduledOOF"
label="Decline and cancel my meetings during this period"
formControl={formControl}
/>
<CippFormCondition
formControl={formControl}
field="DeclineEventsForScheduledOOF"
compareType="is"
compareValue={true}
>
<CippFormComponent
type="richText"
name="DeclineMeetingMessage"
label="Decline Message"
formControl={formControl}
multiline
rows={3}
/>
</CippFormCondition>
</>
)}
</>
);
};
export const useCippUserActions = () => {
const tenant = useSettings().currentTenant;
const { checkPermissions } = usePermissions();
const canWriteUser = checkPermissions(["Identity.User.ReadWrite"]);
const canWriteMailbox = checkPermissions(["Exchange.Mailbox.ReadWrite"]);
const canWriteGroup = checkPermissions(["Identity.Group.ReadWrite"]);
return [
{
//tested
label: "View User",
link: "/identity/administration/users/user?userId=[id]",
multiPost: false,
icon: <EyeIcon />,
color: "success",
},
{
//tested
label: "Edit User",
link: "/identity/administration/users/user/edit?userId=[id]",
icon: <Edit />,
color: "success",
target: "_self",
condition: () => canWriteUser,
},
{
//tested
label: "Research Compromised Account",
type: "GET",
icon: <MagnifyingGlassIcon />,
link: "/identity/administration/users/user/bec?userId=[id]",
confirmText:
"Are you sure you want to research if [userPrincipalName] is a compromised account?",
multiPost: false,
},
{
//tested
label: "Create Temporary Access Password",
type: "POST",
icon: <Password />,
url: "/api/ExecCreateTAP",
data: { ID: "userPrincipalName" },
fields: [
{
type: "number",
name: "lifetimeInMinutes",
label: "Lifetime (Minutes)",
placeholder: "Leave blank for default",
},
{
type: "switch",
name: "isUsableOnce",
label: "One-time use only",
},
{
type: "datePicker",
name: "startDateTime",
label: "Start Date/Time (leave blank for immediate)",
dateTimeType: "datetime",
},
],
confirmText:
"Are you sure you want to create a Temporary Access Password for [userPrincipalName]?",
multiPost: false,
condition: () => canWriteUser,
},
{
//tested
label: "Re-require MFA registration",
type: "POST",
icon: <PhonelinkSetup />,
url: "/api/ExecResetMFA",
data: { ID: "userPrincipalName" },
confirmText: "Are you sure you want to reset MFA for [userPrincipalName]?",
multiPost: false,
condition: () => canWriteUser,
},
{
//tested
label: "Send MFA Push",
type: "POST",
icon: <PhonelinkLock />,
url: "/api/ExecSendPush",
data: { UserEmail: "userPrincipalName" },
confirmText: "Are you sure you want to send an MFA request to [userPrincipalName]?",
multiPost: false,
},
{
//tested
label: "Set Per-User MFA",
type: "POST",
icon: <LockPerson />,
url: "/api/ExecPerUserMFA",
data: { userId: "id", userPrincipalName: "userPrincipalName" },
fields: [
{
type: "autoComplete",
name: "State",
label: "State",
options: [
{ label: "Enforced", value: "Enforced" },
{ label: "Enabled", value: "Enabled" },
{ label: "Disabled", value: "Disabled" },
],
multiple: false,
creatable: false,
validators: { required: "Please select an MFA state" },
},
],
confirmText: "Are you sure you want to set per-user MFA for these users?",
multiPost: false,
condition: () => canWriteUser,
},
{
//tested
label: "Convert Mailbox",
type: "POST",
icon: <Email />,
url: "/api/ExecConvertMailbox",
data: { ID: "userPrincipalName" },
fields: [
{
type: "radio",
name: "MailboxType",
label: "Mailbox Type",
options: [
{ label: "User Mailbox", value: "Regular" },
{ label: "Shared Mailbox", value: "Shared" },
{ label: "Room Mailbox", value: "Room" },
{ label: "Equipment Mailbox", value: "Equipment" },
],
validators: { required: "Please select a mailbox type" },
},
],
confirmText: "Pick the type of mailbox you want to convert [userPrincipalName] to:",
multiPost: false,
condition: () => canWriteMailbox,
},
{
//tested
label: "Enable Online Archive",
type: "POST",
icon: <Archive />,
url: "/api/ExecEnableArchive",
data: { ID: "userPrincipalName" },
confirmText: "Are you sure you want to enable the online archive for [userPrincipalName]?",
multiPost: false,
condition: (row) => canWriteMailbox,
},
{
//tested
label: "Set Out of Office",
type: "POST",
icon: <MeetingRoom />,
url: "/api/ExecSetOoO",
data: {
userId: "userPrincipalName",
tenantFilter: "Tenant",
},
children: ({ formHook: formControl }) => <OutOfOfficeForm formControl={formControl} />,
confirmText: "Are you sure you want to set the out of office?",
multiPost: false,
condition: () => canWriteMailbox,
},
{
label: "Add to Group",
type: "POST",
icon: <GroupAdd />,
url: "/api/EditGroup",
customDataformatter: (row, action, formData) => {
// Build the member list from selected users
let addMember = [];
if (Array.isArray(row)) {
row
.map((r) => ({
label: r.displayName,
value: r.id,
addedFields: {
id: r.id,
userPrincipalName: r.userPrincipalName,
displayName: r.displayName,
},
}))
.forEach((r) => addMember.push(r));
} else {
addMember.push({
label: row.displayName,
value: row.id,
addedFields: {
id: row.id,
userPrincipalName: row.userPrincipalName,
displayName: row.displayName,
},
});
}
// Handle multiple groups - return an array of requests (one per group)
const selectedGroups = Array.isArray(formData.groupId)
? formData.groupId
: [formData.groupId];
return selectedGroups.map((group) => ({
addMember: addMember,
tenantFilter: tenant,
groupId: group,
}));
},
fields: [
{
type: "autoComplete",
name: "groupId",
label: "Select groups to add the user to",
multiple: true,
creatable: false,
validators: { required: "Please select at least one group" },
api: {
url: "/api/ListGroups",
labelField: (option) =>
option?.calculatedGroupType
? `${option.displayName} (${option.calculatedGroupType})`
: (option?.displayName ?? ""),
valueField: "id",
addedField: {
groupType: "groupType",
groupName: "displayName",
},
queryKey: `groups-${tenant}`,
showRefresh: true,
},
},
],
confirmText: "Are you sure you want to add [userPrincipalName] to the selected groups?",
multiPost: false,
allowResubmit: true,
condition: () => canWriteGroup,
},
{
label: "Manage Licenses",
type: "POST",
url: "/api/ExecBulkLicense",
icon: <CloudDone />,
data: { userIds: "id" },
multiPost: true,
allowResubmit: true,
children: ({ formHook: formControl }) => (
<ManageLicensesForm formControl={formControl} tenant={tenant} />
),
confirmText: "Are you sure you want to manage licenses for the selected users?",
condition: () => canWriteUser,
},
{
label: "Disable Email Forwarding",
type: "POST",
url: "/api/ExecEmailForward",
icon: <ForwardToInbox />,
data: {
username: "userPrincipalName",
userid: "userPrincipalName",
ForwardOption: "!disabled",
},
confirmText: "Are you sure you want to disable forwarding of [userPrincipalName]'s emails?",
multiPost: false,
condition: () => canWriteMailbox,
},
{
label: "Pre-provision OneDrive",
type: "POST",
icon: <CloudDone />,
url: "/api/ExecOneDriveProvision",
data: { UserPrincipalName: "userPrincipalName" },
confirmText: "Are you sure you want to pre-provision OneDrive for [userPrincipalName]?",
multiPost: false,
condition: () => canWriteUser,
},
{
label: "Add OneDrive Shortcut",
type: "POST",
icon: <Shortcut />,
url: "/api/ExecOneDriveShortCut",
data: {
username: "userPrincipalName",
userid: "id",
},
fields: [
{
type: "autoComplete",
name: "siteUrl",
label: "Select a Site",
multiple: false,
creatable: true,
validators: { required: "Please select or enter a SharePoint site URL" },
api: {
url: "/api/ListSites",
data: { type: "SharePointSiteUsage", URLOnly: true },
labelField: "webUrl",
valueField: "webUrl",
queryKey: `sharepointSites-${tenant}`,
},
},
],
confirmText: "Select a SharePoint site to create a shortcut for:",
multiPost: false,
condition: () => canWriteUser,
},
{
label: "Set Sign In State",
type: "POST",
icon: <LockPerson />,
url: "/api/ExecDisableUser",
data: { ID: "id" },
fields: [
{
type: "radio",
name: "Enable",
label: "Sign In State",
options: [
{ label: "Enabled", value: true },
{ label: "Disabled", value: false },
],
validators: { required: "Please select a sign-in state" },
},
],
confirmText: "Are you sure you want to set the sign-in state for [userPrincipalName]?",
multiPost: false,
condition: () => canWriteUser,
},
{
label: "Reset Password",
type: "POST",
icon: <LockReset />,
url: "/api/ExecResetPass",
data: {
ID: "userPrincipalName",
displayName: "displayName",
},
fields: [
{
type: "switch",
name: "MustChange",
label: "Must Change Password at Next Logon",
},
],
confirmText: "Are you sure you want to reset the password for [userPrincipalName]?",
multiPost: false,
condition: () => canWriteUser,
},
{
label: "Set Password Expiration",
type: "POST",
icon: <LockClock />,
url: "/api/ExecPasswordNeverExpires",
data: { userId: "id", userPrincipalName: "userPrincipalName" },
fields: [
{
type: "radio",
name: "PasswordPolicy",
label: "Password Policy",
options: [
{ label: "Disable Password Expiration", value: "DisablePasswordExpiration" },
{ label: "Enable Password Expiration", value: "None" },
],
validators: { required: "Please select a password policy" },
},
],
confirmText:
"Set Password Never Expires state for [userPrincipalName]. If the password of the user is older than the set expiration date of the organization, the user will be prompted to change their password at their next login.",
multiPost: false,
condition: () => canWriteUser,
},
{
label: "Clear Immutable ID",
type: "POST",
icon: <Clear />,
url: "/api/ExecClrImmId",
data: {
ID: "id",
},
confirmText: "Are you sure you want to clear the Immutable ID for [userPrincipalName]?",
multiPost: false,
condition: (row) => !row?.onPremisesSyncEnabled && row?.onPremisesImmutableId && canWriteUser,
},
{
label: "Set Source of Authority",
type: "POST",
url: "/api/ExecSetCloudManaged",
icon: <CloudSync />,
data: {
ID: "id",
displayName: "displayName",
type: "!User",
},
fields: [
{
type: "radio",
name: "isCloudManaged",
label: "Source of Authority",
options: [
{ label: "Cloud Managed", value: true },
{ label: "On-Premises Managed", value: false },
],
validators: { required: "Please select a source of authority" },
},
],
confirmText:
"Are you sure you want to change the source of authority for [userPrincipalName]? Setting it to On-Premises Managed will take until the next sync cycle to show the change.",
multiPost: false,
},
{
label: "Reprocess License Assignments",
type: "POST",
icon: <CloudDone />,
url: "/api/ExecReprocessUserLicenses",
data: { ID: "id", userPrincipalName: "userPrincipalName" },
confirmText:
"Are you sure you want to reprocess license assignments for [userPrincipalName]?",
multiPost: false,
condition: (row) => canWriteUser,
},
{
label: "Revoke all user sessions",
type: "POST",
icon: <PersonOff />,
url: "/api/ExecRevokeSessions",
data: { ID: "id", Username: "userPrincipalName" },
confirmText: "Are you sure you want to revoke all sessions for [userPrincipalName]?",
multiPost: false,
condition: () => canWriteUser,
},
{
label: "Delete User",
type: "POST",
icon: <TrashIcon />,
url: "/api/RemoveUser",
data: { ID: "id", userPrincipalName: "userPrincipalName" },
confirmText: "Are you sure you want to delete [userPrincipalName]?",
multiPost: false,
condition: () => canWriteUser,
},
{
label: "Edit Properties",
icon: <EditAttributes />,
multiPost: true,
noConfirm: true,
customFunction: (users, action, formData) => {
// Handle both single user and multiple users
const userData = Array.isArray(users) ? users : [users];
// Store users in session storage to avoid URL length limits
sessionStorage.setItem("patchWizardUsers", JSON.stringify(userData));
// Use Next.js router for internal navigation
import("next/router")
.then(({ default: router }) => {
router.push("/identity/administration/users/patch-wizard");
})
.catch(() => {
// Fallback to window.location if router is not available
window.location.href = "/identity/administration/users/patch-wizard";
});
},
condition: () => canWriteUser,
},
];
};
// Legacy wrapper function for backward compatibility - but this should not be used
// Instead, components should use the useCippUserActions hook
export const CippUserActions = () => {
console.warn("CippUserActions() function is deprecated. Use useCippUserActions() hook instead.");
return useCippUserActions();
};
export default CippUserActions;