@vivoa/plugin-sdk 0.6.5 → 0.6.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,37 @@
1
+ // src/permissions.ts
2
+ var PERMISSION_RE = /^([a-z0-9]+)\.([a-z0-9]+)(?::(.+))?$/i;
3
+ function parsePermission(raw) {
4
+ const m = PERMISSION_RE.exec(raw);
5
+ if (!m) return null;
6
+ const [, domain, action, resource] = m;
7
+ return resource === void 0 ? { domain, action } : { domain, action, resource };
8
+ }
9
+ function isPermissionGranted(required, granted) {
10
+ return granted.some((g) => {
11
+ if (g.domain !== required.domain || g.action !== required.action) {
12
+ return false;
13
+ }
14
+ if (required.resource === void 0) return true;
15
+ return g.resource === "*" || g.resource === required.resource;
16
+ });
17
+ }
18
+ function checkPermissions(required, grantedRaw) {
19
+ const grantedParsed = grantedRaw.map(parsePermission).filter((p) => p !== null);
20
+ const granted = [];
21
+ const missing = [];
22
+ for (const raw of required) {
23
+ const parsed = parsePermission(raw);
24
+ if (parsed && isPermissionGranted(parsed, grantedParsed)) {
25
+ granted.push(raw);
26
+ } else {
27
+ missing.push(raw);
28
+ }
29
+ }
30
+ return { ok: missing.length === 0, granted, missing };
31
+ }
32
+
33
+ export {
34
+ parsePermission,
35
+ isPermissionGranted,
36
+ checkPermissions
37
+ };