@amaster.ai/admin-sdk 1.1.71 → 1.1.73
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/README.md +69 -17
- package/dist/index.cjs +195 -503
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +99 -493
- package/dist/index.d.ts +99 -493
- package/dist/index.js +192 -500
- package/dist/index.js.map +1 -1
- package/package.json +6 -1
package/dist/index.js
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
import { createRolesModule, createPermissionsModule, createDepartmentsModule } from '@amaster.ai/auth-client/rbac';
|
|
2
|
+
import { RequestScopeController, createHttpClient as createHttpClient$1, createFetchAdapter } from '@amaster.ai/http-client';
|
|
3
|
+
import { createEntityClient } from '@amaster.ai/entity-client';
|
|
4
|
+
import { createTenancyManagement } from '@amaster.ai/auth-client/tenant-management';
|
|
5
|
+
|
|
6
|
+
// src/admin-sdk.ts
|
|
7
|
+
|
|
1
8
|
// src/error.ts
|
|
2
9
|
var AdminSDKError = class extends Error {
|
|
3
10
|
constructor(message, statusCode, response) {
|
|
@@ -9,7 +16,7 @@ var AdminSDKError = class extends Error {
|
|
|
9
16
|
};
|
|
10
17
|
|
|
11
18
|
// src/http.ts
|
|
12
|
-
function createHttpClient(baseURL, commonHeaders) {
|
|
19
|
+
function createHttpClient(baseURL, commonHeaders, resolveHeaders) {
|
|
13
20
|
const normalizedBase = baseURL.replace(/\/$/, "");
|
|
14
21
|
return async function request(options) {
|
|
15
22
|
const { method, path, body, query } = options;
|
|
@@ -24,14 +31,18 @@ function createHttpClient(baseURL, commonHeaders) {
|
|
|
24
31
|
const qs = params.toString();
|
|
25
32
|
if (qs) url += "?" + qs;
|
|
26
33
|
}
|
|
34
|
+
const serializedBody = body !== void 0 ? JSON.stringify(body) : void 0;
|
|
27
35
|
const headers = {
|
|
28
36
|
"Content-Type": "application/json",
|
|
29
|
-
...commonHeaders
|
|
37
|
+
...commonHeaders,
|
|
38
|
+
...resolveHeaders ? await resolveHeaders() : {}
|
|
30
39
|
};
|
|
31
40
|
const res = await fetch(url, {
|
|
32
41
|
method,
|
|
42
|
+
...options.redirect ? { redirect: options.redirect } : {},
|
|
43
|
+
...options.credentials ? { credentials: options.credentials } : {},
|
|
33
44
|
headers,
|
|
34
|
-
body:
|
|
45
|
+
body: serializedBody
|
|
35
46
|
});
|
|
36
47
|
let json;
|
|
37
48
|
try {
|
|
@@ -137,501 +148,6 @@ function createAuthModule(request) {
|
|
|
137
148
|
};
|
|
138
149
|
}
|
|
139
150
|
|
|
140
|
-
// src/modules/departments.ts
|
|
141
|
-
function createDepartmentsModule(request) {
|
|
142
|
-
return {
|
|
143
|
-
/**
|
|
144
|
-
* List all departments (flat list).
|
|
145
|
-
*
|
|
146
|
-
* @example
|
|
147
|
-
* ```typescript
|
|
148
|
-
* const depts = await admin.departments.list();
|
|
149
|
-
* const techDept = depts.find(d => d.name === '技术部');
|
|
150
|
-
* ```
|
|
151
|
-
*/
|
|
152
|
-
async list() {
|
|
153
|
-
return request({
|
|
154
|
-
method: "GET",
|
|
155
|
-
path: "/api/internal/org/departments"
|
|
156
|
-
});
|
|
157
|
-
},
|
|
158
|
-
/**
|
|
159
|
-
* Get a single department by ID.
|
|
160
|
-
*
|
|
161
|
-
* @example
|
|
162
|
-
* ```typescript
|
|
163
|
-
* const dept = await admin.departments.get(1);
|
|
164
|
-
* ```
|
|
165
|
-
*/
|
|
166
|
-
async get(id) {
|
|
167
|
-
return request({
|
|
168
|
-
method: "GET",
|
|
169
|
-
path: `/api/internal/org/departments/${id}`
|
|
170
|
-
});
|
|
171
|
-
},
|
|
172
|
-
/**
|
|
173
|
-
* Create a new department.
|
|
174
|
-
*
|
|
175
|
-
* @example
|
|
176
|
-
* ```typescript
|
|
177
|
-
* const dept = await admin.departments.create({
|
|
178
|
-
* name: '测试组',
|
|
179
|
-
* parentId: 1,
|
|
180
|
-
* });
|
|
181
|
-
* ```
|
|
182
|
-
*/
|
|
183
|
-
async create(params) {
|
|
184
|
-
return request({
|
|
185
|
-
method: "POST",
|
|
186
|
-
path: "/api/internal/org/departments",
|
|
187
|
-
body: params
|
|
188
|
-
});
|
|
189
|
-
},
|
|
190
|
-
/**
|
|
191
|
-
* Update a department.
|
|
192
|
-
*
|
|
193
|
-
* @example
|
|
194
|
-
* ```typescript
|
|
195
|
-
* await admin.departments.update(3, { name: '质量保证组' });
|
|
196
|
-
* ```
|
|
197
|
-
*/
|
|
198
|
-
async update(id, params) {
|
|
199
|
-
return request({
|
|
200
|
-
method: "PUT",
|
|
201
|
-
path: `/api/internal/org/departments/${id}`,
|
|
202
|
-
body: params
|
|
203
|
-
});
|
|
204
|
-
},
|
|
205
|
-
/**
|
|
206
|
-
* Delete a department.
|
|
207
|
-
* Note: departments with sub-departments or assigned users cannot be deleted directly.
|
|
208
|
-
*
|
|
209
|
-
* @example
|
|
210
|
-
* ```typescript
|
|
211
|
-
* await admin.departments.delete(3);
|
|
212
|
-
* ```
|
|
213
|
-
*/
|
|
214
|
-
async delete(id) {
|
|
215
|
-
return request({
|
|
216
|
-
method: "DELETE",
|
|
217
|
-
path: `/api/internal/org/departments/${id}`
|
|
218
|
-
});
|
|
219
|
-
},
|
|
220
|
-
/**
|
|
221
|
-
* List members of a department.
|
|
222
|
-
*
|
|
223
|
-
* @param includeChildren - Whether to include members from sub-departments. Defaults to false.
|
|
224
|
-
*
|
|
225
|
-
* @example
|
|
226
|
-
* ```typescript
|
|
227
|
-
* const members = await admin.departments.listUsers(1);
|
|
228
|
-
* const allMembers = await admin.departments.listUsers(1, true);
|
|
229
|
-
* ```
|
|
230
|
-
*/
|
|
231
|
-
async listUsers(id, includeChildren = false) {
|
|
232
|
-
return request({
|
|
233
|
-
method: "GET",
|
|
234
|
-
path: `/api/internal/org/departments/${id}/users`,
|
|
235
|
-
query: { include_children: includeChildren }
|
|
236
|
-
});
|
|
237
|
-
},
|
|
238
|
-
/**
|
|
239
|
-
* Add a user to a department.
|
|
240
|
-
*
|
|
241
|
-
* @example
|
|
242
|
-
* ```typescript
|
|
243
|
-
* await admin.departments.addUser(deptId, {
|
|
244
|
-
* userUid: user.uid,
|
|
245
|
-
* isPrimary: true,
|
|
246
|
-
* position: '高级工程师',
|
|
247
|
-
* });
|
|
248
|
-
* ```
|
|
249
|
-
*/
|
|
250
|
-
async addUser(deptId, params) {
|
|
251
|
-
return request({
|
|
252
|
-
method: "POST",
|
|
253
|
-
path: `/api/internal/org/departments/${deptId}/users`,
|
|
254
|
-
body: params
|
|
255
|
-
});
|
|
256
|
-
},
|
|
257
|
-
/**
|
|
258
|
-
* Remove a user from a department.
|
|
259
|
-
*
|
|
260
|
-
* @example
|
|
261
|
-
* ```typescript
|
|
262
|
-
* await admin.departments.removeUser(deptId, user.uid);
|
|
263
|
-
* ```
|
|
264
|
-
*/
|
|
265
|
-
async removeUser(deptId, userUid) {
|
|
266
|
-
return request({
|
|
267
|
-
method: "DELETE",
|
|
268
|
-
path: `/api/internal/org/departments/${deptId}/users/${userUid}`
|
|
269
|
-
});
|
|
270
|
-
}
|
|
271
|
-
};
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
// src/modules/permissions.ts
|
|
275
|
-
function createPermissionsModule(request) {
|
|
276
|
-
return {
|
|
277
|
-
/**
|
|
278
|
-
* List all defined permissions.
|
|
279
|
-
*
|
|
280
|
-
* @example
|
|
281
|
-
* ```typescript
|
|
282
|
-
* const perms = await admin.permissions.list();
|
|
283
|
-
* const orderRead = perms.find(p => p.resource === 'order' && p.action === 'read');
|
|
284
|
-
* ```
|
|
285
|
-
*/
|
|
286
|
-
async list() {
|
|
287
|
-
return request({
|
|
288
|
-
method: "GET",
|
|
289
|
-
path: "/api/internal/rbac/permissions"
|
|
290
|
-
});
|
|
291
|
-
},
|
|
292
|
-
/**
|
|
293
|
-
* Create a new permission definition.
|
|
294
|
-
*
|
|
295
|
-
* @example
|
|
296
|
-
* ```typescript
|
|
297
|
-
* const perm = await admin.permissions.create({
|
|
298
|
-
* resource: 'order',
|
|
299
|
-
* action: 'export',
|
|
300
|
-
* name: '导出订单',
|
|
301
|
-
* });
|
|
302
|
-
* ```
|
|
303
|
-
*/
|
|
304
|
-
async create(params) {
|
|
305
|
-
return request({
|
|
306
|
-
method: "POST",
|
|
307
|
-
path: "/api/internal/rbac/permissions",
|
|
308
|
-
body: params
|
|
309
|
-
});
|
|
310
|
-
},
|
|
311
|
-
/**
|
|
312
|
-
* Update a permission definition.
|
|
313
|
-
*
|
|
314
|
-
* @example
|
|
315
|
-
* ```typescript
|
|
316
|
-
* await admin.permissions.update(10, { name: '导出报表' });
|
|
317
|
-
* ```
|
|
318
|
-
*/
|
|
319
|
-
async update(id, params) {
|
|
320
|
-
return request({
|
|
321
|
-
method: "PUT",
|
|
322
|
-
path: `/api/internal/rbac/permissions/${id}`,
|
|
323
|
-
body: params
|
|
324
|
-
});
|
|
325
|
-
},
|
|
326
|
-
/**
|
|
327
|
-
* Delete a permission definition.
|
|
328
|
-
*
|
|
329
|
-
* @example
|
|
330
|
-
* ```typescript
|
|
331
|
-
* await admin.permissions.delete(10);
|
|
332
|
-
* ```
|
|
333
|
-
*/
|
|
334
|
-
async delete(id) {
|
|
335
|
-
return request({
|
|
336
|
-
method: "DELETE",
|
|
337
|
-
path: `/api/internal/rbac/permissions/${id}`
|
|
338
|
-
});
|
|
339
|
-
},
|
|
340
|
-
/**
|
|
341
|
-
* List all permissions assigned to a role.
|
|
342
|
-
*
|
|
343
|
-
* @example
|
|
344
|
-
* ```typescript
|
|
345
|
-
* const perms = await admin.permissions.listForRole(roleId);
|
|
346
|
-
* ```
|
|
347
|
-
*/
|
|
348
|
-
async listForRole(roleId) {
|
|
349
|
-
return request({
|
|
350
|
-
method: "GET",
|
|
351
|
-
path: `/api/internal/rbac/roles/${roleId}/permissions`
|
|
352
|
-
});
|
|
353
|
-
},
|
|
354
|
-
/**
|
|
355
|
-
* Assign a permission to a role with a data scope.
|
|
356
|
-
*
|
|
357
|
-
* @example
|
|
358
|
-
* ```typescript
|
|
359
|
-
* // Department-scoped permission
|
|
360
|
-
* await admin.permissions.assignToRole(roleId, {
|
|
361
|
-
* permissionId: 10,
|
|
362
|
-
* scopeType: 'dep',
|
|
363
|
-
* });
|
|
364
|
-
*
|
|
365
|
-
* // Custom whitelist scope
|
|
366
|
-
* await admin.permissions.assignToRole(roleId, {
|
|
367
|
-
* permissionId: 20,
|
|
368
|
-
* scopeType: 'custom',
|
|
369
|
-
* scopeFilter: JSON.stringify({ id: [1, 2, 3] }),
|
|
370
|
-
* });
|
|
371
|
-
* ```
|
|
372
|
-
*/
|
|
373
|
-
async assignToRole(roleId, params) {
|
|
374
|
-
return request({
|
|
375
|
-
method: "POST",
|
|
376
|
-
path: `/api/internal/rbac/roles/${roleId}/permissions`,
|
|
377
|
-
body: params
|
|
378
|
-
});
|
|
379
|
-
},
|
|
380
|
-
/**
|
|
381
|
-
* Remove a permission from a role.
|
|
382
|
-
*
|
|
383
|
-
* @example
|
|
384
|
-
* ```typescript
|
|
385
|
-
* await admin.permissions.removeFromRole(roleId, permId);
|
|
386
|
-
* ```
|
|
387
|
-
*/
|
|
388
|
-
async removeFromRole(roleId, permId) {
|
|
389
|
-
return request({
|
|
390
|
-
method: "DELETE",
|
|
391
|
-
path: `/api/internal/rbac/roles/${roleId}/permissions/${permId}`
|
|
392
|
-
});
|
|
393
|
-
},
|
|
394
|
-
/**
|
|
395
|
-
* Update the data scope of a role–permission assignment.
|
|
396
|
-
*
|
|
397
|
-
* @example
|
|
398
|
-
* ```typescript
|
|
399
|
-
* await admin.permissions.updateRolePermissionScope(roleId, permId, {
|
|
400
|
-
* scopeType: 'all',
|
|
401
|
-
* });
|
|
402
|
-
* ```
|
|
403
|
-
*/
|
|
404
|
-
async updateRolePermissionScope(roleId, permId, params) {
|
|
405
|
-
return request({
|
|
406
|
-
method: "PUT",
|
|
407
|
-
path: `/api/internal/rbac/roles/${roleId}/permissions/${permId}`,
|
|
408
|
-
body: params
|
|
409
|
-
});
|
|
410
|
-
},
|
|
411
|
-
/**
|
|
412
|
-
* Assign a direct (user-level) permission exception to a user.
|
|
413
|
-
*
|
|
414
|
-
* This bypasses role-based assignment and gives the user a direct
|
|
415
|
-
* data-scope override for the specified permission.
|
|
416
|
-
*
|
|
417
|
-
* @example
|
|
418
|
-
* ```typescript
|
|
419
|
-
* await admin.permissions.assignToUser('u-123456', {
|
|
420
|
-
* permissionId: 10,
|
|
421
|
-
* scopeType: 'dep',
|
|
422
|
-
* });
|
|
423
|
-
* ```
|
|
424
|
-
*/
|
|
425
|
-
async assignToUser(uid, params) {
|
|
426
|
-
return request({
|
|
427
|
-
method: "POST",
|
|
428
|
-
path: `/api/internal/rbac/users/${uid}/permissions`,
|
|
429
|
-
body: params
|
|
430
|
-
});
|
|
431
|
-
},
|
|
432
|
-
/**
|
|
433
|
-
* Remove a direct permission exception from a user.
|
|
434
|
-
*
|
|
435
|
-
* @example
|
|
436
|
-
* ```typescript
|
|
437
|
-
* await admin.permissions.removeFromUser('u-123456', permId);
|
|
438
|
-
* ```
|
|
439
|
-
*/
|
|
440
|
-
async removeFromUser(uid, permId) {
|
|
441
|
-
return request({
|
|
442
|
-
method: "DELETE",
|
|
443
|
-
path: `/api/internal/rbac/users/${uid}/permissions/${permId}`
|
|
444
|
-
});
|
|
445
|
-
},
|
|
446
|
-
/**
|
|
447
|
-
* List permission exceptions for a specific user.
|
|
448
|
-
*
|
|
449
|
-
* @example
|
|
450
|
-
* ```typescript
|
|
451
|
-
* const exceptions = await admin.permissions.listForUser('u-123456');
|
|
452
|
-
* ```
|
|
453
|
-
*/
|
|
454
|
-
async listForUser(uid) {
|
|
455
|
-
return request({
|
|
456
|
-
method: "GET",
|
|
457
|
-
path: `/api/internal/rbac/users/${uid}/permissions`
|
|
458
|
-
});
|
|
459
|
-
},
|
|
460
|
-
/**
|
|
461
|
-
* Update the data scope of a user-level permission exception.
|
|
462
|
-
*
|
|
463
|
-
* @example
|
|
464
|
-
* ```typescript
|
|
465
|
-
* await admin.permissions.updateUserPermissionScope('u-123456', permId, {
|
|
466
|
-
* scopeType: 'custom',
|
|
467
|
-
* scopeFilter: JSON.stringify({ id: [10, 20] }),
|
|
468
|
-
* });
|
|
469
|
-
* ```
|
|
470
|
-
*/
|
|
471
|
-
async updateUserPermissionScope(uid, permId, params) {
|
|
472
|
-
return request({
|
|
473
|
-
method: "PUT",
|
|
474
|
-
path: `/api/internal/rbac/users/${uid}/permissions/${permId}`,
|
|
475
|
-
body: params
|
|
476
|
-
});
|
|
477
|
-
}
|
|
478
|
-
};
|
|
479
|
-
}
|
|
480
|
-
|
|
481
|
-
// src/modules/roles.ts
|
|
482
|
-
function createRolesModule(request) {
|
|
483
|
-
return {
|
|
484
|
-
/**
|
|
485
|
-
* List all roles.
|
|
486
|
-
*
|
|
487
|
-
* @example
|
|
488
|
-
* ```typescript
|
|
489
|
-
* const roles = await admin.roles.list();
|
|
490
|
-
* const managerRole = roles.find(r => r.code === 'manager');
|
|
491
|
-
* ```
|
|
492
|
-
*/
|
|
493
|
-
async list() {
|
|
494
|
-
return request({
|
|
495
|
-
method: "GET",
|
|
496
|
-
path: "/api/internal/rbac/roles"
|
|
497
|
-
});
|
|
498
|
-
},
|
|
499
|
-
/**
|
|
500
|
-
* Get a role by its numeric ID.
|
|
501
|
-
*
|
|
502
|
-
* @example
|
|
503
|
-
* ```typescript
|
|
504
|
-
* const role = await admin.roles.get(1);
|
|
505
|
-
* ```
|
|
506
|
-
*/
|
|
507
|
-
async get(id) {
|
|
508
|
-
return request({
|
|
509
|
-
method: "GET",
|
|
510
|
-
path: `/api/internal/rbac/roles/${id}`
|
|
511
|
-
});
|
|
512
|
-
},
|
|
513
|
-
/**
|
|
514
|
-
* Create a new role.
|
|
515
|
-
*
|
|
516
|
-
* @example
|
|
517
|
-
* ```typescript
|
|
518
|
-
* const role = await admin.roles.create({
|
|
519
|
-
* code: 'editor',
|
|
520
|
-
* displayName: '编辑',
|
|
521
|
-
* description: '负责内容编辑',
|
|
522
|
-
* });
|
|
523
|
-
* ```
|
|
524
|
-
*/
|
|
525
|
-
async create(params) {
|
|
526
|
-
return request({
|
|
527
|
-
method: "POST",
|
|
528
|
-
path: "/api/internal/rbac/roles",
|
|
529
|
-
body: params
|
|
530
|
-
});
|
|
531
|
-
},
|
|
532
|
-
/**
|
|
533
|
-
* Update a role.
|
|
534
|
-
* Note: system roles (isSystem=true) cannot be modified.
|
|
535
|
-
*
|
|
536
|
-
* @example
|
|
537
|
-
* ```typescript
|
|
538
|
-
* await admin.roles.update(3, { displayName: '高级编辑' });
|
|
539
|
-
* ```
|
|
540
|
-
*/
|
|
541
|
-
async update(id, params) {
|
|
542
|
-
return request({
|
|
543
|
-
method: "PUT",
|
|
544
|
-
path: `/api/internal/rbac/roles/${id}`,
|
|
545
|
-
body: params
|
|
546
|
-
});
|
|
547
|
-
},
|
|
548
|
-
/**
|
|
549
|
-
* Delete a role.
|
|
550
|
-
* Note: system roles cannot be deleted.
|
|
551
|
-
*
|
|
552
|
-
* @example
|
|
553
|
-
* ```typescript
|
|
554
|
-
* await admin.roles.delete(3);
|
|
555
|
-
* ```
|
|
556
|
-
*/
|
|
557
|
-
async delete(id) {
|
|
558
|
-
return request({
|
|
559
|
-
method: "DELETE",
|
|
560
|
-
path: `/api/internal/rbac/roles/${id}`
|
|
561
|
-
});
|
|
562
|
-
},
|
|
563
|
-
/**
|
|
564
|
-
* Assign a role to a user, department, team, or another role.
|
|
565
|
-
* For users, prefer assigneeUid (string). For department/team/role, use assigneeId (number).
|
|
566
|
-
*
|
|
567
|
-
* @example
|
|
568
|
-
* ```typescript
|
|
569
|
-
* // Assign to a user (preferred: use uid)
|
|
570
|
-
* await admin.roles.assign(roleId, { assigneeType: 'user', assigneeUid: user.uid });
|
|
571
|
-
*
|
|
572
|
-
* // Assign to an entire department
|
|
573
|
-
* await admin.roles.assign(roleId, { assigneeType: 'department', assigneeId: 5 });
|
|
574
|
-
* ```
|
|
575
|
-
*/
|
|
576
|
-
async assign(roleId, params) {
|
|
577
|
-
return request({
|
|
578
|
-
method: "POST",
|
|
579
|
-
path: `/api/internal/rbac/roles/${roleId}/assign`,
|
|
580
|
-
body: params
|
|
581
|
-
});
|
|
582
|
-
},
|
|
583
|
-
/**
|
|
584
|
-
* Remove a role assignment.
|
|
585
|
-
* For users, prefer assigneeUid. For department/team/role, use assigneeId.
|
|
586
|
-
*
|
|
587
|
-
* @example
|
|
588
|
-
* ```typescript
|
|
589
|
-
* await admin.roles.unassign(roleId, { assigneeType: 'user', assigneeUid: user.uid });
|
|
590
|
-
* ```
|
|
591
|
-
*/
|
|
592
|
-
async unassign(roleId, params) {
|
|
593
|
-
return request({
|
|
594
|
-
method: "POST",
|
|
595
|
-
path: `/api/internal/rbac/roles/${roleId}/unassign`,
|
|
596
|
-
body: params
|
|
597
|
-
});
|
|
598
|
-
},
|
|
599
|
-
/**
|
|
600
|
-
* List all assignees (users, departments, etc.) for a role.
|
|
601
|
-
*
|
|
602
|
-
* @example
|
|
603
|
-
* ```typescript
|
|
604
|
-
* const assignees = await admin.roles.listAssignees(roleId);
|
|
605
|
-
* const users = assignees.filter(a => a.assigneeType === 'user');
|
|
606
|
-
* ```
|
|
607
|
-
*/
|
|
608
|
-
async listAssignees(roleId) {
|
|
609
|
-
return request({
|
|
610
|
-
method: "GET",
|
|
611
|
-
path: `/api/internal/rbac/roles/${roleId}/assignees`
|
|
612
|
-
});
|
|
613
|
-
},
|
|
614
|
-
/**
|
|
615
|
-
* Get all roles assigned to a user (by UID).
|
|
616
|
-
*
|
|
617
|
-
* Useful in Edge Functions to check what roles a user currently holds
|
|
618
|
-
* without making a full ACL check.
|
|
619
|
-
*
|
|
620
|
-
* @example
|
|
621
|
-
* ```typescript
|
|
622
|
-
* const roles = await admin.roles.getUserRoles('u-123456');
|
|
623
|
-
* if (roles.some(r => r.code === 'admin')) { ... }
|
|
624
|
-
* ```
|
|
625
|
-
*/
|
|
626
|
-
async getUserRoles(uid) {
|
|
627
|
-
return request({
|
|
628
|
-
method: "GET",
|
|
629
|
-
path: `/api/internal/rbac/users/${uid}/roles`
|
|
630
|
-
});
|
|
631
|
-
}
|
|
632
|
-
};
|
|
633
|
-
}
|
|
634
|
-
|
|
635
151
|
// src/modules/users.ts
|
|
636
152
|
function createUsersModule(request) {
|
|
637
153
|
return {
|
|
@@ -728,11 +244,177 @@ function createUsersModule(request) {
|
|
|
728
244
|
}
|
|
729
245
|
};
|
|
730
246
|
}
|
|
247
|
+
function createApplicationContext(options) {
|
|
248
|
+
const initial = { appId: options.appId ?? "context", env: options.env ?? "dev" };
|
|
249
|
+
if (!options.appId && !options.baseURL)
|
|
250
|
+
throw new AdminSDKError("An application domain or appId is required", 400);
|
|
251
|
+
let pending;
|
|
252
|
+
return {
|
|
253
|
+
initial,
|
|
254
|
+
resolve() {
|
|
255
|
+
if (options.appId && options.baseURL === void 0) return Promise.resolve(initial);
|
|
256
|
+
if (pending) return pending;
|
|
257
|
+
pending = (async () => {
|
|
258
|
+
const headers = { "Cache-Control": "no-cache" };
|
|
259
|
+
const controller = new AbortController();
|
|
260
|
+
const timeout = setTimeout(() => controller.abort(), 1e4);
|
|
261
|
+
try {
|
|
262
|
+
const response = await fetch(options.baseURL.replace(/\/$/, "") + "/api/auth/context", {
|
|
263
|
+
method: "GET",
|
|
264
|
+
headers,
|
|
265
|
+
credentials: "omit",
|
|
266
|
+
redirect: "error",
|
|
267
|
+
signal: controller.signal
|
|
268
|
+
});
|
|
269
|
+
if (!response.ok)
|
|
270
|
+
throw new AdminSDKError("Cannot verify application context", response.status);
|
|
271
|
+
const text = await response.text();
|
|
272
|
+
if (text.length > 16384)
|
|
273
|
+
throw new AdminSDKError("Cannot verify application context", 503);
|
|
274
|
+
const envelope = JSON.parse(text);
|
|
275
|
+
const value = envelope?.statusCode === 200 ? envelope.data : null;
|
|
276
|
+
if (value?.protocolVersion !== "1" || typeof value.applicationCode !== "string" || typeof value.environment !== "string" || typeof value.tenancyEnabled !== "boolean" || options.appId !== void 0 && value.applicationCode !== options.appId) {
|
|
277
|
+
throw new AdminSDKError("Cannot verify application context", 503);
|
|
278
|
+
}
|
|
279
|
+
const scope = new RequestScopeController({
|
|
280
|
+
applicationCode: value.applicationCode,
|
|
281
|
+
environment: value.environment,
|
|
282
|
+
storage: null
|
|
283
|
+
});
|
|
284
|
+
if (scope.environment !== value.environment)
|
|
285
|
+
throw new AdminSDKError("Cannot verify application context", 503);
|
|
286
|
+
return { appId: scope.applicationCode, env: scope.environment };
|
|
287
|
+
} catch (error) {
|
|
288
|
+
if (error instanceof AdminSDKError) throw error;
|
|
289
|
+
throw new AdminSDKError("Cannot verify application context", 503);
|
|
290
|
+
} finally {
|
|
291
|
+
clearTimeout(timeout);
|
|
292
|
+
}
|
|
293
|
+
})();
|
|
294
|
+
pending.catch(() => {
|
|
295
|
+
pending = void 0;
|
|
296
|
+
});
|
|
297
|
+
return pending;
|
|
298
|
+
}
|
|
299
|
+
};
|
|
300
|
+
}
|
|
301
|
+
function deferOperations(template, resolve) {
|
|
302
|
+
let actual;
|
|
303
|
+
return Object.fromEntries(
|
|
304
|
+
Object.keys(template).map((name) => {
|
|
305
|
+
return [
|
|
306
|
+
name,
|
|
307
|
+
async (...args) => {
|
|
308
|
+
const captured = JSON.parse(JSON.stringify(args));
|
|
309
|
+
actual ?? (actual = resolve());
|
|
310
|
+
let module;
|
|
311
|
+
try {
|
|
312
|
+
module = await actual;
|
|
313
|
+
} catch {
|
|
314
|
+
actual = void 0;
|
|
315
|
+
return {
|
|
316
|
+
data: null,
|
|
317
|
+
status: 503,
|
|
318
|
+
error: {
|
|
319
|
+
status: 503,
|
|
320
|
+
code: "dependency_unverifiable",
|
|
321
|
+
message: "Cannot verify application context"
|
|
322
|
+
}
|
|
323
|
+
};
|
|
324
|
+
}
|
|
325
|
+
const operation = module[name];
|
|
326
|
+
return operation(...captured);
|
|
327
|
+
}
|
|
328
|
+
];
|
|
329
|
+
})
|
|
330
|
+
);
|
|
331
|
+
}
|
|
332
|
+
function createApplicationTenantAccess(baseURL, appTenantId, serviceKey, resolve, initial) {
|
|
333
|
+
if (!/^[A-Za-z0-9_-]{1,128}$/.test(appTenantId)) throw new AdminSDKError("Invalid tenant identifier", 400);
|
|
334
|
+
if (!serviceKey || /[\r\n\0]/.test(serviceKey)) throw new AdminSDKError("An application service key is required", 401);
|
|
335
|
+
const applicationKey = serviceKey;
|
|
336
|
+
const headers = { "X-Service-Key": applicationKey };
|
|
337
|
+
const management = createHttpClient(baseURL, headers, async () => {
|
|
338
|
+
const context = await resolve();
|
|
339
|
+
return { "x-tenant-id": context.appId, "x-env": context.env, "x-app-tenant-id": appTenantId };
|
|
340
|
+
});
|
|
341
|
+
const changes = { sequence: 0 };
|
|
342
|
+
const request = async (options) => {
|
|
343
|
+
const write = options.method !== "GET";
|
|
344
|
+
if (write) changes.sequence++;
|
|
345
|
+
try {
|
|
346
|
+
return await management({ ...options, credentials: "omit", redirect: "error" });
|
|
347
|
+
} finally {
|
|
348
|
+
if (write) changes.sequence++;
|
|
349
|
+
}
|
|
350
|
+
};
|
|
351
|
+
function makeScope(context) {
|
|
352
|
+
const scope = new RequestScopeController({ applicationCode: context.appId, environment: context.env, storage: null });
|
|
353
|
+
scope.setSubject({ type: "service", id: "legacy-application-administrator" });
|
|
354
|
+
scope.selectTenant(appTenantId);
|
|
355
|
+
return scope;
|
|
356
|
+
}
|
|
357
|
+
let controls;
|
|
358
|
+
let pending;
|
|
359
|
+
function connect() {
|
|
360
|
+
return pending ?? (pending = resolve().then((context) => {
|
|
361
|
+
const scope = makeScope(context);
|
|
362
|
+
const http = createHttpClient$1({
|
|
363
|
+
baseURL,
|
|
364
|
+
requestScope: scope,
|
|
365
|
+
logErrors: false,
|
|
366
|
+
adapter: createFetchAdapter({ onRequest: ({ init }) => {
|
|
367
|
+
const authenticated = new Headers(init.headers);
|
|
368
|
+
authenticated.delete("Authorization");
|
|
369
|
+
authenticated.set("X-Service-Key", applicationKey);
|
|
370
|
+
return { init: { ...init, headers: authenticated, credentials: "omit", redirect: "error" } };
|
|
371
|
+
} })
|
|
372
|
+
});
|
|
373
|
+
controls = createTenancyManagement(scope, http, changes);
|
|
374
|
+
return http;
|
|
375
|
+
}));
|
|
376
|
+
}
|
|
377
|
+
const entity = {
|
|
378
|
+
async request(config) {
|
|
379
|
+
const captured = { ...config, data: config.data === void 0 ? void 0 : JSON.stringify(config.data) };
|
|
380
|
+
const operation = connect();
|
|
381
|
+
try {
|
|
382
|
+
return await (await operation).request(captured);
|
|
383
|
+
} catch (error) {
|
|
384
|
+
if (pending === operation) pending = void 0;
|
|
385
|
+
const status = error instanceof AdminSDKError ? error.statusCode : 503;
|
|
386
|
+
return { data: null, status, error: { status, code: "dependency_unverifiable", message: "Cannot verify application context" } };
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
};
|
|
390
|
+
const managementControls = deferOperations(createTenancyManagement(makeScope(initial), entity, changes), async () => {
|
|
391
|
+
connect();
|
|
392
|
+
try {
|
|
393
|
+
await pending;
|
|
394
|
+
} catch (error) {
|
|
395
|
+
pending = void 0;
|
|
396
|
+
throw error;
|
|
397
|
+
}
|
|
398
|
+
return controls;
|
|
399
|
+
});
|
|
400
|
+
return {
|
|
401
|
+
...managementControls,
|
|
402
|
+
auth: createAuthModule(request),
|
|
403
|
+
users: createUsersModule(request),
|
|
404
|
+
roles: createRolesModule(request),
|
|
405
|
+
permissions: createPermissionsModule(request),
|
|
406
|
+
departments: createDepartmentsModule(request),
|
|
407
|
+
entity: createEntityClient(entity, { preserveIdentityStrings: true })
|
|
408
|
+
};
|
|
409
|
+
}
|
|
731
410
|
|
|
732
411
|
// src/admin-sdk.ts
|
|
733
412
|
var AdminSDK = class {
|
|
734
413
|
constructor(options) {
|
|
735
|
-
const { baseURL = "http://runtime:3000",
|
|
414
|
+
const { baseURL = "http://runtime:3000", serviceKey } = options;
|
|
415
|
+
const context = createApplicationContext(options);
|
|
416
|
+
const { appId, env } = context.initial;
|
|
417
|
+
const discover = options.baseURL !== void 0;
|
|
736
418
|
const headers = {
|
|
737
419
|
"x-tenant-id": appId,
|
|
738
420
|
"x-env": env
|
|
@@ -741,7 +423,17 @@ var AdminSDK = class {
|
|
|
741
423
|
headers["X-Service-Key"] = serviceKey;
|
|
742
424
|
headers["X-Bypass-Auth"] = "true";
|
|
743
425
|
}
|
|
744
|
-
const request = createHttpClient(
|
|
426
|
+
const request = createHttpClient(
|
|
427
|
+
baseURL,
|
|
428
|
+
headers,
|
|
429
|
+
discover ? async () => {
|
|
430
|
+
const scope = await context.resolve();
|
|
431
|
+
return { "x-tenant-id": scope.appId, "x-env": scope.env };
|
|
432
|
+
} : void 0
|
|
433
|
+
);
|
|
434
|
+
this.tenancy = {
|
|
435
|
+
forTenant: (appTenantId) => createApplicationTenantAccess(baseURL, appTenantId, serviceKey, () => context.resolve(), context.initial)
|
|
436
|
+
};
|
|
745
437
|
this.auth = createAuthModule(request);
|
|
746
438
|
this.users = createUsersModule(request);
|
|
747
439
|
this.roles = createRolesModule(request);
|