@develit-services/rbac 0.0.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.
@@ -0,0 +1,771 @@
1
+ import { WorkerEntrypoint } from 'cloudflare:workers';
2
+ import { uuidv4, first, createInternalError, develitWorker, action, service } from '@develit-io/backend-sdk';
3
+ import { m as jwtPayloadSchema, c as createRoleInputSchema, a as assignRoleToUserInputSchema, b as assignRolesToUserInputSchema, r as revokeRoleFromUserInputSchema, f as grantScopeToUserInputSchema, h as grantScopesToUserInputSchema, j as revokeScopeFromUserInputSchema, e as grantScopeToRoleInputSchema, i as revokeScopeFromRoleInputSchema, S as SCOPES, g as getUserPermissionsInputSchema, v as verifyAccessInputSchema, d as deleteRoleInputSchema, u as updateRoleInputSchema } from '../shared/rbac.C9brkvW9.mjs';
4
+ import { s as schema } from '../shared/rbac.CJLU5iuV.mjs';
5
+ import { eq, and, count, inArray } from 'drizzle-orm';
6
+ import { z } from 'zod';
7
+ import { drizzle } from 'drizzle-orm/d1';
8
+ import 'drizzle-orm/sqlite-core';
9
+
10
+ const tables = schema;
11
+
12
+ const assignRoleToUserCommand = async ({
13
+ db,
14
+ userId,
15
+ roleId
16
+ }) => {
17
+ const id = uuidv4();
18
+ const command = db.insert(tables.userRole).values({
19
+ id,
20
+ roleId,
21
+ userId
22
+ }).returning();
23
+ return { command, id };
24
+ };
25
+
26
+ const assignRolesToUserCommand = async ({
27
+ db,
28
+ userId,
29
+ roleIds
30
+ }) => {
31
+ const userRoles = roleIds.map((roleId) => ({
32
+ id: uuidv4(),
33
+ roleId,
34
+ userId
35
+ }));
36
+ const command = db.insert(tables.userRole).values(userRoles).returning();
37
+ return { command, ids: userRoles.map((ur) => ur.id) };
38
+ };
39
+
40
+ const createRoleCommand = async ({
41
+ db,
42
+ name
43
+ }) => {
44
+ const id = uuidv4();
45
+ const command = db.insert(tables.role).values({
46
+ id,
47
+ name
48
+ }).returning();
49
+ return { command, id };
50
+ };
51
+
52
+ const deleteRoleCommand = async ({
53
+ db,
54
+ id
55
+ }) => {
56
+ const command = db.delete(tables.role).where(eq(tables.role.id, id));
57
+ return { command };
58
+ };
59
+
60
+ const grantScopeToRoleCommand = async ({
61
+ db,
62
+ scope,
63
+ roleId,
64
+ resourceId
65
+ }) => {
66
+ const id = uuidv4();
67
+ const command = db.insert(tables.roleScope).values({
68
+ id,
69
+ scope,
70
+ roleId,
71
+ resourceId
72
+ }).returning();
73
+ return { command, id };
74
+ };
75
+
76
+ const grantScopeToUserCommand = async ({
77
+ db,
78
+ userId,
79
+ scope,
80
+ resourceId
81
+ }) => {
82
+ const id = uuidv4();
83
+ const command = db.insert(tables.userScope).values({
84
+ id,
85
+ scope,
86
+ userId,
87
+ resourceId
88
+ }).returning();
89
+ return { command, id };
90
+ };
91
+
92
+ const grantScopesToUserCommand = async ({
93
+ db,
94
+ userId,
95
+ scopes
96
+ }) => {
97
+ const userScopes = scopes.map((scope) => ({
98
+ id: uuidv4(),
99
+ scope: scope.scope,
100
+ userId,
101
+ resourceId: scope.resourceId
102
+ }));
103
+ const command = db.insert(tables.userScope).values(userScopes).returning();
104
+ return { command, ids: userScopes.map((us) => us.id) };
105
+ };
106
+
107
+ const revokeRoleFromUserCommand = async ({
108
+ db,
109
+ userId,
110
+ roleId
111
+ }) => {
112
+ const command = db.delete(tables.userRole).where(
113
+ and(
114
+ eq(tables.userRole.userId, userId),
115
+ eq(tables.userRole.roleId, roleId)
116
+ )
117
+ );
118
+ return { command };
119
+ };
120
+
121
+ const revokeScopeFromRoleCommand = async ({
122
+ db,
123
+ roleId,
124
+ scope,
125
+ resourceId
126
+ }) => {
127
+ const command = db.delete(tables.roleScope).where(
128
+ and(
129
+ eq(tables.roleScope.roleId, roleId),
130
+ eq(tables.roleScope.scope, scope),
131
+ resourceId ? eq(tables.userScope.resourceId, resourceId) : void 0
132
+ )
133
+ );
134
+ return { command };
135
+ };
136
+
137
+ const revokeScopeFromUserCommand = async ({
138
+ db,
139
+ userId,
140
+ scope,
141
+ resourceId
142
+ }) => {
143
+ const command = db.delete(tables.userScope).where(
144
+ and(
145
+ eq(tables.userScope.userId, userId),
146
+ eq(tables.userScope.scope, scope),
147
+ resourceId ? eq(tables.userScope.resourceId, resourceId) : void 0
148
+ )
149
+ );
150
+ return { command };
151
+ };
152
+
153
+ const updateRoleCommand = async ({
154
+ db,
155
+ id,
156
+ name
157
+ }) => {
158
+ const command = db.update(tables.role).set({ name }).where(eq(tables.role.id, id));
159
+ return { command };
160
+ };
161
+
162
+ const getAllRolesQuery = async ({
163
+ db
164
+ }) => {
165
+ const roles = await db.select().from(tables.role);
166
+ const result = [];
167
+ for (const role of roles) {
168
+ const scopeCount = await db.select({ count: count() }).from(tables.roleScope).where(eq(tables.roleScope.roleId, role.id));
169
+ const userCount = await db.select({ count: count() }).from(tables.userRole).where(eq(tables.userRole.roleId, role.id));
170
+ result.push({
171
+ id: role.id,
172
+ name: role.name,
173
+ numberOfScopes: scopeCount[0]?.count ?? 0,
174
+ numberOfUsers: userCount[0]?.count ?? 0
175
+ });
176
+ }
177
+ return result;
178
+ };
179
+
180
+ const getRoleQuery = async ({
181
+ db,
182
+ roleId
183
+ }) => {
184
+ return await db.select().from(tables.role).where(eq(tables.role.id, roleId)).then(first);
185
+ };
186
+
187
+ const getRolesByUserQuery = async ({
188
+ db,
189
+ userId
190
+ }) => {
191
+ return await db.select({
192
+ id: tables.role.id,
193
+ name: tables.role.name
194
+ }).from(tables.userRole).leftJoin(tables.role, eq(tables.role.id, tables.userRole.roleId)).where(and(eq(tables.userRole.userId, userId)));
195
+ };
196
+
197
+ const getScopesByRolesQuery = async ({
198
+ db,
199
+ roleIds
200
+ }) => {
201
+ return await db.select({
202
+ id: tables.roleScope.id,
203
+ scope: tables.roleScope.scope,
204
+ resourceId: tables.roleScope.resourceId
205
+ }).from(tables.roleScope).where(inArray(tables.roleScope.roleId, roleIds));
206
+ };
207
+
208
+ const getScopesByUserQuery = async ({
209
+ db,
210
+ userId
211
+ }) => {
212
+ return await db.select({
213
+ id: tables.userScope.id,
214
+ scope: tables.userScope.scope,
215
+ resourceId: tables.userScope.resourceId
216
+ }).from(tables.userScope).where(and(eq(tables.userScope.userId, userId)));
217
+ };
218
+
219
+ function parseScopeTemplate(scope) {
220
+ if (typeof scope !== "string") {
221
+ throw createInternalError(null, {
222
+ message: "Scope template must be a string",
223
+ status: 400,
224
+ code: "INVALID_SCOPE_TYPE"
225
+ });
226
+ }
227
+ if (scope.length === 0) {
228
+ return [];
229
+ }
230
+ const placeholderRegex = /\{([^.]+)\.(.+?)\}/g;
231
+ const allBraceMatches = /\{[^}]*\}/g;
232
+ const allMatches = [...scope.matchAll(allBraceMatches)];
233
+ const validMatches = [...scope.matchAll(placeholderRegex)];
234
+ if (allMatches.length !== validMatches.length) {
235
+ throw createInternalError(null, {
236
+ message: "Invalid placeholder format found. Expected format: {type.key}",
237
+ status: 400,
238
+ code: "INVALID_PLACEHOLDER_FORMAT"
239
+ });
240
+ }
241
+ const placeholders = [];
242
+ let match;
243
+ placeholderRegex.lastIndex = 0;
244
+ while ((match = placeholderRegex.exec(scope)) !== null) {
245
+ const type = match[1];
246
+ const path = match[2];
247
+ if (!type || !path) {
248
+ throw createInternalError(null, {
249
+ message: `Placeholder '${match[0]}' has empty type or path`,
250
+ status: 400,
251
+ code: "EMPTY_PLACEHOLDER_PARTS"
252
+ });
253
+ }
254
+ placeholders.push({
255
+ type,
256
+ // jwt
257
+ path,
258
+ // userData.organizationId
259
+ fullMatch: match[0],
260
+ // "{jwt.userData.organizationId}"
261
+ position: match.index
262
+ // Position in string for ordered replacement
263
+ });
264
+ }
265
+ return placeholders.sort((a, b) => a.position - b.position);
266
+ }
267
+ function extractResourcesFromPath(scope, resourcePath) {
268
+ const placeholders = parseScopeTemplate(scope);
269
+ if (placeholders.length === 0) return {};
270
+ let regexPattern = scope.replace(/\./g, "\\.");
271
+ placeholders.forEach(() => {
272
+ regexPattern = regexPattern.replace(/\{[^}]+\}/, "([^.]+)");
273
+ });
274
+ const regex = new RegExp(`^${regexPattern}$`);
275
+ const match = resourcePath.match(regex);
276
+ if (!match) {
277
+ throw createInternalError(null, {
278
+ message: `Resource path '${resourcePath}' does not match scope template '${scope}'`,
279
+ status: 400,
280
+ code: "PATH_MISMATCH"
281
+ });
282
+ }
283
+ const result = {};
284
+ placeholders.forEach((placeholder, index) => {
285
+ const capturedValue = match[index + 1];
286
+ const { error } = z.uuid().safeParse(capturedValue);
287
+ if (error)
288
+ throw createInternalError(null, {
289
+ message: `Invalid UUID format: '${capturedValue}' for placeholder '${placeholder.type}.${placeholder.path}'`,
290
+ status: 400,
291
+ code: "INVALID_UUID"
292
+ });
293
+ result[`${placeholder.type}.${placeholder.path}`] = capturedValue;
294
+ });
295
+ return result;
296
+ }
297
+ const inputGetValueByKeySchema = z.object({
298
+ type: z.string().nonempty("Type parameter cannot be empty"),
299
+ path: z.string().nonempty("Path parameter cannot be empty"),
300
+ jwt: jwtPayloadSchema.optional()
301
+ });
302
+ const parseJson = (data) => {
303
+ try {
304
+ return JSON.parse(data);
305
+ } catch (_e) {
306
+ return data;
307
+ }
308
+ };
309
+ function getNestedValue(jwt, path) {
310
+ if (path === "") {
311
+ return jwt;
312
+ }
313
+ const keys = path.split(".");
314
+ let current = jwt;
315
+ for (const key of keys) {
316
+ if (current && typeof current === "object" && current !== null && !Array.isArray(current) && key in current) {
317
+ current = parseJson(current[key]);
318
+ } else {
319
+ return void 0;
320
+ }
321
+ }
322
+ return current;
323
+ }
324
+ function getValueByKey(type, path, jwt) {
325
+ const { error } = inputGetValueByKeySchema.safeParse({ type, path, jwt });
326
+ if (error) {
327
+ const message = error.issues.map((e) => e.message).join(", ");
328
+ throw createInternalError(error, {
329
+ message,
330
+ status: 500
331
+ });
332
+ }
333
+ switch (type) {
334
+ case "jwt": {
335
+ if (!jwt)
336
+ throw createInternalError(null, {
337
+ message: "JWT is required when using JWT parameters in scope template",
338
+ status: 400
339
+ });
340
+ const value = getNestedValue(jwt, path);
341
+ if (value === void 0 || value === null)
342
+ throw createInternalError(null, {
343
+ message: `Property '${path}' not found in JWT payload.`,
344
+ status: 400
345
+ });
346
+ return value;
347
+ }
348
+ case "params":
349
+ break;
350
+ default:
351
+ throw createInternalError(null, {
352
+ message: `Unsupported param type '${type}'`,
353
+ status: 400
354
+ });
355
+ }
356
+ }
357
+
358
+ var __defProp = Object.defineProperty;
359
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
360
+ var __decorateClass = (decorators, target, key, kind) => {
361
+ var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
362
+ for (var i = decorators.length - 1, decorator; i >= 0; i--)
363
+ if (decorator = decorators[i])
364
+ result = (kind ? decorator(target, key, result) : decorator(result)) || result;
365
+ if (kind && result) __defProp(target, key, result);
366
+ return result;
367
+ };
368
+ let RbacServiceBase = class extends develitWorker(
369
+ WorkerEntrypoint
370
+ ) {
371
+ constructor(ctx, env) {
372
+ super(ctx, env);
373
+ this.db = drizzle(this.env.RBAC_D1, { schema: tables });
374
+ }
375
+ async createRole(input) {
376
+ return this.handleAction(
377
+ { data: input, schema: createRoleInputSchema },
378
+ { successMessage: "Role successfully created." },
379
+ async ({ name }) => {
380
+ const { command } = await createRoleCommand({ db: this.db, name });
381
+ const [roles] = await this.db.batch([command]);
382
+ return first(roles);
383
+ }
384
+ );
385
+ }
386
+ async assignRoleToUser(input) {
387
+ return this.handleAction(
388
+ { data: input, schema: assignRoleToUserInputSchema },
389
+ { successMessage: "Role successfully assigned." },
390
+ async ({ userId, roleId }) => {
391
+ const [role, userRole] = await Promise.all([
392
+ await getRoleQuery({ db: this.db, roleId }),
393
+ await getRolesByUserQuery({ db: this.db, userId })
394
+ ]);
395
+ if (!role) {
396
+ throw createInternalError(null, {
397
+ message: "Role not found.",
398
+ status: 404
399
+ });
400
+ }
401
+ if (userRole.some((r) => r.id === roleId)) {
402
+ throw createInternalError(null, {
403
+ message: "Role already assigned to user.",
404
+ status: 409
405
+ });
406
+ }
407
+ const { command } = await assignRoleToUserCommand({
408
+ db: this.db,
409
+ userId,
410
+ roleId
411
+ });
412
+ const [record] = await this.db.batch([command]);
413
+ return first(record);
414
+ }
415
+ );
416
+ }
417
+ async assignRolesToUser(input) {
418
+ return this.handleAction(
419
+ { data: input, schema: assignRolesToUserInputSchema },
420
+ { successMessage: "Roles successfully assigned." },
421
+ async ({ userId, roles: roleIds }) => {
422
+ for (const roleId of roleIds) {
423
+ const [role, userRole] = await Promise.all([
424
+ await getRoleQuery({ db: this.db, roleId }),
425
+ await getRolesByUserQuery({ db: this.db, userId })
426
+ ]);
427
+ if (!role) {
428
+ throw createInternalError(null, {
429
+ message: "Role not found.",
430
+ status: 404
431
+ });
432
+ }
433
+ if (userRole.some((r) => r.id === roleId)) {
434
+ throw createInternalError(null, {
435
+ message: "Role already assigned to user.",
436
+ status: 409
437
+ });
438
+ }
439
+ }
440
+ const { command } = await assignRolesToUserCommand({
441
+ db: this.db,
442
+ userId,
443
+ roleIds
444
+ });
445
+ const [record] = await this.db.batch([command]);
446
+ return first(record);
447
+ }
448
+ );
449
+ }
450
+ async revokeRoleFromUser(input) {
451
+ return this.handleAction(
452
+ { data: input, schema: revokeRoleFromUserInputSchema },
453
+ { successMessage: "Role successfully revoked." },
454
+ async ({ userId, roleId }) => {
455
+ const { command } = await revokeRoleFromUserCommand({
456
+ db: this.db,
457
+ userId,
458
+ roleId
459
+ });
460
+ await this.db.batch([command]);
461
+ return {};
462
+ }
463
+ );
464
+ }
465
+ async grantScopeToUser(input) {
466
+ return this.handleAction(
467
+ { data: input, schema: grantScopeToUserInputSchema },
468
+ { successMessage: "Scope successfully granted to user." },
469
+ async ({ userId, scope, resourceId }) => {
470
+ const userScope = await getScopesByUserQuery({ db: this.db, userId });
471
+ if (userScope.some((s) => s.scope === scope)) {
472
+ throw createInternalError(null, {
473
+ message: "Scope already assigned to user.",
474
+ status: 409
475
+ });
476
+ }
477
+ const { command } = await grantScopeToUserCommand({
478
+ db: this.db,
479
+ userId,
480
+ scope,
481
+ resourceId
482
+ });
483
+ const [record] = await this.db.batch([command]);
484
+ return first(record);
485
+ }
486
+ );
487
+ }
488
+ async grantScopesToUser(input) {
489
+ return this.handleAction(
490
+ { data: input, schema: grantScopesToUserInputSchema },
491
+ { successMessage: "Scopes successfully granted to user." },
492
+ async ({ userId, scopes }) => {
493
+ for (const scope of scopes) {
494
+ const userScopes = await getScopesByUserQuery({ db: this.db, userId });
495
+ if (userScopes.some((s) => s.scope === scope.scope)) {
496
+ throw createInternalError(null, {
497
+ message: "Scope already assigned to user.",
498
+ status: 409
499
+ });
500
+ }
501
+ }
502
+ const { command } = await grantScopesToUserCommand({
503
+ db: this.db,
504
+ userId,
505
+ scopes
506
+ });
507
+ const [record] = await this.db.batch([command]);
508
+ return first(record);
509
+ }
510
+ );
511
+ }
512
+ async revokeScopeFromUser(input) {
513
+ return this.handleAction(
514
+ { data: input, schema: revokeScopeFromUserInputSchema },
515
+ { successMessage: "Scope successfully revoked from user." },
516
+ async ({ userId, scope, resourceId }) => {
517
+ const { command } = await revokeScopeFromUserCommand({
518
+ db: this.db,
519
+ userId,
520
+ scope,
521
+ resourceId
522
+ });
523
+ await this.db.batch([command]);
524
+ return {};
525
+ }
526
+ );
527
+ }
528
+ async grantScopeToRole(input) {
529
+ return this.handleAction(
530
+ { data: input, schema: grantScopeToRoleInputSchema },
531
+ { successMessage: "Scope successfully granted to role." },
532
+ async ({ roleId, scope, resourceId }) => {
533
+ const { command } = await grantScopeToRoleCommand({
534
+ db: this.db,
535
+ scope,
536
+ roleId,
537
+ resourceId
538
+ });
539
+ const [record] = await this.db.batch([command]);
540
+ return first(record);
541
+ }
542
+ );
543
+ }
544
+ async revokeScopeFromRole(input) {
545
+ return this.handleAction(
546
+ { data: input, schema: revokeScopeFromRoleInputSchema },
547
+ { successMessage: "Scope successfully revoked from role." },
548
+ async ({ roleId, scope, resourceId }) => {
549
+ const { command } = await revokeScopeFromRoleCommand({
550
+ db: this.db,
551
+ roleId,
552
+ scope,
553
+ resourceId
554
+ });
555
+ await this.db.batch([command]);
556
+ return {};
557
+ }
558
+ );
559
+ }
560
+ async getPermissions() {
561
+ return this.handleAction(
562
+ null,
563
+ { successMessage: "Permissions successfully returned." },
564
+ async () => {
565
+ const roles = await getAllRolesQuery({ db: this.db });
566
+ const roleScopes = await Promise.all(
567
+ roles.filter((role) => role.id).map(async (role) => {
568
+ const scopes = await getScopesByRolesQuery({
569
+ db: this.db,
570
+ roleIds: [role.id]
571
+ });
572
+ return {
573
+ roleId: role.id,
574
+ roleName: role.name,
575
+ scopes: scopes.map((scope) => ({
576
+ id: scope.id,
577
+ scope: scope.scope,
578
+ resourceId: scope.resourceId
579
+ }))
580
+ };
581
+ })
582
+ );
583
+ return {
584
+ roles,
585
+ rolesCount: roles.length,
586
+ scopes: [...SCOPES],
587
+ scopesCount: [...SCOPES].length,
588
+ roleScopes,
589
+ roleScopesCount: roleScopes.length
590
+ };
591
+ }
592
+ );
593
+ }
594
+ async getUserPermissions(input) {
595
+ return this.handleAction(
596
+ { data: input, schema: getUserPermissionsInputSchema },
597
+ { successMessage: "User permissions successfully returned." },
598
+ async ({ userId }) => {
599
+ const resultRoles = await getRolesByUserQuery({ db: this.db, userId });
600
+ const resultScopes = await getScopesByUserQuery({ db: this.db, userId });
601
+ const resultRoleScopes = await getScopesByRolesQuery({
602
+ db: this.db,
603
+ roleIds: resultRoles.filter((role) => role.id).map((role) => role.id)
604
+ });
605
+ const roles = resultRoles.map((role) => ({
606
+ id: role.id,
607
+ name: role.name
608
+ }));
609
+ const scopes = resultScopes.map((scope) => ({
610
+ id: scope.id,
611
+ scope: scope.scope,
612
+ resourceId: scope.resourceId
613
+ }));
614
+ const roleScopes = resultRoleScopes.map((scope) => ({
615
+ id: scope.id,
616
+ scope: scope.scope,
617
+ resourceId: scope.resourceId
618
+ }));
619
+ return {
620
+ roles,
621
+ rolesCount: roles.length,
622
+ scopes,
623
+ scopesCount: scopes.length,
624
+ roleScopes,
625
+ roleScopesCount: roleScopes.length
626
+ };
627
+ }
628
+ );
629
+ }
630
+ async verifyAccess(input) {
631
+ return this.handleAction(
632
+ // TODO: This input schema is just copied from auth and is not 100% type safe
633
+ { data: input, schema: verifyAccessInputSchema },
634
+ { successMessage: "Access verification completed." },
635
+ async ({ userId, accessRequests, jwt }) => {
636
+ const userPermissionsResponse = await this.getUserPermissions({
637
+ userId
638
+ });
639
+ if (!userPermissionsResponse.data) {
640
+ throw createInternalError(null, {
641
+ message: "Unable to retrieve user permissions",
642
+ status: 500
643
+ });
644
+ }
645
+ const allScopes = [
646
+ ...userPermissionsResponse.data.roleScopes,
647
+ ...userPermissionsResponse.data.scopes
648
+ ];
649
+ const allAccessRequestsSatisfied = accessRequests.every((request) => {
650
+ const placeholders = parseScopeTemplate(request.scope);
651
+ return allScopes.some((userScope) => {
652
+ const scopesMatch = userScope.scope === request.scope;
653
+ let resourceMatches = false;
654
+ if (placeholders.length > 0) {
655
+ if (!request.resourcePath) {
656
+ throw createInternalError(null, {
657
+ message: `Resource path is required when scope '${request.scope}' contains placeholders`,
658
+ status: 400,
659
+ code: "RESOURCE_PATH_REQUIRED"
660
+ });
661
+ }
662
+ const extractedResources = extractResourcesFromPath(
663
+ request.scope,
664
+ request.resourcePath
665
+ );
666
+ const allPlaceholdersMatch = placeholders.every((placeholder) => {
667
+ const extractedValue = extractedResources[`${placeholder.type}.${placeholder.path}`];
668
+ const jwtParam = placeholder.type === "jwt" ? jwt : void 0;
669
+ const expectedValue = getValueByKey(
670
+ placeholder.type,
671
+ placeholder.path,
672
+ jwtParam
673
+ );
674
+ if (expectedValue === void 0) {
675
+ return false;
676
+ }
677
+ return String(extractedValue) === String(expectedValue);
678
+ });
679
+ const resourceIdMatches = userScope.resourceId === null || userScope.resourceId === request.resourceId;
680
+ resourceMatches = allPlaceholdersMatch && resourceIdMatches;
681
+ } else {
682
+ resourceMatches = userScope.resourceId === null || userScope.resourceId === request.resourceId;
683
+ }
684
+ return scopesMatch && resourceMatches;
685
+ });
686
+ });
687
+ return {
688
+ isVerified: allAccessRequestsSatisfied
689
+ };
690
+ }
691
+ );
692
+ }
693
+ async deleteRole(input) {
694
+ return this.handleAction(
695
+ { data: input, schema: deleteRoleInputSchema },
696
+ { successMessage: "Role successfully deleted" },
697
+ async ({ id }) => {
698
+ const { command } = await deleteRoleCommand({ db: this.db, id });
699
+ await this.db.batch([command]);
700
+ return {};
701
+ }
702
+ );
703
+ }
704
+ async updateRole(input) {
705
+ return this.handleAction(
706
+ { data: input, schema: updateRoleInputSchema },
707
+ { successMessage: "Role successfully updated" },
708
+ async ({ id, name }) => {
709
+ const { command } = await updateRoleCommand({ db: this.db, id, name });
710
+ await this.db.batch([command]);
711
+ return {};
712
+ }
713
+ );
714
+ }
715
+ };
716
+ __decorateClass([
717
+ action("createRole")
718
+ ], RbacServiceBase.prototype, "createRole", 1);
719
+ __decorateClass([
720
+ action("assignRoleToUser")
721
+ ], RbacServiceBase.prototype, "assignRoleToUser", 1);
722
+ __decorateClass([
723
+ action("assign-roles-to-user")
724
+ ], RbacServiceBase.prototype, "assignRolesToUser", 1);
725
+ __decorateClass([
726
+ action("revokeRoleFromUser")
727
+ ], RbacServiceBase.prototype, "revokeRoleFromUser", 1);
728
+ __decorateClass([
729
+ action("grantScopeToUser")
730
+ ], RbacServiceBase.prototype, "grantScopeToUser", 1);
731
+ __decorateClass([
732
+ action("grant-scopes-to-user")
733
+ ], RbacServiceBase.prototype, "grantScopesToUser", 1);
734
+ __decorateClass([
735
+ action("revokeScopeFromUser")
736
+ ], RbacServiceBase.prototype, "revokeScopeFromUser", 1);
737
+ __decorateClass([
738
+ action("grantScopeToRole")
739
+ ], RbacServiceBase.prototype, "grantScopeToRole", 1);
740
+ __decorateClass([
741
+ action("revokeScopeFromRole")
742
+ ], RbacServiceBase.prototype, "revokeScopeFromRole", 1);
743
+ __decorateClass([
744
+ action("getPermissions")
745
+ ], RbacServiceBase.prototype, "getPermissions", 1);
746
+ __decorateClass([
747
+ action("getUserPermissions")
748
+ ], RbacServiceBase.prototype, "getUserPermissions", 1);
749
+ __decorateClass([
750
+ action("verifyAccess")
751
+ ], RbacServiceBase.prototype, "verifyAccess", 1);
752
+ __decorateClass([
753
+ action("deleteRole")
754
+ ], RbacServiceBase.prototype, "deleteRole", 1);
755
+ __decorateClass([
756
+ action("updateRole")
757
+ ], RbacServiceBase.prototype, "updateRole", 1);
758
+ RbacServiceBase = __decorateClass([
759
+ service("rbac")
760
+ ], RbacServiceBase);
761
+ function defineRbacService() {
762
+ return class RbacService extends RbacServiceBase {
763
+ constructor(ctx, env) {
764
+ super(ctx, env);
765
+ }
766
+ };
767
+ }
768
+
769
+ const RbacService = defineRbacService();
770
+
771
+ export { RbacService as default, defineRbacService };