Просмотр исходного кода

Merge branch 'feat/growi-bot' into imprv/gw5898-saving-proxy-url-to-db

kaori 4 лет назад
Родитель
Сommit
0d503a257f

+ 1 - 1
packages/slackbot-proxy/src/Server.ts

@@ -75,7 +75,7 @@ export class Server {
   @Inject()
   injector: InjectorService;
 
-  $onInit(): Promise<any> | void {
+  $beforeInit(): Promise<any> | void {
     const serverUri = process.env.SERVER_URI;
 
     if (serverUri === undefined) {

+ 1 - 0
src/client/js/components/Admin/SlackIntegration/CustomBotWithProxySettings.jsx

@@ -97,6 +97,7 @@ const CustomBotWithProxySettings = (props) => {
         </div>
       </div>
       <DeleteSlackBotSettingsModal
+        isResetAll={false}
         isOpen={isDeleteConfirmModalShown}
         onClose={() => setIsDeleteConfirmModalShown(false)}
         onClickDeleteButton={deleteSlackSettingsHandler}

+ 1 - 0
src/client/js/components/Admin/SlackIntegration/CustomBotWithoutProxySettings.jsx

@@ -60,6 +60,7 @@ const CustomBotWithoutProxySettings = (props) => {
         />
       </div>
       <DeleteSlackBotSettingsModal
+        isResetAll={false}
         isOpen={isDeleteConfirmModalShown}
         onClose={() => setIsDeleteConfirmModalShown(false)}
         onClickDeleteButton={deleteSlackSettingsHandler}

+ 11 - 2
src/client/js/components/Admin/SlackIntegration/DeleteSlackBotSettingsModal.jsx

@@ -36,7 +36,16 @@ const DeleteSlackBotSettingsModal = React.memo((props) => {
       <ModalHeader tag="h4" toggle={closeButtonHandler} className="bg-danger text-light">
         <span>
           <i className="icon-fw icon-fire"></i>
-          {t('admin:slack_integration.delete_slackbot_settings')}
+          {props.isResetAll && (
+            <>
+              {t('admin:slack_integration.reset_all_settings')}
+            </>
+          )}
+          {!props.isResetAll && (
+            <>
+              {t('admin:slack_integration.delete_slackbot_settings')}
+            </>
+          )}
         </span>
       </ModalHeader>
       <ModalBody>
@@ -56,7 +65,7 @@ const DeleteSlackBotSettingsModal = React.memo((props) => {
 
 DeleteSlackBotSettingsModal.propTypes = {
   t: PropTypes.func.isRequired, // i18next
-
+  isResetAll: PropTypes.bool.isRequired,
   isOpen: PropTypes.bool.isRequired,
   onClose: PropTypes.func,
   onClickDeleteButton: PropTypes.func,

+ 10 - 0
src/client/js/components/Admin/SlackIntegration/SlackIntegration.jsx

@@ -10,6 +10,7 @@ import CustomBotWithoutProxySettings from './CustomBotWithoutProxySettings';
 import CustomBotWithProxySettings from './CustomBotWithProxySettings';
 import ConfirmBotChangeModal from './ConfirmBotChangeModal';
 import BotTypeCard from './BotTypeCard';
+import DeleteSlackBotSettingsModal from './DeleteSlackBotSettingsModal';
 
 const botTypes = ['officialBot', 'customBotWithoutProxy', 'customBotWithProxy'];
 
@@ -25,6 +26,7 @@ const SlackIntegration = (props) => {
   const [isRegisterSlackCredentials, setIsRegisterSlackCredentials] = useState(false);
   const [isSendTestMessage, setIsSendTestMessage] = useState(false);
   const [slackWSNameInWithoutProxy, setSlackWSNameInWithoutProxy] = useState(null);
+  const [isDeleteConfirmModalShown, setIsDeleteConfirmModalShown] = useState(false);
 
   const fetchSlackIntegrationData = useCallback(async() => {
     try {
@@ -124,6 +126,13 @@ const SlackIntegration = (props) => {
         onCancelClick={cancelBotChangeHandler}
       />
 
+      {/* TODO add onClickDeleteButton */}
+      <DeleteSlackBotSettingsModal
+        isResetAll
+        isOpen={isDeleteConfirmModalShown}
+        onClose={() => setIsDeleteConfirmModalShown(false)}
+      />
+
       <div className="selecting-bot-type mb-5">
         <h2 className="admin-setting-header mb-4">
           {t('admin:slack_integration.selecting_bot_types.slack_bot')}
@@ -143,6 +152,7 @@ const SlackIntegration = (props) => {
             <button
               className="mx-3 btn btn-outline-danger flex-end"
               type="button"
+              onClick={() => setIsDeleteConfirmModalShown(true)}
             >{t('admin:slack_integration.reset_all_settings')}
             </button>
           )}

+ 21 - 7
src/server/models/slack-app-integration.js

@@ -1,10 +1,24 @@
-module.exports = function(crowi) {
-  const mongoose = require('mongoose');
+const crypto = require('crypto');
+const mongoose = require('mongoose');
+
+const schema = new mongoose.Schema({
+  tokenGtoP: { type: String, required: true, unique: true },
+  tokenPtoG: { type: String, required: true, unique: true },
+});
+class SlackAppIntegration {
 
-  const slackAppIntegrationSchema = new mongoose.Schema({
-    tokenGtoP: { type: String, required: true, unique: true },
-    tokenPtoG: { type: String, required: true, unique: true },
-  });
+  static generateAccessToken() {
+    const hasher1 = crypto.createHash('sha512');
+    const hasher2 = crypto.createHash('sha512');
+    const tokenGtoP = hasher1.update(`${new Date().getTime().toString()}proxy`).digest('base64');
+    const tokenPtoG = hasher2.update(`${new Date().getTime().toString()}growi`).digest('base64');
+    return [tokenGtoP, tokenPtoG];
+  }
 
-  return mongoose.model('SlackAppIntegration', slackAppIntegrationSchema);
+}
+
+module.exports = function(crowi) {
+  SlackAppIntegration.crowi = crowi;
+  schema.loadClass(SlackAppIntegration);
+  return mongoose.model('SlackAppIntegration', schema);
 };

+ 38 - 0
src/server/routes/apiv3/slack-integration-settings.js

@@ -1,3 +1,4 @@
+const mongoose = require('mongoose');
 const express = require('express');
 const { body } = require('express-validator');
 const axios = require('axios');
@@ -264,5 +265,42 @@ module.exports = (crowi) => {
       }
     });
 
+  /**
+   * @swagger
+   *
+   *    /slack-integration/access-token:
+   *      put:
+   *        tags: [SlackIntegration]
+   *        operationId:
+   *        summary: /slack-integration
+   *        description: Generate accessToken
+   *        responses:
+   *          200:
+   *            description: Succeeded to update access token for slack
+   */
+  router.put('/access-tokens', loginRequiredStrictly, adminRequired, csrf, async(req, res) => {
+    const SlackAppIntegration = mongoose.model('SlackAppIntegration');
+    let checkTokens;
+    let tokenGtoP;
+    let tokenPtoG;
+    do {
+      // TODO imple generate tokens at GW-5859. The following tokens is temporary.
+      tokenGtoP = 'v2';
+      tokenPtoG = 'v2';
+      // eslint-disable-next-line no-await-in-loop
+      checkTokens = await SlackAppIntegration.findOne({ $or: [{ tokenGtoP }, { tokenPtoG }] });
+    } while (checkTokens != null);
+
+    try {
+      const slackAppTokens = await SlackAppIntegration.create({ tokenGtoP, tokenPtoG });
+      return res.apiv3(slackAppTokens, 200);
+    }
+    catch (error) {
+      const msg = 'Error occured in updating access token for slack app tokens';
+      logger.error('Error', error);
+      return res.apiv3Err(new ErrorV3(msg, 'update-slackAppTokens-failed'), 500);
+    }
+  });
+
   return router;
 };