misc: updated ldap edit

This commit is contained in:
Sheen Capadngan
2024-10-24 02:10:55 +08:00
parent e034aa381a
commit 66d2cc8947
2 changed files with 125 additions and 36 deletions
@@ -287,7 +287,8 @@ export const LdapInputForm = ({
const ldifFields = [ const ldifFields = [
"provider.creationLdif", "provider.creationLdif",
"provider.revocationLdif", "provider.revocationLdif",
"provider.rollbackLdif" "provider.rollbackLdif",
"provider.rotationLdif"
] as const; ] as const;
ldifFields.forEach((f) => { ldifFields.forEach((f) => {
@@ -5,22 +5,47 @@ import { z } from "zod";
import { TtlFormLabel } from "@app/components/features"; import { TtlFormLabel } from "@app/components/features";
import { createNotification } from "@app/components/notifications"; import { createNotification } from "@app/components/notifications";
import { Button, FormControl, Input, TextArea } from "@app/components/v2"; import { Button, FormControl, Input, Select, SelectItem, TextArea } from "@app/components/v2";
import { useUpdateDynamicSecret } from "@app/hooks/api"; import { useUpdateDynamicSecret } from "@app/hooks/api";
import { TDynamicSecret } from "@app/hooks/api/dynamicSecret/types"; import { TDynamicSecret } from "@app/hooks/api/dynamicSecret/types";
enum CredentialType {
Dynamic = "dynamic",
Static = "static"
}
const credentialTypes = [
{
label: "Dynamic",
value: CredentialType.Dynamic
},
{
label: "Static",
value: CredentialType.Static
}
] as const;
const formSchema = z.object({ const formSchema = z.object({
inputs: z inputs: z.discriminatedUnion("credentialType", [
.object({ z.object({
url: z.string().trim().min(1), url: z.string().trim().min(1),
binddn: z.string().trim().min(1), binddn: z.string().trim().min(1),
bindpass: z.string().trim().min(1), bindpass: z.string().trim().min(1),
ca: z.string().optional(), ca: z.string().optional(),
credentialType: z.literal(CredentialType.Dynamic),
creationLdif: z.string().min(1), creationLdif: z.string().min(1),
revocationLdif: z.string().min(1), revocationLdif: z.string().min(1),
rollbackLdif: z.string().optional() rollbackLdif: z.string().optional()
}),
z.object({
url: z.string().trim().min(1),
binddn: z.string().trim().min(1),
bindpass: z.string().trim().min(1),
ca: z.string().optional(),
credentialType: z.literal(CredentialType.Static),
rotationLdif: z.string().min(1)
}) })
.partial(), ]),
defaultTTL: z.string().superRefine((val, ctx) => { defaultTTL: z.string().superRefine((val, ctx) => {
const valMs = ms(val); const valMs = ms(val);
if (valMs < 60 * 1000) if (valMs < 60 * 1000)
@@ -67,7 +92,9 @@ export const EditDynamicSecretLdapForm = ({
const { const {
control, control,
formState: { isSubmitting }, formState: { isSubmitting },
handleSubmit handleSubmit,
watch,
setValue
} = useForm<TForm>({ } = useForm<TForm>({
resolver: zodResolver(formSchema), resolver: zodResolver(formSchema),
values: { values: {
@@ -81,6 +108,7 @@ export const EditDynamicSecretLdapForm = ({
}); });
const updateDynamicSecret = useUpdateDynamicSecret(); const updateDynamicSecret = useUpdateDynamicSecret();
const selectedCredentialType = watch("inputs.credentialType");
const handleUpdateDynamicSecret = async ({ inputs, maxTTL, defaultTTL, newName }: TForm) => { const handleUpdateDynamicSecret = async ({ inputs, maxTTL, defaultTTL, newName }: TForm) => {
// wait till previous request is finished // wait till previous request is finished
@@ -204,43 +232,103 @@ export const EditDynamicSecretLdapForm = ({
/> />
<Controller <Controller
control={control} control={control}
name="inputs.creationLdif" name="inputs.credentialType"
render={({ field, fieldState: { error } }) => ( render={({ field, fieldState: { error } }) => (
<FormControl <FormControl
label="Creation LDIF" label="Credential Type"
isError={Boolean(error)} isError={Boolean(error?.message)}
errorText={error?.message} errorText={error?.message}
className="w-full"
> >
<TextArea {...field} /> <Select
</FormControl> defaultValue={field.value}
)} {...field}
/> className="w-full"
<Controller onValueChange={(e) => {
control={control} const ldifFields = [
name="inputs.revocationLdif" "inputs.creationLdif",
render={({ field, fieldState: { error } }) => ( "inputs.revocationLdif",
<FormControl "inputs.rollbackLdif",
label="Revocation LDIF" "inputs.rotationLdif"
isError={Boolean(error)} ] as const;
errorText={error?.message}
> ldifFields.forEach((f) => {
<TextArea {...field} /> setValue(f, "");
</FormControl> });
)}
/> field.onChange(e);
<Controller }}
control={control} >
name="inputs.rollbackLdif" {credentialTypes.map((credentialType) => (
render={({ field, fieldState: { error } }) => ( <SelectItem
<FormControl value={credentialType.value}
label="Rollback LDIF" key={`credential-type-${credentialType.value}`}
isError={Boolean(error)} >
errorText={error?.message} {credentialType.label}
> </SelectItem>
<TextArea {...field} /> ))}
</Select>
</FormControl> </FormControl>
)} )}
/> />
{selectedCredentialType === CredentialType.Dynamic && (
<>
<Controller
control={control}
name="inputs.creationLdif"
render={({ field, fieldState: { error } }) => (
<FormControl
label="Creation LDIF"
isError={Boolean(error)}
errorText={error?.message}
>
<TextArea {...field} />
</FormControl>
)}
/>
<Controller
control={control}
name="inputs.revocationLdif"
render={({ field, fieldState: { error } }) => (
<FormControl
label="Revocation LDIF"
isError={Boolean(error)}
errorText={error?.message}
>
<TextArea {...field} />
</FormControl>
)}
/>
<Controller
control={control}
name="inputs.rollbackLdif"
render={({ field, fieldState: { error } }) => (
<FormControl
label="Rollback LDIF"
isError={Boolean(error)}
errorText={error?.message}
>
<TextArea {...field} />
</FormControl>
)}
/>
</>
)}
{selectedCredentialType === CredentialType.Static && (
<Controller
control={control}
name="inputs.rotationLdif"
render={({ field, fieldState: { error } }) => (
<FormControl
label="Rotation LDIF"
isError={Boolean(error?.message)}
errorText={error?.message}
>
<TextArea {...field} />
</FormControl>
)}
/>
)}
</div> </div>
<div className="mt-4 flex items-center space-x-4"> <div className="mt-4 flex items-center space-x-4">
<Button type="submit" isLoading={isSubmitting}> <Button type="submit" isLoading={isSubmitting}>