slack-integration.ts 933 B

123456789101112131415161718192021222324252627
  1. import type { IChannelOptionalId } from '@growi/slack';
  2. import { getSupportedGrowiActionsRegExp } from '@growi/slack/dist/utils/get-supported-growi-actions-regexps';
  3. import { permissionParser } from '@growi/slack/dist/utils/permission-parser';
  4. type CommandPermission = { [key:string]: string[] | boolean }
  5. export const checkPermission = (
  6. commandPermission: CommandPermission, commandOrActionIdOrCallbackId: string, fromChannel: IChannelOptionalId,
  7. ): boolean => {
  8. let isPermitted = false;
  9. // help
  10. if (commandOrActionIdOrCallbackId === 'help') {
  11. return true;
  12. }
  13. Object.entries(commandPermission).forEach((entry) => {
  14. const [command, value] = entry;
  15. const permission = value;
  16. const commandRegExp = getSupportedGrowiActionsRegExp(command);
  17. if (!commandRegExp.test(commandOrActionIdOrCallbackId)) return;
  18. isPermitted = permissionParser(permission, fromChannel);
  19. });
  20. return isPermitted;
  21. };