UNPKG

better-auth

Version:

The most comprehensive authentication framework for TypeScript.

1 lines 1.94 kB
{"version":3,"file":"permission.mjs","names":[],"sources":["../../../src/plugins/organization/permission.ts"],"sourcesContent":["import type { Role } from \"../access\";\nimport type { OrganizationOptions } from \"./types\";\n\nexport const hasPermissionFn = (\n\tinput: HasPermissionBaseInput,\n\tacRoles: {\n\t\t[x: string]: Role<any> | undefined;\n\t},\n) => {\n\tif (!input.permissions && !input.permission) return false;\n\n\tconst roles = input.role.split(\",\");\n\tconst creatorRole = input.options.creatorRole || \"owner\";\n\tconst isCreator = roles.includes(creatorRole);\n\n\tconst allowCreatorsAllPermissions = input.allowCreatorAllPermissions || false;\n\tif (isCreator && allowCreatorsAllPermissions) return true;\n\n\tfor (const role of roles) {\n\t\tconst _role = acRoles[role as keyof typeof acRoles];\n\t\tconst result = _role?.authorize(input.permissions ?? input.permission);\n\t\tif (result?.success) {\n\t\t\treturn true;\n\t\t}\n\t}\n\treturn false;\n};\n\nexport type PermissionExclusive =\n\t| {\n\t\t\t/**\n\t\t\t * @deprecated Use `permissions` instead\n\t\t\t */\n\t\t\tpermission: { [key: string]: string[] };\n\t\t\tpermissions?: never | undefined;\n\t }\n\t| {\n\t\t\tpermissions: { [key: string]: string[] };\n\t\t\tpermission?: never | undefined;\n\t };\n\nexport let cacheAllRoles = new Map<\n\tstring,\n\t{\n\t\t[x: string]: Role<any> | undefined;\n\t}\n>();\n\nexport type HasPermissionBaseInput = {\n\trole: string;\n\toptions: OrganizationOptions;\n\tallowCreatorAllPermissions?: boolean | undefined;\n} & PermissionExclusive;\n"],"mappings":";AAGA,MAAa,mBACZ,OACA,YAGI;AACJ,KAAI,CAAC,MAAM,eAAe,CAAC,MAAM,WAAY,QAAO;CAEpD,MAAM,QAAQ,MAAM,KAAK,MAAM,IAAI;CACnC,MAAM,cAAc,MAAM,QAAQ,eAAe;CACjD,MAAM,YAAY,MAAM,SAAS,YAAY;CAE7C,MAAM,8BAA8B,MAAM,8BAA8B;AACxE,KAAI,aAAa,4BAA6B,QAAO;AAErD,MAAK,MAAM,QAAQ,MAGlB,MAFc,QAAQ,OACA,UAAU,MAAM,eAAe,MAAM,WAAW,GAC1D,QACX,QAAO;AAGT,QAAO;;AAgBR,IAAW,gCAAgB,IAAI,KAK5B"}