@omnibase/core-js 0.7.1 → 0.7.3
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/chunk-V4FWENQQ.js +378 -0
- package/dist/index.cjs +284 -9
- package/dist/index.d.cts +1 -2
- package/dist/index.d.ts +1 -2
- package/dist/index.js +9 -6
- package/dist/payments/index.d.cts +647 -3
- package/dist/payments/index.d.ts +647 -3
- package/dist/permissions/index.cjs +283 -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
|
+
};
|
|
@@ -0,0 +1,378 @@
|
|
|
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(
|
|
29
|
+
"/api/v1/permissions/definitions",
|
|
30
|
+
{
|
|
31
|
+
method: "GET"
|
|
32
|
+
}
|
|
33
|
+
);
|
|
34
|
+
const data = await response.json();
|
|
35
|
+
if (!response.ok || data.error) {
|
|
36
|
+
throw new Error(data.error || "Failed to fetch definitions");
|
|
37
|
+
}
|
|
38
|
+
return data.data.definitions;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* List all roles for the current tenant
|
|
42
|
+
*
|
|
43
|
+
* Returns both system roles (defined in roles.config.json) and
|
|
44
|
+
* custom roles created via the API. System roles have `tenant_id = null`.
|
|
45
|
+
*
|
|
46
|
+
* @returns List of roles
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* ```typescript
|
|
50
|
+
* const roles = await omnibase.permissions.roles.list();
|
|
51
|
+
*
|
|
52
|
+
* const systemRoles = roles.filter(r => r.tenant_id === null);
|
|
53
|
+
* const customRoles = roles.filter(r => r.tenant_id !== null);
|
|
54
|
+
*
|
|
55
|
+
* console.log(`System roles: ${systemRoles.map(r => r.role_name).join(', ')}`);
|
|
56
|
+
* console.log(`Custom roles: ${customRoles.map(r => r.role_name).join(', ')}`);
|
|
57
|
+
* ```
|
|
58
|
+
*/
|
|
59
|
+
async list() {
|
|
60
|
+
const response = await this.client.fetch("/api/v1/permissions/roles", {
|
|
61
|
+
method: "GET"
|
|
62
|
+
});
|
|
63
|
+
const data = await response.json();
|
|
64
|
+
if (!response.ok || data.error) {
|
|
65
|
+
throw new Error(data.error || "Failed to list roles");
|
|
66
|
+
}
|
|
67
|
+
return data.data.roles;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Create a new custom role
|
|
71
|
+
*
|
|
72
|
+
* Creates a tenant-specific role with the specified permissions.
|
|
73
|
+
* Permissions use the format `namespace#relation` or `namespace:id#relation`.
|
|
74
|
+
*
|
|
75
|
+
* @param request - Role creation request
|
|
76
|
+
* @returns Created role
|
|
77
|
+
*
|
|
78
|
+
* @example
|
|
79
|
+
* ```typescript
|
|
80
|
+
* const role = await omnibase.permissions.roles.create({
|
|
81
|
+
* role_name: 'billing_manager',
|
|
82
|
+
* permissions: [
|
|
83
|
+
* 'tenant#manage_billing',
|
|
84
|
+
* 'tenant#view_invoices',
|
|
85
|
+
* 'tenant#update_payment_methods'
|
|
86
|
+
* ]
|
|
87
|
+
* });
|
|
88
|
+
*
|
|
89
|
+
* console.log(`Created role: ${role.id}`);
|
|
90
|
+
* ```
|
|
91
|
+
*
|
|
92
|
+
* @example
|
|
93
|
+
* Resource-specific permissions:
|
|
94
|
+
* ```typescript
|
|
95
|
+
* const devRole = await omnibase.permissions.roles.create({
|
|
96
|
+
* role_name: 'project_developer',
|
|
97
|
+
* permissions: [
|
|
98
|
+
* 'project:proj_abc123#deploy',
|
|
99
|
+
* 'project:proj_abc123#view_logs',
|
|
100
|
+
* 'tenant#invite_user'
|
|
101
|
+
* ]
|
|
102
|
+
* });
|
|
103
|
+
* ```
|
|
104
|
+
*/
|
|
105
|
+
async create(request) {
|
|
106
|
+
const response = await this.client.fetch("/api/v1/permissions/roles", {
|
|
107
|
+
method: "POST",
|
|
108
|
+
headers: { "Content-Type": "application/json" },
|
|
109
|
+
body: JSON.stringify(request)
|
|
110
|
+
});
|
|
111
|
+
const data = await response.json();
|
|
112
|
+
if (!response.ok || data.error) {
|
|
113
|
+
throw new Error(data.error || "Failed to create role");
|
|
114
|
+
}
|
|
115
|
+
return data.data;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Update an existing role's permissions
|
|
119
|
+
*
|
|
120
|
+
* Updates the permissions for a role and automatically updates all
|
|
121
|
+
* Keto relationships for users assigned to this role. Old permissions
|
|
122
|
+
* are removed and new ones are created.
|
|
123
|
+
*
|
|
124
|
+
* @param roleId - ID of role to update
|
|
125
|
+
* @param request - Update request with new permissions
|
|
126
|
+
* @returns Updated role
|
|
127
|
+
*
|
|
128
|
+
* @example
|
|
129
|
+
* ```typescript
|
|
130
|
+
* const updatedRole = await omnibase.permissions.roles.update('role_123', {
|
|
131
|
+
* permissions: [
|
|
132
|
+
* 'tenant#manage_billing',
|
|
133
|
+
* 'tenant#view_invoices',
|
|
134
|
+
* 'tenant#manage_users' // Added new permission
|
|
135
|
+
* ]
|
|
136
|
+
* });
|
|
137
|
+
*
|
|
138
|
+
* console.log(`Updated role with ${updatedRole.permissions.length} permissions`);
|
|
139
|
+
* ```
|
|
140
|
+
*/
|
|
141
|
+
async update(roleId, request) {
|
|
142
|
+
const response = await this.client.fetch(
|
|
143
|
+
`/api/v1/permissions/roles/${roleId}`,
|
|
144
|
+
{
|
|
145
|
+
method: "PUT",
|
|
146
|
+
headers: { "Content-Type": "application/json" },
|
|
147
|
+
body: JSON.stringify(request)
|
|
148
|
+
}
|
|
149
|
+
);
|
|
150
|
+
const data = await response.json();
|
|
151
|
+
if (!response.ok || data.error) {
|
|
152
|
+
throw new Error(data.error || "Failed to update role");
|
|
153
|
+
}
|
|
154
|
+
return data.data;
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Delete a role
|
|
158
|
+
*
|
|
159
|
+
* Deletes the role and automatically removes all Keto relationships
|
|
160
|
+
* for users assigned to this role. Cannot delete system roles.
|
|
161
|
+
*
|
|
162
|
+
* @param roleId - ID of role to delete
|
|
163
|
+
*
|
|
164
|
+
* @example
|
|
165
|
+
* ```typescript
|
|
166
|
+
* await omnibase.permissions.roles.delete('role_123');
|
|
167
|
+
* console.log('Role deleted successfully');
|
|
168
|
+
* ```
|
|
169
|
+
*/
|
|
170
|
+
async delete(roleId) {
|
|
171
|
+
const response = await this.client.fetch(
|
|
172
|
+
`/api/v1/permissions/roles/${roleId}`,
|
|
173
|
+
{
|
|
174
|
+
method: "DELETE"
|
|
175
|
+
}
|
|
176
|
+
);
|
|
177
|
+
const data = await response.json();
|
|
178
|
+
if (!response.ok || data.error) {
|
|
179
|
+
throw new Error(data.error || "Failed to delete role");
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Assign a role to a user
|
|
184
|
+
*
|
|
185
|
+
* Assigns a role to a user and automatically creates all necessary
|
|
186
|
+
* Keto relationship tuples based on the role's permissions. The user
|
|
187
|
+
* immediately gains all permissions defined in the role.
|
|
188
|
+
*
|
|
189
|
+
* Supports assignment by either role ID or role name for flexibility.
|
|
190
|
+
*
|
|
191
|
+
* @param userId - ID of user to assign role to
|
|
192
|
+
* @param request - Assignment request with either role_id or role_name
|
|
193
|
+
*
|
|
194
|
+
* @example
|
|
195
|
+
* Assign by role ID:
|
|
196
|
+
* ```typescript
|
|
197
|
+
* await omnibase.permissions.roles.assign('user_123', {
|
|
198
|
+
* role_id: 'role_456'
|
|
199
|
+
* });
|
|
200
|
+
* ```
|
|
201
|
+
*
|
|
202
|
+
* @example
|
|
203
|
+
* Assign by role name (system or custom role):
|
|
204
|
+
* ```typescript
|
|
205
|
+
* // Assign system role
|
|
206
|
+
* await omnibase.permissions.roles.assign('user_123', {
|
|
207
|
+
* role_name: 'owner'
|
|
208
|
+
* });
|
|
209
|
+
*
|
|
210
|
+
* // Assign custom role
|
|
211
|
+
* await omnibase.permissions.roles.assign('user_456', {
|
|
212
|
+
* role_name: 'billing_manager'
|
|
213
|
+
* });
|
|
214
|
+
* ```
|
|
215
|
+
*
|
|
216
|
+
* @example
|
|
217
|
+
* Verify permissions after assignment:
|
|
218
|
+
* ```typescript
|
|
219
|
+
* await omnibase.permissions.roles.assign('user_123', {
|
|
220
|
+
* role_name: 'admin'
|
|
221
|
+
* });
|
|
222
|
+
*
|
|
223
|
+
* // User now has all permissions from the admin role
|
|
224
|
+
* const canManage = await omnibase.permissions.permissions.checkPermission(
|
|
225
|
+
* undefined,
|
|
226
|
+
* {
|
|
227
|
+
* namespace: 'Tenant',
|
|
228
|
+
* object: 'tenant_789',
|
|
229
|
+
* relation: 'manage_billing',
|
|
230
|
+
* subjectId: 'user_123'
|
|
231
|
+
* }
|
|
232
|
+
* );
|
|
233
|
+
* // canManage.data.allowed === true
|
|
234
|
+
* ```
|
|
235
|
+
*/
|
|
236
|
+
async assign(userId, request) {
|
|
237
|
+
const response = await this.client.fetch(
|
|
238
|
+
`/api/v1/permissions/users/${userId}/roles`,
|
|
239
|
+
{
|
|
240
|
+
method: "POST",
|
|
241
|
+
headers: { "Content-Type": "application/json" },
|
|
242
|
+
body: JSON.stringify(request)
|
|
243
|
+
}
|
|
244
|
+
);
|
|
245
|
+
const data = await response.json();
|
|
246
|
+
if (!response.ok || data.error) {
|
|
247
|
+
throw new Error(data.error || "Failed to assign role");
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
};
|
|
251
|
+
|
|
252
|
+
// src/permissions/handler.ts
|
|
253
|
+
var PermissionsClient = class {
|
|
254
|
+
/**
|
|
255
|
+
* Ory Keto RelationshipApi for managing subject-object relationships
|
|
256
|
+
*
|
|
257
|
+
* Provides methods for creating, updating, and deleting relationships between
|
|
258
|
+
* subjects (users, groups) and objects (tenants, resources). This API handles
|
|
259
|
+
* write operations and is used to establish permission structures.
|
|
260
|
+
*
|
|
261
|
+
* Key methods:
|
|
262
|
+
* - `createRelationship()` - Creates a new relationship tuple
|
|
263
|
+
* - `deleteRelationships()` - Removes existing relationship tuples
|
|
264
|
+
* - `getRelationships()` - Queries existing relationships
|
|
265
|
+
* - `patchRelationships()` - Updates multiple relationships atomically
|
|
266
|
+
*
|
|
267
|
+
* @example
|
|
268
|
+
* ```typescript
|
|
269
|
+
* // Create a relationship
|
|
270
|
+
* await client.relationships.createRelationship(
|
|
271
|
+
* undefined,
|
|
272
|
+
* {
|
|
273
|
+
* namespace: 'Tenant',
|
|
274
|
+
* object: 'tenant_123',
|
|
275
|
+
* relation: 'members',
|
|
276
|
+
* subjectId: 'user_456'
|
|
277
|
+
* }
|
|
278
|
+
* );
|
|
279
|
+
* ```
|
|
280
|
+
*
|
|
281
|
+
* @since 1.0.0
|
|
282
|
+
* @group Relationships
|
|
283
|
+
*/
|
|
284
|
+
relationships;
|
|
285
|
+
/**
|
|
286
|
+
* Ory Keto PermissionApi for checking permissions
|
|
287
|
+
*
|
|
288
|
+
* Provides methods for querying whether a subject has a specific permission
|
|
289
|
+
* on an object. This API handles read operations and is optimized for fast
|
|
290
|
+
* permission checks in your application logic.
|
|
291
|
+
*
|
|
292
|
+
* Key methods:
|
|
293
|
+
* - `checkPermission()` - Checks if a subject has permission on an object
|
|
294
|
+
* - `checkPermissionOrError()` - Same as above but throws error if denied
|
|
295
|
+
* - `expandPermissions()` - Expands relationships to show all granted permissions
|
|
296
|
+
*
|
|
297
|
+
* @example
|
|
298
|
+
* ```typescript
|
|
299
|
+
* // Check permission
|
|
300
|
+
* const result = await client.permissions.checkPermission(
|
|
301
|
+
* undefined,
|
|
302
|
+
* {
|
|
303
|
+
* namespace: 'Tenant',
|
|
304
|
+
* object: 'tenant_123',
|
|
305
|
+
* relation: 'view',
|
|
306
|
+
* subjectId: 'user_456'
|
|
307
|
+
* }
|
|
308
|
+
* );
|
|
309
|
+
*
|
|
310
|
+
* console.log('Has permission:', result.data.allowed);
|
|
311
|
+
* ```
|
|
312
|
+
*
|
|
313
|
+
* @since 1.0.0
|
|
314
|
+
* @group Permissions
|
|
315
|
+
*/
|
|
316
|
+
permissions;
|
|
317
|
+
/**
|
|
318
|
+
* Handler for managing roles and role-based permissions
|
|
319
|
+
*
|
|
320
|
+
* Provides methods for creating custom roles, assigning permissions,
|
|
321
|
+
* and managing role assignments. Works alongside the Keto-based
|
|
322
|
+
* permissions system to provide dynamic RBAC capabilities.
|
|
323
|
+
*
|
|
324
|
+
* @example
|
|
325
|
+
* ```typescript
|
|
326
|
+
* // Create a custom role
|
|
327
|
+
* const role = await omnibase.permissions.roles.create({
|
|
328
|
+
* role_name: 'billing_manager',
|
|
329
|
+
* permissions: ['tenant#manage_billing', 'tenant#view_invoices']
|
|
330
|
+
* });
|
|
331
|
+
*
|
|
332
|
+
* // Assign role to user
|
|
333
|
+
* await omnibase.permissions.roles.assign('user_123', {
|
|
334
|
+
* role_id: role.id
|
|
335
|
+
* });
|
|
336
|
+
* ```
|
|
337
|
+
*
|
|
338
|
+
* @since 0.7.0
|
|
339
|
+
* @group Roles
|
|
340
|
+
*/
|
|
341
|
+
roles;
|
|
342
|
+
/**
|
|
343
|
+
* Creates a new PermissionsClient instance
|
|
344
|
+
*
|
|
345
|
+
* Initializes the client with separate endpoints for read and write operations.
|
|
346
|
+
* The client automatically appends the appropriate Keto API paths to the base URL
|
|
347
|
+
* for optimal performance and security separation.
|
|
348
|
+
*
|
|
349
|
+
* @param apiBaseUrl - The base URL for your Omnibase API instance
|
|
350
|
+
* @param client - The main OmnibaseClient instance (for roles handler)
|
|
351
|
+
*
|
|
352
|
+
* @throws {Error} When the base URL is invalid or cannot be reached
|
|
353
|
+
*
|
|
354
|
+
* @example
|
|
355
|
+
* ```typescript
|
|
356
|
+
* const client = new PermissionsClient('https://api.example.com', omnibaseClient);
|
|
357
|
+
* ```
|
|
358
|
+
*
|
|
359
|
+
* @since 1.0.0
|
|
360
|
+
* @group Client
|
|
361
|
+
*/
|
|
362
|
+
constructor(apiBaseUrl, client) {
|
|
363
|
+
this.relationships = new RelationshipApi(
|
|
364
|
+
void 0,
|
|
365
|
+
`${apiBaseUrl}/api/v1/permissions/write`
|
|
366
|
+
);
|
|
367
|
+
this.permissions = new PermissionApi(
|
|
368
|
+
void 0,
|
|
369
|
+
`${apiBaseUrl}/api/v1/permissions/read`
|
|
370
|
+
);
|
|
371
|
+
this.roles = new RolesHandler(client);
|
|
372
|
+
}
|
|
373
|
+
};
|
|
374
|
+
|
|
375
|
+
export {
|
|
376
|
+
RolesHandler,
|
|
377
|
+
PermissionsClient
|
|
378
|
+
};
|