LdapSecuritySettingContents.tsx 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. import React, {
  2. useState, useEffect, useCallback,
  3. } from 'react';
  4. import { useTranslation } from 'next-i18next';
  5. import { useForm } from 'react-hook-form';
  6. import AdminGeneralSecurityContainer from '~/client/services/AdminGeneralSecurityContainer';
  7. import AdminLdapSecurityContainer from '~/client/services/AdminLdapSecurityContainer';
  8. import { toastSuccess, toastError } from '~/client/util/toastr';
  9. import { withUnstatedContainers } from '../../UnstatedUtils';
  10. import LdapAuthTestModal from './LdapAuthTestModal';
  11. type Props = {
  12. adminGeneralSecurityContainer: AdminGeneralSecurityContainer;
  13. adminLdapSecurityContainer: AdminLdapSecurityContainer;
  14. };
  15. const LdapSecuritySettingContents = (props: Props) => {
  16. const { adminGeneralSecurityContainer, adminLdapSecurityContainer } = props;
  17. const { t } = useTranslation('admin');
  18. const { isLdapEnabled } = adminGeneralSecurityContainer.state;
  19. const {
  20. serverUrl, ldapBindDN, ldapBindDNPassword, ldapSearchFilter,
  21. ldapAttrMapUsername, ldapAttrMapMail, ldapAttrMapName,
  22. ldapGroupSearchBase, ldapGroupSearchFilter, ldapGroupDnProperty,
  23. } = adminLdapSecurityContainer.state;
  24. const [isLdapAuthTestModalShown, setIsLdapAuthTestModalShown] = useState(false);
  25. const { register, handleSubmit, reset } = useForm();
  26. useEffect(() => {
  27. reset({
  28. serverUrl,
  29. ldapBindDN,
  30. ldapBindDNPassword,
  31. ldapSearchFilter,
  32. ldapAttrMapUsername,
  33. ldapAttrMapMail,
  34. ldapAttrMapName,
  35. ldapGroupSearchBase,
  36. ldapGroupSearchFilter,
  37. ldapGroupDnProperty,
  38. });
  39. }, [
  40. reset, serverUrl, ldapBindDN, ldapBindDNPassword, ldapSearchFilter,
  41. ldapAttrMapUsername, ldapAttrMapMail, ldapAttrMapName,
  42. ldapGroupSearchBase, ldapGroupSearchFilter, ldapGroupDnProperty,
  43. ]);
  44. const onSubmit = useCallback(async(data) => {
  45. try {
  46. await adminLdapSecurityContainer.changeServerUrl(data.serverUrl);
  47. await adminLdapSecurityContainer.changeBindDN(data.ldapBindDN);
  48. await adminLdapSecurityContainer.changeBindDNPassword(data.ldapBindDNPassword);
  49. await adminLdapSecurityContainer.changeSearchFilter(data.ldapSearchFilter);
  50. await adminLdapSecurityContainer.changeAttrMapUsername(data.ldapAttrMapUsername);
  51. await adminLdapSecurityContainer.changeAttrMapMail(data.ldapAttrMapMail);
  52. await adminLdapSecurityContainer.changeAttrMapName(data.ldapAttrMapName);
  53. await adminLdapSecurityContainer.changeGroupSearchBase(data.ldapGroupSearchBase);
  54. await adminLdapSecurityContainer.changeGroupSearchFilter(data.ldapGroupSearchFilter);
  55. await adminLdapSecurityContainer.changeGroupDnProperty(data.ldapGroupDnProperty);
  56. await adminLdapSecurityContainer.updateLdapSetting();
  57. await adminGeneralSecurityContainer.retrieveSetupStratedies();
  58. toastSuccess(t('security_settings.ldap.updated_ldap'));
  59. }
  60. catch (err) {
  61. toastError(err);
  62. }
  63. }, [t, adminLdapSecurityContainer, adminGeneralSecurityContainer]);
  64. const openLdapAuthTestModal = useCallback(() => {
  65. setIsLdapAuthTestModalShown(true);
  66. }, []);
  67. const closeLdapAuthTestModal = useCallback(() => {
  68. setIsLdapAuthTestModalShown(false);
  69. }, []);
  70. return (
  71. <React.Fragment>
  72. <h2 className="alert-anchor border-bottom mb-4">
  73. LDAP
  74. </h2>
  75. <div className="row my-4">
  76. <div className="col-6 offset-3">
  77. <div className="form-check form-switch form-check-success">
  78. <input
  79. id="isLdapEnabled"
  80. className="form-check-input"
  81. type="checkbox"
  82. checked={isLdapEnabled}
  83. onChange={() => { adminGeneralSecurityContainer.switchIsLdapEnabled() }}
  84. />
  85. <label className="form-label form-check-label" htmlFor="isLdapEnabled">
  86. {t('security_settings.ldap.enable_ldap')}
  87. </label>
  88. </div>
  89. {(!adminGeneralSecurityContainer.state.setupStrategies.includes('ldap') && isLdapEnabled)
  90. && <div className="badge text-bg-warning">{t('security_settings.setup_is_not_yet_complete')}</div>}
  91. </div>
  92. </div>
  93. {isLdapEnabled && (
  94. <form onSubmit={handleSubmit(onSubmit)}>
  95. <h3 className="border-bottom mb-4">{t('security_settings.configuration')}</h3>
  96. <div className="row my-3">
  97. <label htmlFor="serverUrl" className="text-start text-md-end col-md-3 col-form-label">
  98. Server URL
  99. </label>
  100. <div className="col-md-9">
  101. <input
  102. className="form-control"
  103. type="text"
  104. {...register('serverUrl')}
  105. />
  106. <small>
  107. <p
  108. className="form-text text-muted"
  109. // eslint-disable-next-line react/no-danger
  110. dangerouslySetInnerHTML={{ __html: t('security_settings.ldap.server_url_detail') }}
  111. />
  112. {t('security_settings.example')}: <code>ldaps://ldap.company.com/ou=people,dc=company,dc=com</code>
  113. </small>
  114. </div>
  115. </div>
  116. <div className="row my-3">
  117. <label className="form-label text-start text-md-end col-md-3 col-form-label">
  118. <strong>{t('security_settings.ldap.bind_mode')}</strong>
  119. </label>
  120. <div className="col-md-9">
  121. <div className="dropdown">
  122. <button
  123. className="btn btn-outline-secondary dropdown-toggle"
  124. type="button"
  125. id="dropdownMenuButton"
  126. data-bs-toggle="dropdown"
  127. aria-haspopup="true"
  128. aria-expanded="true"
  129. >
  130. {adminLdapSecurityContainer.state.isUserBind
  131. ? <span className="pull-left">{t('security_settings.ldap.bind_user')}</span>
  132. : <span className="pull-left">{t('security_settings.ldap.bind_manager')}</span>}
  133. </button>
  134. <div className="dropdown-menu" aria-labelledby="dropdownMenuButton">
  135. <button className="dropdown-item" type="button" onClick={() => { adminLdapSecurityContainer.changeLdapBindMode(true) }}>
  136. {t('security_settings.ldap.bind_user')}
  137. </button>
  138. <button className="dropdown-item" type="button" onClick={() => { adminLdapSecurityContainer.changeLdapBindMode(false) }}>
  139. {t('security_settings.ldap.bind_manager')}
  140. </button>
  141. </div>
  142. </div>
  143. </div>
  144. </div>
  145. <div className="row my-3">
  146. <label className="form-label text-start text-md-end col-md-3 col-form-label">
  147. <strong>Bind DN</strong>
  148. </label>
  149. <div className="col-md-9">
  150. <input
  151. className="form-control"
  152. type="text"
  153. {...register('ldapBindDN')}
  154. />
  155. {(adminLdapSecurityContainer.state.isUserBind === true) ? (
  156. <p className="form-text text-muted">
  157. <small>
  158. {t('security_settings.ldap.bind_DN_user_detail1')}<br />
  159. {/* eslint-disable-next-line react/no-danger */}
  160. <span dangerouslySetInnerHTML={{ __html: t('security_settings.ldap.bind_DN_user_detail2') }} /><br />
  161. {t('security_settings.example')}1: <code>uid={'{{ username }}'},dc=domain,dc=com</code><br />
  162. {t('security_settings.example')}2: <code>{'{{ username }}'}@domain.com</code>
  163. </small>
  164. </p>
  165. )
  166. : (
  167. <p className="form-text text-muted">
  168. <small>
  169. {t('security_settings.ldap.bind_DN_manager_detail')}<br />
  170. {t('security_settings.example')}1: <code>uid=admin,dc=domain,dc=com</code><br />
  171. {t('security_settings.example')}2: <code>admin@domain.com</code>
  172. </small>
  173. </p>
  174. )}
  175. </div>
  176. </div>
  177. <div className="row my-3">
  178. <label className="text-start text-md-end col-md-3 col-form-label" htmlFor="bindDNPassword">
  179. <strong>{t('security_settings.ldap.bind_DN_password')}</strong>
  180. </label>
  181. <div className="col-md-9">
  182. {(adminLdapSecurityContainer.state.isUserBind) ? (
  183. <p className="card custom-card">
  184. <small>
  185. {t('security_settings.ldap.bind_DN_password_user_detail')}
  186. </small>
  187. </p>
  188. )
  189. : (
  190. <>
  191. <input
  192. className="form-control"
  193. type="password"
  194. {...register('ldapBindDNPassword')}
  195. />
  196. <p className="form-text text-muted">
  197. <small>{t('security_settings.ldap.bind_DN_password_manager_detail')}</small>
  198. </p>
  199. </>
  200. )}
  201. </div>
  202. </div>
  203. <div className="row my-3">
  204. <label className="form-label text-start text-md-end col-md-3 col-form-label">
  205. <strong>{t('security_settings.ldap.search_filter')}</strong>
  206. </label>
  207. <div className="col-md-9">
  208. <input
  209. className="form-control"
  210. type="text"
  211. {...register('ldapSearchFilter')}
  212. />
  213. <p className="form-text text-muted">
  214. <small>
  215. {t('security_settings.ldap.search_filter_detail1')}<br />
  216. {/* eslint-disable-next-line react/no-danger */}
  217. <span dangerouslySetInnerHTML={{ __html: t('security_settings.ldap.search_filter_detail2') }} /><br />
  218. {/* eslint-disable-next-line react/no-danger */}
  219. <span dangerouslySetInnerHTML={{ __html: t('security_settings.ldap.search_filter_detail3') }} />
  220. </small>
  221. </p>
  222. <p className="form-text text-muted">
  223. <small>
  224. {t('security_settings.example')}1 - {t('security_settings.ldap.search_filter_example1')}:
  225. <code>(|(uid={'{{username}}'})(mail={'{{username}}'}))</code><br />
  226. {t('security_settings.example')}2 - {t('security_settings.ldap.search_filter_example2')}:
  227. <code>(sAMAccountName={'{{username}}'})</code>
  228. </small>
  229. </p>
  230. </div>
  231. </div>
  232. <h3 className="alert-anchor border-bottom mb-4">
  233. Attribute Mapping ({t('optional')})
  234. </h3>
  235. <div className="row my-3">
  236. <label className="form-label text-start text-md-end col-md-3 col-form-label" htmlFor="attrMapUsername">
  237. <strong>{t('username')}</strong>
  238. </label>
  239. <div className="col-md-9">
  240. <input
  241. className="form-control"
  242. type="text"
  243. placeholder="Default: uid"
  244. {...register('ldapAttrMapUsername')}
  245. />
  246. <p className="form-text text-muted">
  247. {/* eslint-disable-next-line react/no-danger */}
  248. <small dangerouslySetInnerHTML={{ __html: t('security_settings.ldap.username_detail') }} />
  249. </p>
  250. </div>
  251. </div>
  252. <div className="row my-3">
  253. <div className="offset-md-3 col-md-9">
  254. <div className="form-check form-check-success">
  255. <input
  256. type="checkbox"
  257. className="form-check-input"
  258. id="isSameUsernameTreatedAsIdenticalUser"
  259. checked={adminLdapSecurityContainer.state.isSameUsernameTreatedAsIdenticalUser}
  260. onChange={() => { adminLdapSecurityContainer.switchIsSameUsernameTreatedAsIdenticalUser() }}
  261. />
  262. <label
  263. className="form-check-label"
  264. htmlFor="isSameUsernameTreatedAsIdenticalUser"
  265. // eslint-disable-next-line react/no-danger
  266. dangerouslySetInnerHTML={{ __html: t('security_settings.Treat username matching as identical') }}
  267. />
  268. </div>
  269. <p className="form-text text-muted">
  270. {/* eslint-disable-next-line react/no-danger */}
  271. <small dangerouslySetInnerHTML={{ __html: t('security_settings.Treat username matching as identical_warn') }} />
  272. </p>
  273. </div>
  274. </div>
  275. <div className="row my-3">
  276. <label className="form-label text-start text-md-end col-md-3 col-form-label" htmlFor="attrMapMail">
  277. <strong>{t('Email')}</strong>
  278. </label>
  279. <div className="col-md-9">
  280. <input
  281. className="form-control"
  282. type="text"
  283. placeholder="Default: mail"
  284. {...register('ldapAttrMapMail')}
  285. />
  286. <p className="form-text text-muted">
  287. <small>
  288. {t('security_settings.ldap.mail_detail')}
  289. </small>
  290. </p>
  291. </div>
  292. </div>
  293. <div className="row my-3">
  294. <label className="form-label text-start text-md-end col-md-3 col-form-label" htmlFor="attrMapName">
  295. <strong>{t('Name')}</strong>
  296. </label>
  297. <div className="col-md-9">
  298. <input
  299. className="form-control"
  300. type="text"
  301. {...register('ldapAttrMapName')}
  302. />
  303. <p className="form-text text-muted">
  304. <small>
  305. {t('security_settings.ldap.name_detail')}
  306. </small>
  307. </p>
  308. </div>
  309. </div>
  310. <h3 className="alert-anchor border-bottom mb-4">
  311. {t('security_settings.ldap.group_search_filter')} ({t('optional')})
  312. </h3>
  313. <div className="row my-3">
  314. <label className="form-label text-start text-md-end col-md-3 col-form-label" htmlFor="groupSearchBase">
  315. <strong>{t('security_settings.ldap.group_search_base_DN')}</strong>
  316. </label>
  317. <div className="col-md-9">
  318. <input
  319. className="form-control"
  320. type="text"
  321. {...register('ldapGroupSearchBase')}
  322. />
  323. <p className="form-text text-muted">
  324. <small>
  325. {/* eslint-disable-next-line react/no-danger */}
  326. <span dangerouslySetInnerHTML={{ __html: t('security_settings.ldap.group_search_base_DN_detail') }} /><br />
  327. {t('security_settings.example')}: <code>ou=groups,dc=domain,dc=com</code>
  328. </small>
  329. </p>
  330. </div>
  331. </div>
  332. <div className="row my-3">
  333. <label className="form-label text-start text-md-end col-md-3 col-form-label" htmlFor="groupSearchFilter">
  334. <strong>{t('security_settings.ldap.group_search_filter')}</strong>
  335. </label>
  336. <div className="col-md-9">
  337. <input
  338. className="form-control"
  339. type="text"
  340. {...register('ldapGroupSearchFilter')}
  341. />
  342. <p className="form-text text-muted">
  343. <small>
  344. {/* eslint-disable react/no-danger */}
  345. <span dangerouslySetInnerHTML={{ __html: t('security_settings.ldap.group_search_filter_detail1') }} /><br />
  346. <span dangerouslySetInnerHTML={{ __html: t('security_settings.ldap.group_search_filter_detail2') }} /><br />
  347. <span dangerouslySetInnerHTML={{ __html: t('security_settings.ldap.group_search_filter_detail3') }} />
  348. {/* eslint-enable react/no-danger */}
  349. </small>
  350. </p>
  351. <p className="form-text text-muted">
  352. <small>
  353. {t('security_settings.example')}:
  354. {/* eslint-disable-next-line react/no-danger */}
  355. <span dangerouslySetInnerHTML={{ __html: t('security_settings.ldap.group_search_filter_detail4') }} />
  356. </small>
  357. </p>
  358. </div>
  359. </div>
  360. <div className="row my-3">
  361. <label className="form-label text-start text-md-end col-md-3 col-form-label" htmlFor="groupDnProperty">
  362. <strong>{t('security_settings.ldap.group_search_user_DN_property')}</strong>
  363. </label>
  364. <div className="col-md-9">
  365. <input
  366. className="form-control"
  367. type="text"
  368. placeholder="Default: uid"
  369. {...register('ldapGroupDnProperty')}
  370. />
  371. <p className="form-text text-muted">
  372. {/* eslint-disable-next-line react/no-danger */}
  373. <small dangerouslySetInnerHTML={{ __html: t('security_settings.ldap.group_search_user_DN_property_detail') }} />
  374. </p>
  375. </div>
  376. </div>
  377. <div className="row my-3">
  378. <div className="offset-3 col-5">
  379. <button
  380. type="submit"
  381. className="btn btn-primary"
  382. disabled={adminLdapSecurityContainer.state.retrieveError != null}
  383. >
  384. {t('Update')}
  385. </button>
  386. <button
  387. type="button"
  388. className="btn btn-outline-secondary ms-2"
  389. onClick={openLdapAuthTestModal}
  390. >{t('security_settings.ldap.test_config')}
  391. </button>
  392. </div>
  393. </div>
  394. </form>
  395. )}
  396. <LdapAuthTestModal isOpen={isLdapAuthTestModalShown} onClose={closeLdapAuthTestModal} />
  397. </React.Fragment>
  398. );
  399. };
  400. const LdapSecuritySettingContentsWrapper = withUnstatedContainers(LdapSecuritySettingContents, [
  401. AdminGeneralSecurityContainer,
  402. AdminLdapSecurityContainer,
  403. ]);
  404. export default LdapSecuritySettingContentsWrapper;