{"version":3,"file":"permissions.mjs","names":["currentUserPermissions: UserPermissions | undefined"],"sources":["../../src/helpers/permissions/index.ts"],"sourcesContent":["import { getCurrentPermissions } from '~/modules/auth/methods';\nimport type { UserPermissions } from '~/modules/auth/types';\n\nimport { isSDKFunction, type LambdaFunction } from './decorator';\n\nlet currentUserPermissions: UserPermissions | undefined;\n\n/**\n * Refresh current user permissions\n */\nexport async function refreshPermissions(): Promise<void> {\n  currentUserPermissions = await getCurrentPermissions();\n}\n\n/**\n * Check if the user have the permission to use the function\n *\n * @param fnc The function\n * @param namespaceId The namespaces where the function can be used, check if at least one is true\n *\n * @returns If the user have the permission\n */\nexport function hasPermission(\n  fnc: LambdaFunction,\n  namespaceIds?: string[]\n): boolean {\n  if (!isSDKFunction(fnc)) {\n    // oxlint-disable-next-line no-console\n    console.warn(\n      `[ezr][hasPermission] Function ${fnc.name} is not compatible with ezrPermission decorator`\n    );\n    return true;\n  }\n\n  if (!currentUserPermissions) {\n    // oxlint-disable-next-line no-console\n    console.warn(\n      '[ezr][hasPermission] No current permissions set, please call refreshPermissions() first'\n    );\n    return false;\n  }\n\n  const { general, namespaces } = currentUserPermissions;\n  return fnc.ezrPermissions.every(({ permission, isNamespaced }) => {\n    if (!isNamespaced) {\n      return general[permission] || false;\n    }\n\n    // If no namespace provided, check if at least one is true\n    const nIds = namespaceIds ?? Object.keys(namespaces);\n\n    return nIds.some((id) => namespaces[id]?.[permission] || false);\n  });\n}\n"],"mappings":";;;;;;AAKA,IAAIA;;;;AAKJ,SAAsB;;;;sDAAoC;AACxD,iCAA+B,uBAAuB;;;;;;;;;;;;AAWxD,SAAgB,cACd,KACA,cACS;AACT,KAAI,CAAC,cAAc,IAAI,EAAE;AAEvB,UAAQ,KACN,iCAAiC,IAAI,KAAK,iDAC3C;AACD,SAAO;;AAGT,KAAI,CAAC,wBAAwB;AAE3B,UAAQ,KACN,0FACD;AACD,SAAO;;CAGT,MAAM,EAAE,SAAS,eAAe;AAChC,QAAO,IAAI,eAAe,OAAO,EAAE,YAAY,mBAAmB;AAChE,MAAI,CAAC,aACH,QAAO,QAAQ,eAAe;AAMhC,UAFa,kEAAgB,OAAO,KAAK,WAAW,EAExC,MAAM,OAAO;;wCAAW,qEAAM,gBAAe;IAAM;GAC/D"}