Răsfoiți Sursa

save setting

itizawa 6 ani în urmă
părinte
comite
ecf3282862

+ 14 - 1
src/client/js/components/Admin/Security/GoogleSecuritySetting.jsx

@@ -5,7 +5,7 @@ import { withTranslation } from 'react-i18next';
 import loggerFactory from '@alias/logger';
 import loggerFactory from '@alias/logger';
 
 
 import { createSubscribedElement } from '../../UnstatedUtils';
 import { createSubscribedElement } from '../../UnstatedUtils';
-import { toastError } from '../../../util/apiNotification';
+import { toastSuccess, toastError } from '../../../util/apiNotification';
 
 
 import AppContainer from '../../../services/AppContainer';
 import AppContainer from '../../../services/AppContainer';
 import AdminGeneralSecurityContainer from '../../../services/AdminGeneralSecurityContainer';
 import AdminGeneralSecurityContainer from '../../../services/AdminGeneralSecurityContainer';
@@ -38,6 +38,19 @@ class GoogleSecurityManagement extends React.Component {
     }
     }
   }
   }
 
 
+  async onClickSubmit() {
+    const { t, adminGoogleSecurityContainer } = this.props;
+
+    try {
+      await adminGoogleSecurityContainer.updateGoogleSetting();
+      toastSuccess(t('security_setting.OAuth.Twitter.updated_twitter'));
+    }
+    catch (err) {
+      toastError(err);
+      logger.error(err);
+    }
+  }
+
   render() {
   render() {
     const { t, adminGeneralSecurityContainer, adminGoogleSecurityContainer } = this.props;
     const { t, adminGeneralSecurityContainer, adminGoogleSecurityContainer } = this.props;
     return (
     return (

+ 24 - 3
src/client/js/services/AdminGoogleSecurityContainer.js

@@ -20,7 +20,7 @@ export default class AdminGoogleSecurityContainer extends Container {
     this.appContainer = appContainer;
     this.appContainer = appContainer;
 
 
     this.state = {
     this.state = {
-      appSiteUrl: urljoin(pathUtils.removeTrailingSlash(appContainer.config.crowi.url), '/passport/google/callback'),
+      callbackUrl: urljoin(pathUtils.removeTrailingSlash(appContainer.config.crowi.url), '/passport/google/callback'),
       googleClientId: '',
       googleClientId: '',
       googleClientSecret: '',
       googleClientSecret: '',
       isSameUsernameTreatedAsIdenticalUser: false,
       isSameUsernameTreatedAsIdenticalUser: false,
@@ -36,8 +36,8 @@ export default class AdminGoogleSecurityContainer extends Container {
     const response = await this.appContainer.apiv3.get('/security-setting/');
     const response = await this.appContainer.apiv3.get('/security-setting/');
     const { googleOAuth } = response.data.securityParams;
     const { googleOAuth } = response.data.securityParams;
     this.setState({
     this.setState({
-      twitterConsumerKey: googleOAuth.twitterConsumerKey || '',
-      twitterConsumerSecret: googleOAuth.twitterConsumerSecret || '',
+      googleClientId: googleOAuth.googleClientId || '',
+      googleClientSecret: googleOAuth.googleClientSecret || '',
       isSameUsernameTreatedAsIdenticalUser: googleOAuth.isSameUsernameTreatedAsIdenticalUser || false,
       isSameUsernameTreatedAsIdenticalUser: googleOAuth.isSameUsernameTreatedAsIdenticalUser || false,
     });
     });
   }
   }
@@ -70,4 +70,25 @@ export default class AdminGoogleSecurityContainer extends Container {
     this.setState({ isSameUsernameTreatedAsIdenticalUser: !this.state.isSameUsernameTreatedAsIdenticalUser });
     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({
+      googleClientId: securitySettingParams.googleClientId,
+      googleClientSecret: securitySettingParams.googleClientSecret,
+      isSameUsernameTreatedAsIdenticalUser: securitySettingParams.isSameUsernameTreatedAsIdenticalUser,
+    });
+    return response;
+  }
+
 }
 }

+ 51 - 2
src/server/routes/apiv3/security-setting.js

@@ -19,6 +19,11 @@ const validator = {
     body('hideRestrictedByOwner').isBoolean(),
     body('hideRestrictedByOwner').isBoolean(),
     body('hideRestrictedByGroup').isBoolean(),
     body('hideRestrictedByGroup').isBoolean(),
   ],
   ],
+  googleOAuth: [
+    body('googleClientId').isString(),
+    body('googleClientSecret').isString(),
+    body('isSameUsernameTreatedAsIdenticalUser').isBoolean(),
+  ],
   githubOAuth: [
   githubOAuth: [
     body('githubClientId').isString(),
     body('githubClientId').isString(),
     body('githubClientSecret').isString(),
     body('githubClientSecret').isString(),
@@ -215,6 +220,50 @@ module.exports = (crowi) => {
     }
     }
   });
   });
 
 
