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

+ 32 - 6
packages/slackbot-proxy/src/controllers/slack.ts

@@ -335,26 +335,52 @@ export class SlackCtrl {
     }
     }
 
 
 
 
+    const allowedRelations:RelationMock[] = [];
+    const disallowedGrowiUrls: Set<string> = new Set();
+    let notAllowedCommandName!:string;
     const actionId:string = payload?.actions?.[0].action_id;
     const actionId:string = payload?.actions?.[0].action_id;
+
     await Promise.all(relations.map(async(relation) => {
     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, commandName } = permission;
 
 
+      if (!isPermittedForInteractions) {
+        disallowedGrowiUrls.add(relation.growiUri);
+        notAllowedCommandName = commandName;
+      }
 
 
-    const disallowedGrowiUrls = this.relationsService.getDisallowedGrowiUrls();
-    const commandName = this.relationsService.getCommandName();
+      allowedRelations.push(relation);
+    }));
 
 
     if (relations.length === disallowedGrowiUrls.size) {
     if (relations.length === disallowedGrowiUrls.size) {
       // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
       // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
       const client = generateWebClient(authorizeResult.botToken!);
       const client = generateWebClient(authorizeResult.botToken!);
-      return postNotAllowedMessage(client, body, disallowedGrowiUrls, commandName);
+      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
      * forward to GROWI server
      */
      */
 
 
-    const allowedRelations = this.relationsService.getAllowedRelations();
     allowedRelations.map(async(relation) => {
     allowedRelations.map(async(relation) => {
       try {
       try {
         // generate API URL
         // generate API URL

+ 7 - 32
packages/slackbot-proxy/src/services/RelationsService.ts

@@ -107,73 +107,48 @@ export class RelationsService {
   }
   }
 
 
 
 
-  allowedRelations:RelationMock[] = [];
-
-  getAllowedRelations():RelationMock[] {
-    return this.allowedRelations;
-  }
-
-  disallowedGrowiUrls: Set<string> = new Set();
-
-  getDisallowedGrowiUrls():Set<string> {
-    return this.disallowedGrowiUrls;
-  }
-
-  commandName:string;
-
-  getCommandName():string {
-    return this.commandName;
-  }
-
-
   async checkPermissionForInteractions(
   async checkPermissionForInteractions(
       // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
       // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
       relation:RelationMock, channelName:string, callbackId:string, actionId:string,
       relation:RelationMock, channelName:string, callbackId:string, actionId:string,
-  ):Promise<void> {
+  ):Promise<{isPermittedForInteractions:boolean, commandName:string}> {
+
+    let isPermittedForInteractions!:boolean;
+    let commandName!:string;
 
 
     const singleUse = Object.keys(relation.permissionsForSingleUseCommands);
     const singleUse = Object.keys(relation.permissionsForSingleUseCommands);
     const broadCastUse = Object.keys(relation.permissionsForBroadcastUseCommands);
     const broadCastUse = Object.keys(relation.permissionsForBroadcastUseCommands);
     let permissionForInteractions:boolean|string[];
     let permissionForInteractions:boolean|string[];
-    let isPermittedForInteractions!:boolean;
 
 
     [...singleUse, ...broadCastUse].forEach(async(tempCommandName) => {
     [...singleUse, ...broadCastUse].forEach(async(tempCommandName) => {
 
 
       // ex. search OR search:handlerName
       // ex. search OR search:handlerName
       const commandRegExp = new RegExp(`(^${tempCommandName}$)|(^${tempCommandName}:\\w+)`);
       const commandRegExp = new RegExp(`(^${tempCommandName}$)|(^${tempCommandName}:\\w+)`);
-
       // skip this forEach loop if the requested command is not in permissionsForBroadcastUseCommands and permissionsForSingleUseCommands
       // skip this forEach loop if the requested command is not in permissionsForBroadcastUseCommands and permissionsForSingleUseCommands
       if (!commandRegExp.test(actionId) && !commandRegExp.test(callbackId)) {
       if (!commandRegExp.test(actionId) && !commandRegExp.test(callbackId)) {
         return;
         return;
       }
       }
 
 
-      this.commandName = tempCommandName;
+      commandName = tempCommandName;
 
 
       // case: singleUse
       // case: singleUse
       permissionForInteractions = relation.permissionsForSingleUseCommands[tempCommandName];
       permissionForInteractions = relation.permissionsForSingleUseCommands[tempCommandName];
-
       // case: broadcastUse
       // case: broadcastUse
       if (permissionForInteractions == null) {
       if (permissionForInteractions == null) {
         permissionForInteractions = relation.permissionsForBroadcastUseCommands[tempCommandName];
         permissionForInteractions = relation.permissionsForBroadcastUseCommands[tempCommandName];
       }
       }
-
-      isPermittedForInteractions = false;
       if (permissionForInteractions === true) {
       if (permissionForInteractions === true) {
         isPermittedForInteractions = true;
         isPermittedForInteractions = true;
         return;
         return;
       }
       }
-
       // check permission at channel level
       // check permission at channel level
-      if (Array.isArray(permissionForInteractions) && permissionForInteractions.includes(channelName)) {
+      if (Array.isArray(permissionForInteractions)) {
         isPermittedForInteractions = true;
         isPermittedForInteractions = true;
         return;
         return;
       }
       }
     });
     });
 
 
-    if (!isPermittedForInteractions) {
-      this.disallowedGrowiUrls.add(relation.growiUri);
-    }
 
 
-    this.allowedRelations.push(relation);
+    return { isPermittedForInteractions, commandName };
   }
   }
 
 
 }
 }