AdminGoogleSecurityContainer.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import { Container } from 'unstated';
  2. import loggerFactory from '@alias/logger';
  3. import { pathUtils } from 'growi-commons';
  4. import urljoin from 'url-join';
  5. import removeNullPropertyFromObject from '../../../lib/util/removeNullPropertyFromObject';
  6. const logger = loggerFactory('growi:security:AdminGoogleSecurityContainer');
  7. /**
  8. * Service container for admin security page (GoogleSecurityManagement.jsx)
  9. * @extends {Container} unstated Container
  10. */
  11. export default class AdminGoogleSecurityContainer extends Container {
  12. constructor(appContainer) {
  13. super();
  14. this.appContainer = appContainer;
  15. this.dummyGoogleClientId = 0;
  16. this.state = {
  17. retrieveError: null,
  18. callbackUrl: urljoin(pathUtils.removeTrailingSlash(appContainer.config.crowi.url), '/passport/google/callback'),
  19. // set dummy value tile for using suspense
  20. googleClientId: this.dummyGoogleClientId,
  21. googleClientSecret: '',
  22. isSameUsernameTreatedAsIdenticalUser: false,
  23. };
  24. }
  25. /**
  26. * retrieve security data
  27. */
  28. async retrieveSecurityData() {
  29. try {
  30. const response = await this.appContainer.apiv3.get('/security-setting/');
  31. const { googleOAuth } = response.data.securityParams;
  32. this.setState({
  33. googleClientId: googleOAuth.googleClientId,
  34. googleClientSecret: googleOAuth.googleClientSecret,
  35. isSameUsernameTreatedAsIdenticalUser: googleOAuth.isSameUsernameTreatedAsIdenticalUser,
  36. });
  37. }
  38. catch (err) {
  39. this.setState({ retrieveError: err });
  40. logger.error(err);
  41. throw new Error('Failed to fetch data');
  42. }
  43. }
  44. /**
  45. * Workaround for the mangling in production build to break constructor.name
  46. */
  47. static getClassName() {
  48. return 'AdminGoogleSecurityContainer';
  49. }
  50. /**
  51. * Change googleClientId
  52. */
  53. changeGoogleClientId(value) {
  54. this.setState({ googleClientId: value });
  55. }
  56. /**
  57. * Change googleClientSecret
  58. */
  59. changeGoogleClientSecret(value) {
  60. this.setState({ googleClientSecret: value });
  61. }
  62. /**
  63. * Switch isSameUsernameTreatedAsIdenticalUser
  64. */
  65. switchIsSameUsernameTreatedAsIdenticalUser() {
  66. this.setState({ isSameUsernameTreatedAsIdenticalUser: !this.state.isSameUsernameTreatedAsIdenticalUser });
  67. }
  68. /**
  69. * Update googleSetting
  70. */
  71. async updateGoogleSetting() {
  72. const { googleClientId, googleClientSecret, isSameUsernameTreatedAsIdenticalUser } = this.state;
  73. let requestParams = {
  74. googleClientId, googleClientSecret, isSameUsernameTreatedAsIdenticalUser,
  75. };
  76. requestParams = await removeNullPropertyFromObject(requestParams);
  77. const response = await this.appContainer.apiv3.put('/security-setting/google-oauth', requestParams);
  78. const { securitySettingParams } = response.data;
  79. this.setState({
  80. googleClientId: securitySettingParams.googleClientId,
  81. googleClientSecret: securitySettingParams.googleClientSecret,
  82. isSameUsernameTreatedAsIdenticalUser: securitySettingParams.isSameUsernameTreatedAsIdenticalUser,
  83. });
  84. return response;
  85. }
  86. }