LdapAuthTest.jsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { withTranslation } from 'react-i18next';
  4. import loggerFactory from '@alias/logger';
  5. import { withUnstatedContainers } from '../../UnstatedUtils';
  6. import { toastSuccess, toastError } from '../../../util/apiNotification';
  7. import AppContainer from '../../../services/AppContainer';
  8. import AdminLdapSecurityContainer from '../../../services/AdminLdapSecurityContainer';
  9. const logger = loggerFactory('growi:security:AdminLdapSecurityContainer');
  10. class LdapAuthTest extends React.Component {
  11. constructor(props) {
  12. super(props);
  13. this.state = {
  14. logs: '',
  15. errorMessage: null,
  16. successMessage: null,
  17. };
  18. this.addLogs = this.addLogs.bind(this);
  19. this.testLdapCredentials = this.testLdapCredentials.bind(this);
  20. }
  21. /**
  22. * add logs
  23. */
  24. addLogs(log) {
  25. const newLog = `${new Date()} - ${log}\n\n`;
  26. this.setState({
  27. logs: `${newLog}${this.state.logs}`,
  28. });
  29. }
  30. /**
  31. * Test ldap auth
  32. */
  33. async testLdapCredentials() {
  34. try {
  35. const response = await this.props.appContainer.apiPost('/login/testLdap', {
  36. loginForm: {
  37. username: this.props.username,
  38. password: this.props.password,
  39. },
  40. });
  41. // add logs
  42. if (response.err) {
  43. toastError(response.err);
  44. this.addLogs(response.err);
  45. }
  46. if (response.status === 'warning') {
  47. this.addLogs(response.message);
  48. this.setState({ errorMessage: response.message, successMessage: null });
  49. }
  50. if (response.status === 'success') {
  51. toastSuccess(response.message);
  52. this.setState({ successMessage: response.message, errorMessage: null });
  53. }
  54. if (response.ldapConfiguration) {
  55. const prettified = JSON.stringify(response.ldapConfiguration.server, undefined, 4);
  56. this.addLogs(`LDAP Configuration : ${prettified}`);
  57. }
  58. if (response.ldapAccountInfo) {
  59. const prettified = JSON.stringify(response.ldapAccountInfo, undefined, 4);
  60. this.addLogs(`Retrieved LDAP Account : ${prettified}`);
  61. }
  62. }
  63. // Catch server communication error
  64. catch (err) {
  65. toastError(err);
  66. logger.error(err);
  67. }
  68. }
  69. render() {
  70. const { t } = this.props;
  71. return (
  72. <React.Fragment>
  73. {this.state.successMessage != null && <div className="alert alert-success">{this.state.successMessage}</div>}
  74. {this.state.errorMessage != null && <div className="alert alert-warning">{this.state.errorMessage}</div>}
  75. <div className="form-group row">
  76. <label htmlFor="username" className="col-3 col-form-label">{t('username')}</label>
  77. <div className="col-6">
  78. <input
  79. className="form-control"
  80. name="username"
  81. value={this.props.username}
  82. onChange={(e) => { this.props.onChangeUsername(e.target.value) }}
  83. />
  84. </div>
  85. </div>
  86. <div className="form-group row">
  87. <label htmlFor="password" className="col-3 col-form-label">{t('Password')}</label>
  88. <div className="col-6">
  89. <input
  90. className="form-control"
  91. type="password"
  92. name="password"
  93. value={this.props.password}
  94. onChange={(e) => { this.props.onChangePassword(e.target.value) }}
  95. />
  96. </div>
  97. </div>
  98. <div className="form-group">
  99. <label><h5>Logs</h5></label>
  100. <textarea id="taLogs" className="col form-control" rows="4" value={this.state.logs} readOnly />
  101. </div>
  102. <div>
  103. <button type="button" className="btn btn-outline-secondary offset-5 col-2" onClick={this.testLdapCredentials}>Test</button>
  104. </div>
  105. </React.Fragment>
  106. );
  107. }
  108. }
  109. LdapAuthTest.propTypes = {
  110. t: PropTypes.func.isRequired, // i18next
  111. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  112. adminLdapSecurityContainer: PropTypes.instanceOf(AdminLdapSecurityContainer).isRequired,
  113. username: PropTypes.string.isRequired,
  114. password: PropTypes.string.isRequired,
  115. onChangeUsername: PropTypes.func.isRequired,
  116. onChangePassword: PropTypes.func.isRequired,
  117. };
  118. const LdapAuthTestWrapper = withUnstatedContainers(LdapAuthTest, [AppContainer, AdminLdapSecurityContainer]);
  119. export default withTranslation()(LdapAuthTestWrapper);