@dads-framework/tenants 0.0.0-stage → 0.2.0
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/LICENSE.md +21 -0
- package/README.md +187 -2
- package/dist/index.d.ts +14 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +13 -0
- package/dist/index.js.map +1 -0
- package/dist/install-tenant-schema-v2.d.ts +4 -0
- package/dist/install-tenant-schema-v2.d.ts.map +1 -0
- package/dist/install-tenant-schema-v2.js +10 -0
- package/dist/install-tenant-schema-v2.js.map +1 -0
- package/dist/membership-facts.d.ts +5 -0
- package/dist/membership-facts.d.ts.map +1 -0
- package/dist/membership-facts.js +19 -0
- package/dist/membership-facts.js.map +1 -0
- package/dist/membership-permissions.d.ts +10 -0
- package/dist/membership-permissions.d.ts.map +1 -0
- package/dist/membership-permissions.js +24 -0
- package/dist/membership-permissions.js.map +1 -0
- package/dist/migrations/20261120040000_tenants.migration.d.ts +5 -0
- package/dist/migrations/20261120040000_tenants.migration.d.ts.map +1 -0
- package/dist/migrations/20261120040000_tenants.migration.js +31 -0
- package/dist/migrations/20261120040000_tenants.migration.js.map +1 -0
- package/dist/migrations/20261120080000_tenant_scim.migration.d.ts +5 -0
- package/dist/migrations/20261120080000_tenant_scim.migration.d.ts.map +1 -0
- package/dist/migrations/20261120080000_tenant_scim.migration.js +34 -0
- package/dist/migrations/20261120080000_tenant_scim.migration.js.map +1 -0
- package/dist/migrations/20261120080100_tenant_scim_groups.migration.d.ts +5 -0
- package/dist/migrations/20261120080100_tenant_scim_groups.migration.d.ts.map +1 -0
- package/dist/migrations/20261120080100_tenant_scim_groups.migration.js +34 -0
- package/dist/migrations/20261120080100_tenant_scim_groups.migration.js.map +1 -0
- package/dist/tenant-access.d.ts +26 -0
- package/dist/tenant-access.d.ts.map +1 -0
- package/dist/tenant-access.js +27 -0
- package/dist/tenant-access.js.map +1 -0
- package/dist/tenant-composition.d.ts +27 -0
- package/dist/tenant-composition.d.ts.map +1 -0
- package/dist/tenant-composition.js +40 -0
- package/dist/tenant-composition.js.map +1 -0
- package/dist/tenant-invitations.d.ts +31 -0
- package/dist/tenant-invitations.d.ts.map +1 -0
- package/dist/tenant-invitations.js +118 -0
- package/dist/tenant-invitations.js.map +1 -0
- package/dist/tenant-memberships.d.ts +20 -0
- package/dist/tenant-memberships.d.ts.map +1 -0
- package/dist/tenant-memberships.js +84 -0
- package/dist/tenant-memberships.js.map +1 -0
- package/dist/tenant-migrations.d.ts +5 -0
- package/dist/tenant-migrations.d.ts.map +1 -0
- package/dist/tenant-migrations.js +6 -0
- package/dist/tenant-migrations.js.map +1 -0
- package/dist/tenant-scim-directory.d.ts +5 -0
- package/dist/tenant-scim-directory.d.ts.map +1 -0
- package/dist/tenant-scim-directory.js +142 -0
- package/dist/tenant-scim-directory.js.map +1 -0
- package/dist/tenant-scim-group-directory.d.ts +5 -0
- package/dist/tenant-scim-group-directory.d.ts.map +1 -0
- package/dist/tenant-scim-group-directory.js +143 -0
- package/dist/tenant-scim-group-directory.js.map +1 -0
- package/dist/tenant-scim-group-members.d.ts +8 -0
- package/dist/tenant-scim-group-members.d.ts.map +1 -0
- package/dist/tenant-scim-group-members.js +47 -0
- package/dist/tenant-scim-group-members.js.map +1 -0
- package/dist/tenant-scim-group-record.d.ts +15 -0
- package/dist/tenant-scim-group-record.d.ts.map +1 -0
- package/dist/tenant-scim-group-record.js +47 -0
- package/dist/tenant-scim-group-record.js.map +1 -0
- package/dist/tenant-scim-group-tables.d.ts +189 -0
- package/dist/tenant-scim-group-tables.d.ts.map +1 -0
- package/dist/tenant-scim-group-tables.js +28 -0
- package/dist/tenant-scim-group-tables.js.map +1 -0
- package/dist/tenant-scim-membership.d.ts +12 -0
- package/dist/tenant-scim-membership.d.ts.map +1 -0
- package/dist/tenant-scim-membership.js +87 -0
- package/dist/tenant-scim-membership.js.map +1 -0
- package/dist/tenant-scim-record.d.ts +19 -0
- package/dist/tenant-scim-record.d.ts.map +1 -0
- package/dist/tenant-scim-record.js +52 -0
- package/dist/tenant-scim-record.js.map +1 -0
- package/dist/tenant-scim-table.d.ts +296 -0
- package/dist/tenant-scim-table.d.ts.map +1 -0
- package/dist/tenant-scim-table.js +29 -0
- package/dist/tenant-scim-table.js.map +1 -0
- package/dist/tenant-tables.d.ts +219 -0
- package/dist/tenant-tables.d.ts.map +1 -0
- package/dist/tenant-tables.js +32 -0
- package/dist/tenant-tables.js.map +1 -0
- package/dist/tenant-transaction.d.ts +20 -0
- package/dist/tenant-transaction.d.ts.map +1 -0
- package/dist/tenant-transaction.js +23 -0
- package/dist/tenant-transaction.js.map +1 -0
- package/dist/tenants.d.ts +32 -0
- package/dist/tenants.d.ts.map +1 -0
- package/dist/tenants.js +94 -0
- package/dist/tenants.js.map +1 -0
- package/dist/tenants.tsbuildinfo +1 -0
- package/package.json +45 -4
- package/src/migrations/20261120040000_tenants.migration.ts +36 -0
- package/src/migrations/20261120080000_tenant_scim.migration.ts +39 -0
- package/src/migrations/20261120080100_tenant_scim_groups.migration.ts +39 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Bryton Cooper
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,188 @@
|
|
|
1
|
-
#
|
|
1
|
+
# `@dads-framework/tenants`
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Manage tenants, memberships, and one-time invitations with relationship authorization.
|
|
4
|
+
|
|
5
|
+
## Try it
|
|
6
|
+
|
|
7
|
+
In the generated starter, [`libraries/app-runtime/index.ts`](../../../apps/kit-workbench/libraries/app-runtime/index.ts) declares tenant
|
|
8
|
+
access against the application's account entity. [`assembly/services.ts`](../../../apps/kit-workbench/assembly/services.ts)
|
|
9
|
+
constructs `Tenants` and supplies transaction-bound authorization so membership
|
|
10
|
+
rows and relationship facts commit together.
|
|
11
|
+
|
|
12
|
+
Use Node.js 22 or later and a disposable Postgres database. Install the tuple
|
|
13
|
+
table and complete tenant schema in one migration before running the script below.
|
|
14
|
+
|
|
15
|
+
```sh
|
|
16
|
+
npm install @dads-framework/access @dads-framework/postgres @dads-framework/postgres-access @dads-framework/tenants zod
|
|
17
|
+
npm install -D tsx
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
In your migration runner, pass its builder to
|
|
21
|
+
`pgm.sql(relationshipSchemaMigration.sql)` and `installTenantSchemaV2(pgm)`.
|
|
22
|
+
The first export comes from `@dads-framework/postgres-access` and the second
|
|
23
|
+
from `@dads-framework/tenants`. Set `DATABASE_URL` to connect to the migrated
|
|
24
|
+
database. This script uses fixture account IDs. In an application, resolve
|
|
25
|
+
those IDs from verified sessions before invoking the tenant service.
|
|
26
|
+
|
|
27
|
+
Save as `create-tenant.mts` and run `npx tsx create-tenant.mts`.
|
|
28
|
+
|
|
29
|
+
<!-- dads:check-example -->
|
|
30
|
+
|
|
31
|
+
```ts
|
|
32
|
+
import { access, accessModel, entityHandle } from "@dads-framework/access";
|
|
33
|
+
import { createPostgresConnection } from "@dads-framework/postgres";
|
|
34
|
+
import {
|
|
35
|
+
PostgresAuthorization,
|
|
36
|
+
TransactionalAccessChanges,
|
|
37
|
+
} from "@dads-framework/postgres-access";
|
|
38
|
+
import {
|
|
39
|
+
createTenantAccess,
|
|
40
|
+
TenantUnavailableError,
|
|
41
|
+
Tenants,
|
|
42
|
+
} from "@dads-framework/tenants";
|
|
43
|
+
import { z } from "zod";
|
|
44
|
+
|
|
45
|
+
const connectionString = process.env.DATABASE_URL;
|
|
46
|
+
if (!connectionString) throw new Error("Set DATABASE_URL.");
|
|
47
|
+
|
|
48
|
+
const account = entityHandle({ wireName: "account", idSchema: z.string() });
|
|
49
|
+
const tenantAccess = createTenantAccess(account);
|
|
50
|
+
const model = accessModel([
|
|
51
|
+
access(account, { roles: {}, permissions: () => ({}) }),
|
|
52
|
+
tenantAccess.tenant,
|
|
53
|
+
]);
|
|
54
|
+
|
|
55
|
+
const connection = createPostgresConnection(
|
|
56
|
+
{ connectionString, schema: {} },
|
|
57
|
+
console,
|
|
58
|
+
);
|
|
59
|
+
try {
|
|
60
|
+
const backend = new PostgresAuthorization({
|
|
61
|
+
model,
|
|
62
|
+
namespace: "tenant-example",
|
|
63
|
+
executor: connection.pool,
|
|
64
|
+
});
|
|
65
|
+
const tenants = new Tenants({
|
|
66
|
+
database: connection.drizzle,
|
|
67
|
+
access: tenantAccess,
|
|
68
|
+
authorizationAt: (tx) => backend.inDrizzleTransaction(tx),
|
|
69
|
+
changes: new TransactionalAccessChanges(backend, connection.drizzle),
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
const tenant = await tenants.create("alice", "Example workspace");
|
|
73
|
+
const context = await tenants.context("alice", tenant.id);
|
|
74
|
+
console.log(context.tenant.name);
|
|
75
|
+
try {
|
|
76
|
+
await tenants.context("outside-account", tenant.id);
|
|
77
|
+
throw new Error("Expected access to be denied.");
|
|
78
|
+
} catch (error) {
|
|
79
|
+
if (!(error instanceof TenantUnavailableError)) throw error;
|
|
80
|
+
console.log(error.constructor.name);
|
|
81
|
+
}
|
|
82
|
+
} finally {
|
|
83
|
+
await connection.close();
|
|
84
|
+
}
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
The script prints `Example workspace` and `TenantUnavailableError`. The
|
|
88
|
+
creator's owner relationship commits in the same transaction as the tenant
|
|
89
|
+
and its membership row.
|
|
90
|
+
|
|
91
|
+
## Use it
|
|
92
|
+
|
|
93
|
+
Use the account ID from the application's authenticated session. Pass request tenant IDs through `tenantIdSchema`. `list(accountId)` asks authorization for visible tenant IDs and loads only those rows. `context(accountId, tenantId)` checks the tenant before returning its metadata, feature flag audience, and audit scope. `rename` requires the owner relationship.
|
|
94
|
+
|
|
95
|
+
`memberships.roster` is available to owners and admins. Owners can promote members and remove admins. Admins can invite and remove ordinary members. The bootstrap owner relationship stays in place.
|
|
96
|
+
|
|
97
|
+
`memberships.permissions(actorId, tenantId, accountId)` returns the selected member's role and checks their effective `view`, `manageMembers`, and `manageTenant` permissions. It requires membership management permission on that tenant. Missing members produce `TenantUnavailableError`.
|
|
98
|
+
|
|
99
|
+
`invitations.issue(accountId, tenantId, recipientEmail)` produces a one-time token and its expiration. Deliver that token to the recipient through the application's invitation link or email. `invitations.accept(token, session.account)` accepts only the intended account with a verified email. The account session supplies `id`, `email`, and `emailVerified`. The token digest, expiry, and redemption state stay in storage. Tenant invitations grant membership and work with identity systems that support verified account emails.
|
|
100
|
+
|
|
101
|
+
Applications with tenant sign-in requirements can pass a
|
|
102
|
+
`TenantInvitationAcceptanceCheck` as the third argument to
|
|
103
|
+
`invitations.accept`. The callback receives the locked invitation's tenant,
|
|
104
|
+
the verified accepting account, and the same Postgres transaction. Return
|
|
105
|
+
`true` to permit redemption. Denial leaves the invitation unconsumed and
|
|
106
|
+
membership unchanged. Use the supplied tenant and account for policy lookup.
|
|
107
|
+
|
|
108
|
+
Use `context.audience` for feature flag evaluation and `context.auditScope` for tenant audit reads. `tenantAuditConfiguration` defines the `tenant_id` dimension for tenant and membership records. It excludes invitation rows. Bind `createTenantDirectory(database)` as the flag manager's platform-authorized tenant directory.
|
|
109
|
+
|
|
110
|
+
## What it refuses to do
|
|
111
|
+
|
|
112
|
+
Membership administration rejects access across tenant boundaries. The service denies missing tenants and tenants the caller cannot access through the same error. Invitation redemption rejects missing, expired, consumed, or mismatched invitations through the same error. The issuer's membership management permission must still be active.
|
|
113
|
+
|
|
114
|
+
## Contract
|
|
115
|
+
|
|
116
|
+
The data and authorization ports use the same serializable Postgres transaction. On an empty database, install the package's `tenantMigrations` through the
|
|
117
|
+
application's runner or call `installTenantSchemaV2` in an application-owned
|
|
118
|
+
migration. Both options install tenant, membership, invitation, and SCIM
|
|
119
|
+
user and group storage. Choose one path. Invitation acceptance always checks
|
|
120
|
+
directory user storage for a deactivated account binding. The Postgres access adapter has [Trusted experimental status](../postgres-access/README.md#trusted-experimental). Applications can own additional kinds and sub-scopes in their composed access model.
|
|
121
|
+
|
|
122
|
+
Translate `TenantUnavailableError` and `TenantInvitationUnavailableError` at the transport boundary. A database error rejects the transaction and leaves membership state unchanged. When a transaction retries after a PostgreSQL serialization failure, run the whole operation again.
|
|
123
|
+
|
|
124
|
+
## Upgrade an existing tenant schema
|
|
125
|
+
|
|
126
|
+
Keep applied migration files intact. If your application used only
|
|
127
|
+
`installTenantSchemaV1`, add a new forward migration that calls
|
|
128
|
+
`installTenantScimSchemaV1` followed by `installTenantScimGroupSchemaV1`.
|
|
129
|
+
Apply it before serving invitation acceptance. If the SCIM user installer
|
|
130
|
+
already ran, add only the group installer. Databases with both SCIM
|
|
131
|
+
migrations already applied, including the generated starter, have the
|
|
132
|
+
complete schema and need no additional tenant migration. Reserve
|
|
133
|
+
`installTenantSchemaV2` for fresh databases.
|
|
134
|
+
|
|
135
|
+
## SCIM provisioning
|
|
136
|
+
|
|
137
|
+
Use the same tenant options and synchronous Postgres access adapter for the
|
|
138
|
+
tenant service and the SCIM directory.
|
|
139
|
+
|
|
140
|
+
```ts
|
|
141
|
+
import { createScimHandler } from "@dads-framework/identity/scim";
|
|
142
|
+
import {
|
|
143
|
+
createTenantScimDirectory,
|
|
144
|
+
createTenantScimGroupDirectory,
|
|
145
|
+
} from "@dads-framework/tenants";
|
|
146
|
+
|
|
147
|
+
const scim = createScimHandler({
|
|
148
|
+
baseUrl: "https://app.example.com/scim/v2",
|
|
149
|
+
authenticate: resolveTenantFromScimBearer,
|
|
150
|
+
directory: createTenantScimDirectory(tenantOptions),
|
|
151
|
+
groups: createTenantScimGroupDirectory(tenantOptions),
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
// A tenant owner supplies an app-verified mapping to an existing local member.
|
|
155
|
+
await tenants.bindScimUser(
|
|
156
|
+
ownerSession.account.id,
|
|
157
|
+
tenant.id,
|
|
158
|
+
scimUser.id,
|
|
159
|
+
memberAccountId,
|
|
160
|
+
);
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
Mount the handler at its canonical base URL. Resolve each credential to the
|
|
164
|
+
tenant it manages and reject unknown or revoked credentials. SCIM profile
|
|
165
|
+
fields, including email and external ID, are directory data. Account linking
|
|
166
|
+
requires an explicit owner action and an existing non-owner membership in
|
|
167
|
+
that tenant. A local account can link to one SCIM user per tenant.
|
|
168
|
+
|
|
169
|
+
SCIM creation and reactivation leave membership grants to the application.
|
|
170
|
+
Setting `active` to `false` or deleting a linked SCIM user removes its
|
|
171
|
+
membership and relationship in the same transaction. Invitation redemption
|
|
172
|
+
requires an active linked user. Deletion removes the directory profile and
|
|
173
|
+
retains its resource ID and account binding. To onboard that same local
|
|
174
|
+
account again, provision a new active SCIM user, have an owner move the
|
|
175
|
+
verified account binding with `bindScimUser`, and grant membership through an
|
|
176
|
+
invitation. The deleted resource ID stays retired. Back up the table with
|
|
177
|
+
the tenant data. The default tenant audit configuration excludes SCIM
|
|
178
|
+
profile data.
|
|
179
|
+
|
|
180
|
+
SCIM Groups contain references to current SCIM users in the same tenant.
|
|
181
|
+
Deleting a user removes its group memberships in the same transaction.
|
|
182
|
+
Applications authorize tenant resources through their own role model. Group
|
|
183
|
+
membership carries no application authorization or staff role.
|
|
184
|
+
|
|
185
|
+
Authorize each tenant request on the primary database, including requests
|
|
186
|
+
made with existing account sessions. Use `Tenants.context` or the same
|
|
187
|
+
transactional authorization model for tenant resources. Application-owned
|
|
188
|
+
grants outside the tenant role model require their own revocation policy.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export { assignableTenantRoleSchema, createTenantAccess, tenantEntity, tenantIdSchema, tenantRoles, type TenantAccess, type TenantId, type TenantRole, } from "./tenant-access.js";
|
|
2
|
+
export { createTenantDirectory, tenantAuditConfiguration, } from "./tenant-composition.js";
|
|
3
|
+
export { type AuthenticatedTenantAccount, type TenantInvitationAcceptanceCheck, TenantInvitationUnavailableError, } from "./tenant-invitations.js";
|
|
4
|
+
export type { TenantMembership } from "./tenant-memberships.js";
|
|
5
|
+
export { TenantUnavailableError, type TenantsOptions, type TenantTransaction, } from "./tenant-transaction.js";
|
|
6
|
+
export { Tenants, type TenantContext, type TenantRecord } from "./tenants.js";
|
|
7
|
+
export { tenantMigrations } from "./tenant-migrations.js";
|
|
8
|
+
export { installTenantSchemaV2 } from "./install-tenant-schema-v2.js";
|
|
9
|
+
export { installTenantSchemaV1 } from "./migrations/20261120040000_tenants.migration.js";
|
|
10
|
+
export { createTenantScimDirectory } from "./tenant-scim-directory.js";
|
|
11
|
+
export { installTenantScimSchemaV1 } from "./migrations/20261120080000_tenant_scim.migration.js";
|
|
12
|
+
export { createTenantScimGroupDirectory } from "./tenant-scim-group-directory.js";
|
|
13
|
+
export { installTenantScimGroupSchemaV1 } from "./migrations/20261120080100_tenant_scim_groups.migration.js";
|
|
14
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,0BAA0B,EAC1B,kBAAkB,EAClB,YAAY,EACZ,cAAc,EACd,WAAW,EACX,KAAK,YAAY,EACjB,KAAK,QAAQ,EACb,KAAK,UAAU,GAChB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EACL,qBAAqB,EACrB,wBAAwB,GACzB,MAAM,yBAAyB,CAAC;AACjC,OAAO,EACL,KAAK,0BAA0B,EAC/B,KAAK,+BAA+B,EACpC,gCAAgC,GACjC,MAAM,yBAAyB,CAAC;AACjC,YAAY,EAAE,gBAAgB,EAAE,MAAM,yBAAyB,CAAC;AAChE,OAAO,EACL,sBAAsB,EACtB,KAAK,cAAc,EACnB,KAAK,iBAAiB,GACvB,MAAM,yBAAyB,CAAC;AACjC,OAAO,EAAE,OAAO,EAAE,KAAK,aAAa,EAAE,KAAK,YAAY,EAAE,MAAM,cAAc,CAAC;AAE9E,OAAO,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAE1D,OAAO,EAAE,qBAAqB,EAAE,MAAM,+BAA+B,CAAC;AACtE,OAAO,EAAE,qBAAqB,EAAE,MAAM,kDAAkD,CAAC;AAEzF,OAAO,EAAE,yBAAyB,EAAE,MAAM,4BAA4B,CAAC;AACvE,OAAO,EAAE,yBAAyB,EAAE,MAAM,sDAAsD,CAAC;AACjG,OAAO,EAAE,8BAA8B,EAAE,MAAM,kCAAkC,CAAC;AAClF,OAAO,EAAE,8BAA8B,EAAE,MAAM,6DAA6D,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export { assignableTenantRoleSchema, createTenantAccess, tenantEntity, tenantIdSchema, tenantRoles, } from "./tenant-access.js";
|
|
2
|
+
export { createTenantDirectory, tenantAuditConfiguration, } from "./tenant-composition.js";
|
|
3
|
+
export { TenantInvitationUnavailableError, } from "./tenant-invitations.js";
|
|
4
|
+
export { TenantUnavailableError, } from "./tenant-transaction.js";
|
|
5
|
+
export { Tenants } from "./tenants.js";
|
|
6
|
+
export { tenantMigrations } from "./tenant-migrations.js";
|
|
7
|
+
export { installTenantSchemaV2 } from "./install-tenant-schema-v2.js";
|
|
8
|
+
export { installTenantSchemaV1 } from "./migrations/20261120040000_tenants.migration.js";
|
|
9
|
+
export { createTenantScimDirectory } from "./tenant-scim-directory.js";
|
|
10
|
+
export { installTenantScimSchemaV1 } from "./migrations/20261120080000_tenant_scim.migration.js";
|
|
11
|
+
export { createTenantScimGroupDirectory } from "./tenant-scim-group-directory.js";
|
|
12
|
+
export { installTenantScimGroupSchemaV1 } from "./migrations/20261120080100_tenant_scim_groups.migration.js";
|
|
13
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,0BAA0B,EAC1B,kBAAkB,EAClB,YAAY,EACZ,cAAc,EACd,WAAW,GAIZ,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EACL,qBAAqB,EACrB,wBAAwB,GACzB,MAAM,yBAAyB,CAAC;AACjC,OAAO,EAGL,gCAAgC,GACjC,MAAM,yBAAyB,CAAC;AAEjC,OAAO,EACL,sBAAsB,GAGvB,MAAM,yBAAyB,CAAC;AACjC,OAAO,EAAE,OAAO,EAAyC,MAAM,cAAc,CAAC;AAE9E,OAAO,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAE1D,OAAO,EAAE,qBAAqB,EAAE,MAAM,+BAA+B,CAAC;AACtE,OAAO,EAAE,qBAAqB,EAAE,MAAM,kDAAkD,CAAC;AAEzF,OAAO,EAAE,yBAAyB,EAAE,MAAM,4BAA4B,CAAC;AACvE,OAAO,EAAE,yBAAyB,EAAE,MAAM,sDAAsD,CAAC;AACjG,OAAO,EAAE,8BAA8B,EAAE,MAAM,kCAAkC,CAAC;AAClF,OAAO,EAAE,8BAA8B,EAAE,MAAM,6DAA6D,CAAC"}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { MigrationBuilder } from "node-pg-migrate";
|
|
2
|
+
/** Install the V2 tenant schema on a fresh database, including directory storage. */
|
|
3
|
+
export declare function installTenantSchemaV2(pgm: Pick<MigrationBuilder, "sql">): void;
|
|
4
|
+
//# sourceMappingURL=install-tenant-schema-v2.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"install-tenant-schema-v2.d.ts","sourceRoot":"","sources":["../src/install-tenant-schema-v2.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAC;AAMxD,qFAAqF;AACrF,wBAAgB,qBAAqB,CACnC,GAAG,EAAE,IAAI,CAAC,gBAAgB,EAAE,KAAK,CAAC,GACjC,IAAI,CAIN"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { installTenantSchemaV1 } from "./migrations/20261120040000_tenants.migration.js";
|
|
2
|
+
import { installTenantScimSchemaV1 } from "./migrations/20261120080000_tenant_scim.migration.js";
|
|
3
|
+
import { installTenantScimGroupSchemaV1 } from "./migrations/20261120080100_tenant_scim_groups.migration.js";
|
|
4
|
+
/** Install the V2 tenant schema on a fresh database, including directory storage. */
|
|
5
|
+
export function installTenantSchemaV2(pgm) {
|
|
6
|
+
installTenantSchemaV1(pgm);
|
|
7
|
+
installTenantScimSchemaV1(pgm);
|
|
8
|
+
installTenantScimGroupSchemaV1(pgm);
|
|
9
|
+
}
|
|
10
|
+
//# sourceMappingURL=install-tenant-schema-v2.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"install-tenant-schema-v2.js","sourceRoot":"","sources":["../src/install-tenant-schema-v2.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,qBAAqB,EAAE,MAAM,kDAAkD,CAAC;AACzF,OAAO,EAAE,yBAAyB,EAAE,MAAM,sDAAsD,CAAC;AACjG,OAAO,EAAE,8BAA8B,EAAE,MAAM,6DAA6D,CAAC;AAE7G,qFAAqF;AACrF,MAAM,UAAU,qBAAqB,CACnC,GAAkC;IAElC,qBAAqB,CAAC,GAAG,CAAC,CAAC;IAC3B,yBAAyB,CAAC,GAAG,CAAC,CAAC;IAC/B,8BAA8B,CAAC,GAAG,CAAC,CAAC;AACtC,CAAC"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { type AccessChange } from "@dads-framework/access/runtime";
|
|
2
|
+
import { type TenantId, type TenantRole } from "./tenant-access.js";
|
|
3
|
+
import { type TenantsOptions } from "./tenant-transaction.js";
|
|
4
|
+
export declare function membershipFact(options: TenantsOptions, tenantId: TenantId, accountId: string, role: TenantRole, operation: AccessChange["operation"]): AccessChange;
|
|
5
|
+
//# sourceMappingURL=membership-facts.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"membership-facts.d.ts","sourceRoot":"","sources":["../src/membership-facts.ts"],"names":[],"mappings":"AAEA,OAAO,EAEL,KAAK,YAAY,EAElB,MAAM,gCAAgC,CAAC;AAExC,OAAO,EAEL,KAAK,QAAQ,EACb,KAAK,UAAU,EAChB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAkB,KAAK,cAAc,EAAE,MAAM,yBAAyB,CAAC;AAE9E,wBAAgB,cAAc,CAC5B,OAAO,EAAE,cAAc,EACvB,QAAQ,EAAE,QAAQ,EAClB,SAAS,EAAE,MAAM,EACjB,IAAI,EAAE,UAAU,EAChB,SAAS,EAAE,YAAY,CAAC,WAAW,CAAC,GACnC,YAAY,CAiBd"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { outboxKeyForRelationship, } from "@dads-framework/access/runtime";
|
|
3
|
+
import { tenantRoleToken, } from "./tenant-access.js";
|
|
4
|
+
import { accountSubject } from "./tenant-transaction.js";
|
|
5
|
+
export function membershipFact(options, tenantId, accountId, role, operation) {
|
|
6
|
+
const token = tenantRoleToken(options.access, role);
|
|
7
|
+
const relationship = {
|
|
8
|
+
resource: { type: token.entityWireName, id: tenantId },
|
|
9
|
+
relation: token.roleName,
|
|
10
|
+
subject: accountSubject(options, accountId),
|
|
11
|
+
};
|
|
12
|
+
return {
|
|
13
|
+
idempotencyKey: outboxKeyForRelationship(operation, relationship, randomUUID()),
|
|
14
|
+
relationship,
|
|
15
|
+
operation,
|
|
16
|
+
ownerPartitionId: tenantId,
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
//# sourceMappingURL=membership-facts.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"membership-facts.js","sourceRoot":"","sources":["../src/membership-facts.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAEzC,OAAO,EACL,wBAAwB,GAGzB,MAAM,gCAAgC,CAAC;AAExC,OAAO,EACL,eAAe,GAGhB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,cAAc,EAAuB,MAAM,yBAAyB,CAAC;AAE9E,MAAM,UAAU,cAAc,CAC5B,OAAuB,EACvB,QAAkB,EAClB,SAAiB,EACjB,IAAgB,EAChB,SAAoC;IAEpC,MAAM,KAAK,GAAG,eAAe,CAAC,OAAO,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;IACpD,MAAM,YAAY,GAAsB;QACtC,QAAQ,EAAE,EAAE,IAAI,EAAE,KAAK,CAAC,cAAc,EAAE,EAAE,EAAE,QAAQ,EAAE;QACtD,QAAQ,EAAE,KAAK,CAAC,QAAQ;QACxB,OAAO,EAAE,cAAc,CAAC,OAAO,EAAE,SAAS,CAAC;KAC5C,CAAC;IACF,OAAO;QACL,cAAc,EAAE,wBAAwB,CACtC,SAAS,EACT,YAAY,EACZ,UAAU,EAAE,CACb;QACD,YAAY;QACZ,SAAS;QACT,gBAAgB,EAAE,QAAQ;KAC3B,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { TenantId } from "./tenant-access.js";
|
|
2
|
+
import { type TenantsOptions } from "./tenant-transaction.js";
|
|
3
|
+
/** Inspect a member of this tenant using a single authorized database snapshot. */
|
|
4
|
+
export declare function inspectMemberPermissions(options: TenantsOptions, actorId: string, tenantId: TenantId, accountId: string): Promise<{
|
|
5
|
+
role: "admin" | "member" | "owner";
|
|
6
|
+
view: boolean;
|
|
7
|
+
manageMembers: boolean;
|
|
8
|
+
manageTenant: boolean;
|
|
9
|
+
}>;
|
|
10
|
+
//# sourceMappingURL=membership-permissions.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"membership-permissions.d.ts","sourceRoot":"","sources":["../src/membership-permissions.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAC;AAEnD,OAAO,EAKL,KAAK,cAAc,EACpB,MAAM,yBAAyB,CAAC;AAEjC,mFAAmF;AACnF,wBAAgB,wBAAwB,CACtC,OAAO,EAAE,cAAc,EACvB,OAAO,EAAE,MAAM,EACf,QAAQ,EAAE,QAAQ,EAClB,SAAS,EAAE,MAAM;;;;;GAmDlB"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { and, eq } from "drizzle-orm";
|
|
2
|
+
import { tenantMembership } from "./tenant-tables.js";
|
|
3
|
+
import { accountSubject, checkTenantPermission, TenantUnavailableError, tenantTransaction, } from "./tenant-transaction.js";
|
|
4
|
+
/** Inspect a member of this tenant using a single authorized database snapshot. */
|
|
5
|
+
export function inspectMemberPermissions(options, actorId, tenantId, accountId) {
|
|
6
|
+
const actor = accountSubject(options, actorId);
|
|
7
|
+
const member = accountSubject(options, accountId);
|
|
8
|
+
return tenantTransaction(options, async (tx) => {
|
|
9
|
+
if (!(await checkTenantPermission(options, tx, actor, tenantId, "manage_members"))) {
|
|
10
|
+
throw new TenantUnavailableError();
|
|
11
|
+
}
|
|
12
|
+
const [membership] = await tx
|
|
13
|
+
.select({ role: tenantMembership.role })
|
|
14
|
+
.from(tenantMembership)
|
|
15
|
+
.where(and(eq(tenantMembership.tenantId, tenantId), eq(tenantMembership.accountId, member.id)));
|
|
16
|
+
if (membership === undefined)
|
|
17
|
+
throw new TenantUnavailableError();
|
|
18
|
+
const view = await checkTenantPermission(options, tx, member, tenantId, "view");
|
|
19
|
+
const manageMembers = await checkTenantPermission(options, tx, member, tenantId, "manage_members");
|
|
20
|
+
const manageTenant = await checkTenantPermission(options, tx, member, tenantId, "manage_tenant");
|
|
21
|
+
return { role: membership.role, view, manageMembers, manageTenant };
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
//# sourceMappingURL=membership-permissions.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"membership-permissions.js","sourceRoot":"","sources":["../src/membership-permissions.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,EAAE,EAAE,MAAM,aAAa,CAAC;AAGtC,OAAO,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AACtD,OAAO,EACL,cAAc,EACd,qBAAqB,EACrB,sBAAsB,EACtB,iBAAiB,GAElB,MAAM,yBAAyB,CAAC;AAEjC,mFAAmF;AACnF,MAAM,UAAU,wBAAwB,CACtC,OAAuB,EACvB,OAAe,EACf,QAAkB,EAClB,SAAiB;IAEjB,MAAM,KAAK,GAAG,cAAc,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;IAC/C,MAAM,MAAM,GAAG,cAAc,CAAC,OAAO,EAAE,SAAS,CAAC,CAAC;IAClD,OAAO,iBAAiB,CAAC,OAAO,EAAE,KAAK,EAAE,EAAE,EAAE,EAAE;QAC7C,IACE,CAAC,CAAC,MAAM,qBAAqB,CAC3B,OAAO,EACP,EAAE,EACF,KAAK,EACL,QAAQ,EACR,gBAAgB,CACjB,CAAC,EACF,CAAC;YACD,MAAM,IAAI,sBAAsB,EAAE,CAAC;QACrC,CAAC;QAED,MAAM,CAAC,UAAU,CAAC,GAAG,MAAM,EAAE;aAC1B,MAAM,CAAC,EAAE,IAAI,EAAE,gBAAgB,CAAC,IAAI,EAAE,CAAC;aACvC,IAAI,CAAC,gBAAgB,CAAC;aACtB,KAAK,CACJ,GAAG,CACD,EAAE,CAAC,gBAAgB,CAAC,QAAQ,EAAE,QAAQ,CAAC,EACvC,EAAE,CAAC,gBAAgB,CAAC,SAAS,EAAE,MAAM,CAAC,EAAE,CAAC,CAC1C,CACF,CAAC;QACJ,IAAI,UAAU,KAAK,SAAS;YAAE,MAAM,IAAI,sBAAsB,EAAE,CAAC;QAEjE,MAAM,IAAI,GAAG,MAAM,qBAAqB,CACtC,OAAO,EACP,EAAE,EACF,MAAM,EACN,QAAQ,EACR,MAAM,CACP,CAAC;QACF,MAAM,aAAa,GAAG,MAAM,qBAAqB,CAC/C,OAAO,EACP,EAAE,EACF,MAAM,EACN,QAAQ,EACR,gBAAgB,CACjB,CAAC;QACF,MAAM,YAAY,GAAG,MAAM,qBAAqB,CAC9C,OAAO,EACP,EAAE,EACF,MAAM,EACN,QAAQ,EACR,eAAe,CAChB,CAAC;QACF,OAAO,EAAE,IAAI,EAAE,UAAU,CAAC,IAAI,EAAE,IAAI,EAAE,aAAa,EAAE,YAAY,EAAE,CAAC;IACtE,CAAC,CAAC,CAAC;AACL,CAAC"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { MigrationBuilder } from "node-pg-migrate";
|
|
2
|
+
export declare function installTenantSchemaV1(pgm: Pick<MigrationBuilder, "sql">): void;
|
|
3
|
+
export declare function up(pgm: MigrationBuilder): void;
|
|
4
|
+
export declare function down(pgm: MigrationBuilder): void;
|
|
5
|
+
//# sourceMappingURL=20261120040000_tenants.migration.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"20261120040000_tenants.migration.d.ts","sourceRoot":"","sources":["../../src/migrations/20261120040000_tenants.migration.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAC;AAExD,wBAAgB,qBAAqB,CACnC,GAAG,EAAE,IAAI,CAAC,gBAAgB,EAAE,KAAK,CAAC,GACjC,IAAI,CAqBN;AAED,wBAAgB,EAAE,CAAC,GAAG,EAAE,gBAAgB,GAAG,IAAI,CAE9C;AAED,wBAAgB,IAAI,CAAC,GAAG,EAAE,gBAAgB,GAAG,IAAI,CAIhD"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export function installTenantSchemaV1(pgm) {
|
|
2
|
+
pgm.sql(`
|
|
3
|
+
CREATE TABLE tenant (
|
|
4
|
+
id uuid PRIMARY KEY,
|
|
5
|
+
name text NOT NULL
|
|
6
|
+
);
|
|
7
|
+
CREATE TABLE tenant_membership (
|
|
8
|
+
tenant_id uuid NOT NULL REFERENCES tenant(id),
|
|
9
|
+
account_id text NOT NULL,
|
|
10
|
+
role text NOT NULL CHECK (role IN ('owner', 'admin', 'member')),
|
|
11
|
+
PRIMARY KEY (tenant_id, account_id)
|
|
12
|
+
);
|
|
13
|
+
CREATE TABLE tenant_invitation (
|
|
14
|
+
token_hash text PRIMARY KEY,
|
|
15
|
+
tenant_id uuid NOT NULL REFERENCES tenant(id),
|
|
16
|
+
recipient_email text NOT NULL,
|
|
17
|
+
issued_by text NOT NULL,
|
|
18
|
+
expires_at timestamptz NOT NULL,
|
|
19
|
+
accepted_at timestamptz
|
|
20
|
+
);
|
|
21
|
+
`);
|
|
22
|
+
}
|
|
23
|
+
export function up(pgm) {
|
|
24
|
+
installTenantSchemaV1(pgm);
|
|
25
|
+
}
|
|
26
|
+
export function down(pgm) {
|
|
27
|
+
pgm.dropTable("tenant_invitation");
|
|
28
|
+
pgm.dropTable("tenant_membership");
|
|
29
|
+
pgm.dropTable("tenant");
|
|
30
|
+
}
|
|
31
|
+
//# sourceMappingURL=20261120040000_tenants.migration.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"20261120040000_tenants.migration.js","sourceRoot":"","sources":["../../src/migrations/20261120040000_tenants.migration.ts"],"names":[],"mappings":"AAEA,MAAM,UAAU,qBAAqB,CACnC,GAAkC;IAElC,GAAG,CAAC,GAAG,CAAC;;;;;;;;;;;;;;;;;;;GAmBP,CAAC,CAAC;AACL,CAAC;AAED,MAAM,UAAU,EAAE,CAAC,GAAqB;IACtC,qBAAqB,CAAC,GAAG,CAAC,CAAC;AAC7B,CAAC;AAED,MAAM,UAAU,IAAI,CAAC,GAAqB;IACxC,GAAG,CAAC,SAAS,CAAC,mBAAmB,CAAC,CAAC;IACnC,GAAG,CAAC,SAAS,CAAC,mBAAmB,CAAC,CAAC;IACnC,GAAG,CAAC,SAAS,CAAC,QAAQ,CAAC,CAAC;AAC1B,CAAC"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { MigrationBuilder } from "node-pg-migrate";
|
|
2
|
+
export declare function installTenantScimSchemaV1(pgm: Pick<MigrationBuilder, "sql">): void;
|
|
3
|
+
export declare function up(pgm: MigrationBuilder): void;
|
|
4
|
+
export declare function down(pgm: MigrationBuilder): void;
|
|
5
|
+
//# sourceMappingURL=20261120080000_tenant_scim.migration.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"20261120080000_tenant_scim.migration.d.ts","sourceRoot":"","sources":["../../src/migrations/20261120080000_tenant_scim.migration.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAC;AAExD,wBAAgB,yBAAyB,CACvC,GAAG,EAAE,IAAI,CAAC,gBAAgB,EAAE,KAAK,CAAC,GACjC,IAAI,CA0BN;AAED,wBAAgB,EAAE,CAAC,GAAG,EAAE,gBAAgB,GAAG,IAAI,CAE9C;AAED,wBAAgB,IAAI,CAAC,GAAG,EAAE,gBAAgB,GAAG,IAAI,CAEhD"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
export function installTenantScimSchemaV1(pgm) {
|
|
2
|
+
pgm.sql(`
|
|
3
|
+
CREATE TABLE tenant_scim_user (
|
|
4
|
+
tenant_id uuid NOT NULL REFERENCES tenant(id),
|
|
5
|
+
id uuid NOT NULL,
|
|
6
|
+
user_name text NOT NULL,
|
|
7
|
+
user_name_key text NOT NULL,
|
|
8
|
+
active boolean NOT NULL,
|
|
9
|
+
external_id text,
|
|
10
|
+
display_name text,
|
|
11
|
+
display_name_key text,
|
|
12
|
+
name jsonb,
|
|
13
|
+
emails jsonb,
|
|
14
|
+
email_keys text[],
|
|
15
|
+
account_id text,
|
|
16
|
+
created_at timestamptz NOT NULL,
|
|
17
|
+
modified_at timestamptz NOT NULL,
|
|
18
|
+
deleted_at timestamptz,
|
|
19
|
+
PRIMARY KEY (tenant_id, id),
|
|
20
|
+
UNIQUE (tenant_id, account_id),
|
|
21
|
+
CHECK (deleted_at IS NULL OR NOT active)
|
|
22
|
+
);
|
|
23
|
+
CREATE UNIQUE INDEX tenant_scim_user_live_name
|
|
24
|
+
ON tenant_scim_user (tenant_id, user_name_key)
|
|
25
|
+
WHERE deleted_at IS NULL;
|
|
26
|
+
`);
|
|
27
|
+
}
|
|
28
|
+
export function up(pgm) {
|
|
29
|
+
installTenantScimSchemaV1(pgm);
|
|
30
|
+
}
|
|
31
|
+
export function down(pgm) {
|
|
32
|
+
pgm.dropTable("tenant_scim_user");
|
|
33
|
+
}
|
|
34
|
+
//# sourceMappingURL=20261120080000_tenant_scim.migration.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"20261120080000_tenant_scim.migration.js","sourceRoot":"","sources":["../../src/migrations/20261120080000_tenant_scim.migration.ts"],"names":[],"mappings":"AAEA,MAAM,UAAU,yBAAyB,CACvC,GAAkC;IAElC,GAAG,CAAC,GAAG,CAAC;;;;;;;;;;;;;;;;;;;;;;;;GAwBP,CAAC,CAAC;AACL,CAAC;AAED,MAAM,UAAU,EAAE,CAAC,GAAqB;IACtC,yBAAyB,CAAC,GAAG,CAAC,CAAC;AACjC,CAAC;AAED,MAAM,UAAU,IAAI,CAAC,GAAqB;IACxC,GAAG,CAAC,SAAS,CAAC,kBAAkB,CAAC,CAAC;AACpC,CAAC"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { MigrationBuilder } from "node-pg-migrate";
|
|
2
|
+
export declare function installTenantScimGroupSchemaV1(pgm: Pick<MigrationBuilder, "sql">): void;
|
|
3
|
+
export declare function up(pgm: MigrationBuilder): void;
|
|
4
|
+
export declare function down(pgm: MigrationBuilder): void;
|
|
5
|
+
//# sourceMappingURL=20261120080100_tenant_scim_groups.migration.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"20261120080100_tenant_scim_groups.migration.d.ts","sourceRoot":"","sources":["../../src/migrations/20261120080100_tenant_scim_groups.migration.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAC;AAExD,wBAAgB,8BAA8B,CAC5C,GAAG,EAAE,IAAI,CAAC,gBAAgB,EAAE,KAAK,CAAC,GACjC,IAAI,CAyBN;AAED,wBAAgB,EAAE,CAAC,GAAG,EAAE,gBAAgB,GAAG,IAAI,CAE9C;AAED,wBAAgB,IAAI,CAAC,GAAG,EAAE,gBAAgB,GAAG,IAAI,CAGhD"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
export function installTenantScimGroupSchemaV1(pgm) {
|
|
2
|
+
pgm.sql(`
|
|
3
|
+
CREATE TABLE tenant_scim_group (
|
|
4
|
+
tenant_id uuid NOT NULL REFERENCES tenant(id),
|
|
5
|
+
id uuid NOT NULL,
|
|
6
|
+
display_name text NOT NULL,
|
|
7
|
+
display_name_key text NOT NULL,
|
|
8
|
+
external_id text,
|
|
9
|
+
created_at timestamptz NOT NULL,
|
|
10
|
+
modified_at timestamptz NOT NULL,
|
|
11
|
+
PRIMARY KEY (tenant_id, id)
|
|
12
|
+
);
|
|
13
|
+
CREATE TABLE tenant_scim_group_member (
|
|
14
|
+
tenant_id uuid NOT NULL,
|
|
15
|
+
group_id uuid NOT NULL,
|
|
16
|
+
user_id uuid NOT NULL,
|
|
17
|
+
PRIMARY KEY (tenant_id, group_id, user_id),
|
|
18
|
+
FOREIGN KEY (tenant_id, group_id)
|
|
19
|
+
REFERENCES tenant_scim_group (tenant_id, id) ON DELETE CASCADE,
|
|
20
|
+
FOREIGN KEY (tenant_id, user_id)
|
|
21
|
+
REFERENCES tenant_scim_user (tenant_id, id)
|
|
22
|
+
);
|
|
23
|
+
CREATE INDEX tenant_scim_group_member_user
|
|
24
|
+
ON tenant_scim_group_member (tenant_id, user_id);
|
|
25
|
+
`);
|
|
26
|
+
}
|
|
27
|
+
export function up(pgm) {
|
|
28
|
+
installTenantScimGroupSchemaV1(pgm);
|
|
29
|
+
}
|
|
30
|
+
export function down(pgm) {
|
|
31
|
+
pgm.dropTable("tenant_scim_group_member");
|
|
32
|
+
pgm.dropTable("tenant_scim_group");
|
|
33
|
+
}
|
|
34
|
+
//# sourceMappingURL=20261120080100_tenant_scim_groups.migration.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"20261120080100_tenant_scim_groups.migration.js","sourceRoot":"","sources":["../../src/migrations/20261120080100_tenant_scim_groups.migration.ts"],"names":[],"mappings":"AAEA,MAAM,UAAU,8BAA8B,CAC5C,GAAkC;IAElC,GAAG,CAAC,GAAG,CAAC;;;;;;;;;;;;;;;;;;;;;;;GAuBP,CAAC,CAAC;AACL,CAAC;AAED,MAAM,UAAU,EAAE,CAAC,GAAqB;IACtC,8BAA8B,CAAC,GAAG,CAAC,CAAC;AACtC,CAAC;AAED,MAAM,UAAU,IAAI,CAAC,GAAqB;IACxC,GAAG,CAAC,SAAS,CAAC,0BAA0B,CAAC,CAAC;IAC1C,GAAG,CAAC,SAAS,CAAC,mBAAmB,CAAC,CAAC;AACrC,CAAC"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { type EntityHandle, type RoleToken } from "@dads-framework/access";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
export declare const tenantIdSchema: z.core.$ZodBranded<z.ZodUUID, "TenantId", "out">;
|
|
4
|
+
export type TenantId = z.output<typeof tenantIdSchema>;
|
|
5
|
+
export declare const tenantEntity: EntityHandle<"tenants/tenant">;
|
|
6
|
+
export declare const tenantRoles: readonly ["owner", "admin", "member"];
|
|
7
|
+
export type TenantRole = (typeof tenantRoles)[number];
|
|
8
|
+
export declare const assignableTenantRoleSchema: z.ZodEnum<{
|
|
9
|
+
admin: "admin";
|
|
10
|
+
member: "member";
|
|
11
|
+
}>;
|
|
12
|
+
export declare function createTenantAccess(account: EntityHandle): {
|
|
13
|
+
account: EntityHandle;
|
|
14
|
+
tenant: import("@dads-framework/access").AccessDefinition<{
|
|
15
|
+
owner: EntityHandle;
|
|
16
|
+
admin: EntityHandle;
|
|
17
|
+
member: EntityHandle;
|
|
18
|
+
}, {
|
|
19
|
+
view: RoleToken[];
|
|
20
|
+
manage_members: RoleToken[];
|
|
21
|
+
manage_tenant: RoleToken[];
|
|
22
|
+
}>;
|
|
23
|
+
};
|
|
24
|
+
export type TenantAccess = ReturnType<typeof createTenantAccess>;
|
|
25
|
+
export declare function tenantRoleToken(tenant: TenantAccess, role: TenantRole): RoleToken;
|
|
26
|
+
//# sourceMappingURL=tenant-access.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tenant-access.d.ts","sourceRoot":"","sources":["../src/tenant-access.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,KAAK,YAAY,EACjB,KAAK,SAAS,EACf,MAAM,wBAAwB,CAAC;AAChC,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,eAAO,MAAM,cAAc,kDAA+B,CAAC;AAC3D,MAAM,MAAM,QAAQ,GAAG,CAAC,CAAC,MAAM,CAAC,OAAO,cAAc,CAAC,CAAC;AAEvD,eAAO,MAAM,YAAY,gCAIvB,CAAC;AAEH,eAAO,MAAM,WAAW,YAAI,OAAO,EAAE,OAAO,EAAE,QAAQ,CAAU,CAAC;AACjE,MAAM,MAAM,UAAU,GAAG,CAAC,OAAO,WAAW,CAAC,CAAC,MAAM,CAAC,CAAC;AACtD,eAAO,MAAM,0BAA0B;;;EAElB,CAAC;AAEtB,wBAAgB,kBAAkB,CAAC,OAAO,EAAE,YAAY;;;;;;;;;;;EAavD;AAED,MAAM,MAAM,YAAY,GAAG,UAAU,CAAC,OAAO,kBAAkB,CAAC,CAAC;AAEjE,wBAAgB,eAAe,CAC7B,MAAM,EAAE,YAAY,EACpB,IAAI,EAAE,UAAU,GACf,SAAS,CAEX"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { access, entityHandle, } from "@dads-framework/access";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
export const tenantIdSchema = z.uuid().brand();
|
|
4
|
+
export const tenantEntity = entityHandle({
|
|
5
|
+
wireName: "tenants/tenant",
|
|
6
|
+
idSchema: tenantIdSchema,
|
|
7
|
+
kind: "scope",
|
|
8
|
+
});
|
|
9
|
+
export const tenantRoles = ["owner", "admin", "member"];
|
|
10
|
+
export const assignableTenantRoleSchema = z
|
|
11
|
+
.enum(tenantRoles)
|
|
12
|
+
.exclude(["owner"]);
|
|
13
|
+
export function createTenantAccess(account) {
|
|
14
|
+
const tenant = access(tenantEntity, {
|
|
15
|
+
roles: { owner: account, admin: account, member: account },
|
|
16
|
+
permissions: (roles) => ({
|
|
17
|
+
view: [roles.owner, roles.admin, roles.member],
|
|
18
|
+
manage_members: [roles.owner, roles.admin],
|
|
19
|
+
manage_tenant: [roles.owner],
|
|
20
|
+
}),
|
|
21
|
+
});
|
|
22
|
+
return { account, tenant };
|
|
23
|
+
}
|
|
24
|
+
export function tenantRoleToken(tenant, role) {
|
|
25
|
+
return tenant.tenant.roles[role];
|
|
26
|
+
}
|
|
27
|
+
//# sourceMappingURL=tenant-access.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tenant-access.js","sourceRoot":"","sources":["../src/tenant-access.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,MAAM,EACN,YAAY,GAGb,MAAM,wBAAwB,CAAC;AAChC,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,MAAM,CAAC,MAAM,cAAc,GAAG,CAAC,CAAC,IAAI,EAAE,CAAC,KAAK,EAAc,CAAC;AAG3D,MAAM,CAAC,MAAM,YAAY,GAAG,YAAY,CAAC;IACvC,QAAQ,EAAE,gBAAgB;IAC1B,QAAQ,EAAE,cAAc;IACxB,IAAI,EAAE,OAAO;CACd,CAAC,CAAC;AAEH,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,OAAO,EAAE,OAAO,EAAE,QAAQ,CAAU,CAAC;AAEjE,MAAM,CAAC,MAAM,0BAA0B,GAAG,CAAC;KACxC,IAAI,CAAC,WAAW,CAAC;KACjB,OAAO,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC;AAEtB,MAAM,UAAU,kBAAkB,CAAC,OAAqB;IACtD,MAAM,MAAM,GAAG,MAAM,CAAC,YAAY,EAAE;QAClC,KAAK,EAAE,EAAE,KAAK,EAAE,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE,OAAO,EAGvD;QACD,WAAW,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC;YACvB,IAAI,EAAE,CAAC,KAAK,CAAC,KAAK,EAAE,KAAK,CAAC,KAAK,EAAE,KAAK,CAAC,MAAM,CAAC;YAC9C,cAAc,EAAE,CAAC,KAAK,CAAC,KAAK,EAAE,KAAK,CAAC,KAAK,CAAC;YAC1C,aAAa,EAAE,CAAC,KAAK,CAAC,KAAK,CAAC;SAC7B,CAAC;KACH,CAAC,CAAC;IACH,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,CAAC;AAC7B,CAAC;AAID,MAAM,UAAU,eAAe,CAC7B,MAAoB,EACpB,IAAgB;IAEhB,OAAO,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;AACnC,CAAC"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { TenantDatabase } from "./tenant-transaction.js";
|
|
2
|
+
/** Use only behind platform authorization, such as flag management. */
|
|
3
|
+
export declare function createTenantDirectory(database: Pick<TenantDatabase, "select">): {
|
|
4
|
+
findActiveTenants(ids: readonly string[]): Promise<{
|
|
5
|
+
id: string & import("zod").$brand<"TenantId">;
|
|
6
|
+
name: string;
|
|
7
|
+
}[]>;
|
|
8
|
+
};
|
|
9
|
+
/** Configure these tables after installing the generic audit trigger function. */
|
|
10
|
+
export declare const tenantAuditConfiguration: {
|
|
11
|
+
tables: {
|
|
12
|
+
tenant: {
|
|
13
|
+
keyColumns: string[];
|
|
14
|
+
scopeColumns: {
|
|
15
|
+
tenant_id: string;
|
|
16
|
+
};
|
|
17
|
+
};
|
|
18
|
+
tenant_membership: {
|
|
19
|
+
keyColumns: string[];
|
|
20
|
+
scopeColumns: {
|
|
21
|
+
tenant_id: string;
|
|
22
|
+
};
|
|
23
|
+
};
|
|
24
|
+
};
|
|
25
|
+
excludeTables: string[];
|
|
26
|
+
};
|
|
27
|
+
//# sourceMappingURL=tenant-composition.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tenant-composition.d.ts","sourceRoot":"","sources":["../src/tenant-composition.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,yBAAyB,CAAC;AAE9D,uEAAuE;AACvE,wBAAgB,qBAAqB,CACnC,QAAQ,EAAE,IAAI,CAAC,cAAc,EAAE,QAAQ,CAAC;IAGhC,iBAAiB,MAAM,SAAS,MAAM,EAAE;;;;EAcjD;AAED,kFAAkF;AAClF,eAAO,MAAM,wBAAwB;IACnC,MAAM;QACJ,MAAM;YAAI,UAAU;YAAU,YAAY;gBAAI,SAAS;;;QACvD,iBAAiB;YACf,UAAU;YACV,YAAY;gBAAI,SAAS;;;;IAG7B,aAAa;CAMd,CAAC"}
|