GoogleSecuritySetting.jsx 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /* eslint-disable react/no-danger */
  2. import React from 'react';
  3. import PropTypes from 'prop-types';
  4. import { withTranslation } from 'react-i18next';
  5. import { createSubscribedElement } from '../../UnstatedUtils';
  6. import { toastSuccess, toastError } from '../../../util/apiNotification';
  7. import AppContainer from '../../../services/AppContainer';
  8. import AdminGeneralSecurityContainer from '../../../services/AdminGeneralSecurityContainer';
  9. import AdminGoogleSecurityContainer from '../../../services/AdminGoogleSecurityContainer';
  10. class GoogleSecurityManagement extends React.Component {
  11. constructor(props) {
  12. super(props);
  13. this.state = {
  14. isRetrieving: true,
  15. };
  16. this.onClickSubmit = this.onClickSubmit.bind(this);
  17. }
  18. async componentDidMount() {
  19. const { adminGoogleSecurityContainer } = this.props;
  20. try {
  21. await adminGoogleSecurityContainer.retrieveSecurityData();
  22. }
  23. catch (err) {
  24. toastError(err);
  25. }
  26. this.setState({ isRetrieving: false });
  27. }
  28. async onClickSubmit() {
  29. const { t, adminGoogleSecurityContainer, adminGeneralSecurityContainer } = this.props;
  30. try {
  31. await adminGoogleSecurityContainer.updateGoogleSetting();
  32. await adminGeneralSecurityContainer.retrieveSetupStratedies();
  33. toastSuccess(t('security_setting.OAuth.Google.updated_google'));
  34. }
  35. catch (err) {
  36. toastError(err);
  37. }
  38. }
  39. render() {
  40. const { t, adminGeneralSecurityContainer, adminGoogleSecurityContainer } = this.props;
  41. const { isGoogleEnabled } = adminGeneralSecurityContainer.state;
  42. if (this.state.isRetrieving) {
  43. return null;
  44. }
  45. return (
  46. <React.Fragment>
  47. <h2 className="alert-anchor border-bottom">
  48. {t('security_setting.OAuth.Google.name')}
  49. </h2>
  50. {this.state.retrieveError != null && (
  51. <div className="alert alert-danger">
  52. <p>{t('Error occurred')} : {this.state.err}</p>
  53. </div>
  54. )}
  55. <div className="row mb-5">
  56. <div className="offset-3 col-6">
  57. <div className="custom-control custom-switch custom-checkbox-success">
  58. <input
  59. id="isGoogleEnabled"
  60. className="custom-control-input"
  61. type="checkbox"
  62. checked={adminGeneralSecurityContainer.state.isGoogleEnabled || false}
  63. onChange={() => { adminGeneralSecurityContainer.switchIsGoogleOAuthEnabled() }}
  64. />
  65. <label className="custom-control-label" htmlFor="isGoogleEnabled">
  66. {t('security_setting.OAuth.Google.enable_google')}
  67. </label>
  68. </div>
  69. {(!adminGeneralSecurityContainer.state.setupStrategies.includes('google') && isGoogleEnabled)
  70. && <div className="badge badge-warning">{t('security_setting.setup_is_not_yet_complete')}</div>}
  71. </div>
  72. </div>
  73. <div className="row mb-5">
  74. <label className="col-3 text-right py-2">{t('security_setting.callback_URL')}</label>
  75. <div className="col-6">
  76. <input
  77. className="form-control"
  78. type="text"
  79. value={adminGoogleSecurityContainer.state.callbackUrl}
  80. readOnly
  81. />
  82. <p className="form-text text-muted small">{t('security_setting.desc_of_callback_URL', { AuthName: 'OAuth' })}</p>
  83. {!adminGeneralSecurityContainer.state.appSiteUrl && (
  84. <div className="alert alert-danger">
  85. <i
  86. className="icon-exclamation"
  87. // eslint-disable-next-line max-len
  88. dangerouslySetInnerHTML={{ __html: t('security_setting.alert_siteUrl_is_not_set', { link: `<a href="/admin/app">${t('App settings')}<i class="icon-login"></i></a>` }) }}
  89. />
  90. </div>
  91. )}
  92. </div>
  93. </div>
  94. {isGoogleEnabled && (
  95. <React.Fragment>
  96. <h3 className="border-bottom">{t('security_setting.configuration')}</h3>
  97. <div className="row mb-5">
  98. <label htmlFor="googleClientId" className="col-3 text-right py-2">{t('security_setting.clientID')}</label>
  99. <div className="col-6">
  100. <input
  101. className="form-control"
  102. type="text"
  103. name="googleClientId"
  104. defaultValue={adminGoogleSecurityContainer.state.googleClientId || ''}
  105. onChange={e => adminGoogleSecurityContainer.changeGoogleClientId(e.target.value)}
  106. />
  107. <p className="form-text text-muted">
  108. <small dangerouslySetInnerHTML={{ __html: t('security_setting.Use env var if empty', { env: 'OAUTH_GOOGLE_CLIENT_ID' }) }} />
  109. </p>
  110. </div>
  111. </div>
  112. <div className="row mb-5">
  113. <label htmlFor="googleClientSecret" className="col-3 text-right py-2">{t('security_setting.client_secret')}</label>
  114. <div className="col-6">
  115. <input
  116. className="form-control"
  117. type="text"
  118. name="googleClientSecret"
  119. defaultValue={adminGoogleSecurityContainer.state.googleClientSecret || ''}
  120. onChange={e => adminGoogleSecurityContainer.changeGoogleClientSecret(e.target.value)}
  121. />
  122. <p className="form-text text-muted">
  123. <small dangerouslySetInnerHTML={{ __html: t('security_setting.Use env var if empty', { env: 'OAUTH_GOOGLE_CLIENT_SECRET' }) }} />
  124. </p>
  125. </div>
  126. </div>
  127. <div className="row mb-5">
  128. <div className="offset-3 col-6">
  129. <div className="custom-control custom-checkbox custom-checkbox-success">
  130. <input
  131. id="bindByUserNameGoogle"
  132. className="custom-control-input"
  133. type="checkbox"
  134. checked={adminGoogleSecurityContainer.state.isSameUsernameTreatedAsIdenticalUser || false}
  135. onChange={() => { adminGoogleSecurityContainer.switchIsSameUsernameTreatedAsIdenticalUser() }}
  136. />
  137. <label
  138. className="custom-control-label"
  139. htmlFor="bindByUserNameGoogle"
  140. dangerouslySetInnerHTML={{ __html: t('security_setting.Treat email matching as identical') }}
  141. />
  142. </div>
  143. <p className="form-text text-muted">
  144. <small dangerouslySetInnerHTML={{ __html: t('security_setting.Treat email matching as identical_warn') }} />
  145. </p>
  146. </div>
  147. </div>
  148. <div className="row my-3">
  149. <div className="offset-3 col-5">
  150. <button
  151. type="button"
  152. className="btn btn-primary"
  153. disabled={adminGoogleSecurityContainer.state.retrieveError != null}
  154. onClick={this.onClickSubmit}
  155. >
  156. {t('Update')}
  157. </button>
  158. </div>
  159. </div>
  160. </React.Fragment>
  161. )}
  162. <hr />
  163. <div style={{ minHeight: '300px' }}>
  164. <h4>
  165. <i className="icon-question" aria-hidden="true"></i>
  166. <a href="#collapseHelpForGoogleOauth" data-toggle="collapse"> {t('security_setting.OAuth.how_to.google')}</a>
  167. </h4>
  168. <ol id="collapseHelpForGoogleOauth" className="collapse">
  169. {/* eslint-disable-next-line max-len */}
  170. <li dangerouslySetInnerHTML={{ __html: t('security_setting.OAuth.Google.register_1', { link: '<a href="https://console.cloud.google.com/apis/credentials" target=_blank>Google Cloud Platform API Manager</a>' }) }} />
  171. <li dangerouslySetInnerHTML={{ __html: t('security_setting.OAuth.Google.register_2') }} />
  172. <li dangerouslySetInnerHTML={{ __html: t('security_setting.OAuth.Google.register_3') }} />
  173. <li dangerouslySetInnerHTML={{ __html: t('security_setting.OAuth.Google.register_4', { url: adminGoogleSecurityContainer.state.callbackUrl }) }} />
  174. <li dangerouslySetInnerHTML={{ __html: t('security_setting.OAuth.Google.register_5') }} />
  175. </ol>
  176. </div>
  177. </React.Fragment>
  178. );
  179. }
  180. }
  181. GoogleSecurityManagement.propTypes = {
  182. t: PropTypes.func.isRequired, // i18next
  183. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  184. adminGeneralSecurityContainer: PropTypes.instanceOf(AdminGeneralSecurityContainer).isRequired,
  185. adminGoogleSecurityContainer: PropTypes.instanceOf(AdminGoogleSecurityContainer).isRequired,
  186. };
  187. const GoogleSecurityManagementWrapper = (props) => {
  188. return createSubscribedElement(GoogleSecurityManagement, props, [AppContainer, AdminGeneralSecurityContainer, AdminGoogleSecurityContainer]);
  189. };
  190. export default withTranslation()(GoogleSecurityManagementWrapper);