slack-integration.ts 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import { getSupportedGrowiActionsRegExp, IChannelOptionalId } from '@growi/slack';
  2. type CommandPermission = { [key:string]: string[] | boolean }
  3. export const checkPermission = (
  4. commandPermission: CommandPermission, commandOrActionIdOrCallbackId: string, fromChannel: IChannelOptionalId,
  5. ):boolean => {
  6. let isPermitted = false;
  7. // help
  8. if (commandOrActionIdOrCallbackId === 'help') {
  9. return true;
  10. }
  11. Object.entries(commandPermission).forEach((entry) => {
  12. const [command, value] = entry;
  13. const permission = value;
  14. const commandRegExp = getSupportedGrowiActionsRegExp(command);
  15. if (!commandRegExp.test(commandOrActionIdOrCallbackId)) return;
  16. // permission check
  17. if (permission === true) {
  18. isPermitted = true;
  19. return;
  20. }
  21. if (Array.isArray(permission)) {
  22. if (permission.includes(fromChannel.name)) {
  23. isPermitted = true;
  24. return;
  25. }
  26. if (fromChannel.id == null) return;
  27. if (permission.includes(fromChannel.id)) {
  28. isPermitted = true;
  29. return;
  30. }
  31. }
  32. });
  33. return isPermitted;
  34. };