forked from KelvinTegelaar/CIPP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCippCADeployDrawer.jsx
More file actions
305 lines (285 loc) · 11.3 KB
/
CippCADeployDrawer.jsx
File metadata and controls
305 lines (285 loc) · 11.3 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
import { useEffect, useMemo, useState, useCallback } from "react";
import { Alert, Button, Stack, Typography } from "@mui/material";
import { RocketLaunch } from "@mui/icons-material";
import { useForm, useWatch } from "react-hook-form";
import { CippOffCanvas } from "./CippOffCanvas";
import { ApiGetCall, ApiPostCall } from "../../api/ApiCall";
import CippFormComponent from "./CippFormComponent";
import CippJsonView from "../CippFormPages/CippJSONView";
import { CippApiResults } from "./CippApiResults";
import { useSettings } from "../../hooks/use-settings";
import { CippFormTenantSelector } from "./CippFormTenantSelector";
import { CippFormCondition } from "./CippFormCondition";
export const CippCADeployDrawer = ({
buttonText = "Deploy CA Policy",
requiredPermissions = [],
PermissionButton = Button,
templateId = null, // New prop for pre-supplying template ID
open = null, // External control for drawer visibility
onClose = null, // External close handler
}) => {
const [internalDrawerVisible, setInternalDrawerVisible] = useState(false);
const formControl = useForm();
const tenantFilter = useSettings()?.tenantFilter;
const CATemplates = ApiGetCall({ url: "/api/ListCATemplates", queryKey: "CATemplates" });
const [JSONData, setJSONData] = useState();
const watcher = useWatch({ control: formControl.control, name: "TemplateList" });
const selectedReplaceMode = useWatch({
control: formControl.control,
name: "replacename",
});
const watchedTenant = useWatch({ control: formControl.control, name: "tenantFilter" });
const authContextState = useMemo(() => {
const ids = JSONData?.conditions?.applications?.includeAuthenticationContextClassReferences ?? [];
const info = JSONData?.AuthenticationContextInfo ?? [];
const infoIds = new Set(info.map((i) => i.id));
const missingIds = ids.filter((id) => !infoIds.has(id));
return { ids, info, missingIds, hasAuthContexts: ids.length > 0 };
}, [JSONData]);
const isMultiTenant = useMemo(() => {
const val = watchedTenant?.value ?? watchedTenant;
if (Array.isArray(val)) return val.length > 1 || val.some((v) => (v?.value ?? v) === "AllTenants");
return val === "AllTenants";
}, [watchedTenant]);
const blockMultiTenantAuthContext =
authContextState.missingIds.length > 0 && isMultiTenant;
// Use external open state if provided, otherwise use internal state
const drawerVisible = open !== null ? open : internalDrawerVisible;
const isExternallyControlled = open !== null && onClose !== null;
const updateTemplate = useCallback(
(templateGuid) => {
if (CATemplates.isSuccess && templateGuid) {
const template = CATemplates.data.find((template) => template.GUID === templateGuid);
if (template) {
setJSONData(template);
formControl.setValue("rawjson", JSON.stringify(template, null));
}
}
},
[CATemplates.isSuccess, CATemplates.data, formControl.setValue]
);
// Effect to set template when templateId prop is provided
useEffect(() => {
if (templateId && CATemplates.isSuccess) {
// Find the template to get the display name
const template = CATemplates.data.find((template) => template.GUID === templateId);
if (template) {
// Pre-select the template when drawer opens
formControl.setValue("TemplateList", { value: templateId, label: template.displayName });
updateTemplate(templateId);
}
}
}, [templateId, CATemplates.isSuccess, formControl, updateTemplate]);
useEffect(() => {
updateTemplate(watcher?.value);
}, [updateTemplate, watcher?.value]);
const deployPolicy = ApiPostCall({
urlFromData: true,
relatedQueryKeys: ["CATemplates", `Conditional Access Policies - ${tenantFilter}`],
});
const handleSubmit = () => {
const formData = formControl.getValues();
console.log("Submitting CA form data:", formData);
deployPolicy.mutate({
url: "/api/AddCAPolicy",
relatedQueryKeys: ["CATemplates", "Conditional Access Policies"],
data: { ...formData },
});
};
const handleCloseDrawer = () => {
if (isExternallyControlled) {
onClose();
} else {
setInternalDrawerVisible(false);
}
formControl.reset();
};
return (
<>
{!isExternallyControlled && (
<PermissionButton
requiredPermissions={requiredPermissions}
onClick={() => setInternalDrawerVisible(true)}
startIcon={<RocketLaunch />}
>
{buttonText}
</PermissionButton>
)}
<CippOffCanvas
title="Deploy Conditional Access Policy"
visible={drawerVisible}
onClose={handleCloseDrawer}
size="lg"
footer={
<Stack spacing={2}>
<CippApiResults apiObject={deployPolicy} />
<Stack direction="row" justifyContent="flex-start" spacing={2}>
<Button
variant="contained"
color="primary"
onClick={handleSubmit}
disabled={deployPolicy.isLoading || blockMultiTenantAuthContext}
>
{deployPolicy.isLoading
? "Deploying..."
: deployPolicy.isSuccess
? "Redeploy Policy"
: "Deploy Policy"}
</Button>
<Button variant="outlined" onClick={handleCloseDrawer}>
Close
</Button>
</Stack>
</Stack>
}
>
<Stack spacing={2}>
<CippFormTenantSelector
formControl={formControl}
name="tenantFilter"
required={true}
disableClearable={false}
preselectedEnabled={true}
allTenants={true}
type="multiple"
/>
<CippFormComponent
type="autoComplete"
name="TemplateList"
label={templateId ? "Selected Template" : "Please choose a template to apply."}
isFetching={CATemplates.isLoading}
multiple={false}
formControl={formControl}
disabled={!!templateId} // Disable if templateId is provided
options={
CATemplates.isSuccess
? CATemplates.data.map((template) => ({
label: template.displayName,
value: template.GUID,
}))
: []
}
/>
<CippFormComponent
type="hidden"
name="rawjson"
label="Conditional Access Parameters"
placeholder="Enter the JSON information to use as parameters, or select from a template"
formControl={formControl}
/>
<CippJsonView object={JSONData} />
<CippFormComponent
type="radio"
name="replacename"
label="How should groups and users be handled?"
formControl={formControl}
options={[
{ value: "leave", label: "Leave the groups and users as is" },
{ value: "displayName", label: "Replace by display name" },
{ value: "AllUsers", label: "Remove all exclusions, apply to all users" },
]}
/>
<CippFormComponent
type="radio"
name="newstate"
label="Policy State"
formControl={formControl}
options={[
{ value: "donotchange", label: "Do not change state" },
{ value: "Enabled", label: "Set to enabled" },
{ value: "Disabled", label: "Set to disabled" },
{ value: "enabledForReportingButNotEnforced", label: "Set to report only" },
]}
/>
<CippFormComponent
type="switch"
name="overwrite"
label="Overwrite Existing Policy"
formControl={formControl}
/>
<CippFormComponent
type="switch"
name="DisableSD"
label="Disable Security Defaults if enabled when creating policy"
formControl={formControl}
/>
<CippFormCondition
formControl={formControl}
field="replacename"
compareType="is"
compareValue="displayName"
action="disable"
>
<CippFormComponent
type="switch"
name="CreateGroups"
label="Create groups if they do not exist"
formControl={formControl}
helperText={
selectedReplaceMode !== "displayName"
? "Select 'Replace by display name' to create groups specified in the template."
: "Enable this option to create groups that do not exist in the tenant."
}
/>
</CippFormCondition>
{authContextState.hasAuthContexts && (
<>
{authContextState.missingIds.length > 0 && (
<>
<Alert severity="warning">
This template references authentication contexts but was captured without
metadata (for example, imported from a repo). Provide a display name for each
referenced context so deployment can match it by name in the target tenant.
</Alert>
{authContextState.missingIds.map((id) => (
<Stack
key={id}
spacing={1}
sx={{ pl: 2, borderLeft: "2px solid", borderColor: "divider" }}
>
<Typography variant="subtitle2">Authentication context {id}</Typography>
<CippFormComponent
type="textField"
name={`AuthContextMapping.${id}.displayName`}
label="Display Name *"
formControl={formControl}
validators={{ required: "Display name is required" }}
/>
<CippFormComponent
type="textField"
name={`AuthContextMapping.${id}.description`}
label="Description"
multiline
rows={2}
formControl={formControl}
/>
<CippFormComponent
type="switch"
name={`AuthContextMapping.${id}.isAvailable`}
label="Publish to apps"
formControl={formControl}
/>
</Stack>
))}
</>
)}
{blockMultiTenantAuthContext && (
<Alert severity="error">
Cannot deploy to multiple tenants while the template is missing authentication
context metadata. Deploy to a single tenant and provide the mapping, or recapture
the template from a tenant where the contexts are defined.
</Alert>
)}
<CippFormComponent
type="switch"
name="CreateAuthContexts"
label="Create authentication contexts if they do not exist"
formControl={formControl}
/>
</>
)}
</Stack>
</CippOffCanvas>
</>
);
};