LdapGroupSyncSettingsForm.tsx 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. import {
  2. FC, useCallback, useEffect, useState,
  3. } from 'react';
  4. import { useTranslation } from 'react-i18next';
  5. import { apiv3Put } from '~/client/util/apiv3-client';
  6. import { toastError, toastSuccess } from '~/client/util/toastr';
  7. import { LdapGroupSyncSettings } from '~/interfaces/external-user-group';
  8. import { useSWRxLdapGroupSyncSettings } from '~/stores/external-user-group';
  9. export const LdapGroupSyncSettingsForm: FC = () => {
  10. const { t } = useTranslation('admin');
  11. const { data: ldapGroupSyncSettings } = useSWRxLdapGroupSyncSettings();
  12. const [formValues, setFormValues] = useState<LdapGroupSyncSettings>({
  13. ldapGroupSearchBase: '',
  14. ldapGroupMembershipAttribute: '',
  15. ldapGroupMembershipAttributeType: 'DN',
  16. ldapGroupChildGroupAttribute: '',
  17. autoGenerateUserOnLdapGroupSync: false,
  18. preserveDeletedLdapGroups: false,
  19. ldapGroupNameAttribute: '',
  20. ldapGroupDescriptionAttribute: '',
  21. });
  22. useEffect(() => {
  23. if (ldapGroupSyncSettings != null) {
  24. setFormValues(ldapGroupSyncSettings);
  25. }
  26. }, [ldapGroupSyncSettings, setFormValues]);
  27. const submitHandler = useCallback(async(e) => {
  28. e.preventDefault();
  29. try {
  30. await apiv3Put('/external-user-groups/ldap/sync-settings', formValues);
  31. toastSuccess(t('external_user_group.ldap.updated_group_sync_settings'));
  32. }
  33. catch (errs) {
  34. toastError(t(errs[0]?.message));
  35. }
  36. }, [formValues, t]);
  37. return <>
  38. <h3 className="border-bottom mb-3">{t('external_user_group.ldap.group_sync_settings')}</h3>
  39. <form onSubmit={submitHandler}>
  40. <div className="row form-group">
  41. <label
  42. htmlFor="ldapGroupSearchBase"
  43. className="text-left text-md-right col-md-3 col-form-label">
  44. {t('external_user_group.ldap.group_search_base_DN')}
  45. </label>
  46. <div className="col-md-6">
  47. <input
  48. className="form-control"
  49. type="text"
  50. name="ldapGroupSearchBase"
  51. id="ldapGroupSearchBase"
  52. value={formValues.ldapGroupSearchBase}
  53. onChange={e => setFormValues({ ...formValues, ldapGroupSearchBase: e.target.value })}
  54. />
  55. <p className="form-text text-muted">
  56. <small>{t('external_user_group.ldap.group_search_base_dn_detail')}</small>
  57. </p>
  58. </div>
  59. </div>
  60. <div className="row form-group">
  61. <label htmlFor="ldapGroupMembershipAttribute" className="text-left text-md-right col-md-3 col-form-label">
  62. {t('external_user_group.ldap.membership_attribute')}
  63. </label>
  64. <div className="col-md-6">
  65. <input
  66. className="form-control"
  67. required
  68. type="text"
  69. name="ldapGroupMembershipAttribute"
  70. id="ldapGroupMembershipAttribute"
  71. value={formValues.ldapGroupMembershipAttribute}
  72. onChange={e => setFormValues({ ...formValues, ldapGroupMembershipAttribute: e.target.value })}
  73. />
  74. <p className="form-text text-muted">
  75. <small>
  76. {t('external_user_group.ldap.membership_attribute_detail')} <br />
  77. e.g.) <code>member</code>, <code>memberUid</code>
  78. </small>
  79. </p>
  80. </div>
  81. </div>
  82. <div className="row form-group">
  83. <label htmlFor="ldapGroupMembershipAttributeType" className="text-left text-md-right col-md-3 col-form-label">
  84. {t('external_user_group.ldap.membership_attribute_type')}
  85. </label>
  86. <div className="col-md-6">
  87. <select
  88. className="form-control"
  89. required
  90. name="ldapGroupMembershipAttributeType"
  91. id="ldapGroupMembershipAttributeType"
  92. value={formValues.ldapGroupMembershipAttributeType}
  93. onChange={(e) => {
  94. if (e.target.value === 'DN' || e.target.value === 'UID') {
  95. setFormValues({ ...formValues, ldapGroupMembershipAttributeType: e.target.value });
  96. }
  97. }}>
  98. <option value="DN">DN</option>
  99. <option value="UID">UID</option>
  100. </select>
  101. <p className="form-text text-muted">
  102. <small>
  103. {t('external_user_group.ldap.membership_attribute_type_detail')}
  104. </small>
  105. </p>
  106. </div>
  107. </div>
  108. <div className="row form-group">
  109. <label htmlFor="ldapGroupChildGroupAttribute" className="text-left text-md-right col-md-3 col-form-label">
  110. {t('external_user_group.ldap.child_group_attribute')}
  111. </label>
  112. <div className="col-md-6">
  113. <input
  114. className="form-control"
  115. required
  116. type="text"
  117. name="ldapGroupChildGroupAttribute"
  118. id="ldapGroupChildGroupAttribute"
  119. value={formValues.ldapGroupChildGroupAttribute}
  120. onChange={e => setFormValues({ ...formValues, ldapGroupChildGroupAttribute: e.target.value })}/>
  121. <p className="form-text text-muted">
  122. <small>
  123. {t('external_user_group.ldap.child_group_attribute_detail')}<br />
  124. e.g.) <code>member</code>
  125. </small>
  126. </p>
  127. </div>
  128. </div>
  129. <div className="row form-group">
  130. <label
  131. className="text-left text-md-right col-md-3 col-form-label"
  132. >
  133. {/* {t('external_user_group.ldap.auto_generate_user_on_sync')} */}
  134. </label>
  135. <div className="col-md-6">
  136. <div className="custom-control custom-checkbox custom-checkbox-info">
  137. <input
  138. type="checkbox"
  139. className="custom-control-input"
  140. name="autoGenerateUserOnLdapGroupSync"
  141. id="autoGenerateUserOnLdapGroupSync"
  142. checked={formValues.autoGenerateUserOnLdapGroupSync}
  143. onChange={() => setFormValues({ ...formValues, autoGenerateUserOnLdapGroupSync: !formValues.autoGenerateUserOnLdapGroupSync })}
  144. />
  145. <label
  146. className="custom-control-label"
  147. htmlFor="autoGenerateUserOnLdapGroupSync"
  148. >
  149. {t('external_user_group.ldap.auto_generate_user_on_sync')}
  150. </label>
  151. </div>
  152. </div>
  153. </div>
  154. <div className="row form-group">
  155. <label
  156. className="text-left text-md-right col-md-3 col-form-label"
  157. >
  158. {/* {t('external_user_group.ldap.preserve_deleted_ldap_groups')} */}
  159. </label>
  160. <div className="col-md-6">
  161. <div className="custom-control custom-checkbox custom-checkbox-info">
  162. <input
  163. type="checkbox"
  164. className="custom-control-input"
  165. name="preserveDeletedLdapGroups"
  166. id="preserveDeletedLdapGroups"
  167. checked={formValues.preserveDeletedLdapGroups}
  168. onChange={() => setFormValues({ ...formValues, preserveDeletedLdapGroups: !formValues.preserveDeletedLdapGroups })}
  169. />
  170. <label
  171. className="custom-control-label"
  172. htmlFor="preserveDeletedLdapGroups"
  173. >
  174. {t('external_user_group.ldap.preserve_deleted_ldap_groups')}
  175. </label>
  176. </div>
  177. </div>
  178. </div>
  179. <div className="px-5">
  180. <h4 className="border-bottom mb-3">Attribute Mapping ({t('optional')})</h4>
  181. </div>
  182. <div className="row form-group">
  183. <label htmlFor="ldapGroupNameAttribute" className="text-left text-md-right col-md-3 col-form-label">{t('Name')}</label>
  184. <div className="col-md-6">
  185. <input
  186. className="form-control"
  187. type="text"
  188. name="ldapGroupNameAttribute"
  189. id="ldapGroupNameAttribute"
  190. value={formValues.ldapGroupNameAttribute}
  191. onChange={e => setFormValues({ ...formValues, ldapGroupNameAttribute: e.target.value })}
  192. placeholder="Default: cn"
  193. />
  194. <p className="form-text text-muted">
  195. <small>
  196. {t('external_user_group.ldap.name_mapper_detail')}
  197. </small>
  198. </p>
  199. </div>
  200. </div>
  201. <div className="row form-group">
  202. <label htmlFor="ldapGroupDescriptionAttribute" className="text-left text-md-right col-md-3 col-form-label">
  203. {t('Description')}
  204. </label>
  205. <div className="col-md-6">
  206. <input
  207. className="form-control"
  208. type="text"
  209. name="ldapGroupDescriptionAttribute"
  210. id="ldapGroupDescriptionAttribute"
  211. value={formValues.ldapGroupDescriptionAttribute || ''}
  212. onChange={e => setFormValues({ ...formValues, ldapGroupDescriptionAttribute: e.target.value })}
  213. />
  214. <p className="form-text text-muted">
  215. <small>
  216. {t('external_user_group.ldap.description_mapper_detail')}
  217. </small>
  218. </p>
  219. </div>
  220. </div>
  221. <div className="row my-3">
  222. <div className="offset-3 col-5">
  223. <button
  224. type="submit"
  225. className="btn btn-primary"
  226. >
  227. {t('Update')}
  228. </button>
  229. </div>
  230. </div>
  231. </form>
  232. </>;
  233. };