@omnibase/core-js 0.7.1 → 0.7.2
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/chunk-JNM7XP7L.js +345 -0
- package/dist/index.cjs +251 -9
- package/dist/index.d.cts +1 -2
- package/dist/index.d.ts +1 -2
- package/dist/index.js +6 -3
- package/dist/payments/index.d.cts +603 -3
- package/dist/payments/index.d.ts +603 -3
- package/dist/permissions/index.cjs +250 -10
- package/dist/permissions/index.d.cts +2 -195
- package/dist/permissions/index.d.ts +2 -195
- package/dist/permissions/index.js +5 -3
- package/dist/storage/index.d.cts +1 -2
- package/dist/storage/index.d.ts +1 -2
- package/dist/tenants/index.d.cts +1 -2
- package/dist/tenants/index.d.ts +1 -2
- package/package.json +1 -1
|
@@ -0,0 +1,345 @@
|
|
|
1
|
+
// src/permissions/handler.ts
|
|
2
|
+
import { RelationshipApi, PermissionApi } from "@ory/client";
|
|
3
|
+
|
|
4
|
+
// src/permissions/roles.ts
|
|
5
|
+
var RolesHandler = class {
|
|
6
|
+
constructor(client) {
|
|
7
|
+
this.client = client;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Get available namespace definitions for UI
|
|
11
|
+
*
|
|
12
|
+
* Returns all namespaces and their available relations/permissions.
|
|
13
|
+
* Useful for building role configuration UIs.
|
|
14
|
+
*
|
|
15
|
+
* @returns List of namespace definitions
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```typescript
|
|
19
|
+
* const definitions = await omnibase.permissions.roles.getDefinitions();
|
|
20
|
+
*
|
|
21
|
+
* // Output: [{ namespace: 'Tenant', relations: ['invite_user', 'delete_tenant', ...] }]
|
|
22
|
+
* definitions.forEach(def => {
|
|
23
|
+
* console.log(`${def.namespace} supports: ${def.relations.join(', ')}`);
|
|
24
|
+
* });
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
async getDefinitions() {
|
|
28
|
+
const response = await this.client.fetch("/api/v1/roles/definitions", {
|
|
29
|
+
method: "GET"
|
|
30
|
+
});
|
|
31
|
+
const data = await response.json();
|
|
32
|
+
if (!response.ok || data.error) {
|
|
33
|
+
throw new Error(data.error || "Failed to fetch definitions");
|
|
34
|
+
}
|
|
35
|
+
return data.data.definitions;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* List all roles for the current tenant
|
|
39
|
+
*
|
|
40
|
+
* Returns both system roles (defined in roles.config.json) and
|
|
41
|
+
* custom roles created via the API. System roles have `tenant_id = null`.
|
|
42
|
+
*
|
|
43
|
+
* @returns List of roles
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* ```typescript
|
|
47
|
+
* const roles = await omnibase.permissions.roles.list();
|
|
48
|
+
*
|
|
49
|
+
* const systemRoles = roles.filter(r => r.tenant_id === null);
|
|
50
|
+
* const customRoles = roles.filter(r => r.tenant_id !== null);
|
|
51
|
+
*
|
|
52
|
+
* console.log(`System roles: ${systemRoles.map(r => r.role_name).join(', ')}`);
|
|
53
|
+
* console.log(`Custom roles: ${customRoles.map(r => r.role_name).join(', ')}`);
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
async list() {
|
|
57
|
+
const response = await this.client.fetch("/api/v1/roles/roles", {
|
|
58
|
+
method: "GET"
|
|
59
|
+
});
|
|
60
|
+
const data = await response.json();
|
|
61
|
+
if (!response.ok || data.error) {
|
|
62
|
+
throw new Error(data.error || "Failed to list roles");
|
|
63
|
+
}
|
|
64
|
+
return data.data.roles;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Create a new custom role
|
|
68
|
+
*
|
|
69
|
+
* Creates a tenant-specific role with the specified permissions.
|
|
70
|
+
* Permissions use the format `namespace#relation` or `namespace:id#relation`.
|
|
71
|
+
*
|
|
72
|
+
* @param request - Role creation request
|
|
73
|
+
* @returns Created role
|
|
74
|
+
*
|
|
75
|
+
* @example
|
|
76
|
+
* ```typescript
|
|
77
|
+
* const role = await omnibase.permissions.roles.create({
|
|
78
|
+
* role_name: 'billing_manager',
|
|
79
|
+
* permissions: [
|
|
80
|
+
* 'tenant#manage_billing',
|
|
81
|
+
* 'tenant#view_invoices',
|
|
82
|
+
* 'tenant#update_payment_methods'
|
|
83
|
+
* ]
|
|
84
|
+
* });
|
|
85
|
+
*
|
|
86
|
+
* console.log(`Created role: ${role.id}`);
|
|
87
|
+
* ```
|
|
88
|
+
*
|
|
89
|
+
* @example
|
|
90
|
+
* Resource-specific permissions:
|
|
91
|
+
* ```typescript
|
|
92
|
+
* const devRole = await omnibase.permissions.roles.create({
|
|
93
|
+
* role_name: 'project_developer',
|
|
94
|
+
* permissions: [
|
|
95
|
+
* 'project:proj_abc123#deploy',
|
|
96
|
+
* 'project:proj_abc123#view_logs',
|
|
97
|
+
* 'tenant#invite_user'
|
|
98
|
+
* ]
|
|
99
|
+
* });
|
|
100
|
+
* ```
|
|
101
|
+
*/
|
|
102
|
+
async create(request) {
|
|
103
|
+
const response = await this.client.fetch("/api/v1/roles/roles", {
|
|
104
|
+
method: "POST",
|
|
105
|
+
headers: { "Content-Type": "application/json" },
|
|
106
|
+
body: JSON.stringify(request)
|
|
107
|
+
});
|
|
108
|
+
const data = await response.json();
|
|
109
|
+
if (!response.ok || data.error) {
|
|
110
|
+
throw new Error(data.error || "Failed to create role");
|
|
111
|
+
}
|
|
112
|
+
return data.data;
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Update an existing role's permissions
|
|
116
|
+
*
|
|
117
|
+
* Updates the permissions for a role and automatically updates all
|
|
118
|
+
* Keto relationships for users assigned to this role. Old permissions
|
|
119
|
+
* are removed and new ones are created.
|
|
120
|
+
*
|
|
121
|
+
* @param roleId - ID of role to update
|
|
122
|
+
* @param request - Update request with new permissions
|
|
123
|
+
* @returns Updated role
|
|
124
|
+
*
|
|
125
|
+
* @example
|
|
126
|
+
* ```typescript
|
|
127
|
+
* const updatedRole = await omnibase.permissions.roles.update('role_123', {
|
|
128
|
+
* permissions: [
|
|
129
|
+
* 'tenant#manage_billing',
|
|
130
|
+
* 'tenant#view_invoices',
|
|
131
|
+
* 'tenant#manage_users' // Added new permission
|
|
132
|
+
* ]
|
|
133
|
+
* });
|
|
134
|
+
*
|
|
135
|
+
* console.log(`Updated role with ${updatedRole.permissions.length} permissions`);
|
|
136
|
+
* ```
|
|
137
|
+
*/
|
|
138
|
+
async update(roleId, request) {
|
|
139
|
+
const response = await this.client.fetch(`/api/v1/roles/roles/${roleId}`, {
|
|
140
|
+
method: "PUT",
|
|
141
|
+
headers: { "Content-Type": "application/json" },
|
|
142
|
+
body: JSON.stringify(request)
|
|
143
|
+
});
|
|
144
|
+
const data = await response.json();
|
|
145
|
+
if (!response.ok || data.error) {
|
|
146
|
+
throw new Error(data.error || "Failed to update role");
|
|
147
|
+
}
|
|
148
|
+
return data.data;
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Delete a role
|
|
152
|
+
*
|
|
153
|
+
* Deletes the role and automatically removes all Keto relationships
|
|
154
|
+
* for users assigned to this role. Cannot delete system roles.
|
|
155
|
+
*
|
|
156
|
+
* @param roleId - ID of role to delete
|
|
157
|
+
*
|
|
158
|
+
* @example
|
|
159
|
+
* ```typescript
|
|
160
|
+
* await omnibase.permissions.roles.delete('role_123');
|
|
161
|
+
* console.log('Role deleted successfully');
|
|
162
|
+
* ```
|
|
163
|
+
*/
|
|
164
|
+
async delete(roleId) {
|
|
165
|
+
const response = await this.client.fetch(`/api/v1/roles/roles/${roleId}`, {
|
|
166
|
+
method: "DELETE"
|
|
167
|
+
});
|
|
168
|
+
const data = await response.json();
|
|
169
|
+
if (!response.ok || data.error) {
|
|
170
|
+
throw new Error(data.error || "Failed to delete role");
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Assign a role to a user
|
|
175
|
+
*
|
|
176
|
+
* Assigns a role to a user and automatically creates all necessary
|
|
177
|
+
* Keto relationship tuples based on the role's permissions. The user
|
|
178
|
+
* immediately gains all permissions defined in the role.
|
|
179
|
+
*
|
|
180
|
+
* @param userId - ID of user to assign role to
|
|
181
|
+
* @param request - Assignment request with role ID
|
|
182
|
+
*
|
|
183
|
+
* @example
|
|
184
|
+
* ```typescript
|
|
185
|
+
* // Assign billing manager role to user
|
|
186
|
+
* await omnibase.permissions.roles.assign('user_123', {
|
|
187
|
+
* role_id: 'role_456'
|
|
188
|
+
* });
|
|
189
|
+
*
|
|
190
|
+
* // User now has all permissions from the role
|
|
191
|
+
* const canManageBilling = await omnibase.permissions.permissions.checkPermission(
|
|
192
|
+
* undefined,
|
|
193
|
+
* {
|
|
194
|
+
* namespace: 'Tenant',
|
|
195
|
+
* object: 'tenant_789',
|
|
196
|
+
* relation: 'manage_billing',
|
|
197
|
+
* subjectId: 'user_123'
|
|
198
|
+
* }
|
|
199
|
+
* );
|
|
200
|
+
* // canManageBilling.data.allowed === true
|
|
201
|
+
* ```
|
|
202
|
+
*/
|
|
203
|
+
async assign(userId, request) {
|
|
204
|
+
const response = await this.client.fetch(
|
|
205
|
+
`/api/v1/roles/users/${userId}/roles`,
|
|
206
|
+
{
|
|
207
|
+
method: "POST",
|
|
208
|
+
headers: { "Content-Type": "application/json" },
|
|
209
|
+
body: JSON.stringify(request)
|
|
210
|
+
}
|
|
211
|
+
);
|
|
212
|
+
const data = await response.json();
|
|
213
|
+
if (!response.ok || data.error) {
|
|
214
|
+
throw new Error(data.error || "Failed to assign role");
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
};
|
|
218
|
+
|
|
219
|
+
// src/permissions/handler.ts
|
|
220
|
+
var PermissionsClient = class {
|
|
221
|
+
/**
|
|
222
|
+
* Ory Keto RelationshipApi for managing subject-object relationships
|
|
223
|
+
*
|
|
224
|
+
* Provides methods for creating, updating, and deleting relationships between
|
|
225
|
+
* subjects (users, groups) and objects (tenants, resources). This API handles
|
|
226
|
+
* write operations and is used to establish permission structures.
|
|
227
|
+
*
|
|
228
|
+
* Key methods:
|
|
229
|
+
* - `createRelationship()` - Creates a new relationship tuple
|
|
230
|
+
* - `deleteRelationships()` - Removes existing relationship tuples
|
|
231
|
+
* - `getRelationships()` - Queries existing relationships
|
|
232
|
+
* - `patchRelationships()` - Updates multiple relationships atomically
|
|
233
|
+
*
|
|
234
|
+
* @example
|
|
235
|
+
* ```typescript
|
|
236
|
+
* // Create a relationship
|
|
237
|
+
* await client.relationships.createRelationship(
|
|
238
|
+
* undefined,
|
|
239
|
+
* {
|
|
240
|
+
* namespace: 'Tenant',
|
|
241
|
+
* object: 'tenant_123',
|
|
242
|
+
* relation: 'members',
|
|
243
|
+
* subjectId: 'user_456'
|
|
244
|
+
* }
|
|
245
|
+
* );
|
|
246
|
+
* ```
|
|
247
|
+
*
|
|
248
|
+
* @since 1.0.0
|
|
249
|
+
* @group Relationships
|
|
250
|
+
*/
|
|
251
|
+
relationships;
|
|
252
|
+
/**
|
|
253
|
+
* Ory Keto PermissionApi for checking permissions
|
|
254
|
+
*
|
|
255
|
+
* Provides methods for querying whether a subject has a specific permission
|
|
256
|
+
* on an object. This API handles read operations and is optimized for fast
|
|
257
|
+
* permission checks in your application logic.
|
|
258
|
+
*
|
|
259
|
+
* Key methods:
|
|
260
|
+
* - `checkPermission()` - Checks if a subject has permission on an object
|
|
261
|
+
* - `checkPermissionOrError()` - Same as above but throws error if denied
|
|
262
|
+
* - `expandPermissions()` - Expands relationships to show all granted permissions
|
|
263
|
+
*
|
|
264
|
+
* @example
|
|
265
|
+
* ```typescript
|
|
266
|
+
* // Check permission
|
|
267
|
+
* const result = await client.permissions.checkPermission(
|
|
268
|
+
* undefined,
|
|
269
|
+
* {
|
|
270
|
+
* namespace: 'Tenant',
|
|
271
|
+
* object: 'tenant_123',
|
|
272
|
+
* relation: 'view',
|
|
273
|
+
* subjectId: 'user_456'
|
|
274
|
+
* }
|
|
275
|
+
* );
|
|
276
|
+
*
|
|
277
|
+
* console.log('Has permission:', result.data.allowed);
|
|
278
|
+
* ```
|
|
279
|
+
*
|
|
280
|
+
* @since 1.0.0
|
|
281
|
+
* @group Permissions
|
|
282
|
+
*/
|
|
283
|
+
permissions;
|
|
284
|
+
/**
|
|
285
|
+
* Handler for managing roles and role-based permissions
|
|
286
|
+
*
|
|
287
|
+
* Provides methods for creating custom roles, assigning permissions,
|
|
288
|
+
* and managing role assignments. Works alongside the Keto-based
|
|
289
|
+
* permissions system to provide dynamic RBAC capabilities.
|
|
290
|
+
*
|
|
291
|
+
* @example
|
|
292
|
+
* ```typescript
|
|
293
|
+
* // Create a custom role
|
|
294
|
+
* const role = await omnibase.permissions.roles.create({
|
|
295
|
+
* role_name: 'billing_manager',
|
|
296
|
+
* permissions: ['tenant#manage_billing', 'tenant#view_invoices']
|
|
297
|
+
* });
|
|
298
|
+
*
|
|
299
|
+
* // Assign role to user
|
|
300
|
+
* await omnibase.permissions.roles.assign('user_123', {
|
|
301
|
+
* role_id: role.id
|
|
302
|
+
* });
|
|
303
|
+
* ```
|
|
304
|
+
*
|
|
305
|
+
* @since 0.7.0
|
|
306
|
+
* @group Roles
|
|
307
|
+
*/
|
|
308
|
+
roles;
|
|
309
|
+
/**
|
|
310
|
+
* Creates a new PermissionsClient instance
|
|
311
|
+
*
|
|
312
|
+
* Initializes the client with separate endpoints for read and write operations.
|
|
313
|
+
* The client automatically appends the appropriate Keto API paths to the base URL
|
|
314
|
+
* for optimal performance and security separation.
|
|
315
|
+
*
|
|
316
|
+
* @param apiBaseUrl - The base URL for your Omnibase API instance
|
|
317
|
+
* @param client - The main OmnibaseClient instance (for roles handler)
|
|
318
|
+
*
|
|
319
|
+
* @throws {Error} When the base URL is invalid or cannot be reached
|
|
320
|
+
*
|
|
321
|
+
* @example
|
|
322
|
+
* ```typescript
|
|
323
|
+
* const client = new PermissionsClient('https://api.example.com', omnibaseClient);
|
|
324
|
+
* ```
|
|
325
|
+
*
|
|
326
|
+
* @since 1.0.0
|
|
327
|
+
* @group Client
|
|
328
|
+
*/
|
|
329
|
+
constructor(apiBaseUrl, client) {
|
|
330
|
+
this.relationships = new RelationshipApi(
|
|
331
|
+
void 0,
|
|
332
|
+
`${apiBaseUrl}/api/v1/permissions/write`
|
|
333
|
+
);
|
|
334
|
+
this.permissions = new PermissionApi(
|
|
335
|
+
void 0,
|
|
336
|
+
`${apiBaseUrl}/api/v1/permissions/read`
|
|
337
|
+
);
|
|
338
|
+
this.roles = new RolesHandler(client);
|
|
339
|
+
}
|
|
340
|
+
};
|
|
341
|
+
|
|
342
|
+
export {
|
|
343
|
+
RolesHandler,
|
|
344
|
+
PermissionsClient
|
|
345
|
+
};
|
package/dist/index.cjs
CHANGED
|
@@ -21,6 +21,8 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
21
21
|
var index_exports = {};
|
|
22
22
|
__export(index_exports, {
|
|
23
23
|
OmnibaseClient: () => OmnibaseClient,
|
|
24
|
+
PermissionsClient: () => PermissionsClient,
|
|
25
|
+
RolesHandler: () => RolesHandler,
|
|
24
26
|
StorageClient: () => StorageClient
|
|
25
27
|
});
|
|
26
28
|
module.exports = __toCommonJS(index_exports);
|
|
@@ -510,6 +512,223 @@ var PaymentHandler = class {
|
|
|
510
512
|
|
|
511
513
|
// src/permissions/handler.ts
|
|
512
514
|
var import_client = require("@ory/client");
|
|
515
|
+
|
|
516
|
+
// src/permissions/roles.ts
|
|
517
|
+
var RolesHandler = class {
|
|
518
|
+
constructor(client) {
|
|
519
|
+
this.client = client;
|
|
520
|
+
}
|
|
521
|
+
/**
|
|
522
|
+
* Get available namespace definitions for UI
|
|
523
|
+
*
|
|
524
|
+
* Returns all namespaces and their available relations/permissions.
|
|
525
|
+
* Useful for building role configuration UIs.
|
|
526
|
+
*
|
|
527
|
+
* @returns List of namespace definitions
|
|
528
|
+
*
|
|
529
|
+
* @example
|
|
530
|
+
* ```typescript
|
|
531
|
+
* const definitions = await omnibase.permissions.roles.getDefinitions();
|
|
532
|
+
*
|
|
533
|
+
* // Output: [{ namespace: 'Tenant', relations: ['invite_user', 'delete_tenant', ...] }]
|
|
534
|
+
* definitions.forEach(def => {
|
|
535
|
+
* console.log(`${def.namespace} supports: ${def.relations.join(', ')}`);
|
|
536
|
+
* });
|
|
537
|
+
* ```
|
|
538
|
+
*/
|
|
539
|
+
async getDefinitions() {
|
|
540
|
+
const response = await this.client.fetch("/api/v1/roles/definitions", {
|
|
541
|
+
method: "GET"
|
|
542
|
+
});
|
|
543
|
+
const data = await response.json();
|
|
544
|
+
if (!response.ok || data.error) {
|
|
545
|
+
throw new Error(data.error || "Failed to fetch definitions");
|
|
546
|
+
}
|
|
547
|
+
return data.data.definitions;
|
|
548
|
+
}
|
|
549
|
+
/**
|
|
550
|
+
* List all roles for the current tenant
|
|
551
|
+
*
|
|
552
|
+
* Returns both system roles (defined in roles.config.json) and
|
|
553
|
+
* custom roles created via the API. System roles have `tenant_id = null`.
|
|
554
|
+
*
|
|
555
|
+
* @returns List of roles
|
|
556
|
+
*
|
|
557
|
+
* @example
|
|
558
|
+
* ```typescript
|
|
559
|
+
* const roles = await omnibase.permissions.roles.list();
|
|
560
|
+
*
|
|
561
|
+
* const systemRoles = roles.filter(r => r.tenant_id === null);
|
|
562
|
+
* const customRoles = roles.filter(r => r.tenant_id !== null);
|
|
563
|
+
*
|
|
564
|
+
* console.log(`System roles: ${systemRoles.map(r => r.role_name).join(', ')}`);
|
|
565
|
+
* console.log(`Custom roles: ${customRoles.map(r => r.role_name).join(', ')}`);
|
|
566
|
+
* ```
|
|
567
|
+
*/
|
|
568
|
+
async list() {
|
|
569
|
+
const response = await this.client.fetch("/api/v1/roles/roles", {
|
|
570
|
+
method: "GET"
|
|
571
|
+
});
|
|
572
|
+
const data = await response.json();
|
|
573
|
+
if (!response.ok || data.error) {
|
|
574
|
+
throw new Error(data.error || "Failed to list roles");
|
|
575
|
+
}
|
|
576
|
+
return data.data.roles;
|
|
577
|
+
}
|
|
578
|
+
/**
|
|
579
|
+
* Create a new custom role
|
|
580
|
+
*
|
|
581
|
+
* Creates a tenant-specific role with the specified permissions.
|
|
582
|
+
* Permissions use the format `namespace#relation` or `namespace:id#relation`.
|
|
583
|
+
*
|
|
584
|
+
* @param request - Role creation request
|
|
585
|
+
* @returns Created role
|
|
586
|
+
*
|
|
587
|
+
* @example
|
|
588
|
+
* ```typescript
|
|
589
|
+
* const role = await omnibase.permissions.roles.create({
|
|
590
|
+
* role_name: 'billing_manager',
|
|
591
|
+
* permissions: [
|
|
592
|
+
* 'tenant#manage_billing',
|
|
593
|
+
* 'tenant#view_invoices',
|
|
594
|
+
* 'tenant#update_payment_methods'
|
|
595
|
+
* ]
|
|
596
|
+
* });
|
|
597
|
+
*
|
|
598
|
+
* console.log(`Created role: ${role.id}`);
|
|
599
|
+
* ```
|
|
600
|
+
*
|
|
601
|
+
* @example
|
|
602
|
+
* Resource-specific permissions:
|
|
603
|
+
* ```typescript
|
|
604
|
+
* const devRole = await omnibase.permissions.roles.create({
|
|
605
|
+
* role_name: 'project_developer',
|
|
606
|
+
* permissions: [
|
|
607
|
+
* 'project:proj_abc123#deploy',
|
|
608
|
+
* 'project:proj_abc123#view_logs',
|
|
609
|
+
* 'tenant#invite_user'
|
|
610
|
+
* ]
|
|
611
|
+
* });
|
|
612
|
+
* ```
|
|
613
|
+
*/
|
|
614
|
+
async create(request) {
|
|
615
|
+
const response = await this.client.fetch("/api/v1/roles/roles", {
|
|
616
|
+
method: "POST",
|
|
617
|
+
headers: { "Content-Type": "application/json" },
|
|
618
|
+
body: JSON.stringify(request)
|
|
619
|
+
});
|
|
620
|
+
const data = await response.json();
|
|
621
|
+
if (!response.ok || data.error) {
|
|
622
|
+
throw new Error(data.error || "Failed to create role");
|
|
623
|
+
}
|
|
624
|
+
return data.data;
|
|
625
|
+
}
|
|
626
|
+
/**
|
|
627
|
+
* Update an existing role's permissions
|
|
628
|
+
*
|
|
629
|
+
* Updates the permissions for a role and automatically updates all
|
|
630
|
+
* Keto relationships for users assigned to this role. Old permissions
|
|
631
|
+
* are removed and new ones are created.
|
|
632
|
+
*
|
|
633
|
+
* @param roleId - ID of role to update
|
|
634
|
+
* @param request - Update request with new permissions
|
|
635
|
+
* @returns Updated role
|
|
636
|
+
*
|
|
637
|
+
* @example
|
|
638
|
+
* ```typescript
|
|
639
|
+
* const updatedRole = await omnibase.permissions.roles.update('role_123', {
|
|
640
|
+
* permissions: [
|
|
641
|
+
* 'tenant#manage_billing',
|
|
642
|
+
* 'tenant#view_invoices',
|
|
643
|
+
* 'tenant#manage_users' // Added new permission
|
|
644
|
+
* ]
|
|
645
|
+
* });
|
|
646
|
+
*
|
|
647
|
+
* console.log(`Updated role with ${updatedRole.permissions.length} permissions`);
|
|
648
|
+
* ```
|
|
649
|
+
*/
|
|
650
|
+
async update(roleId, request) {
|
|
651
|
+
const response = await this.client.fetch(`/api/v1/roles/roles/${roleId}`, {
|
|
652
|
+
method: "PUT",
|
|
653
|
+
headers: { "Content-Type": "application/json" },
|
|
654
|
+
body: JSON.stringify(request)
|
|
655
|
+
});
|
|
656
|
+
const data = await response.json();
|
|
657
|
+
if (!response.ok || data.error) {
|
|
658
|
+
throw new Error(data.error || "Failed to update role");
|
|
659
|
+
}
|
|
660
|
+
return data.data;
|
|
661
|
+
}
|
|
662
|
+
/**
|
|
663
|
+
* Delete a role
|
|
664
|
+
*
|
|
665
|
+
* Deletes the role and automatically removes all Keto relationships
|
|
666
|
+
* for users assigned to this role. Cannot delete system roles.
|
|
667
|
+
*
|
|
668
|
+
* @param roleId - ID of role to delete
|
|
669
|
+
*
|
|
670
|
+
* @example
|
|
671
|
+
* ```typescript
|
|
672
|
+
* await omnibase.permissions.roles.delete('role_123');
|
|
673
|
+
* console.log('Role deleted successfully');
|
|
674
|
+
* ```
|
|
675
|
+
*/
|
|
676
|
+
async delete(roleId) {
|
|
677
|
+
const response = await this.client.fetch(`/api/v1/roles/roles/${roleId}`, {
|
|
678
|
+
method: "DELETE"
|
|
679
|
+
});
|
|
680
|
+
const data = await response.json();
|
|
681
|
+
if (!response.ok || data.error) {
|
|
682
|
+
throw new Error(data.error || "Failed to delete role");
|
|
683
|
+
}
|
|
684
|
+
}
|
|
685
|
+
/**
|
|
686
|
+
* Assign a role to a user
|
|
687
|
+
*
|
|
688
|
+
* Assigns a role to a user and automatically creates all necessary
|
|
689
|
+
* Keto relationship tuples based on the role's permissions. The user
|
|
690
|
+
* immediately gains all permissions defined in the role.
|
|
691
|
+
*
|
|
692
|
+
* @param userId - ID of user to assign role to
|
|
693
|
+
* @param request - Assignment request with role ID
|
|
694
|
+
*
|
|
695
|
+
* @example
|
|
696
|
+
* ```typescript
|
|
697
|
+
* // Assign billing manager role to user
|
|
698
|
+
* await omnibase.permissions.roles.assign('user_123', {
|
|
699
|
+
* role_id: 'role_456'
|
|
700
|
+
* });
|
|
701
|
+
*
|
|
702
|
+
* // User now has all permissions from the role
|
|
703
|
+
* const canManageBilling = await omnibase.permissions.permissions.checkPermission(
|
|
704
|
+
* undefined,
|
|
705
|
+
* {
|
|
706
|
+
* namespace: 'Tenant',
|
|
707
|
+
* object: 'tenant_789',
|
|
708
|
+
* relation: 'manage_billing',
|
|
709
|
+
* subjectId: 'user_123'
|
|
710
|
+
* }
|
|
711
|
+
* );
|
|
712
|
+
* // canManageBilling.data.allowed === true
|
|
713
|
+
* ```
|
|
714
|
+
*/
|
|
715
|
+
async assign(userId, request) {
|
|
716
|
+
const response = await this.client.fetch(
|
|
717
|
+
`/api/v1/roles/users/${userId}/roles`,
|
|
718
|
+
{
|
|
719
|
+
method: "POST",
|
|
720
|
+
headers: { "Content-Type": "application/json" },
|
|
721
|
+
body: JSON.stringify(request)
|
|
722
|
+
}
|
|
723
|
+
);
|
|
724
|
+
const data = await response.json();
|
|
725
|
+
if (!response.ok || data.error) {
|
|
726
|
+
throw new Error(data.error || "Failed to assign role");
|
|
727
|
+
}
|
|
728
|
+
}
|
|
729
|
+
};
|
|
730
|
+
|
|
731
|
+
// src/permissions/handler.ts
|
|
513
732
|
var PermissionsClient = class {
|
|
514
733
|
/**
|
|
515
734
|
* Ory Keto RelationshipApi for managing subject-object relationships
|
|
@@ -574,6 +793,31 @@ var PermissionsClient = class {
|
|
|
574
793
|
* @group Permissions
|
|
575
794
|
*/
|
|
576
795
|
permissions;
|
|
796
|
+
/**
|
|
797
|
+
* Handler for managing roles and role-based permissions
|
|
798
|
+
*
|
|
799
|
+
* Provides methods for creating custom roles, assigning permissions,
|
|
800
|
+
* and managing role assignments. Works alongside the Keto-based
|
|
801
|
+
* permissions system to provide dynamic RBAC capabilities.
|
|
802
|
+
*
|
|
803
|
+
* @example
|
|
804
|
+
* ```typescript
|
|
805
|
+
* // Create a custom role
|
|
806
|
+
* const role = await omnibase.permissions.roles.create({
|
|
807
|
+
* role_name: 'billing_manager',
|
|
808
|
+
* permissions: ['tenant#manage_billing', 'tenant#view_invoices']
|
|
809
|
+
* });
|
|
810
|
+
*
|
|
811
|
+
* // Assign role to user
|
|
812
|
+
* await omnibase.permissions.roles.assign('user_123', {
|
|
813
|
+
* role_id: role.id
|
|
814
|
+
* });
|
|
815
|
+
* ```
|
|
816
|
+
*
|
|
817
|
+
* @since 0.7.0
|
|
818
|
+
* @group Roles
|
|
819
|
+
*/
|
|
820
|
+
roles;
|
|
577
821
|
/**
|
|
578
822
|
* Creates a new PermissionsClient instance
|
|
579
823
|
*
|
|
@@ -582,24 +826,19 @@ var PermissionsClient = class {
|
|
|
582
826
|
* for optimal performance and security separation.
|
|
583
827
|
*
|
|
584
828
|
* @param apiBaseUrl - The base URL for your Omnibase API instance
|
|
829
|
+
* @param client - The main OmnibaseClient instance (for roles handler)
|
|
585
830
|
*
|
|
586
831
|
* @throws {Error} When the base URL is invalid or cannot be reached
|
|
587
832
|
*
|
|
588
833
|
* @example
|
|
589
834
|
* ```typescript
|
|
590
|
-
* const client = new PermissionsClient('https://api.example.com');
|
|
591
|
-
* ```
|
|
592
|
-
*
|
|
593
|
-
* @example
|
|
594
|
-
* Local development:
|
|
595
|
-
* ```typescript
|
|
596
|
-
* const client = new PermissionsClient('http://localhost:8080');
|
|
835
|
+
* const client = new PermissionsClient('https://api.example.com', omnibaseClient);
|
|
597
836
|
* ```
|
|
598
837
|
*
|
|
599
838
|
* @since 1.0.0
|
|
600
839
|
* @group Client
|
|
601
840
|
*/
|
|
602
|
-
constructor(apiBaseUrl) {
|
|
841
|
+
constructor(apiBaseUrl, client) {
|
|
603
842
|
this.relationships = new import_client.RelationshipApi(
|
|
604
843
|
void 0,
|
|
605
844
|
`${apiBaseUrl}/api/v1/permissions/write`
|
|
@@ -608,6 +847,7 @@ var PermissionsClient = class {
|
|
|
608
847
|
void 0,
|
|
609
848
|
`${apiBaseUrl}/api/v1/permissions/read`
|
|
610
849
|
);
|
|
850
|
+
this.roles = new RolesHandler(client);
|
|
611
851
|
}
|
|
612
852
|
};
|
|
613
853
|
|
|
@@ -1389,7 +1629,7 @@ var TenantHandler = class {
|
|
|
1389
1629
|
var OmnibaseClient = class {
|
|
1390
1630
|
constructor(config) {
|
|
1391
1631
|
this.config = config;
|
|
1392
|
-
this.permissions = new PermissionsClient(this.config.api_url);
|
|
1632
|
+
this.permissions = new PermissionsClient(this.config.api_url, this);
|
|
1393
1633
|
}
|
|
1394
1634
|
/**
|
|
1395
1635
|
* Main payment handler for all payment-related operations
|
|
@@ -1527,5 +1767,7 @@ var OmnibaseClient = class {
|
|
|
1527
1767
|
// Annotate the CommonJS export names for ESM import in node:
|
|
1528
1768
|
0 && (module.exports = {
|
|
1529
1769
|
OmnibaseClient,
|
|
1770
|
+
PermissionsClient,
|
|
1771
|
+
RolesHandler,
|
|
1530
1772
|
StorageClient
|
|
1531
1773
|
});
|
package/dist/index.d.cts
CHANGED
|
@@ -1,3 +1,2 @@
|
|
|
1
|
-
export {
|
|
2
|
-
import './permissions/index.cjs';
|
|
1
|
+
export { q as ApiResponse, A as AssignRoleRequest, C as CreateRoleRequest, D as DownloadResult, N as NamespaceDefinition, p as OmnibaseClient, O as OmnibaseClientConfig, P as PermissionsClient, a as Role, R as RolesHandler, S as StorageClient, U as UpdateRoleRequest, b as UploadOptions, c as UploadResult } from './payments/index.cjs';
|
|
3
2
|
import '@ory/client';
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,2 @@
|
|
|
1
|
-
export {
|
|
2
|
-
import './permissions/index.js';
|
|
1
|
+
export { q as ApiResponse, A as AssignRoleRequest, C as CreateRoleRequest, D as DownloadResult, N as NamespaceDefinition, p as OmnibaseClient, O as OmnibaseClientConfig, P as PermissionsClient, a as Role, R as RolesHandler, S as StorageClient, U as UpdateRoleRequest, b as UploadOptions, c as UploadResult } from './payments/index.js';
|
|
3
2
|
import '@ory/client';
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import {
|
|
2
|
-
PermissionsClient
|
|
3
|
-
|
|
2
|
+
PermissionsClient,
|
|
3
|
+
RolesHandler
|
|
4
|
+
} from "./chunk-JNM7XP7L.js";
|
|
4
5
|
import {
|
|
5
6
|
PaymentHandler
|
|
6
7
|
} from "./chunk-QPW6G4PA.js";
|
|
@@ -15,7 +16,7 @@ import {
|
|
|
15
16
|
var OmnibaseClient = class {
|
|
16
17
|
constructor(config) {
|
|
17
18
|
this.config = config;
|
|
18
|
-
this.permissions = new PermissionsClient(this.config.api_url);
|
|
19
|
+
this.permissions = new PermissionsClient(this.config.api_url, this);
|
|
19
20
|
}
|
|
20
21
|
/**
|
|
21
22
|
* Main payment handler for all payment-related operations
|
|
@@ -152,5 +153,7 @@ var OmnibaseClient = class {
|
|
|
152
153
|
};
|
|
153
154
|
export {
|
|
154
155
|
OmnibaseClient,
|
|
156
|
+
PermissionsClient,
|
|
157
|
+
RolesHandler,
|
|
155
158
|
StorageClient
|
|
156
159
|
};
|