qarpeo 0.3.5 → 0.4.1
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.
- package/dist/chunks/{tools.mjs → manifest.get.mjs} +229 -3
- package/dist/cli/index.mjs +74 -5
- package/dist/features/console.d.ts +4 -0
- package/dist/module.mjs +299 -57
- package/dist/runtime/app/components/{BlobImage.vue → QImg.vue} +5 -4
- package/dist/runtime/app/composables/console.d.ts +60 -0
- package/dist/runtime/app/composables/console.mjs +169 -0
- package/dist/runtime/app/composables/pwa.d.ts +34 -0
- package/dist/runtime/app/composables/pwa.mjs +86 -0
- package/dist/runtime/app/composables/rbac.d.ts +33 -0
- package/dist/runtime/app/composables/rbac.mjs +60 -0
- package/dist/runtime/app/composables/site.d.ts +73 -0
- package/dist/runtime/app/composables/site.mjs +174 -1
- package/dist/runtime/app/middleware/console.global.d.ts +2 -0
- package/dist/runtime/app/middleware/console.global.mjs +37 -0
- package/dist/runtime/app/plugins/pwa.client.d.ts +2 -0
- package/dist/runtime/app/plugins/pwa.client.mjs +28 -0
- package/dist/runtime/app/plugins/pwa.d.ts +2 -0
- package/dist/runtime/app/plugins/pwa.mjs +42 -0
- package/dist/runtime/app/plugins/seo.mjs +39 -10
- package/dist/runtime/schema.d.ts +32 -12
- package/dist/runtime/schema.mjs +47 -125
- package/dist/runtime/server/routes/__qarpeo/cloud/agents/run.post.d.ts +13 -0
- package/dist/runtime/server/routes/__qarpeo/cloud/agents/run.post.mjs +46 -0
- package/dist/runtime/server/routes/__qarpeo/cloud/cms/entry.get.d.ts +22 -0
- package/dist/runtime/server/routes/__qarpeo/cloud/cms/entry.get.mjs +40 -0
- package/dist/runtime/server/routes/__qarpeo/cloud/cms/entry.post.d.ts +20 -0
- package/dist/runtime/server/routes/__qarpeo/cloud/cms/entry.post.mjs +52 -0
- package/dist/runtime/server/routes/__qarpeo/cloud/cms.get.d.ts +26 -0
- package/dist/runtime/server/routes/__qarpeo/cloud/cms.get.mjs +80 -0
- package/dist/runtime/server/routes/__qarpeo/cloud/comments/moderate.post.d.ts +16 -0
- package/dist/runtime/server/routes/__qarpeo/cloud/comments/moderate.post.mjs +30 -0
- package/dist/runtime/server/routes/__qarpeo/cloud/comments.get.d.ts +21 -0
- package/dist/runtime/server/routes/__qarpeo/cloud/comments.get.mjs +49 -0
- package/dist/runtime/server/routes/__qarpeo/cloud/extras.get.d.ts +22 -0
- package/dist/runtime/server/routes/__qarpeo/cloud/extras.get.mjs +65 -0
- package/dist/runtime/server/routes/__qarpeo/cloud/mail/unsuppress.post.d.ts +12 -0
- package/dist/runtime/server/routes/__qarpeo/cloud/mail/unsuppress.post.mjs +17 -0
- package/dist/runtime/server/routes/__qarpeo/cloud/migrate/cms.post.d.ts +22 -0
- package/dist/runtime/server/routes/__qarpeo/cloud/migrate/cms.post.mjs +87 -0
- package/dist/runtime/server/routes/__qarpeo/cloud/migrate/products.post.d.ts +25 -0
- package/dist/runtime/server/routes/__qarpeo/cloud/migrate/products.post.mjs +119 -0
- package/dist/runtime/server/routes/__qarpeo/cloud/migrate/users.post.d.ts +19 -0
- package/dist/runtime/server/routes/__qarpeo/cloud/migrate/users.post.mjs +86 -0
- package/dist/runtime/server/routes/__qarpeo/cloud/migrate.post.d.ts +21 -0
- package/dist/runtime/server/routes/__qarpeo/cloud/migrate.post.mjs +91 -0
- package/dist/runtime/server/routes/__qarpeo/cloud/notifications/test.post.d.ts +13 -0
- package/dist/runtime/server/routes/__qarpeo/cloud/notifications/test.post.mjs +49 -0
- package/dist/runtime/server/routes/__qarpeo/cloud/operations.get.d.ts +43 -0
- package/dist/runtime/server/routes/__qarpeo/cloud/operations.get.mjs +111 -0
- package/dist/runtime/server/routes/__qarpeo/cloud/overview.get.d.ts +22 -0
- package/dist/runtime/server/routes/__qarpeo/cloud/overview.get.mjs +44 -0
- package/dist/runtime/server/routes/__qarpeo/cloud/store/order-status.post.d.ts +9 -0
- package/dist/runtime/server/routes/__qarpeo/cloud/store/order-status.post.mjs +27 -0
- package/dist/runtime/server/routes/__qarpeo/cloud/store/orders.get.d.ts +23 -0
- package/dist/runtime/server/routes/__qarpeo/cloud/store/orders.get.mjs +56 -0
- package/dist/runtime/server/routes/__qarpeo/cloud/store/products.get.d.ts +21 -0
- package/dist/runtime/server/routes/__qarpeo/cloud/store/products.get.mjs +50 -0
- package/dist/runtime/server/routes/__qarpeo/cloud/tasks/run.post.d.ts +12 -0
- package/dist/runtime/server/routes/__qarpeo/cloud/tasks/run.post.mjs +24 -0
- package/dist/runtime/server/routes/__qarpeo/cloud/users/action.post.d.ts +26 -0
- package/dist/runtime/server/routes/__qarpeo/cloud/users/action.post.mjs +40 -0
- package/dist/runtime/server/routes/__qarpeo/cloud/users.get.d.ts +39 -0
- package/dist/runtime/server/routes/__qarpeo/cloud/users.get.mjs +69 -0
- package/dist/runtime/server/routes/__qarpeo/manifest.get.d.ts +39 -0
- package/dist/runtime/server/routes/__qarpeo/manifest.get.mjs +226 -0
- package/dist/runtime/server/routes/__qarpeo/og.get.d.ts +15 -0
- package/dist/runtime/server/routes/__qarpeo/og.get.mjs +109 -0
- package/dist/runtime/server/routes/__qarpeo/rbac/me.get.d.ts +26 -0
- package/dist/runtime/server/routes/__qarpeo/rbac/me.get.mjs +23 -0
- package/dist/runtime/server/routes/__qarpeo/rbac/roles.delete.d.ts +12 -0
- package/dist/runtime/server/routes/__qarpeo/rbac/roles.delete.mjs +12 -0
- package/dist/runtime/server/routes/__qarpeo/rbac/roles.get.d.ts +16 -0
- package/dist/runtime/server/routes/__qarpeo/rbac/roles.get.mjs +15 -0
- package/dist/runtime/server/routes/__qarpeo/rbac/roles.post.d.ts +11 -0
- package/dist/runtime/server/routes/__qarpeo/rbac/roles.post.mjs +16 -0
- package/dist/runtime/server/routes/__qarpeo/rbac/users.post.d.ts +14 -0
- package/dist/runtime/server/routes/__qarpeo/rbac/users.post.mjs +19 -0
- package/dist/runtime/server/routes/manifest.webmanifest.get.d.ts +2 -0
- package/dist/runtime/server/routes/manifest.webmanifest.get.mjs +31 -0
- package/dist/runtime/server/routes/robots.txt.get.mjs +20 -4
- package/dist/runtime/server/routes/sitemap.xml.get.mjs +47 -6
- package/dist/runtime/server/routes/sw.js.get.d.ts +8 -0
- package/dist/runtime/server/routes/sw.js.get.mjs +141 -0
- package/dist/runtime/server/utils/auth.d.ts +2 -14
- package/dist/runtime/server/utils/auth.mjs +32 -36
- package/dist/runtime/server/utils/cloud-auth.d.ts +8 -0
- package/dist/runtime/server/utils/cloud-auth.mjs +23 -0
- package/dist/runtime/server/utils/console.d.ts +19 -0
- package/dist/runtime/server/utils/console.mjs +34 -0
- package/dist/runtime/server/utils/legacy-hash.d.ts +76 -0
- package/dist/runtime/server/utils/legacy-hash.mjs +96 -0
- package/dist/runtime/server/utils/rbac.d.ts +89 -0
- package/dist/runtime/server/utils/rbac.mjs +427 -0
- package/dist/runtime/server/utils/role-env.d.ts +19 -0
- package/dist/runtime/server/utils/role-env.mjs +34 -0
- package/dist/runtime/server.d.ts +2 -0
- package/dist/runtime/server.mjs +2 -0
- package/dist/types/console.d.ts +67 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/options.d.ts +9 -0
- package/dist/types/site.d.ts +102 -0
- package/dist/types/storage.d.ts +5 -11
- package/package.json +1 -1
- package/dist/runtime/providers/qarpeo.d.ts +0 -23
- package/dist/runtime/providers/qarpeo.mjs +0 -46
|
@@ -0,0 +1,427 @@
|
|
|
1
|
+
import { createError } from "nuxt/server";
|
|
2
|
+
import { getD1Binding } from "../../index.mjs";
|
|
3
|
+
import { getRoleFromEnv } from "./role-env.mjs";
|
|
4
|
+
import { getAuthContext } from "./context.mjs";
|
|
5
|
+
import { resolveTableName } from "./table-names.mjs";
|
|
6
|
+
export const PERMISSION_CATALOG = [
|
|
7
|
+
{ key: "console:access", module: "console", description: "Access the /console workspace" },
|
|
8
|
+
{ key: "cms:read", module: "cms", description: "View CMS collections and entries" },
|
|
9
|
+
{ key: "cms:write", module: "cms", description: "Create and edit CMS drafts" },
|
|
10
|
+
{ key: "cms:publish", module: "cms", description: "Publish or unpublish CMS entries" },
|
|
11
|
+
{ key: "cms:delete", module: "cms", description: "Delete CMS entries" },
|
|
12
|
+
{ key: "store:read", module: "store", description: "View store products and orders" },
|
|
13
|
+
{ key: "store:products.write", module: "store", description: "Create and edit products/variants" },
|
|
14
|
+
{ key: "store:orders.manage", module: "store", description: "Fulfill and update order status" },
|
|
15
|
+
{ key: "store:refunds", module: "store", description: "Issue order refunds" },
|
|
16
|
+
{ key: "comments:read", module: "comments", description: "View comments and moderation queue" },
|
|
17
|
+
{ key: "comments:moderate", module: "comments", description: "Approve, mark spam, or delete comments" },
|
|
18
|
+
{ key: "media:upload", module: "media", description: "Upload files to R2 media library" },
|
|
19
|
+
{ key: "media:delete", module: "media", description: "Delete files from R2 media library" },
|
|
20
|
+
{ key: "users:read", module: "users", description: "View users and staff list" },
|
|
21
|
+
{ key: "users:manage", module: "users", description: "Ban/unban users, revoke sessions, assign roles" },
|
|
22
|
+
{ key: "roles:manage", module: "roles", description: "Create/edit custom roles and user permissions" }
|
|
23
|
+
];
|
|
24
|
+
export const DEFAULT_ROLE_DEFINITIONS = {
|
|
25
|
+
super: {
|
|
26
|
+
id: "super",
|
|
27
|
+
name: "Super Admin",
|
|
28
|
+
description: "Full unrestricted access. Immutable.",
|
|
29
|
+
permissions: ["*"],
|
|
30
|
+
isSystem: true,
|
|
31
|
+
immutable: true
|
|
32
|
+
},
|
|
33
|
+
admin: {
|
|
34
|
+
id: "admin",
|
|
35
|
+
name: "Admin",
|
|
36
|
+
description: "Manage CMS, Store, Comments, Media, and Users.",
|
|
37
|
+
permissions: ["console:access", "cms:*", "store:*", "comments:*", "media:*", "users:read", "users:manage"],
|
|
38
|
+
isSystem: true
|
|
39
|
+
},
|
|
40
|
+
cms_editor: {
|
|
41
|
+
id: "cms_editor",
|
|
42
|
+
name: "CMS Editor",
|
|
43
|
+
description: "Create, edit, and publish CMS content and upload media.",
|
|
44
|
+
permissions: ["console:access", "cms:*", "media:upload"],
|
|
45
|
+
isSystem: true
|
|
46
|
+
},
|
|
47
|
+
store_manager: {
|
|
48
|
+
id: "store_manager",
|
|
49
|
+
name: "Store Manager",
|
|
50
|
+
description: "Manage store catalog, orders, refunds, and media.",
|
|
51
|
+
permissions: ["console:access", "store:*", "media:*"],
|
|
52
|
+
isSystem: true
|
|
53
|
+
},
|
|
54
|
+
store_staff: {
|
|
55
|
+
id: "store_staff",
|
|
56
|
+
name: "Store Staff",
|
|
57
|
+
description: "View catalog and process customer orders.",
|
|
58
|
+
permissions: ["console:access", "store:read", "store:orders.manage"],
|
|
59
|
+
isSystem: true
|
|
60
|
+
},
|
|
61
|
+
moderator: {
|
|
62
|
+
id: "moderator",
|
|
63
|
+
name: "Moderator",
|
|
64
|
+
description: "Moderate comments and review CMS entries.",
|
|
65
|
+
permissions: ["console:access", "comments:*", "cms:read"],
|
|
66
|
+
isSystem: true
|
|
67
|
+
},
|
|
68
|
+
developer: {
|
|
69
|
+
id: "developer",
|
|
70
|
+
name: "Developer",
|
|
71
|
+
description: "Read-only console visibility across modules.",
|
|
72
|
+
permissions: ["console:access", "cms:read", "store:read", "comments:read"],
|
|
73
|
+
isSystem: true
|
|
74
|
+
},
|
|
75
|
+
user: {
|
|
76
|
+
id: "user",
|
|
77
|
+
name: "User",
|
|
78
|
+
description: "Standard authenticated customer/member without console access.",
|
|
79
|
+
permissions: [],
|
|
80
|
+
isSystem: true
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
const ROLE_CACHE_TTL_MS = 3e4;
|
|
84
|
+
let _rolesCache = null;
|
|
85
|
+
const _userPermCache = /* @__PURE__ */ new Map();
|
|
86
|
+
export function invalidateRbacCache(userId) {
|
|
87
|
+
_rolesCache = null;
|
|
88
|
+
if (userId) _userPermCache.delete(userId);
|
|
89
|
+
else _userPermCache.clear();
|
|
90
|
+
}
|
|
91
|
+
function safeParseArray(raw) {
|
|
92
|
+
if (Array.isArray(raw)) return raw.filter((v) => typeof v === "string" && v.length > 0);
|
|
93
|
+
if (typeof raw !== "string" || !raw.trim()) return [];
|
|
94
|
+
try {
|
|
95
|
+
const parsed = JSON.parse(raw);
|
|
96
|
+
return Array.isArray(parsed) ? parsed.filter((v) => typeof v === "string" && v.length > 0) : [];
|
|
97
|
+
} catch {
|
|
98
|
+
return [];
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
export function matchPermission(granted, requiredPermission) {
|
|
102
|
+
if (!requiredPermission) return false;
|
|
103
|
+
for (const g of granted) {
|
|
104
|
+
if (g === "*") return true;
|
|
105
|
+
if (g === requiredPermission) return true;
|
|
106
|
+
if (g.endsWith(":*")) {
|
|
107
|
+
const prefix = g.slice(0, -1);
|
|
108
|
+
if (requiredPermission.startsWith(prefix)) return true;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return false;
|
|
112
|
+
}
|
|
113
|
+
let _ensureRbacTablesPromise = null;
|
|
114
|
+
export async function ensureRbacTables(d1, event) {
|
|
115
|
+
const db = d1 ?? getD1Binding(event);
|
|
116
|
+
if (!db) return;
|
|
117
|
+
if (_ensureRbacTablesPromise) return _ensureRbacTablesPromise;
|
|
118
|
+
const roleTbl = resolveTableName("role", event);
|
|
119
|
+
const userPermTbl = resolveTableName("user_permission", event);
|
|
120
|
+
const p = db.batch([
|
|
121
|
+
db.prepare(`
|
|
122
|
+
CREATE TABLE IF NOT EXISTS ${roleTbl} (
|
|
123
|
+
id TEXT PRIMARY KEY,
|
|
124
|
+
name TEXT NOT NULL,
|
|
125
|
+
description TEXT,
|
|
126
|
+
permissions_json TEXT NOT NULL DEFAULT '[]',
|
|
127
|
+
is_system INTEGER NOT NULL DEFAULT 0,
|
|
128
|
+
created_at INTEGER NOT NULL,
|
|
129
|
+
updated_at INTEGER NOT NULL
|
|
130
|
+
)
|
|
131
|
+
`),
|
|
132
|
+
db.prepare(`
|
|
133
|
+
CREATE TABLE IF NOT EXISTS ${userPermTbl} (
|
|
134
|
+
user_id TEXT PRIMARY KEY,
|
|
135
|
+
extra_roles_json TEXT NOT NULL DEFAULT '[]',
|
|
136
|
+
allow_json TEXT NOT NULL DEFAULT '[]',
|
|
137
|
+
deny_json TEXT NOT NULL DEFAULT '[]',
|
|
138
|
+
updated_by TEXT,
|
|
139
|
+
updated_at INTEGER NOT NULL
|
|
140
|
+
)
|
|
141
|
+
`)
|
|
142
|
+
]).then(() => void 0).catch((err) => {
|
|
143
|
+
_ensureRbacTablesPromise = null;
|
|
144
|
+
throw err;
|
|
145
|
+
});
|
|
146
|
+
_ensureRbacTablesPromise = p;
|
|
147
|
+
return p;
|
|
148
|
+
}
|
|
149
|
+
async function getRoleMap(event) {
|
|
150
|
+
if (_rolesCache && _rolesCache.expiresAt > Date.now()) {
|
|
151
|
+
return _rolesCache.map;
|
|
152
|
+
}
|
|
153
|
+
const map = /* @__PURE__ */ new Map();
|
|
154
|
+
for (const [key, def] of Object.entries(DEFAULT_ROLE_DEFINITIONS)) {
|
|
155
|
+
map.set(key, { ...def, permissions: [...def.permissions] });
|
|
156
|
+
}
|
|
157
|
+
try {
|
|
158
|
+
const db = getD1Binding(event);
|
|
159
|
+
if (db) {
|
|
160
|
+
const roleTbl = resolveTableName("role", event);
|
|
161
|
+
const res = await db.prepare(`SELECT id, name, description, permissions_json, is_system FROM ${roleTbl}`).all().catch(() => null);
|
|
162
|
+
for (const row of res?.results ?? []) {
|
|
163
|
+
const id = String(row.id || "").trim();
|
|
164
|
+
if (!id || id === "super") continue;
|
|
165
|
+
const base = DEFAULT_ROLE_DEFINITIONS[id];
|
|
166
|
+
map.set(id, {
|
|
167
|
+
id,
|
|
168
|
+
name: String(row.name || base?.name || id),
|
|
169
|
+
description: String(row.description || base?.description || ""),
|
|
170
|
+
permissions: safeParseArray(row.permissions_json),
|
|
171
|
+
isSystem: Boolean(base) || Boolean(row.is_system),
|
|
172
|
+
isOverridden: Boolean(base)
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
} catch {
|
|
177
|
+
}
|
|
178
|
+
_rolesCache = { expiresAt: Date.now() + ROLE_CACHE_TTL_MS, map };
|
|
179
|
+
return map;
|
|
180
|
+
}
|
|
181
|
+
async function getUserOverrides(userId, event) {
|
|
182
|
+
const cached = _userPermCache.get(userId);
|
|
183
|
+
if (cached && cached.expiresAt > Date.now()) {
|
|
184
|
+
return cached.data;
|
|
185
|
+
}
|
|
186
|
+
const fallback = {
|
|
187
|
+
userId,
|
|
188
|
+
extraRoles: [],
|
|
189
|
+
allow: [],
|
|
190
|
+
deny: []
|
|
191
|
+
};
|
|
192
|
+
try {
|
|
193
|
+
const db = getD1Binding(event);
|
|
194
|
+
if (db) {
|
|
195
|
+
const userPermTbl = resolveTableName("user_permission", event);
|
|
196
|
+
const row = await db.prepare(
|
|
197
|
+
`SELECT extra_roles_json, allow_json, deny_json FROM ${userPermTbl} WHERE user_id = ? LIMIT 1`
|
|
198
|
+
).bind(userId).first().catch(() => null);
|
|
199
|
+
if (row) {
|
|
200
|
+
const data = {
|
|
201
|
+
userId,
|
|
202
|
+
extraRoles: safeParseArray(row.extra_roles_json),
|
|
203
|
+
allow: safeParseArray(row.allow_json),
|
|
204
|
+
deny: safeParseArray(row.deny_json)
|
|
205
|
+
};
|
|
206
|
+
_userPermCache.set(userId, { expiresAt: Date.now() + ROLE_CACHE_TTL_MS, data });
|
|
207
|
+
return data;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
} catch {
|
|
211
|
+
}
|
|
212
|
+
_userPermCache.set(userId, { expiresAt: Date.now() + ROLE_CACHE_TTL_MS, data: fallback });
|
|
213
|
+
return fallback;
|
|
214
|
+
}
|
|
215
|
+
export async function resolveUserPermissions(user, event) {
|
|
216
|
+
const envRole = user.email && event ? getRoleFromEnv(event, user.email) : "user";
|
|
217
|
+
const primaryRole = envRole === "super" ? "super" : user.role || envRole || "user";
|
|
218
|
+
if (primaryRole === "super") {
|
|
219
|
+
return {
|
|
220
|
+
userId: user.id,
|
|
221
|
+
primaryRole: "super",
|
|
222
|
+
roles: ["super"],
|
|
223
|
+
isSuper: true,
|
|
224
|
+
rolePermissions: ["*"],
|
|
225
|
+
allow: [],
|
|
226
|
+
deny: []
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
const [roleMap, overrides] = await Promise.all([
|
|
230
|
+
getRoleMap(event),
|
|
231
|
+
getUserOverrides(user.id, event)
|
|
232
|
+
]);
|
|
233
|
+
const roles = Array.from(
|
|
234
|
+
/* @__PURE__ */ new Set([primaryRole, ...overrides.extraRoles.filter((r) => r && r !== "super")])
|
|
235
|
+
);
|
|
236
|
+
const rolePermSet = /* @__PURE__ */ new Set();
|
|
237
|
+
for (const r of roles) {
|
|
238
|
+
const def = roleMap.get(r);
|
|
239
|
+
if (def) {
|
|
240
|
+
for (const p of def.permissions) rolePermSet.add(p);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
return {
|
|
244
|
+
userId: user.id,
|
|
245
|
+
primaryRole,
|
|
246
|
+
roles,
|
|
247
|
+
isSuper: false,
|
|
248
|
+
rolePermissions: Array.from(rolePermSet),
|
|
249
|
+
allow: overrides.allow,
|
|
250
|
+
deny: overrides.deny
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
export function evaluatePermission(resolved, permission) {
|
|
254
|
+
if (resolved.isSuper) return true;
|
|
255
|
+
if (matchPermission(resolved.deny, permission)) return false;
|
|
256
|
+
if (matchPermission(resolved.allow, permission)) return true;
|
|
257
|
+
if (matchPermission(resolved.rolePermissions, permission)) return true;
|
|
258
|
+
return false;
|
|
259
|
+
}
|
|
260
|
+
export async function hasPermission(event, permission) {
|
|
261
|
+
const auth = await getAuthContext(event);
|
|
262
|
+
if (!auth?.user?.id) return false;
|
|
263
|
+
if (!auth.hasScope(permission)) return false;
|
|
264
|
+
const resolved = await resolveUserPermissions(auth.user, event);
|
|
265
|
+
return evaluatePermission(resolved, permission);
|
|
266
|
+
}
|
|
267
|
+
export async function requirePermission(event, permission) {
|
|
268
|
+
const auth = await getAuthContext(event);
|
|
269
|
+
if (!auth?.user?.id) {
|
|
270
|
+
throw createError({
|
|
271
|
+
statusCode: 401,
|
|
272
|
+
statusMessage: "Unauthorized",
|
|
273
|
+
message: "Authentication required."
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
if (!auth.hasScope(permission)) {
|
|
277
|
+
throw createError({
|
|
278
|
+
statusCode: 403,
|
|
279
|
+
statusMessage: "Forbidden",
|
|
280
|
+
message: `API token missing scope: ${permission}`
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
const resolved = await resolveUserPermissions(auth.user, event);
|
|
284
|
+
if (!evaluatePermission(resolved, permission)) {
|
|
285
|
+
throw createError({
|
|
286
|
+
statusCode: 403,
|
|
287
|
+
statusMessage: "Forbidden",
|
|
288
|
+
message: `Missing permission: ${permission}`
|
|
289
|
+
});
|
|
290
|
+
}
|
|
291
|
+
return resolved;
|
|
292
|
+
}
|
|
293
|
+
export async function listRoles(event) {
|
|
294
|
+
const map = await getRoleMap(event);
|
|
295
|
+
return Array.from(map.values());
|
|
296
|
+
}
|
|
297
|
+
export async function upsertRole(event, input) {
|
|
298
|
+
const actor = await requirePermission(event, "roles:manage");
|
|
299
|
+
const roleId = input.id.trim().toLowerCase().replace(/[^a-z0-9_-]/g, "");
|
|
300
|
+
if (!roleId) {
|
|
301
|
+
throw createError({ statusCode: 400, statusMessage: "Bad Request", message: "Invalid role id." });
|
|
302
|
+
}
|
|
303
|
+
if (roleId === "super") {
|
|
304
|
+
throw createError({
|
|
305
|
+
statusCode: 403,
|
|
306
|
+
statusMessage: "Forbidden",
|
|
307
|
+
message: "The super role is immutable and cannot be modified."
|
|
308
|
+
});
|
|
309
|
+
}
|
|
310
|
+
const permissions = Array.from(new Set(input.permissions.map((p) => p.trim()).filter(Boolean)));
|
|
311
|
+
if (!actor.isSuper) {
|
|
312
|
+
for (const p of permissions) {
|
|
313
|
+
if (p === "*" || p === "roles:manage" || !evaluatePermission(actor, p)) {
|
|
314
|
+
throw createError({
|
|
315
|
+
statusCode: 403,
|
|
316
|
+
statusMessage: "Forbidden",
|
|
317
|
+
message: `Cannot grant permission "${p}" without holding it (or super role).`
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
const db = getD1Binding(event);
|
|
323
|
+
if (!db) {
|
|
324
|
+
throw createError({ statusCode: 500, message: "D1 database binding is required to save roles." });
|
|
325
|
+
}
|
|
326
|
+
await ensureRbacTables(db, event);
|
|
327
|
+
const roleTbl = resolveTableName("role", event);
|
|
328
|
+
const now = Date.now();
|
|
329
|
+
const isSystem = DEFAULT_ROLE_DEFINITIONS[roleId] ? 1 : 0;
|
|
330
|
+
await db.prepare(
|
|
331
|
+
`INSERT INTO ${roleTbl} (id, name, description, permissions_json, is_system, created_at, updated_at)
|
|
332
|
+
VALUES (?, ?, ?, ?, ?, ?, ?)
|
|
333
|
+
ON CONFLICT(id) DO UPDATE SET
|
|
334
|
+
name = excluded.name,
|
|
335
|
+
description = excluded.description,
|
|
336
|
+
permissions_json = excluded.permissions_json,
|
|
337
|
+
updated_at = excluded.updated_at`
|
|
338
|
+
).bind(
|
|
339
|
+
roleId,
|
|
340
|
+
input.name.trim() || roleId,
|
|
341
|
+
input.description?.trim() || "",
|
|
342
|
+
JSON.stringify(permissions),
|
|
343
|
+
isSystem,
|
|
344
|
+
now,
|
|
345
|
+
now
|
|
346
|
+
).run();
|
|
347
|
+
invalidateRbacCache();
|
|
348
|
+
return {
|
|
349
|
+
id: roleId,
|
|
350
|
+
name: input.name.trim() || roleId,
|
|
351
|
+
description: input.description?.trim() || "",
|
|
352
|
+
permissions,
|
|
353
|
+
isSystem: Boolean(isSystem),
|
|
354
|
+
isOverridden: Boolean(isSystem)
|
|
355
|
+
};
|
|
356
|
+
}
|
|
357
|
+
export async function deleteRole(event, roleId) {
|
|
358
|
+
await requirePermission(event, "roles:manage");
|
|
359
|
+
const cleanId = roleId.trim().toLowerCase();
|
|
360
|
+
if (cleanId === "super") {
|
|
361
|
+
throw createError({
|
|
362
|
+
statusCode: 403,
|
|
363
|
+
statusMessage: "Forbidden",
|
|
364
|
+
message: "The super role cannot be deleted."
|
|
365
|
+
});
|
|
366
|
+
}
|
|
367
|
+
const db = getD1Binding(event);
|
|
368
|
+
if (!db) return { reset: false };
|
|
369
|
+
await ensureRbacTables(db, event);
|
|
370
|
+
const roleTbl = resolveTableName("role", event);
|
|
371
|
+
await db.prepare(`DELETE FROM ${roleTbl} WHERE id = ?`).bind(cleanId).run();
|
|
372
|
+
invalidateRbacCache();
|
|
373
|
+
return { reset: Boolean(DEFAULT_ROLE_DEFINITIONS[cleanId]) };
|
|
374
|
+
}
|
|
375
|
+
export async function setUserPermissions(event, targetUserId, input) {
|
|
376
|
+
const actor = await requirePermission(event, "roles:manage");
|
|
377
|
+
const extraRoles = Array.from(new Set((input.extraRoles || []).map((r) => r.trim()).filter(Boolean)));
|
|
378
|
+
const allow = Array.from(new Set((input.allow || []).map((p) => p.trim()).filter(Boolean)));
|
|
379
|
+
const deny = Array.from(new Set((input.deny || []).map((p) => p.trim()).filter(Boolean)));
|
|
380
|
+
if (extraRoles.includes("super")) {
|
|
381
|
+
throw createError({
|
|
382
|
+
statusCode: 403,
|
|
383
|
+
statusMessage: "Forbidden",
|
|
384
|
+
message: "Cannot assign super via user permission overrides."
|
|
385
|
+
});
|
|
386
|
+
}
|
|
387
|
+
if (!actor.isSuper) {
|
|
388
|
+
for (const p of allow) {
|
|
389
|
+
if (p === "*" || p === "roles:manage" || !evaluatePermission(actor, p)) {
|
|
390
|
+
throw createError({
|
|
391
|
+
statusCode: 403,
|
|
392
|
+
statusMessage: "Forbidden",
|
|
393
|
+
message: `Cannot grant permission "${p}" without holding it (or super role).`
|
|
394
|
+
});
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
const db = getD1Binding(event);
|
|
399
|
+
if (!db) {
|
|
400
|
+
throw createError({
|
|
401
|
+
statusCode: 500,
|
|
402
|
+
message: "D1 database binding is required to save user permissions."
|
|
403
|
+
});
|
|
404
|
+
}
|
|
405
|
+
await ensureRbacTables(db, event);
|
|
406
|
+
const userPermTbl = resolveTableName("user_permission", event);
|
|
407
|
+
const now = Date.now();
|
|
408
|
+
await db.prepare(
|
|
409
|
+
`INSERT INTO ${userPermTbl} (user_id, extra_roles_json, allow_json, deny_json, updated_by, updated_at)
|
|
410
|
+
VALUES (?, ?, ?, ?, ?, ?)
|
|
411
|
+
ON CONFLICT(user_id) DO UPDATE SET
|
|
412
|
+
extra_roles_json = excluded.extra_roles_json,
|
|
413
|
+
allow_json = excluded.allow_json,
|
|
414
|
+
deny_json = excluded.deny_json,
|
|
415
|
+
updated_by = excluded.updated_by,
|
|
416
|
+
updated_at = excluded.updated_at`
|
|
417
|
+
).bind(
|
|
418
|
+
targetUserId,
|
|
419
|
+
JSON.stringify(extraRoles),
|
|
420
|
+
JSON.stringify(allow),
|
|
421
|
+
JSON.stringify(deny),
|
|
422
|
+
actor.userId,
|
|
423
|
+
now
|
|
424
|
+
).run();
|
|
425
|
+
invalidateRbacCache(targetUserId);
|
|
426
|
+
return { userId: targetUserId, extraRoles, allow, deny };
|
|
427
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { RequestEvent } from "nuxt/server";
|
|
2
|
+
/**
|
|
3
|
+
* Qarpeo role hierarchy — no better-auth dependency so it can be safely imported
|
|
4
|
+
* by rbac.ts and other utils without pulling better-auth/crypto into every bundle.
|
|
5
|
+
*/
|
|
6
|
+
export type QarpeoRole = "super" | "admin" | "moderator" | "developer" | "user";
|
|
7
|
+
/**
|
|
8
|
+
* Resolves the configured role for an email address from environment variables:
|
|
9
|
+
* - `QARPEO_SUPER_EMAILS` → 'super'
|
|
10
|
+
* - `QARPEO_ADMIN_EMAILS` → 'admin'
|
|
11
|
+
* - `QARPEO_MODERATOR_EMAILS` → 'moderator'
|
|
12
|
+
* - `QARPEO_DEVELOPER_EMAILS` → 'developer'
|
|
13
|
+
* - Default → 'user'
|
|
14
|
+
*
|
|
15
|
+
* Checks both Cloudflare Worker env (via event context or globalThis) and process.env.
|
|
16
|
+
* Supports both `(event, email)` and `(email, event?)` argument orders.
|
|
17
|
+
*/
|
|
18
|
+
export declare function getRoleFromEnv(event: RequestEvent, email: string): QarpeoRole;
|
|
19
|
+
export declare function getRoleFromEnv(email: string, event?: RequestEvent): QarpeoRole;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
function parseEmailList(raw) {
|
|
2
|
+
if (!raw || typeof raw !== "string") return [];
|
|
3
|
+
return raw.split(",").map((e) => e.trim().toLowerCase()).filter(Boolean);
|
|
4
|
+
}
|
|
5
|
+
export function getRoleFromEnv(arg1, arg2) {
|
|
6
|
+
let event;
|
|
7
|
+
let email;
|
|
8
|
+
if (typeof arg1 === "string") {
|
|
9
|
+
email = arg1;
|
|
10
|
+
event = arg2;
|
|
11
|
+
} else {
|
|
12
|
+
event = arg1;
|
|
13
|
+
email = typeof arg2 === "string" ? arg2 : "";
|
|
14
|
+
}
|
|
15
|
+
const normalizedEmail = (email || "").trim().toLowerCase();
|
|
16
|
+
if (!normalizedEmail) return "user";
|
|
17
|
+
const cfEnv = event?.context?.cloudflare?.env || globalThis.__qarpeo_env__ || {};
|
|
18
|
+
const getVal = (key) => {
|
|
19
|
+
if (cfEnv[key] !== void 0 && cfEnv[key] !== null) return String(cfEnv[key]);
|
|
20
|
+
if (typeof process !== "undefined" && process?.env && process.env[key] !== void 0) {
|
|
21
|
+
return String(process.env[key]);
|
|
22
|
+
}
|
|
23
|
+
return "";
|
|
24
|
+
};
|
|
25
|
+
const superEmails = parseEmailList(getVal("QARPEO_SUPER_EMAILS"));
|
|
26
|
+
if (superEmails.includes(normalizedEmail)) return "super";
|
|
27
|
+
const adminEmails = parseEmailList(getVal("QARPEO_ADMIN_EMAILS"));
|
|
28
|
+
if (adminEmails.includes(normalizedEmail)) return "admin";
|
|
29
|
+
const moderatorEmails = parseEmailList(getVal("QARPEO_MODERATOR_EMAILS"));
|
|
30
|
+
if (moderatorEmails.includes(normalizedEmail)) return "moderator";
|
|
31
|
+
const developerEmails = parseEmailList(getVal("QARPEO_DEVELOPER_EMAILS"));
|
|
32
|
+
if (developerEmails.includes(normalizedEmail)) return "developer";
|
|
33
|
+
return "user";
|
|
34
|
+
}
|
package/dist/runtime/server.d.ts
CHANGED
|
@@ -37,6 +37,8 @@ export * from "./hyperdrive";
|
|
|
37
37
|
export * from "./browser";
|
|
38
38
|
export * from "./service";
|
|
39
39
|
export * from "./auth";
|
|
40
|
+
export * from "./server/utils/rbac";
|
|
41
|
+
export * from "./server/utils/console";
|
|
40
42
|
export * from "./realtime-server";
|
|
41
43
|
export * from "./cms";
|
|
42
44
|
export * from "./comments-server";
|
package/dist/runtime/server.mjs
CHANGED
|
@@ -16,6 +16,8 @@ export * from "./hyperdrive.mjs";
|
|
|
16
16
|
export * from "./browser.mjs";
|
|
17
17
|
export * from "./service.mjs";
|
|
18
18
|
export * from "./auth.mjs";
|
|
19
|
+
export * from "./server/utils/rbac.mjs";
|
|
20
|
+
export * from "./server/utils/console.mjs";
|
|
19
21
|
export * from "./realtime-server.mjs";
|
|
20
22
|
export * from "./cms.mjs";
|
|
21
23
|
export * from "./comments-server.mjs";
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
export type ConsoleModuleKey = "console" | "cms" | "store" | "comments" | "media" | "users" | "roles";
|
|
2
|
+
export interface ConsoleNavItem {
|
|
3
|
+
/** Unique identifier for the nav item (used for overrides/deduping) */
|
|
4
|
+
id: string;
|
|
5
|
+
/** Display label in the console sidebar */
|
|
6
|
+
label: string;
|
|
7
|
+
/** Target route path (e.g. "/console/cms") */
|
|
8
|
+
to: string;
|
|
9
|
+
/** Optional icon name (e.g. "lucide:file-text") */
|
|
10
|
+
icon?: string;
|
|
11
|
+
/** Required permission(s) to see this nav item */
|
|
12
|
+
permission?: string | string[];
|
|
13
|
+
/** Permission match mode when `permission` is an array */
|
|
14
|
+
match?: "all" | "any";
|
|
15
|
+
/** Optional module tag for grouping */
|
|
16
|
+
module?: ConsoleModuleKey;
|
|
17
|
+
/** Sort order (lower appears first) */
|
|
18
|
+
order?: number;
|
|
19
|
+
/** Optional badge text or count */
|
|
20
|
+
badge?: string | number;
|
|
21
|
+
}
|
|
22
|
+
export interface ConsolePageMeta {
|
|
23
|
+
/** Page title inside the console */
|
|
24
|
+
title?: string;
|
|
25
|
+
/** Required permission(s) in addition to `console:access` */
|
|
26
|
+
permission?: string | string[];
|
|
27
|
+
/** Permission match mode when `permission` is an array (default: "all") */
|
|
28
|
+
match?: "all" | "any";
|
|
29
|
+
/** Nuxt layout name (default: "console") */
|
|
30
|
+
layout?: string | false;
|
|
31
|
+
}
|
|
32
|
+
export interface ConsoleOptions {
|
|
33
|
+
/**
|
|
34
|
+
* Enable the website-injected `/console` staff workspace plumbing.
|
|
35
|
+
*
|
|
36
|
+
* @default true (when `console` option is provided as object)
|
|
37
|
+
*/
|
|
38
|
+
enabled?: boolean;
|
|
39
|
+
/**
|
|
40
|
+
* Base route prefix for the console workspace.
|
|
41
|
+
*
|
|
42
|
+
* @default "/console"
|
|
43
|
+
*/
|
|
44
|
+
prefix?: string;
|
|
45
|
+
/**
|
|
46
|
+
* Display title for the console workspace.
|
|
47
|
+
*
|
|
48
|
+
* @default "Console"
|
|
49
|
+
*/
|
|
50
|
+
title?: string;
|
|
51
|
+
/**
|
|
52
|
+
* Redirect path when an unauthenticated user visits a console route.
|
|
53
|
+
*
|
|
54
|
+
* @default "/login"
|
|
55
|
+
*/
|
|
56
|
+
loginPath?: string;
|
|
57
|
+
/**
|
|
58
|
+
* Redirect path when an authenticated user lacks console/page permissions.
|
|
59
|
+
*
|
|
60
|
+
* @default "/unauthorized"
|
|
61
|
+
*/
|
|
62
|
+
unauthorizedPath?: string;
|
|
63
|
+
/**
|
|
64
|
+
* Additional or overridden console navigation items.
|
|
65
|
+
*/
|
|
66
|
+
nav?: ConsoleNavItem[];
|
|
67
|
+
}
|
package/dist/types/index.d.ts
CHANGED
|
@@ -12,6 +12,7 @@ export * from "./realtime";
|
|
|
12
12
|
export * from "./payments";
|
|
13
13
|
export * from "./site";
|
|
14
14
|
export * from "./comments";
|
|
15
|
+
export * from "./console";
|
|
15
16
|
export * from "./store-options";
|
|
16
17
|
export type { StoreOptions } from "./store-options";
|
|
17
18
|
export * from "./db";
|
package/dist/types/options.d.ts
CHANGED
|
@@ -7,6 +7,7 @@ import type { AuthOptions, EmailOptions } from "./auth";
|
|
|
7
7
|
import type { CacheOptions } from "./cache";
|
|
8
8
|
import type { CMSOptions } from "./cms";
|
|
9
9
|
import type { CommentsOptions } from "./comments";
|
|
10
|
+
import type { ConsoleOptions } from "./console";
|
|
10
11
|
import type { DbOptions } from "./db";
|
|
11
12
|
import type { JobsOptions } from "./jobs";
|
|
12
13
|
import type { MailOptions } from "./email";
|
|
@@ -74,6 +75,8 @@ export interface QarpeoOptions {
|
|
|
74
75
|
site?: QarpeoSiteConfig;
|
|
75
76
|
/** Native SEO engine configuration (alias for site config) */
|
|
76
77
|
seo?: boolean | QarpeoSiteConfig;
|
|
78
|
+
/** Native Edge PWA configuration (manifest.webmanifest, /sw.js, and usePwa composable) */
|
|
79
|
+
pwa?: boolean | import("./site").QarpeoPwaConfig;
|
|
77
80
|
/**
|
|
78
81
|
* Cloudflare Worker service name override.
|
|
79
82
|
*
|
|
@@ -320,6 +323,12 @@ export interface QarpeoOptions {
|
|
|
320
323
|
* @default false
|
|
321
324
|
*/
|
|
322
325
|
store?: boolean | StoreOptions;
|
|
326
|
+
/**
|
|
327
|
+
* Website-injected `/console` staff & operations workspace plumbing (RBAC guards, composables, and nav builder).
|
|
328
|
+
*
|
|
329
|
+
* @default false
|
|
330
|
+
*/
|
|
331
|
+
console?: boolean | ConsoleOptions;
|
|
323
332
|
/**
|
|
324
333
|
* Configuration for Qarpeo DevTools (Dev-Only Management Console & DevTools panel).
|
|
325
334
|
*
|