| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- import { Container } from 'unstated';
- import loggerFactory from '@alias/logger';
- import { pathUtils } from 'growi-commons';
- import urljoin from 'url-join';
- import removeNullPropertyFromObject from '../../../lib/util/removeNullPropertyFromObject';
- const logger = loggerFactory('growi:security:AdminGoogleSecurityContainer');
- /**
- * Service container for admin security page (GoogleSecurityManagement.jsx)
- * @extends {Container} unstated Container
- */
- export default class AdminGoogleSecurityContainer extends Container {
- constructor(appContainer) {
- super();
- this.appContainer = appContainer;
- this.dummyGoogleClientId = 0;
- this.state = {
- retrieveError: null,
- callbackUrl: urljoin(pathUtils.removeTrailingSlash(appContainer.config.crowi.url), '/passport/google/callback'),
- // set dummy value tile for using suspense
- googleClientId: this.dummyGoogleClientId,
- googleClientSecret: '',
- isSameUsernameTreatedAsIdenticalUser: false,
- };
- }
- /**
- * retrieve security data
- */
- async retrieveSecurityData() {
- try {
- const response = await this.appContainer.apiv3.get('/security-setting/');
- const { googleOAuth } = response.data.securityParams;
- this.setState({
- googleClientId: googleOAuth.googleClientId,
- googleClientSecret: googleOAuth.googleClientSecret,
- isSameUsernameTreatedAsIdenticalUser: googleOAuth.isSameUsernameTreatedAsIdenticalUser,
- });
- }
- catch (err) {
- this.setState({ retrieveError: err });
- logger.error(err);
- throw new Error('Failed to fetch data');
- }
- }
- /**
- * Workaround for the mangling in production build to break constructor.name
- */
- static getClassName() {
- return 'AdminGoogleSecurityContainer';
- }
- /**
- * Change googleClientId
- */
- changeGoogleClientId(value) {
- this.setState({ googleClientId: value });
- }
- /**
- * Change googleClientSecret
- */
- changeGoogleClientSecret(value) {
- this.setState({ googleClientSecret: value });
- }
- /**
- * Switch isSameUsernameTreatedAsIdenticalUser
- */
- switchIsSameUsernameTreatedAsIdenticalUser() {
- this.setState({ isSameUsernameTreatedAsIdenticalUser: !this.state.isSameUsernameTreatedAsIdenticalUser });
- }
- /**
- * Update googleSetting
- */
- async updateGoogleSetting() {
- const { googleClientId, googleClientSecret, isSameUsernameTreatedAsIdenticalUser } = this.state;
- let requestParams = {
- googleClientId, googleClientSecret, isSameUsernameTreatedAsIdenticalUser,
- };
- requestParams = await removeNullPropertyFromObject(requestParams);
- const response = await this.appContainer.apiv3.put('/security-setting/google-oauth', requestParams);
- const { securitySettingParams } = response.data;
- this.setState({
- googleClientId: securitySettingParams.googleClientId,
- googleClientSecret: securitySettingParams.googleClientSecret,
- isSameUsernameTreatedAsIdenticalUser: securitySettingParams.isSameUsernameTreatedAsIdenticalUser,
- });
- return response;
- }
- }
|