GoogleSecuritySetting.jsx 7.9 KB

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