Update resource policy pincode

This commit is contained in:
Fred KISSIE
2026-03-03 19:49:24 +01:00
parent 1dc8be373c
commit 20b65f549e
2 changed files with 97 additions and 31 deletions

View File

@@ -15,9 +15,13 @@ const setResourcePolicyHeaderAuthParamsSchema = z.object({
}); });
const setResourcePolicyHeaderAuthBodySchema = z.strictObject({ const setResourcePolicyHeaderAuthBodySchema = z.strictObject({
user: z.string().min(4).max(100).nullable(), headerAuth: z
password: z.string().min(4).max(100).nullable(), .object({
extendedCompatibility: z.boolean().nullable() user: z.string().min(4).max(100),
password: z.string().min(4).max(100),
extendedCompatibility: z.boolean()
})
.nullable()
}); });
registry.registerPath({ registry.registerPath({
@@ -70,7 +74,7 @@ export async function setResourcePolicyHeaderAuth(
} }
const { resourcePolicyId } = parsedParams.data; const { resourcePolicyId } = parsedParams.data;
const { user, password, extendedCompatibility } = parsedBody.data; const { headerAuth } = parsedBody.data;
await db.transaction(async (trx) => { await db.transaction(async (trx) => {
await trx await trx
@@ -82,15 +86,17 @@ export async function setResourcePolicyHeaderAuth(
) )
); );
if (user && password && extendedCompatibility !== null) { if (headerAuth !== null) {
const headerAuthHash = await hashPassword( const headerAuthHash = await hashPassword(
Buffer.from(`${user}:${password}`).toString("base64") Buffer.from(
`${headerAuth.user}:${headerAuth.password}`
).toString("base64")
); );
await trx.insert(resourcePolicyHeaderAuth).values({ await trx.insert(resourcePolicyHeaderAuth).values({
resourcePolicyId, resourcePolicyId,
headerAuthHash, headerAuthHash,
extendedCompatibility: extendedCompatibility extendedCompatibility: headerAuth.extendedCompatibility
}); });
} }
}); });

View File

@@ -93,11 +93,21 @@ export function EditPolicyAuthMethodsSectionForm() {
const [isSetPincodeOpen, setIsSetPincodeOpen] = useState(false); const [isSetPincodeOpen, setIsSetPincodeOpen] = useState(false);
const [isSetHeaderAuthOpen, setIsSetHeaderAuthOpen] = useState(false); const [isSetHeaderAuthOpen, setIsSetHeaderAuthOpen] = useState(false);
const hasPassword = Boolean(form.watch("password") ?? policy.passwordId); const password = form.watch("password");
const hasPincode = Boolean(form.watch("pincode") ?? policy.pincodeId); const pincode = form.watch("pincode");
const hasHeaderAuth = Boolean( const headerAuth = form.watch("headerAuth");
form.watch("headerAuth") ?? policy.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( const [isExpanded, setIsExpanded] = useState(
hasPassword || hasPincode || hasHeaderAuth hasPassword || hasPincode || hasHeaderAuth
@@ -128,28 +138,78 @@ export function EditPolicyAuthMethodsSectionForm() {
const payload = form.getValues(); const payload = form.getValues();
console.log({ payload, policy }); console.log({ payload, policy });
return; const responseArray: Array<Promise<AxiosResponse<{}> | void>> = [];
if (typeof payload.password !== "undefined") {
responseArray.push(
api
.put<AxiosResponse<{}>>(
`/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<AxiosResponse<{}>>(
`/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<AxiosResponse<{}>>(
`/resource-policy/${policy.resourcePolicyId}/header-auth`,
{
headerAuth: payload.headerAuth
}
)
.catch((e) => {
toast({
variant: "destructive",
title: t("policyErrorUpdate"),
description: formatAxiosError(
e,
t("policyErrorUpdateDescription")
)
});
})
);
}
try { try {
const res = await api const responseList = await Promise.all(responseArray);
.put<AxiosResponse<{}>>(
`/resource-policy/${policy.resourcePolicyId}/password`,
{
password: payload.password?.password ?? null
}
)
.catch((e) => {
toast({
variant: "destructive",
title: t("policyErrorUpdate"),
description: formatAxiosError(
e,
t("policyErrorUpdateDescription")
)
});
});
if (res && res.status === 200) { if (responseList.every((res) => res && res.status === 200)) {
toast({ toast({
title: t("success"), title: t("success"),
description: t("policyUpdatedSuccess") description: t("policyUpdatedSuccess")