From 20b65f549e5b160312e4a2265fc02591f4976dce Mon Sep 17 00:00:00 2001 From: Fred KISSIE Date: Tue, 3 Mar 2026 19:49:24 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Update=20resource=20policy=20pincod?= =?UTF-8?q?e?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../policy/setResourcePolicyHeaderAuth.ts | 20 ++-- .../EditPolicyAuthMethodsSectionForm.tsx | 108 ++++++++++++++---- 2 files changed, 97 insertions(+), 31 deletions(-) diff --git a/server/routers/policy/setResourcePolicyHeaderAuth.ts b/server/routers/policy/setResourcePolicyHeaderAuth.ts index 34c55b750..6291ee24e 100644 --- a/server/routers/policy/setResourcePolicyHeaderAuth.ts +++ b/server/routers/policy/setResourcePolicyHeaderAuth.ts @@ -15,9 +15,13 @@ const setResourcePolicyHeaderAuthParamsSchema = z.object({ }); const setResourcePolicyHeaderAuthBodySchema = z.strictObject({ - user: z.string().min(4).max(100).nullable(), - password: z.string().min(4).max(100).nullable(), - extendedCompatibility: z.boolean().nullable() + headerAuth: z + .object({ + user: z.string().min(4).max(100), + password: z.string().min(4).max(100), + extendedCompatibility: z.boolean() + }) + .nullable() }); registry.registerPath({ @@ -70,7 +74,7 @@ export async function setResourcePolicyHeaderAuth( } const { resourcePolicyId } = parsedParams.data; - const { user, password, extendedCompatibility } = parsedBody.data; + const { headerAuth } = parsedBody.data; await db.transaction(async (trx) => { await trx @@ -82,15 +86,17 @@ export async function setResourcePolicyHeaderAuth( ) ); - if (user && password && extendedCompatibility !== null) { + if (headerAuth !== null) { const headerAuthHash = await hashPassword( - Buffer.from(`${user}:${password}`).toString("base64") + Buffer.from( + `${headerAuth.user}:${headerAuth.password}` + ).toString("base64") ); await trx.insert(resourcePolicyHeaderAuth).values({ resourcePolicyId, headerAuthHash, - extendedCompatibility: extendedCompatibility + extendedCompatibility: headerAuth.extendedCompatibility }); } }); diff --git a/src/components/resource-policy/EditPolicyAuthMethodsSectionForm.tsx b/src/components/resource-policy/EditPolicyAuthMethodsSectionForm.tsx index 99d01930a..957d60978 100644 --- a/src/components/resource-policy/EditPolicyAuthMethodsSectionForm.tsx +++ b/src/components/resource-policy/EditPolicyAuthMethodsSectionForm.tsx @@ -93,11 +93,21 @@ export function EditPolicyAuthMethodsSectionForm() { const [isSetPincodeOpen, setIsSetPincodeOpen] = useState(false); const [isSetHeaderAuthOpen, setIsSetHeaderAuthOpen] = useState(false); - const hasPassword = Boolean(form.watch("password") ?? policy.passwordId); - const hasPincode = Boolean(form.watch("pincode") ?? policy.pincodeId); - const hasHeaderAuth = Boolean( - form.watch("headerAuth") ?? policy.headerAuth - ); + const password = form.watch("password"); + const pincode = form.watch("pincode"); + const headerAuth = form.watch("headerAuth"); + + // If explicitly removed (set to `null`) it means the value has been removed + // in the other case (`undefined` or object value), check if the value has been modified + // and fallback to the policy default value + const hasPassword = + password !== null ? Boolean(password ?? policy.passwordId) : false; + + const hasPincode = + pincode !== null ? Boolean(pincode ?? policy.pincodeId) : false; + + const hasHeaderAuth = + headerAuth !== null ? Boolean(headerAuth ?? policy.headerAuth) : false; const [isExpanded, setIsExpanded] = useState( hasPassword || hasPincode || hasHeaderAuth @@ -128,28 +138,78 @@ export function EditPolicyAuthMethodsSectionForm() { const payload = form.getValues(); console.log({ payload, policy }); - return; + const responseArray: Array | void>> = []; + + if (typeof payload.password !== "undefined") { + responseArray.push( + api + .put>( + `/resource-policy/${policy.resourcePolicyId}/password`, + { + password: payload.password?.password ?? null + } + ) + .catch((e) => { + toast({ + variant: "destructive", + title: t("policyErrorUpdate"), + description: formatAxiosError( + e, + t("policyErrorUpdateDescription") + ) + }); + }) + ); + } + + if (typeof payload.pincode !== "undefined") { + responseArray.push( + api + .put>( + `/resource-policy/${policy.resourcePolicyId}/pincode`, + { + pincode: payload.pincode?.pincode ?? null + } + ) + .catch((e) => { + toast({ + variant: "destructive", + title: t("policyErrorUpdate"), + description: formatAxiosError( + e, + t("policyErrorUpdateDescription") + ) + }); + }) + ); + } + + if (typeof payload.headerAuth !== "undefined") { + responseArray.push( + api + .put>( + `/resource-policy/${policy.resourcePolicyId}/header-auth`, + { + headerAuth: payload.headerAuth + } + ) + .catch((e) => { + toast({ + variant: "destructive", + title: t("policyErrorUpdate"), + description: formatAxiosError( + e, + t("policyErrorUpdateDescription") + ) + }); + }) + ); + } try { - const res = await api - .put>( - `/resource-policy/${policy.resourcePolicyId}/password`, - { - password: payload.password?.password ?? null - } - ) - .catch((e) => { - toast({ - variant: "destructive", - title: t("policyErrorUpdate"), - description: formatAxiosError( - e, - t("policyErrorUpdateDescription") - ) - }); - }); + const responseList = await Promise.all(responseArray); - if (res && res.status === 200) { + if (responseList.every((res) => res && res.status === 200)) { toast({ title: t("success"), description: t("policyUpdatedSuccess")