LdapAuthTestModal.jsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 Modal from 'react-bootstrap/es/Modal';
  6. import { createSubscribedElement } from '../../UnstatedUtils';
  7. import { toastError } from '../../../util/apiNotification';
  8. import AppContainer from '../../../services/AppContainer';
  9. import AdminLdapSecurityContainer from '../../../services/AdminLdapSecurityContainer';
  10. const logger = loggerFactory('growi:security:AdminLdapSecurityContainer');
  11. class LdapAuthTestModal extends React.Component {
  12. constructor(props) {
  13. super(props);
  14. this.state = {
  15. username: '',
  16. password: '',
  17. logs: '',
  18. };
  19. this.onChangeUsername = this.onChangeUsername.bind(this);
  20. this.onChangePassword = this.onChangePassword.bind(this);
  21. this.addLogs = this.addLogs.bind(this);
  22. this.testLdapCredentials = this.testLdapCredentials.bind(this);
  23. }
  24. /**
  25. * Change username
  26. */
  27. onChangeUsername(username) {
  28. this.setState({ username });
  29. }
  30. /**
  31. * Change password
  32. */
  33. onChangePassword(password) {
  34. this.setState({ password });
  35. }
  36. /**
  37. * add logs
  38. */
  39. addLogs(log) {
  40. const newLog = `${new Date()} - ${log}\n\n`;
  41. this.setState({
  42. logs: `${newLog}${this.state.logs}`,
  43. });
  44. }
  45. /**
  46. * Test ldap auth
  47. */
  48. async testLdapCredentials() {
  49. try {
  50. const response = await this.props.appContainer.apiPost('/login/testLdap', {
  51. loginForm: {
  52. username: this.state.username,
  53. password: this.state.password,
  54. },
  55. });
  56. // add logs
  57. if (response.err) {
  58. this.addLogs(response.err);
  59. }
  60. if (response.ldapConfiguration) {
  61. const prettified = JSON.stringify(response.ldapConfiguration.server, undefined, 4);
  62. this.addLogs(`LDAP Configuration : ${prettified}`);
  63. }
  64. if (response.ldapAccountInfo) {
  65. const prettified = JSON.stringify(response.ldapAccountInfo, undefined, 4);
  66. this.addLogs(`Retrieved LDAP Account : ${prettified}`);
  67. }
  68. }
  69. // Catch server communication error
  70. catch (err) {
  71. toastError(err);
  72. logger.error(err);
  73. }
  74. }
  75. render() {
  76. const { t } = this.props;
  77. return (
  78. <Modal show={this.props.isOpen} onHide={this.props.onClose}>
  79. <Modal.Header className="modal-header" closeButton>
  80. <Modal.Title>
  81. Test LDAP Account
  82. </Modal.Title>
  83. </Modal.Header>
  84. <Modal.Body>
  85. <div className="row p-3">
  86. <label htmlFor="username" className="col-xs-3 text-right">{t('username')}</label>
  87. <div className="col-xs-6">
  88. <input
  89. className="form-control"
  90. name="username"
  91. value={this.state.username}
  92. onChange={(e) => { this.onChangeUsername(e.target.value) }}
  93. />
  94. </div>
  95. </div>
  96. <div className="row p-3">
  97. <label htmlFor="password" className="col-xs-3 text-right">{t('Password')}</label>
  98. <div className="col-xs-6">
  99. <input
  100. className="form-control"
  101. type="password"
  102. name="password"
  103. value={this.state.password}
  104. onChange={(e) => { this.onChangePassword(e.target.value) }}
  105. />
  106. </div>
  107. </div>
  108. <div>
  109. <h5>Logs</h5>
  110. <textarea id="taLogs" className="col-xs-12" rows="4" value={this.state.logs} readOnly />
  111. </div>
  112. </Modal.Body>
  113. <Modal.Footer>
  114. <button type="button" className="btn btn-default mt-3 col-xs-offset-5 col-xs-2" onClick={this.testLdapCredentials}>Test</button>
  115. </Modal.Footer>
  116. </Modal>
  117. );
  118. }
  119. }
  120. LdapAuthTestModal.propTypes = {
  121. t: PropTypes.func.isRequired, // i18next
  122. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  123. adminLdapSecurityContainer: PropTypes.instanceOf(AdminLdapSecurityContainer).isRequired,
  124. isOpen: PropTypes.bool.isRequired,
  125. onClose: PropTypes.func.isRequired,
  126. };
  127. const LdapAuthTestModalWrapper = (props) => {
  128. return createSubscribedElement(LdapAuthTestModal, props, [AppContainer, AdminLdapSecurityContainer]);
  129. };
  130. export default withTranslation()(LdapAuthTestModalWrapper);