AdminGoogleSecurityContainer.js 3.0 KB

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