LdapGroupManagement.tsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import {
  2. FC, useCallback, useEffect, useState,
  3. } from 'react';
  4. import { useTranslation } from 'react-i18next';
  5. import { apiv3Get, apiv3Put } from '~/client/util/apiv3-client';
  6. import { toastError, toastSuccess } from '~/client/util/toastr';
  7. import LabeledProgressBar from '~/components/Admin/Common/LabeledProgressBar';
  8. import { SocketEventName } from '~/interfaces/websocket';
  9. import { useAdminSocket } from '~/stores/socket-io';
  10. import { useSWRxExternalUserGroupList } from '../../stores/external-user-group';
  11. import { LdapGroupSyncSettingsForm } from './LdapGroupSyncSettingsForm';
  12. export const LdapGroupManagement: FC = () => {
  13. const [isUserBind, setIsUserBind] = useState(false);
  14. const { t } = useTranslation('admin');
  15. const { data: socket } = useAdminSocket();
  16. const { mutate: mutateExternalUserGroups } = useSWRxExternalUserGroupList();
  17. const [syncStatus, setSyncStatus] = useState<'beforeSync' | 'syncExecuting' | 'syncFinished'>('beforeSync');
  18. const [progress, setProgress] = useState({
  19. total: 0,
  20. current: 0,
  21. });
  22. useEffect(() => {
  23. const getIsUserBind = async() => {
  24. try {
  25. const response = await apiv3Get('/security-setting/');
  26. const { ldapAuth } = response.data.securityParams;
  27. setIsUserBind(ldapAuth.isUserBind);
  28. }
  29. catch (e) {
  30. toastError(e);
  31. }
  32. };
  33. getIsUserBind();
  34. }, []);
  35. useEffect(() => {
  36. if (socket != null) {
  37. socket.on(SocketEventName.GroupSyncProgress, (data) => {
  38. setSyncStatus('syncExecuting');
  39. setProgress({
  40. total: data.totalCount,
  41. current: data.count,
  42. });
  43. });
  44. socket.on(SocketEventName.FinishGroupSync, () => {
  45. setSyncStatus('syncFinished');
  46. });
  47. }
  48. }, [socket]);
  49. const onSyncBtnClick = useCallback(async(e) => {
  50. e.preventDefault();
  51. setProgress({ total: 0, current: 0 });
  52. setSyncStatus('syncExecuting');
  53. try {
  54. if (isUserBind) {
  55. const password = e.target.password.value;
  56. await apiv3Put('/external-user-groups/ldap/sync', { password });
  57. }
  58. else {
  59. await apiv3Put('/external-user-groups/ldap/sync');
  60. }
  61. toastSuccess(t('external_user_group.ldap.sync_succeeded'));
  62. mutateExternalUserGroups();
  63. }
  64. catch (errs) {
  65. toastError(t(errs[0]?.message));
  66. }
  67. }, [t, isUserBind, mutateExternalUserGroups]);
  68. const renderProgressBar = () => {
  69. if (syncStatus === 'beforeSync') return null;
  70. const header = syncStatus === 'syncExecuting' ? 'Processing..' : 'Completed';
  71. return (
  72. <LabeledProgressBar
  73. header={header}
  74. currentCount={progress.current}
  75. totalCount={progress.total}
  76. />
  77. );
  78. };
  79. return (
  80. <>
  81. <LdapGroupSyncSettingsForm />
  82. <h3 className="border-bottom mb-3">{t('external_user_group.execute_sync')}</h3>
  83. <div className="row">
  84. <div className="col-md-3"></div>
  85. <div className="col-md-9">
  86. {renderProgressBar()}
  87. </div>
  88. </div>
  89. <form onSubmit={onSyncBtnClick}>
  90. {isUserBind && (
  91. <div className="row form-group">
  92. <label htmlFor="ldapGroupSyncPassword" className="text-left text-md-right col-md-3 col-form-label">{t('external_user_group.ldap.password')}</label>
  93. <div className="col-md-6">
  94. <input
  95. className="form-control"
  96. type="password"
  97. name="password"
  98. id="ldapGroupSyncPassword"
  99. />
  100. <p className="form-text text-muted">
  101. <small>{t('external_user_group.ldap.password_detail')}</small>
  102. </p>
  103. </div>
  104. </div>
  105. )}
  106. <div className="row">
  107. <div className="col-md-3"></div>
  108. <div className="col-md-6"><button className="btn btn-primary" type="submit">{t('external_user_group.sync')}</button></div>
  109. </div>
  110. </form>
  111. </>
  112. );
  113. };