+  /**
+   * @swagger
+   *
+   *    /security-setting/google-oauth:
+   *      put:
+   *        tags: [SecuritySetting]
+   *        description: Update google OAuth
+   *        requestBody:
+   *          required: true
+   *          content:
+   *            application/json:
+   *              schema:
+   *                $ref: '#/components/schemas/SecurityParams/GoogleOAuthSetting'
+   *        responses:
+   *          200:
+   *            description: Succeeded to google OAuth
+   *            content:
+   *              application/json:
+   *                schema:
+   *                  $ref: '#/components/schemas/SecurityParams/GoogleOAuthSetting'
+   */
+  router.put('/google-oauth', loginRequiredStrictly, adminRequired, csrf, validator.googleOAuth, ApiV3FormValidator, async(req, res) => {
+    const requestParams = {
+      'security:passport-google:clientId': req.body.googleClientId,
+      'security:passport-google:clientSecret': req.body.googleClientSecret,
+      'security:passport-google:isSameUsernameTreatedAsIdenticalUser': req.body.isSameUsernameTreatedAsIdenticalUser,
+    };
+
+    try {
+      await crowi.configManager.updateConfigsInTheSameNamespace('crowi', requestParams);
+      const securitySettingParams = {
+        googleClientId: await crowi.configManager.getConfig('crowi', 'security:passport-google:clientId'),
+        googleClientSecret: await crowi.configManager.getConfig('crowi', 'security:passport-google:clientSecret'),
+        isSameUsernameTreatedAsIdenticalUser: await crowi.configManager.getConfig('crowi', 'security:passport-google:isSameUsernameTreatedAsIdenticalUser'),
+      };
+      return res.apiv3({ securitySettingParams });
+    }
+    catch (err) {
+      const msg = 'Error occurred in updating googleOAuth';
+      logger.error('Error', err);
+      return res.apiv3Err(new ErrorV3(msg, 'update-googleOAuth-failed'));
+    }
+  });
+
   /**
   /**
    * @swagger
    * @swagger
    *
    *
@@ -230,7 +279,7 @@ module.exports = (crowi) => {
    *                $ref: '#/components/schemas/SecurityParams/GitHubOAuthSetting'
    *                $ref: '#/components/schemas/SecurityParams/GitHubOAuthSetting'
    *        responses:
    *        responses:
    *          200:
    *          200:
-   *            description: Succeeded to update function
+   *            description: Succeeded to github OAuth
    *            content:
    *            content:
    *              application/json:
    *              application/json:
    *                schema:
    *                schema:
@@ -274,7 +323,7 @@ module.exports = (crowi) => {
    *                $ref: '#/components/schemas/SecurityParams/TwitterOAuthSetting'
    *                $ref: '#/components/schemas/SecurityParams/TwitterOAuthSetting'
    *        responses:
    *        responses:
    *          200:
    *          200:
-   *            description: Succeeded to update function
+   *            description: Succeeded to update twitter OAuth
    *            content:
    *            content:
    *              application/json:
    *              application/json:
    *                schema:
    *                schema: