GoogleSecuritySettingContents.jsx 8.6 KB

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