import React from 'react'; import PropTypes from 'prop-types'; import { useTranslation } from 'next-i18next'; import AdminLdapSecurityContainer from '~/client/services/AdminLdapSecurityContainer'; import { toastSuccess, toastError } from '~/client/util/apiNotification'; import { apiPost } from '~/client/util/apiv1-client'; import loggerFactory from '~/utils/logger'; import { withUnstatedContainers } from '../../UnstatedUtils'; const logger = loggerFactory('growi:security:AdminLdapSecurityContainer'); class LdapAuthTest extends React.Component { constructor(props) { super(props); this.state = { logs: '', errorMessage: null, successMessage: null, }; this.addLogs = this.addLogs.bind(this); this.testLdapCredentials = this.testLdapCredentials.bind(this); } /** * add logs */ addLogs(log) { const newLog = `${new Date()} - ${log}\n\n`; this.setState({ logs: `${newLog}${this.state.logs}`, }); } /** * Test ldap auth */ async testLdapCredentials() { try { const response = await apiPost('/login/testLdap', { loginForm: { username: this.props.username, password: this.props.password, }, }); // add logs if (response.err) { toastError(response.err); this.addLogs(response.err); } if (response.status === 'warning') { this.addLogs(response.message); this.setState({ errorMessage: response.message, successMessage: null }); } if (response.status === 'success') { toastSuccess(response.message); this.setState({ successMessage: response.message, errorMessage: null }); } if (response.ldapConfiguration) { const prettified = JSON.stringify(response.ldapConfiguration.server, undefined, 4); this.addLogs(`LDAP Configuration : ${prettified}`); } if (response.ldapAccountInfo) { const prettified = JSON.stringify(response.ldapAccountInfo, undefined, 4); this.addLogs(`Retrieved LDAP Account : ${prettified}`); } } // Catch server communication error catch (err) { toastError(err); logger.error(err); } } render() { const { t } = this.props; return ( {this.state.successMessage != null && {this.state.successMessage}} {this.state.errorMessage != null && {this.state.errorMessage}} {t('username')} { this.props.onChangeUsername(e.target.value) }} autoComplete="off" /> {t('Password')} { this.props.onChangePassword(e.target.value) }} autoComplete="off" /> Logs Test ); } } const LdapAuthTestFc = (props) => { const { t } = useTranslation(); return ; }; LdapAuthTest.propTypes = { t: PropTypes.func.isRequired, // i18next adminLdapSecurityContainer: PropTypes.instanceOf(AdminLdapSecurityContainer).isRequired, username: PropTypes.string.isRequired, password: PropTypes.string.isRequired, onChangeUsername: PropTypes.func.isRequired, onChangePassword: PropTypes.func.isRequired, }; const LdapAuthTestWrapper = withUnstatedContainers(LdapAuthTestFc, [AdminLdapSecurityContainer]); export default LdapAuthTestWrapper;