AdminGoogleSecurityContainer.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import { isServer } from '@growi/core/dist/utils';
  2. import { Container } from 'unstated';
  3. import loggerFactory from '~/utils/logger';
  4. import { removeNullPropertyFromObject } from '~/utils/object-utils';
  5. import { apiv3Get, apiv3Put } from '../util/apiv3-client';
  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. if (isServer()) {
  15. return;
  16. }
  17. this.dummyGoogleClientId = 0;
  18. this.dummyGoogleClientIdForError = 1;
  19. this.state = {
  20. retrieveError: null,
  21. // set dummy value tile for using suspense
  22. googleClientId: this.dummyGoogleClientId,
  23. googleClientSecret: '',
  24. isSameEmailTreatedAsIdenticalUser: false,
  25. };
  26. }
  27. /**
  28. * retrieve security data
  29. */
  30. async retrieveSecurityData() {
  31. try {
  32. const response = await apiv3Get('/security-setting/');
  33. const { googleOAuth } = response.data.securityParams;
  34. this.setState({
  35. googleClientId: googleOAuth.googleClientId,
  36. googleClientSecret: googleOAuth.googleClientSecret,
  37. isSameEmailTreatedAsIdenticalUser:
  38. googleOAuth.isSameEmailTreatedAsIdenticalUser,
  39. });
  40. } catch (err) {
  41. this.setState({ retrieveError: err });
  42. logger.error(err);
  43. throw new Error('Failed to fetch data');
  44. }
  45. }
  46. /**
  47. * Workaround for the mangling in production build to break constructor.name
  48. */
  49. static getClassName() {
  50. return 'AdminGoogleSecurityContainer';
  51. }
  52. /**
  53. * Switch isSameEmailTreatedAsIdenticalUser
  54. */
  55. switchIsSameEmailTreatedAsIdenticalUser() {
  56. this.setState({
  57. isSameEmailTreatedAsIdenticalUser:
  58. !this.state.isSameEmailTreatedAsIdenticalUser,
  59. });
  60. }
  61. /**
  62. * Update googleSetting
  63. */
  64. async updateGoogleSetting(formData) {
  65. let requestParams =
  66. formData != null
  67. ? {
  68. googleClientId: formData.googleClientId,
  69. googleClientSecret: formData.googleClientSecret,
  70. isSameEmailTreatedAsIdenticalUser:
  71. formData.isSameEmailTreatedAsIdenticalUser,
  72. }
  73. : {
  74. googleClientId: this.state.googleClientId,
  75. googleClientSecret: this.state.googleClientSecret,
  76. isSameEmailTreatedAsIdenticalUser:
  77. this.state.isSameEmailTreatedAsIdenticalUser,
  78. };
  79. requestParams = await removeNullPropertyFromObject(requestParams);
  80. const response = await apiv3Put(
  81. '/security-setting/google-oauth',
  82. requestParams,
  83. );
  84. const { securitySettingParams } = response.data;
  85. this.setState({
  86. googleClientId: securitySettingParams.googleClientId,
  87. googleClientSecret: securitySettingParams.googleClientSecret,
  88. isSameEmailTreatedAsIdenticalUser:
  89. securitySettingParams.isSameEmailTreatedAsIdenticalUser,
  90. });
  91. return response;
  92. }
  93. }