-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathmultiTenant.ts
More file actions
28 lines (21 loc) · 973 Bytes
/
multiTenant.ts
File metadata and controls
28 lines (21 loc) · 973 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import rbac, { MongoRoleAdapter, createTenantRBAC } from '@rbac/rbac';
async function run(): Promise<void> {
const adapter = new MongoRoleAdapter({
uri: 'mongodb://localhost:27017',
dbName: 'rbac',
collection: 'roles'
});
// roles are stored per tenant when tenantId is provided
await adapter.addRole('user', { can: ['products:find'] }, 'tenant-a');
await adapter.addRole('user', { can: ['products:edit'] }, 'tenant-b');
// retrieving RBAC instance scoped to a tenant
const rbacTenantA = await createTenantRBAC(adapter, 'tenant-a');
await rbacTenantA.can('user', 'products:find'); // true
await rbacTenantA.can('user', 'products:edit'); // false
// default tenant still works without specifying tenantId
await adapter.addRole('guest', { can: ['products:view'] });
const roles = await adapter.getRoles();
const defaultRBAC = rbac()(roles);
await defaultRBAC.can('guest', 'products:view'); // true
}
run().catch(console.error);