AdminGitHubSecurityContainer.js 2.9 KB

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