AdminGitHubSecurityContainer.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import { isServer, 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:AdminGitHubSecurityContainer');
  8. /**
  9. * Service container for admin security page (GitHubSecurityManagement.jsx)
  10. * @extends {Container} unstated Container
  11. */
  12. export default class AdminGitHubSecurityContainer extends Container {
  13. constructor(appContainer) {
  14. super();
  15. if (isServer()) {
  16. return;
  17. }
  18. this.dummyGithubClientId = 0;
  19. this.dummyGithubClientIdForError = 1;
  20. this.state = {
  21. retrieveError: null,
  22. // set dummy value tile for using suspense
  23. githubClientId: this.dummyGithubClientId,
  24. githubClientSecret: '',
  25. isSameUsernameTreatedAsIdenticalUser: false,
  26. };
  27. }
  28. /**
  29. * retrieve security data
  30. */
  31. async retrieveSecurityData() {
  32. try {
  33. const response = await apiv3Get('/security-setting/');
  34. const { githubOAuth } = response.data.securityParams;
  35. this.setState({
  36. githubClientId: githubOAuth.githubClientId,
  37. githubClientSecret: githubOAuth.githubClientSecret,
  38. isSameUsernameTreatedAsIdenticalUser: githubOAuth.isSameUsernameTreatedAsIdenticalUser,
  39. });
  40. }
  41. catch (err) {
  42. this.setState({ retrieveError: err });
  43. logger.error(err);
  44. throw new Error('Failed to fetch data');
  45. }
  46. }
  47. /**
  48. * Workaround for the mangling in production build to break constructor.name
  49. */
  50. static getClassName() {
  51. return 'AdminGitHubSecurityContainer';
  52. }
  53. /**
  54. * Change githubClientId
  55. */
  56. changeGitHubClientId(value) {
  57. this.setState({ githubClientId: value });
  58. }
  59. /**
  60. * Change githubClientSecret
  61. */
  62. changeGitHubClientSecret(value) {
  63. this.setState({ githubClientSecret: value });
  64. }
  65. /**
  66. * Switch isSameUsernameTreatedAsIdenticalUser
  67. */
  68. switchIsSameUsernameTreatedAsIdenticalUser() {
  69. this.setState({ isSameUsernameTreatedAsIdenticalUser: !this.state.isSameUsernameTreatedAsIdenticalUser });
  70. }
  71. /**
  72. * Update githubSetting
  73. */
  74. async updateGitHubSetting() {
  75. const { githubClientId, githubClientSecret, isSameUsernameTreatedAsIdenticalUser } = this.state;
  76. let requestParams = { githubClientId, githubClientSecret, isSameUsernameTreatedAsIdenticalUser };
  77. requestParams = await removeNullPropertyFromObject(requestParams);
  78. const response = await apiv3Put('/security-setting/github-oauth', requestParams);
  79. const { securitySettingParams } = response.data;
  80. this.setState({
  81. githubClientId: securitySettingParams.githubClientId,
  82. githubClientSecret: securitySettingParams.githubClientSecret,
  83. isSameUsernameTreatedAsIdenticalUser: securitySettingParams.isSameUsernameTreatedAsIdenticalUser,
  84. });
  85. return response;
  86. }
  87. }