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

fix(Slackbot): Slash commands response when sent from disabled channels (#4754)

* wip

* fix, wip util func

* whitespace revert

* fix
stevenfukase 4 лет назад
Родитель
Сommit
73f0d4ecf1

+ 3 - 21
packages/app/src/server/util/slack-integration.ts

@@ -1,10 +1,10 @@
-import { getSupportedGrowiActionsRegExp, IChannelOptionalId } from '@growi/slack';
+import { getSupportedGrowiActionsRegExp, IChannelOptionalId, permissionParser } from '@growi/slack';
 
 
 type CommandPermission = { [key:string]: string[] | boolean }
 type CommandPermission = { [key:string]: string[] | boolean }
 
 
 export const checkPermission = (
 export const checkPermission = (
     commandPermission: CommandPermission, commandOrActionIdOrCallbackId: string, fromChannel: IChannelOptionalId,
     commandPermission: CommandPermission, commandOrActionIdOrCallbackId: string, fromChannel: IChannelOptionalId,
-):boolean => {
+): boolean => {
   let isPermitted = false;
   let isPermitted = false;
 
 
   // help
   // help
@@ -18,25 +18,7 @@ export const checkPermission = (
     const commandRegExp = getSupportedGrowiActionsRegExp(command);
     const commandRegExp = getSupportedGrowiActionsRegExp(command);
     if (!commandRegExp.test(commandOrActionIdOrCallbackId)) return;
     if (!commandRegExp.test(commandOrActionIdOrCallbackId)) return;
 
 
-    // permission check
-    if (permission === true) {
-      isPermitted = true;
-      return;
-    }
-
-    if (Array.isArray(permission)) {
-      if (permission.includes(fromChannel.name)) {
-        isPermitted = true;
-        return;
-      }
-
-      if (fromChannel.id == null) return;
-
-      if (permission.includes(fromChannel.id)) {
-        isPermitted = true;
-        return;
-      }
-    }
+    isPermitted = permissionParser(permission, fromChannel);
   });
   });
 
 
   return isPermitted;
   return isPermitted;

+ 1 - 0
packages/slack/src/index.ts

@@ -52,6 +52,7 @@ export * from './utils/response-url';
 export * from './utils/slash-command-parser';
 export * from './utils/slash-command-parser';
 export * from './utils/webclient-factory';
 export * from './utils/webclient-factory';
 export * from './utils/required-scopes';
 export * from './utils/required-scopes';
+export * from './utils/permission-parser';
 export * from './utils/interaction-payload-accessor';
 export * from './utils/interaction-payload-accessor';
 export * from './utils/payload-interaction-id-helpers';
 export * from './utils/payload-interaction-id-helpers';
 export * from './utils/respond-util-factory';
 export * from './utils/respond-util-factory';

+ 29 - 0
packages/slack/src/utils/permission-parser.ts

@@ -0,0 +1,29 @@
+import { IChannelOptionalId } from '../interfaces/channel';
+
+
+export const permissionParser = (permissionForCommand: boolean | string[], channel: IChannelOptionalId): boolean => {
+
+  if (permissionForCommand == null) {
+    return false;
+  }
+
+  if (permissionForCommand === true) {
+    return true;
+  }
+
+  if (Array.isArray(permissionForCommand)) {
+    if (permissionForCommand.includes(channel.name)) {
+      return true;
+    }
+
+    if (channel.id == null) {
+      return false;
+    }
+
+    if (permissionForCommand.includes(channel.id)) {
+      return true;
+    }
+  }
+
+  return false;
+};

+ 4 - 20
packages/slackbot-proxy/src/services/RelationsService.ts

@@ -3,7 +3,9 @@ import { Inject, Service } from '@tsed/di';
 import axios from 'axios';
 import axios from 'axios';
 import { addHours } from 'date-fns';
 import { addHours } from 'date-fns';
 
 
-import { REQUEST_TIMEOUT_FOR_PTOG, getSupportedGrowiActionsRegExp, IChannelOptionalId } from '@growi/slack';
+import {
+  REQUEST_TIMEOUT_FOR_PTOG, getSupportedGrowiActionsRegExp, IChannelOptionalId, permissionParser,
+} from '@growi/slack';
 import { Relation, PermissionSettingsInterface } from '~/entities/relation';
 import { Relation, PermissionSettingsInterface } from '~/entities/relation';
 import { RelationRepository } from '~/repositories/relation';
 import { RelationRepository } from '~/repositories/relation';
 
 
@@ -82,25 +84,7 @@ export class RelationsService {
 
 
     const permissionForCommand = permissionSettings[growiCommandType];
     const permissionForCommand = permissionSettings[growiCommandType];
 
 
-    if (permissionForCommand == null) {
-      return false;
-    }
-
-    if (Array.isArray(permissionForCommand)) {
-      if (permissionForCommand.includes(channel.name)) {
-        return true;
-      }
-
-      if (channel.id == null) {
-        return false;
-      }
-
-      if (permissionForCommand.includes(channel.id)) {
-        return true;
-      }
-    }
-    return permissionForCommand as boolean;
-
+    return permissionParser(permissionForCommand, channel);
   }
   }
 
 
   async isPermissionsForSingleUseCommands(relation: Relation, growiCommandType: string, channel: IChannelOptionalId): Promise<boolean> {
   async isPermissionsForSingleUseCommands(relation: Relation, growiCommandType: string, channel: IChannelOptionalId): Promise<boolean> {