zahmis 4 лет назад
Родитель
Сommit
f0e0984882

+ 3 - 2
packages/app/src/server/service/slack-integration.ts

@@ -128,9 +128,10 @@ export class SlackIntegrationService implements S2sMessageHandlable {
   async generateClientByTokenPtoG(tokenPtoG: string): Promise<WebClient> {
     this.isCheckTypeValid();
 
-    const SlackAppIntegration = mongoose.model('SlackAppIntegration');
+    // const SlackAppIntegration = mongoose.model('SlackAppIntegration');
+    const SlackAppIntegrationMock = mongoose.model('SlackAppIntegrationMock');
 
-    const slackAppIntegration = await SlackAppIntegration.findOne({ tokenPtoG });
+    const slackAppIntegration = await SlackAppIntegrationMock.findOne({ tokenPtoG });
 
     if (slackAppIntegration == null) {
       throw new Error('No SlackAppIntegration exists that corresponds to the tokenPtoG specified.');

+ 29 - 1
packages/slackbot-proxy/src/controllers/slack.ts

@@ -330,8 +330,36 @@ export class SlackCtrl {
 
 
     await Promise.all(relations.map(async(relation) => {
-      await this.relationsService.checkPermissionForInteractions(relation, channelName, callbackId, actionId);
+      const permission = await this.relationsService.checkPermissionForInteractions(relation, channelName, callbackId, actionId);
+      const { isPermittedForInteractions, notAllowedCommandName } = permission;
 
+      if (!isPermittedForInteractions) {
+        // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+        const client = generateWebClient(authorizeResult.botToken!);
+
+        //   const linkUrlList = Array.from(disallowedGrowiUrls).map((growiUrl) => {
+        //     return '\n'
+        // + `• ${new URL('/admin/slack-integration', growiUrl).toString()}`;
+        //   });
+
+        const growiDocsLink = 'https://docs.growi.org/en/admin-guide/upgrading/43x.html';
+
+        return client.chat.postEphemeral({
+          text: 'Error occured.',
+          channel: body.channel_id,
+          user: body.user_id,
+          blocks: [
+            markdownSectionBlock('*None of GROWI permitted the command.*'),
+            markdownSectionBlock(`*'${notAllowedCommandName}'* command was not allowed.`),
+            // markdownSectionBlock(
+            //   `To use this command, modify settings from following pages: ${linkUrlList}`,
+            // ),
+            markdownSectionBlock(
+              `Or, if your GROWI version is 4.3.0 or below, upgrade GROWI to use commands and permission settings: ${growiDocsLink}`,
+            ),
+          ],
+        });
+      }
       /*
        * forward to GROWI server
        */

+ 18 - 17
packages/slackbot-proxy/src/services/RelationsService.ts

@@ -4,8 +4,7 @@ import axios from 'axios';
 import { addHours } from 'date-fns';
 
 // import { Relation } from '~/entities/relation';
-import { markdownSectionBlock, generateWebClient, REQUEST_TIMEOUT_FOR_PTOG } from '@growi/slack';
-import { ChatPostEphemeralResponse } from '@slack/web-api';
+import { REQUEST_TIMEOUT_FOR_PTOG } from '@growi/slack';
 import { RelationMock } from '~/entities/relation-mock';
 // import { RelationRepository } from '~/repositories/relation';
 import { RelationMockRepository } from '~/repositories/relation-mock';
@@ -127,15 +126,16 @@ export class RelationsService {
   async checkPermissionForInteractions(
       // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
       relation:RelationMock, channelName:string, callbackId:string, actionId:string,
-  ):Promise<void> {
+  ):Promise<{isPermittedForInteractions:boolean, notAllowedCommandName:string|null}> {
 
     const baseDate = new Date();
     const syncedRelation = await this.syncRelation(relation, baseDate);
-    let isPermittedForInteractions = false;
+
+    let isPermittedForInteractions!:boolean;
+    let notAllowedCommandName!:null|string;
 
     if (syncedRelation == null) {
-      isPermittedForInteractions = false;
-      return;
+      return { isPermittedForInteractions: false, notAllowedCommandName: null };
     }
 
     const singleUse = Object.keys(relation.permissionsForSingleUseCommands);
@@ -144,6 +144,14 @@ export class RelationsService {
 
     [...singleUse, ...broadCastUse].forEach(async(commandName) => {
 
+      // ex. search OR search:handlerName
+      const commandRegExp = new RegExp(`(^${commandName}$)|(^${commandName}:\\w+)`);
+
+      // skip this forEach loop if the requested command is not in permissionsForBroadcastUseCommands and permissionsForSingleUseCommands
+      if (!commandRegExp.test(actionId) && !commandRegExp.test(callbackId)) {
+        return;
+      }
+
       // case: singleUse
       permissionForInteractions = relation.permissionsForSingleUseCommands[commandName];
 
@@ -159,22 +167,15 @@ export class RelationsService {
 
       // check permission at channel level
       if (Array.isArray(permissionForInteractions)) {
-        isPermittedForInteractions = permissionForInteractions.includes(channelName);
+        isPermittedForInteractions = true;
         return;
       }
 
-      // ex. search OR search:handlerName
-      const commandRegExp = new RegExp(`(^${commandName}$)|(^${commandName}:\\w+)`);
+      notAllowedCommandName = commandName;
 
-      // skip this forEach loop if the requested command is not in permissionsForBroadcastUseCommands and permissionsForSingleUseCommands
-      if (!commandRegExp.test(actionId) && !commandRegExp.test(callbackId)) {
-        return;
-      }
-
-      // if (!isPermittedForInteractions) {
-      //   this.postNotAllowedMessage(relations, commandName, body);
-      // }
     });
+
+    return { isPermittedForInteractions, notAllowedCommandName };
   }
 
 }