import { useEffect } from "react"; import { Controller, useForm } from "react-hook-form"; import { zodResolver } from "@hookform/resolvers/zod"; import { z } from "zod"; import { createNotification } from "@app/components/notifications"; import { Button, DeleteActionModal, FormControl, Input, Modal, ModalContent, TextArea } from "@app/components/v2"; import { useOrganization } from "@app/context"; import { useToggle } from "@app/hooks"; import { useCreateLDAPConfig, useGetLDAPConfig, useTestLDAPConnection, useUpdateLDAPConfig } from "@app/hooks/api"; import { UsePopUpState } from "@app/hooks/usePopUp"; const LDAPFormSchema = z.object({ url: z.string().default(""), bindDN: z.string().default(""), bindPass: z.string().default(""), searchBase: z.string().default(""), searchFilter: z.string().default(""), uniqueUserAttribute: z.string().default(""), groupSearchBase: z.string().default(""), groupSearchFilter: z.string().default(""), caCert: z.string().optional() }); export type TLDAPFormData = z.infer; type Props = { popUp: UsePopUpState<["addLDAP"]>; handlePopUpClose: (popUpName: keyof UsePopUpState<["addLDAP"]>) => void; handlePopUpToggle: (popUpName: keyof UsePopUpState<["addLDAP"]>, state?: boolean) => void; hideDelete?: boolean; }; export const LDAPModal = ({ popUp, handlePopUpClose, handlePopUpToggle, hideDelete }: Props) => { const { currentOrg } = useOrganization(); const { mutateAsync: createMutateAsync, isPending: createIsLoading } = useCreateLDAPConfig(); const { mutateAsync: updateMutateAsync, isPending: updateIsLoading } = useUpdateLDAPConfig(); const { mutateAsync: testLDAPConnection } = useTestLDAPConnection(); const { data } = useGetLDAPConfig(currentOrg?.id ?? ""); const { control, handleSubmit, reset, watch } = useForm({ resolver: zodResolver(LDAPFormSchema) }); const [isDeletePopupOpen, setIsDeletePopupOpen] = useToggle(); const handleLdapSoftDelete = async () => { if (!currentOrg) { return; } try { await updateMutateAsync({ organizationId: currentOrg.id, isActive: false, url: "", bindDN: "", bindPass: "", searchBase: "", searchFilter: "", uniqueUserAttribute: "", groupSearchBase: "", groupSearchFilter: "", caCert: "" }); createNotification({ text: "Successfully deleted OIDC configuration.", type: "success" }); } catch { createNotification({ text: "Failed deleting OIDC configuration.", type: "error" }); } }; const watchUrl = watch("url"); const watchBindDN = watch("bindDN"); const watchBindPass = watch("bindPass"); const watchCaCert = watch("caCert"); useEffect(() => { if (data) { reset({ url: data?.url ?? "", bindDN: data?.bindDN ?? "", bindPass: data?.bindPass ?? "", searchBase: data?.searchBase ?? "", searchFilter: data?.searchFilter ?? "", groupSearchBase: data?.groupSearchBase ?? "", groupSearchFilter: data?.groupSearchFilter ?? "", caCert: data?.caCert ?? "", uniqueUserAttribute: data?.uniqueUserAttribute ?? "" }); } }, [data]); const onSSOModalSubmit = async ({ url, bindDN, bindPass, uniqueUserAttribute, searchBase, searchFilter, groupSearchBase, groupSearchFilter, caCert, shouldCloseModal = true }: TLDAPFormData & { shouldCloseModal?: boolean }) => { try { if (!currentOrg) return; if (!data) { await createMutateAsync({ organizationId: currentOrg.id, isActive: false, url, bindDN, bindPass, searchBase, searchFilter, uniqueUserAttribute, groupSearchBase, groupSearchFilter, caCert }); } else { await updateMutateAsync({ organizationId: currentOrg.id, url, bindDN, bindPass, searchBase, searchFilter, uniqueUserAttribute, groupSearchBase, groupSearchFilter, caCert }); } if (shouldCloseModal) { handlePopUpClose("addLDAP"); } createNotification({ text: `Successfully ${!data ? "added" : "updated"} LDAP configuration`, type: "success" }); } catch (err) { console.error(err); createNotification({ text: `Failed to ${!data ? "add" : "update"} LDAP configuration`, type: "error" }); } }; const handleTestLDAPConnection = async () => { try { const result = await testLDAPConnection({ url: watchUrl, bindDN: watchBindDN, bindPass: watchBindPass, caCert: watchCaCert ?? "" }); if (!result) { createNotification({ text: "Failed to test the LDAP connection: Bind operation was unsuccessful", type: "error" }); return; } createNotification({ text: "Successfully tested the LDAP connection: Bind operation was successful", type: "success" }); } catch (err) { console.error(err); createNotification({ text: "Failed to test the LDAP connection", type: "error" }); } }; return ( <> { handlePopUpToggle("addLDAP", isOpen); reset(); }} >
( )} /> ( )} /> ( )} /> ( )} /> ( )} /> ( )} /> ( )} /> ( )} /> (