@dads-framework/tenants 0.0.0-stage → 0.1.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 +59 -2
- package/dist/index.d.ts +9 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +8 -0
- package/dist/index.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/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/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 +35 -0
- package/dist/tenant-composition.js.map +1 -0
- package/dist/tenant-invitations.d.ts +26 -0
- package/dist/tenant-invitations.d.ts.map +1 -0
- package/dist/tenant-invitations.js +106 -0
- package/dist/tenant-invitations.js.map +1 -0
- package/dist/tenant-memberships.d.ts +13 -0
- package/dist/tenant-memberships.d.ts.map +1 -0
- package/dist/tenant-memberships.js +82 -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-tables.d.ts +217 -0
- package/dist/tenant-tables.d.ts.map +1 -0
- package/dist/tenant-tables.js +28 -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 +30 -0
- package/dist/tenants.d.ts.map +1 -0
- package/dist/tenants.js +89 -0
- package/dist/tenants.js.map +1 -0
- package/dist/tenants.tsbuildinfo +1 -0
- package/package.json +44 -4
- package/src/migrations/20261120040000_tenants.migration.ts +36 -0
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
export declare const tenantTable: import("drizzle-orm/pg-core").PgTableWithColumns<{
|
|
2
|
+
name: "tenant";
|
|
3
|
+
schema: undefined;
|
|
4
|
+
columns: {
|
|
5
|
+
id: import("drizzle-orm/pg-core").PgColumn<{
|
|
6
|
+
name: "id";
|
|
7
|
+
tableName: "tenant";
|
|
8
|
+
dataType: "string";
|
|
9
|
+
columnType: "PgUUID";
|
|
10
|
+
data: string & import("zod").$brand<"TenantId">;
|
|
11
|
+
driverParam: string;
|
|
12
|
+
notNull: true;
|
|
13
|
+
hasDefault: false;
|
|
14
|
+
isPrimaryKey: true;
|
|
15
|
+
isAutoincrement: false;
|
|
16
|
+
hasRuntimeDefault: false;
|
|
17
|
+
enumValues: undefined;
|
|
18
|
+
baseColumn: never;
|
|
19
|
+
identity: undefined;
|
|
20
|
+
generated: undefined;
|
|
21
|
+
}, {}, {
|
|
22
|
+
$type: string & import("zod").$brand<"TenantId">;
|
|
23
|
+
}>;
|
|
24
|
+
name: import("drizzle-orm/pg-core").PgColumn<{
|
|
25
|
+
name: "name";
|
|
26
|
+
tableName: "tenant";
|
|
27
|
+
dataType: "string";
|
|
28
|
+
columnType: "PgText";
|
|
29
|
+
data: string;
|
|
30
|
+
driverParam: string;
|
|
31
|
+
notNull: true;
|
|
32
|
+
hasDefault: false;
|
|
33
|
+
isPrimaryKey: false;
|
|
34
|
+
isAutoincrement: false;
|
|
35
|
+
hasRuntimeDefault: false;
|
|
36
|
+
enumValues: [string, ...string[]];
|
|
37
|
+
baseColumn: never;
|
|
38
|
+
identity: undefined;
|
|
39
|
+
generated: undefined;
|
|
40
|
+
}, {}, {}>;
|
|
41
|
+
};
|
|
42
|
+
dialect: 'pg';
|
|
43
|
+
}>;
|
|
44
|
+
export declare const tenantMembership: import("drizzle-orm/pg-core").PgTableWithColumns<{
|
|
45
|
+
name: "tenant_membership";
|
|
46
|
+
schema: undefined;
|
|
47
|
+
columns: {
|
|
48
|
+
tenantId: import("drizzle-orm/pg-core").PgColumn<{
|
|
49
|
+
name: "tenant_id";
|
|
50
|
+
tableName: "tenant_membership";
|
|
51
|
+
dataType: "string";
|
|
52
|
+
columnType: "PgUUID";
|
|
53
|
+
data: string & import("zod").$brand<"TenantId">;
|
|
54
|
+
driverParam: string;
|
|
55
|
+
notNull: true;
|
|
56
|
+
hasDefault: false;
|
|
57
|
+
isPrimaryKey: false;
|
|
58
|
+
isAutoincrement: false;
|
|
59
|
+
hasRuntimeDefault: false;
|
|
60
|
+
enumValues: undefined;
|
|
61
|
+
baseColumn: never;
|
|
62
|
+
identity: undefined;
|
|
63
|
+
generated: undefined;
|
|
64
|
+
}, {}, {
|
|
65
|
+
$type: string & import("zod").$brand<"TenantId">;
|
|
66
|
+
}>;
|
|
67
|
+
accountId: import("drizzle-orm/pg-core").PgColumn<{
|
|
68
|
+
name: "account_id";
|
|
69
|
+
tableName: "tenant_membership";
|
|
70
|
+
dataType: "string";
|
|
71
|
+
columnType: "PgText";
|
|
72
|
+
data: string;
|
|
73
|
+
driverParam: string;
|
|
74
|
+
notNull: true;
|
|
75
|
+
hasDefault: false;
|
|
76
|
+
isPrimaryKey: false;
|
|
77
|
+
isAutoincrement: false;
|
|
78
|
+
hasRuntimeDefault: false;
|
|
79
|
+
enumValues: [string, ...string[]];
|
|
80
|
+
baseColumn: never;
|
|
81
|
+
identity: undefined;
|
|
82
|
+
generated: undefined;
|
|
83
|
+
}, {}, {}>;
|
|
84
|
+
role: import("drizzle-orm/pg-core").PgColumn<{
|
|
85
|
+
name: "role";
|
|
86
|
+
tableName: "tenant_membership";
|
|
87
|
+
dataType: "string";
|
|
88
|
+
columnType: "PgText";
|
|
89
|
+
data: "admin" | "member" | "owner";
|
|
90
|
+
driverParam: string;
|
|
91
|
+
notNull: true;
|
|
92
|
+
hasDefault: false;
|
|
93
|
+
isPrimaryKey: false;
|
|
94
|
+
isAutoincrement: false;
|
|
95
|
+
hasRuntimeDefault: false;
|
|
96
|
+
enumValues: [string, ...string[]];
|
|
97
|
+
baseColumn: never;
|
|
98
|
+
identity: undefined;
|
|
99
|
+
generated: undefined;
|
|
100
|
+
}, {}, {
|
|
101
|
+
$type: "admin" | "member" | "owner";
|
|
102
|
+
}>;
|
|
103
|
+
};
|
|
104
|
+
dialect: 'pg';
|
|
105
|
+
}>;
|
|
106
|
+
export declare const tenantInvitation: import("drizzle-orm/pg-core").PgTableWithColumns<{
|
|
107
|
+
name: "tenant_invitation";
|
|
108
|
+
schema: undefined;
|
|
109
|
+
columns: {
|
|
110
|
+
tokenHash: import("drizzle-orm/pg-core").PgColumn<{
|
|
111
|
+
name: "token_hash";
|
|
112
|
+
tableName: "tenant_invitation";
|
|
113
|
+
dataType: "string";
|
|
114
|
+
columnType: "PgText";
|
|
115
|
+
data: string;
|
|
116
|
+
driverParam: string;
|
|
117
|
+
notNull: true;
|
|
118
|
+
hasDefault: false;
|
|
119
|
+
isPrimaryKey: true;
|
|
120
|
+
isAutoincrement: false;
|
|
121
|
+
hasRuntimeDefault: false;
|
|
122
|
+
enumValues: [string, ...string[]];
|
|
123
|
+
baseColumn: never;
|
|
124
|
+
identity: undefined;
|
|
125
|
+
generated: undefined;
|
|
126
|
+
}, {}, {}>;
|
|
127
|
+
tenantId: import("drizzle-orm/pg-core").PgColumn<{
|
|
128
|
+
name: "tenant_id";
|
|
129
|
+
tableName: "tenant_invitation";
|
|
130
|
+
dataType: "string";
|
|
131
|
+
columnType: "PgUUID";
|
|
132
|
+
data: string & import("zod").$brand<"TenantId">;
|
|
133
|
+
driverParam: string;
|
|
134
|
+
notNull: true;
|
|
135
|
+
hasDefault: false;
|
|
136
|
+
isPrimaryKey: false;
|
|
137
|
+
isAutoincrement: false;
|
|
138
|
+
hasRuntimeDefault: false;
|
|
139
|
+
enumValues: undefined;
|
|
140
|
+
baseColumn: never;
|
|
141
|
+
identity: undefined;
|
|
142
|
+
generated: undefined;
|
|
143
|
+
}, {}, {
|
|
144
|
+
$type: string & import("zod").$brand<"TenantId">;
|
|
145
|
+
}>;
|
|
146
|
+
recipientEmail: import("drizzle-orm/pg-core").PgColumn<{
|
|
147
|
+
name: "recipient_email";
|
|
148
|
+
tableName: "tenant_invitation";
|
|
149
|
+
dataType: "string";
|
|
150
|
+
columnType: "PgText";
|
|
151
|
+
data: string;
|
|
152
|
+
driverParam: string;
|
|
153
|
+
notNull: true;
|
|
154
|
+
hasDefault: false;
|
|
155
|
+
isPrimaryKey: false;
|
|
156
|
+
isAutoincrement: false;
|
|
157
|
+
hasRuntimeDefault: false;
|
|
158
|
+
enumValues: [string, ...string[]];
|
|
159
|
+
baseColumn: never;
|
|
160
|
+
identity: undefined;
|
|
161
|
+
generated: undefined;
|
|
162
|
+
}, {}, {}>;
|
|
163
|
+
issuedBy: import("drizzle-orm/pg-core").PgColumn<{
|
|
164
|
+
name: "issued_by";
|
|
165
|
+
tableName: "tenant_invitation";
|
|
166
|
+
dataType: "string";
|
|
167
|
+
columnType: "PgText";
|
|
168
|
+
data: string;
|
|
169
|
+
driverParam: string;
|
|
170
|
+
notNull: true;
|
|
171
|
+
hasDefault: false;
|
|
172
|
+
isPrimaryKey: false;
|
|
173
|
+
isAutoincrement: false;
|
|
174
|
+
hasRuntimeDefault: false;
|
|
175
|
+
enumValues: [string, ...string[]];
|
|
176
|
+
baseColumn: never;
|
|
177
|
+
identity: undefined;
|
|
178
|
+
generated: undefined;
|
|
179
|
+
}, {}, {}>;
|
|
180
|
+
expiresAt: import("drizzle-orm/pg-core").PgColumn<{
|
|
181
|
+
name: "expires_at";
|
|
182
|
+
tableName: "tenant_invitation";
|
|
183
|
+
dataType: "date";
|
|
184
|
+
columnType: "PgTimestamp";
|
|
185
|
+
data: Date;
|
|
186
|
+
driverParam: string;
|
|
187
|
+
notNull: true;
|
|
188
|
+
hasDefault: false;
|
|
189
|
+
isPrimaryKey: false;
|
|
190
|
+
isAutoincrement: false;
|
|
191
|
+
hasRuntimeDefault: false;
|
|
192
|
+
enumValues: undefined;
|
|
193
|
+
baseColumn: never;
|
|
194
|
+
identity: undefined;
|
|
195
|
+
generated: undefined;
|
|
196
|
+
}, {}, {}>;
|
|
197
|
+
acceptedAt: import("drizzle-orm/pg-core").PgColumn<{
|
|
198
|
+
name: "accepted_at";
|
|
199
|
+
tableName: "tenant_invitation";
|
|
200
|
+
dataType: "date";
|
|
201
|
+
columnType: "PgTimestamp";
|
|
202
|
+
data: Date;
|
|
203
|
+
driverParam: string;
|
|
204
|
+
notNull: false;
|
|
205
|
+
hasDefault: false;
|
|
206
|
+
isPrimaryKey: false;
|
|
207
|
+
isAutoincrement: false;
|
|
208
|
+
hasRuntimeDefault: false;
|
|
209
|
+
enumValues: undefined;
|
|
210
|
+
baseColumn: never;
|
|
211
|
+
identity: undefined;
|
|
212
|
+
generated: undefined;
|
|
213
|
+
}, {}, {}>;
|
|
214
|
+
};
|
|
215
|
+
dialect: 'pg';
|
|
216
|
+
}>;
|
|
217
|
+
//# sourceMappingURL=tenant-tables.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tenant-tables.d.ts","sourceRoot":"","sources":["../src/tenant-tables.ts"],"names":[],"mappings":"AAUA,eAAO,MAAM,WAAW;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAGtB,CAAC;AAEH,eAAO,MAAM,gBAAgB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAW5B,CAAC;AAEF,eAAO,MAAM,gBAAgB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAa3B,CAAC"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { pgTable, primaryKey, text, timestamp, uuid, } from "drizzle-orm/pg-core";
|
|
2
|
+
export const tenantTable = pgTable("tenant", {
|
|
3
|
+
id: uuid("id").$type().primaryKey(),
|
|
4
|
+
name: text("name").notNull(),
|
|
5
|
+
});
|
|
6
|
+
export const tenantMembership = pgTable("tenant_membership", {
|
|
7
|
+
tenantId: uuid("tenant_id")
|
|
8
|
+
.$type()
|
|
9
|
+
.notNull()
|
|
10
|
+
.references(() => tenantTable.id),
|
|
11
|
+
accountId: text("account_id").notNull(),
|
|
12
|
+
role: text("role").$type().notNull(),
|
|
13
|
+
}, (table) => [primaryKey({ columns: [table.tenantId, table.accountId] })]);
|
|
14
|
+
export const tenantInvitation = pgTable("tenant_invitation", {
|
|
15
|
+
tokenHash: text("token_hash").primaryKey(),
|
|
16
|
+
tenantId: uuid("tenant_id")
|
|
17
|
+
.$type()
|
|
18
|
+
.notNull()
|
|
19
|
+
.references(() => tenantTable.id),
|
|
20
|
+
recipientEmail: text("recipient_email").notNull(),
|
|
21
|
+
issuedBy: text("issued_by").notNull(),
|
|
22
|
+
expiresAt: timestamp("expires_at", {
|
|
23
|
+
mode: "date",
|
|
24
|
+
withTimezone: true,
|
|
25
|
+
}).notNull(),
|
|
26
|
+
acceptedAt: timestamp("accepted_at", { mode: "date", withTimezone: true }),
|
|
27
|
+
});
|
|
28
|
+
//# sourceMappingURL=tenant-tables.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tenant-tables.js","sourceRoot":"","sources":["../src/tenant-tables.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,OAAO,EACP,UAAU,EACV,IAAI,EACJ,SAAS,EACT,IAAI,GACL,MAAM,qBAAqB,CAAC;AAI7B,MAAM,CAAC,MAAM,WAAW,GAAG,OAAO,CAAC,QAAQ,EAAE;IAC3C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,EAAE,YAAY,EAAE,IAAI,EAAE,CAAC;CAC3E,CAAC,CAAC"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { type Authorization, type EnqueueAccessChanges, type SubjectRef } from "@dads-framework/access/runtime";
|
|
2
|
+
import type { DrizzleHandle } from "@dads-framework/postgres";
|
|
3
|
+
import type { TenantAccess, TenantId } from "./tenant-access.js";
|
|
4
|
+
export type TenantTransaction = Parameters<Parameters<DrizzleHandle["transaction"]>[0]>[0];
|
|
5
|
+
export type TenantDatabase = Pick<DrizzleHandle, "select" | "transaction">;
|
|
6
|
+
export type TenantsOptions = {
|
|
7
|
+
readonly database: TenantDatabase;
|
|
8
|
+
readonly access: TenantAccess;
|
|
9
|
+
/** Resolves and writes facts through the same primary database transaction. */
|
|
10
|
+
readonly authorizationAt: (transaction: TenantTransaction) => Pick<Authorization, "check" | "lookupResources">;
|
|
11
|
+
readonly changes: Pick<EnqueueAccessChanges<TenantTransaction>, "enqueue">;
|
|
12
|
+
readonly now?: () => Date;
|
|
13
|
+
};
|
|
14
|
+
export declare class TenantUnavailableError extends Error {
|
|
15
|
+
constructor();
|
|
16
|
+
}
|
|
17
|
+
export declare function accountSubject(options: TenantsOptions, accountId: string): SubjectRef;
|
|
18
|
+
export declare function checkTenantPermission(options: TenantsOptions, tx: TenantTransaction, identity: SubjectRef, tenantId: TenantId, permission: keyof TenantAccess["tenant"]["permissions"]): Promise<boolean>;
|
|
19
|
+
export declare function tenantTransaction<Result>(options: TenantsOptions, work: (tx: TenantTransaction) => Promise<Result>): Promise<Result>;
|
|
20
|
+
//# sourceMappingURL=tenant-transaction.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tenant-transaction.d.ts","sourceRoot":"","sources":["../src/tenant-transaction.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,aAAa,EAClB,KAAK,oBAAoB,EACzB,KAAK,UAAU,EAChB,MAAM,gCAAgC,CAAC;AACxC,OAAO,KAAK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K,OAAO,CAAC,MAAM,CAAC,GAC/C,OAAO,CAAC,MAAM,CAAC,CAEjB"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import {} from "@dads-framework/access/runtime";
|
|
2
|
+
export class TenantUnavailableError extends Error {
|
|
3
|
+
constructor() {
|
|
4
|
+
super("Tenant is unavailable");
|
|
5
|
+
this.name = "TenantUnavailableError";
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
export function accountSubject(options, accountId) {
|
|
9
|
+
const id = options.access.account.idSchema.parse(accountId);
|
|
10
|
+
return { type: options.access.account.wireName, id };
|
|
11
|
+
}
|
|
12
|
+
export async function checkTenantPermission(options, tx, identity, tenantId, permission) {
|
|
13
|
+
const token = options.access.tenant.permissions[permission];
|
|
14
|
+
return options.authorizationAt(tx).check({
|
|
15
|
+
subject: identity,
|
|
16
|
+
resource: { type: token.entityWireName, id: tenantId },
|
|
17
|
+
permission: token.permissionName,
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
export function tenantTransaction(options, work) {
|
|
21
|
+
return options.database.transaction(work, { isolationLevel: "serializable" });
|
|
22
|
+
}
|
|
23
|
+
//# sourceMappingURL=tenant-transaction.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tenant-transaction.js","sourceRoot":"","sources":["../src/tenant-transaction.ts"],"names":[],"mappings":"AAAA,OAAO,EAIN,MAAM,gCAAgC,CAAC;AAqBxC,MAAM,OAAO,sBAAuB,SAAQ,KAAK;IAC/C;QACE,KAAK,CAAC,uBAAuB,CAAC,CAAC;QAC/B,IAAI,CAAC,IAAI,GAAG,wBAAwB,CAAC;IACvC,CAAC;CACF;AAED,MAAM,UAAU,cAAc,CAC5B,OAAuB,EACvB,SAAiB;IAEjB,MAAM,EAAE,GAAG,OAAO,CAAC,MAAM,CAAC,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC;IAC5D,OAAO,EAAE,IAAI,EAAE,OAAO,CAAC,MAAM,CAAC,OAAO,CAAC,QAAQ,EAAE,EAAE,EAAE,CAAC;AACvD,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,qBAAqB,CACzC,OAAuB,EACvB,EAAqB,EACrB,QAAoB,EACpB,QAAkB,EAClB,UAAuD;IAEvD,MAAM,KAAK,GAAG,OAAO,CAAC,MAAM,CAAC,MAAM,CAAC,WAAW,CAAC,UAAU,CAAC,CAAC;IAC5D,OAAO,OAAO,CAAC,eAAe,CAAC,EAAE,CAAC,CAAC,KAAK,CAAC;QACvC,OAAO,EAAE,QAAQ;QACjB,QAAQ,EAAE,EAAE,IAAI,EAAE,KAAK,CAAC,cAAc,EAAE,EAAE,EAAE,QAAQ,EAAE;QACtD,UAAU,EAAE,KAAK,CAAC,cAAc;KACjC,CAAC,CAAC;AACL,CAAC;AAED,MAAM,UAAU,iBAAiB,CAC/B,OAAuB,EACvB,IAAgD;IAEhD,OAAO,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAC,IAAI,EAAE,EAAE,cAAc,EAAE,cAAc,EAAE,CAAC,CAAC;AAChF,CAAC"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { type TenantId } from "./tenant-access.js";
|
|
2
|
+
import { TenantInvitations } from "./tenant-invitations.js";
|
|
3
|
+
import { TenantMemberships } from "./tenant-memberships.js";
|
|
4
|
+
import { tenantTable } from "./tenant-tables.js";
|
|
5
|
+
import { type TenantsOptions } from "./tenant-transaction.js";
|
|
6
|
+
export type TenantRecord = typeof tenantTable.$inferSelect;
|
|
7
|
+
export type TenantContext = {
|
|
8
|
+
readonly tenant: TenantRecord;
|
|
9
|
+
readonly audience: {
|
|
10
|
+
readonly kind: "tenant";
|
|
11
|
+
readonly tenantId: TenantId;
|
|
12
|
+
};
|
|
13
|
+
readonly auditScope: {
|
|
14
|
+
readonly kind: "application";
|
|
15
|
+
readonly values: {
|
|
16
|
+
readonly tenant_id: TenantId;
|
|
17
|
+
};
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
export declare class Tenants {
|
|
21
|
+
private readonly options;
|
|
22
|
+
readonly invitations: TenantInvitations;
|
|
23
|
+
readonly memberships: TenantMemberships;
|
|
24
|
+
constructor(options: TenantsOptions);
|
|
25
|
+
create(actorId: string, name: string): Promise<TenantRecord>;
|
|
26
|
+
list(actorId: string): Promise<readonly TenantRecord[]>;
|
|
27
|
+
context(actorId: string, tenantId: TenantId): Promise<TenantContext>;
|
|
28
|
+
rename(actorId: string, tenantId: TenantId, name: string): Promise<TenantRecord>;
|
|
29
|
+
}
|
|
30
|
+
//# sourceMappingURL=tenants.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tenants.d.ts","sourceRoot":"","sources":["../src/tenants.ts"],"names":[],"mappings":"AAKA,OAAO,EAAkB,KAAK,QAAQ,EAAE,MAAM,oBAAoB,CAAC;AACnE,OAAO,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AAC5D,OAAO,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AAC5D,OAAO,EAAoB,WAAW,EAAE,MAAM,oBAAoB,CAAC;AACnE,OAAO,EAKL,KAAK,cAAc,EACpB,MAAM,yBAAyB,CAAC;AAIjC,MAAM,MAAM,YAAY,GAAG,OAAO,WAAW,CAAC,YAAY,CAAC;AAE3D,MAAM,MAAM,aAAa,GAAG;IAC1B,QAAQ,CAAC,MAAM,EAAE,YAAY,CAAC;IAC9B,QAAQ,CAAC,QAAQ,EAAE;QAAE,QAAQ,CAAC,IAAI,EAAE,QAAQ,CAAC;QAAC,QAAQ,CAAC,QAAQ,EAAE,QAAQ,CAAA;KAAE,CAAC;IAC5E,QAAQ,CAAC,UAAU,EAAE;QACnB,QAAQ,CAAC,IAAI,EAAE,aAAa,CAAC;QAC7B,QAAQ,CAAC,MAAM,EAAE;YAAE,QAAQ,CAAC,SAAS,EAAE,QAAQ,CAAA;SAAE,CAAC;KACnD,CAAC;CACH,CAAC;AAEF,qBAAa,OAAO;IAIN,OAAO,CAAC,QAAQ,CAAC,OAAO;IAHpC,QAAQ,CAAC,WAAW,EAAE,iBAAiB,CAAC;IACxC,QAAQ,CAAC,WAAW,EAAE,iBAAiB,CAAC;IAExC,YAA6B,OAAO,EAAE,cAAc,EAGnD;IAED,MAAM,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,YAAY,CAAC,CAiB3D;IAED,IAAI,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,SAAS,YAAY,EAAE,CAAC,CAiBtD;IAED,OAAO,CAAC,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,QAAQ,GAAG,OAAO,CAAC,aAAa,CAAC,CAwBnE;IAED,MAAM,CACJ,OAAO,EAAE,MAAM,EACf,QAAQ,EAAE,QAAQ,EAClB,IAAI,EAAE,MAAM,GACX,OAAO,CAAC,YAAY,CAAC,CAsBvB;CACF"}
|
package/dist/tenants.js
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { eq, inArray } from "drizzle-orm";
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
import { membershipFact } from "./membership-facts.js";
|
|
5
|
+
import { tenantIdSchema } from "./tenant-access.js";
|
|
6
|
+
import { TenantInvitations } from "./tenant-invitations.js";
|
|
7
|
+
import { TenantMemberships } from "./tenant-memberships.js";
|
|
8
|
+
import { tenantMembership, tenantTable } from "./tenant-tables.js";
|
|
9
|
+
import { accountSubject, checkTenantPermission, TenantUnavailableError, tenantTransaction, } from "./tenant-transaction.js";
|
|
10
|
+
const tenantName = z.string().trim().min(1).max(120);
|
|
11
|
+
export class Tenants {
|
|
12
|
+
options;
|
|
13
|
+
invitations;
|
|
14
|
+
memberships;
|
|
15
|
+
constructor(options) {
|
|
16
|
+
this.options = options;
|
|
17
|
+
this.invitations = new TenantInvitations(options);
|
|
18
|
+
this.memberships = new TenantMemberships(options);
|
|
19
|
+
}
|
|
20
|
+
create(actorId, name) {
|
|
21
|
+
const actor = accountSubject(this.options, actorId);
|
|
22
|
+
const tenant = {
|
|
23
|
+
id: tenantIdSchema.parse(randomUUID()),
|
|
24
|
+
name: tenantName.parse(name),
|
|
25
|
+
};
|
|
26
|
+
return tenantTransaction(this.options, async (tx) => {
|
|
27
|
+
await tx.insert(tenantTable).values(tenant);
|
|
28
|
+
await tx
|
|
29
|
+
.insert(tenantMembership)
|
|
30
|
+
.values({ tenantId: tenant.id, accountId: actor.id, role: "owner" });
|
|
31
|
+
await this.options.changes.enqueue([membershipFact(this.options, tenant.id, actor.id, "owner", "WRITE")], tx);
|
|
32
|
+
return tenant;
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
list(actorId) {
|
|
36
|
+
const subject = accountSubject(this.options, actorId);
|
|
37
|
+
const view = this.options.access.tenant.permissions.view;
|
|
38
|
+
return tenantTransaction(this.options, async (tx) => {
|
|
39
|
+
const visible = await this.options.authorizationAt(tx).lookupResources({
|
|
40
|
+
subject,
|
|
41
|
+
resourceType: view.entityWireName,
|
|
42
|
+
permission: view.permissionName,
|
|
43
|
+
});
|
|
44
|
+
if (visible.length === 0)
|
|
45
|
+
return [];
|
|
46
|
+
const ids = visible.map((id) => tenantIdSchema.parse(id));
|
|
47
|
+
return tx
|
|
48
|
+
.select()
|
|
49
|
+
.from(tenantTable)
|
|
50
|
+
.where(inArray(tenantTable.id, ids))
|
|
51
|
+
.orderBy(tenantTable.id);
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
context(actorId, tenantId) {
|
|
55
|
+
const actor = accountSubject(this.options, actorId);
|
|
56
|
+
return tenantTransaction(this.options, async (tx) => {
|
|
57
|
+
if (!(await checkTenantPermission(this.options, tx, actor, tenantId, "view")))
|
|
58
|
+
throw new TenantUnavailableError();
|
|
59
|
+
const [tenant] = await tx
|
|
60
|
+
.select()
|
|
61
|
+
.from(tenantTable)
|
|
62
|
+
.where(eq(tenantTable.id, tenantId));
|
|
63
|
+
if (tenant === undefined)
|
|
64
|
+
throw new TenantUnavailableError();
|
|
65
|
+
return {
|
|
66
|
+
tenant,
|
|
67
|
+
audience: { kind: "tenant", tenantId },
|
|
68
|
+
auditScope: { kind: "application", values: { tenant_id: tenantId } },
|
|
69
|
+
};
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
rename(actorId, tenantId, name) {
|
|
73
|
+
const actor = accountSubject(this.options, actorId);
|
|
74
|
+
const checkedName = tenantName.parse(name);
|
|
75
|
+
return tenantTransaction(this.options, async (tx) => {
|
|
76
|
+
if (!(await checkTenantPermission(this.options, tx, actor, tenantId, "manage_tenant")))
|
|
77
|
+
throw new TenantUnavailableError();
|
|
78
|
+
const [changed] = await tx
|
|
79
|
+
.update(tenantTable)
|
|
80
|
+
.set({ name: checkedName })
|
|
81
|
+
.where(eq(tenantTable.id, tenantId))
|
|
82
|
+
.returning();
|
|
83
|
+
if (changed === undefined)
|
|
84
|
+
throw new TenantUnavailableError();
|
|
85
|
+
return changed;
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
//# sourceMappingURL=tenants.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tenants.js","sourceRoot":"","sources":["../src/tenants.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,EAAE,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAC1C,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,OAAO,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AACvD,OAAO,EAAE,cAAc,EAAiB,MAAM,oBAAoB,CAAC;AACnE,OAAO,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AAC5D,OAAO,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AAC5D,OAAO,EAAE,gBAAgB,EAAE,WAAW,EAAE,MAAM,oBA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