Taichi Masuyama 4 лет назад
Родитель
Сommit
bf6ecb680e

+ 1 - 3
packages/app/src/server/routes/apiv3/slack-integration-settings.js

@@ -416,9 +416,7 @@ module.exports = (crowi) => {
         supportedCommandsForSingleUse: defaultSupportedCommandsNameForSingleUse,
       });
       // MOCK DATA DELETE THIS GW-6972 ---------------
-      /**
-       * this code represents the creation of new SlackAppIntegration model
-       */
+      /* This code represents the creation of the new SlackAppIntegration model instance. */
       const SlackAppIntegrationMock = mongoose.model('SlackAppIntegrationMock');
       const initialSupportedCommandsForBroadcastUse = new Map();
       const initialSupportedCommandsForSingleUse = new Map();

+ 24 - 24
packages/app/src/server/routes/apiv3/slack-integration.js

@@ -59,11 +59,10 @@ module.exports = (crowi) => {
     // MOCK DATA DELETE THIS GW-6972 ---------------
     const SlackAppIntegrationMock = mongoose.model('SlackAppIntegrationMock');
     const slackAppIntegrationMock = await SlackAppIntegrationMock.findOne({ tokenPtoG });
-    const channelsObject = slackAppIntegrationMock.permittedChannelsForEachCommand._doc.channelsObject;
+    const permissionsForBroadcastUseCommands = slackAppIntegrationMock.permissionsForBroadcastUseCommands;
+    const permissionsForSingleUseCommands = slackAppIntegrationMock.permissionsForSingleUseCommands;
     // MOCK DATA DELETE THIS GW-6972 ---------------
     const { supportedCommandsForBroadcastUse, supportedCommandsForSingleUse } = relation;
-    const supportedCommands = supportedCommandsForBroadcastUse.concat(supportedCommandsForSingleUse);
-    const supportedGrowiActionsRegExps = getSupportedGrowiActionsRegExps(supportedCommands);
 
     // get command name from req.body
     let command = '';
@@ -82,35 +81,36 @@ module.exports = (crowi) => {
 
     // code below checks permission at channel level
     const fromChannel = req.body.channel_name || payload.channel.name;
-    [...channelsObject.keys()].forEach((commandName) => {
-      const permittedChannels = channelsObject.get(commandName);
-      // ex. search OR search:hogehoge
+    let isPermitted = false;
+    [...permissionsForBroadcastUseCommands.keys(), ...permissionsForSingleUseCommands.keys()].forEach((commandName) => {
+      // boolean or string[]
+      let permission = permissionsForBroadcastUseCommands.get(commandName);
+      if (permission === undefined) {
+        permission = permissionsForSingleUseCommands.get(commandName);
+      }
+
+      // ex. search OR search:handlerName
       const commandRegExp = new RegExp(`(^${commandName}$)|(^${commandName}:\\w+)`);
 
-      // RegExp check
-      if (commandRegExp.test(commandName) || commandRegExp.test(actionId) || commandRegExp.test(callbackId)) {
-        // check if the channel is permitted
-        if (permittedChannels.includes(fromChannel)) return next();
+      // skip this forEach loop if the requested command is not in permissionsForBroadcastUseCommands key
+      if (!commandRegExp.test(command) && !commandRegExp.test(actionId) && !commandRegExp.test(callbackId)) {
+        return;
       }
-    });
 
-    // code below checks permission at command level
-    let isActionSupported = false;
-    supportedGrowiActionsRegExps.forEach((regexp) => {
-      if (regexp.test(actionId) || regexp.test(callbackId)) {
-        isActionSupported = true;
+      // permission check
+      if (permission === true) {
+        isPermitted = true;
+        return;
+      }
+      if (Array.isArray(permission) && permission.includes(fromChannel)) {
+        isPermitted = true;
       }
     });
 
-    // validate
-    if (command && !supportedCommands.includes(command)) {
-      return res.status(403).send(`It is not allowed to run '${command}' command to this GROWI.`);
-    }
-    if ((actionId || callbackId) && !isActionSupported) {
-      return res.status(403).send(`It is not allowed to run '${command}' command to this GROWI.`);
+    if (isPermitted) {
+      return next();
     }
-
-    next();
+    res.status(403).send(`It is not allowed to run '${command}' command to this GROWI.`);
   }
 
   const addSigningSecretToReq = (req, res, next) => {

+ 0 - 6
packages/slackbot-proxy/src/entities/relation-mock.ts

@@ -4,12 +4,6 @@ import {
 } from 'typeorm';
 import { Installation } from './installation';
 
-
-// expected data see below
-//   commandToChannelMap: {
-//     create: ['srv', 'admin'],
-//     search: ['admin'],
-//   }
 interface PermissionSettingsInterface {
   [commandName: string]: boolean | string[],
 }