| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- import { Container } from 'unstated';
- import loggerFactory from '@alias/logger';
- import { pathUtils } from 'growi-commons';
- import urljoin from 'url-join';
- // eslint-disable-next-line no-unused-vars
- 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.state = {
- callbackUrl: urljoin(pathUtils.removeTrailingSlash(appContainer.config.crowi.url), '/passport/google/callback'),
- isGoogleStrategySetup: false,
- googleClientId: '',
- googleClientSecret: '',
- isSameUsernameTreatedAsIdenticalUser: false,
- };
- }
- /**
- * retrieve security data
- */
- async retrieveSecurityData() {
- const response = await this.appContainer.apiv3.get('/security-setting/');
- const { googleOAuth } = response.data.securityParams;
- this.setState({
- isGoogleStrategySetup: googleOAuth.isGoogleStrategySetup,
- googleClientId: googleOAuth.googleClientId || '',
- googleClientSecret: googleOAuth.googleClientSecret || '',
- isSameUsernameTreatedAsIdenticalUser: googleOAuth.isSameUsernameTreatedAsIdenticalUser || false,
- });
- }
- /**
- * 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 response = await this.appContainer.apiv3.put('/security-setting/google-oauth', {
- googleClientId: this.state.googleClientId,
- googleClientSecret: this.state.googleClientSecret,
- isSameUsernameTreatedAsIdenticalUser: this.state.isSameUsernameTreatedAsIdenticalUser,
- });
- const { securitySettingParams } = response.data;
- this.setState({
- isGoogleStrategySetup: securitySettingParams.isGoogleStrategySetup,
- googleClientId: securitySettingParams.googleClientId,
- googleClientSecret: securitySettingParams.googleClientSecret,
- isSameUsernameTreatedAsIdenticalUser: securitySettingParams.isSameUsernameTreatedAsIdenticalUser,
- });
- return response;
- }
- }
|