GoogleSecuritySetting.jsx 8.2 KB

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