@tansy/multi-tenant 0.0.0-stage → 0.1.1
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/CHANGELOG.md +9 -0
- package/LICENSE +21 -0
- package/README.md +12 -2
- package/admin.d.ts +2 -0
- package/admin.js +27 -0
- package/commands.d.ts +2 -0
- package/commands.js +116 -0
- package/doctor.d.ts +6 -0
- package/doctor.js +42 -0
- package/domains.d.ts +87 -0
- package/domains.js +146 -0
- package/index.d.ts +22 -0
- package/index.js +193 -0
- package/lifecycle.d.ts +122 -0
- package/lifecycle.js +211 -0
- package/messages.d.ts +308 -0
- package/messages.js +300 -0
- package/package.json +37 -4
- package/pages.d.ts +17 -0
- package/pages.js +373 -0
- package/paths.d.ts +13 -0
- package/paths.js +58 -0
- package/routes.d.ts +8 -0
- package/routes.js +684 -0
- package/tenants.d.ts +96 -0
- package/tenants.js +177 -0
- package/tools.d.ts +2 -0
- package/tools.js +138 -0
package/tenants.d.ts
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { type ExtensionContext, type ExtensionRouteContext } from "tansy/plugin";
|
|
2
|
+
/**
|
|
3
|
+
* The tenants' data, kept by better-auth's organization plugin in this plugin's collections:
|
|
4
|
+
* one collection per model, named with the plugin's prefix, and system-owned, so only this
|
|
5
|
+
* plugin writes them. Fields keep better-auth's names, except those records reserve, which
|
|
6
|
+
* get the model's name in front (an organization's `status` is `organizationStatus`).
|
|
7
|
+
*/
|
|
8
|
+
export declare const PLUGIN = "multi-tenant";
|
|
9
|
+
export declare const ORGANIZATIONS = "multi-tenant-organization";
|
|
10
|
+
export declare const MEMBERS = "multi-tenant-member";
|
|
11
|
+
export declare const INVITATIONS = "multi-tenant-invitation";
|
|
12
|
+
export declare const SESSIONS = "multi-tenant-session-data";
|
|
13
|
+
export declare const SYSTEM_COLLECTIONS: string[];
|
|
14
|
+
/** The tenant that existing records go to when a collection first becomes scoped. */
|
|
15
|
+
export declare const DEFAULT_TENANT = "default";
|
|
16
|
+
/** Tenant ids are row scope values, and better-auth's ids fit them too. Checked wherever one enters, so it can go in a filter. */
|
|
17
|
+
export declare const TENANT_ID: RegExp;
|
|
18
|
+
/** Member and invitation ids, which this plugin and better-auth make. */
|
|
19
|
+
export declare const RECORD_KEY: RegExp;
|
|
20
|
+
export declare const TENANT_SLUG: RegExp;
|
|
21
|
+
/** Whoever a piece of work is for. The same shape on a route, an agent tool and a dashboard page. */
|
|
22
|
+
export type TenantPrincipal = ExtensionRouteContext["principal"];
|
|
23
|
+
/** Enough context for the helpers below: what every plugin surface has, plus a caller. */
|
|
24
|
+
export type CallerContext = ExtensionContext & {
|
|
25
|
+
principal: TenantPrincipal;
|
|
26
|
+
};
|
|
27
|
+
/** Names the tenant a signed-in request is for, ahead of the one the session last switched to. */
|
|
28
|
+
export declare const TENANT_HEADER = "x-cms-tenant";
|
|
29
|
+
/** Staff who reach every tenant. */
|
|
30
|
+
export declare const EVERY_TENANT = "ext.multi-tenant:all";
|
|
31
|
+
/** Managing members within one tenant, which a tenant role may hold. */
|
|
32
|
+
export declare const MEMBERS_PERMISSION = "ext.multi-tenant:members";
|
|
33
|
+
export declare const userOf: (principal: TenantPrincipal) => import("tansy/plugin").AuthUser;
|
|
34
|
+
export type TenantStatus = "active" | "suspended";
|
|
35
|
+
export interface Tenant {
|
|
36
|
+
id: string;
|
|
37
|
+
name: string;
|
|
38
|
+
slug: string;
|
|
39
|
+
status: TenantStatus;
|
|
40
|
+
createdAt: string | null;
|
|
41
|
+
}
|
|
42
|
+
export interface Member {
|
|
43
|
+
id: string;
|
|
44
|
+
recordId: number;
|
|
45
|
+
tenant: string;
|
|
46
|
+
userId: string;
|
|
47
|
+
roles: string[];
|
|
48
|
+
createdAt: string | null;
|
|
49
|
+
}
|
|
50
|
+
export interface Invitation {
|
|
51
|
+
id: string;
|
|
52
|
+
recordId: number;
|
|
53
|
+
tenant: string;
|
|
54
|
+
email: string;
|
|
55
|
+
roles: string[];
|
|
56
|
+
status: string;
|
|
57
|
+
expiresAt: string | null;
|
|
58
|
+
inviterId: string | null;
|
|
59
|
+
}
|
|
60
|
+
/** better-auth keeps several roles as one comma-separated value. */
|
|
61
|
+
export declare const roleList: (value: unknown) => string[];
|
|
62
|
+
export declare const newId: () => string;
|
|
63
|
+
/** A tenant as any caller may see it: never better-auth's own row. */
|
|
64
|
+
export declare const publicTenant: ({ id, name, slug, status, createdAt }: Tenant) => {
|
|
65
|
+
id: string;
|
|
66
|
+
name: string;
|
|
67
|
+
slug: string;
|
|
68
|
+
status: TenantStatus;
|
|
69
|
+
createdAt: string | null;
|
|
70
|
+
};
|
|
71
|
+
/** Tenants someone can work in: every one with the permission to read them all, else those they belong to. */
|
|
72
|
+
export declare function reachable(context: CallerContext, userId: string): Promise<{
|
|
73
|
+
tenants: Tenant[];
|
|
74
|
+
memberships: Member[];
|
|
75
|
+
}>;
|
|
76
|
+
/** A tenant by id, for staff surfaces that act on any of them, suspended ones included. */
|
|
77
|
+
export declare function requireTenant(context: ExtensionContext, id: string): Promise<Tenant & {
|
|
78
|
+
recordId: number;
|
|
79
|
+
}>;
|
|
80
|
+
export declare function findTenant(context: ExtensionContext, id: string): Promise<(Tenant & {
|
|
81
|
+
recordId: number;
|
|
82
|
+
}) | null>;
|
|
83
|
+
export declare function listTenants(context: ExtensionContext, ids?: string[]): Promise<Tenant[]>;
|
|
84
|
+
export declare function membersOf(context: ExtensionContext, tenant: string): Promise<Member[]>;
|
|
85
|
+
export declare function membershipsOf(context: ExtensionContext, userId: string): Promise<Member[]>;
|
|
86
|
+
export declare function findMember(context: ExtensionContext, tenant: string, userId: string): Promise<Member | null>;
|
|
87
|
+
export declare function findMemberById(context: ExtensionContext, tenant: string, id: string): Promise<Member | null>;
|
|
88
|
+
export declare function invitationsOf(context: ExtensionContext, tenant: string): Promise<Invitation[]>;
|
|
89
|
+
/** An invitation by its id alone, whichever tenant it's for. */
|
|
90
|
+
export declare function invitationById(context: ExtensionContext, id: string): Promise<Invitation | null>;
|
|
91
|
+
export declare function findInvitation(context: ExtensionContext, tenant: string, id: string): Promise<Invitation | null>;
|
|
92
|
+
/** The tenant a session last switched to, kept on the session as better-auth's `activeOrganizationId`. */
|
|
93
|
+
export declare function activeTenant(context: ExtensionContext, sessionId: string): Promise<string | null>;
|
|
94
|
+
export declare function setActiveTenant(context: ExtensionContext, sessionId: string, tenant: string | null): Promise<void>;
|
|
95
|
+
/** The first tenant, made when the plugin is first needed, so a site that was one tenant keeps its records in it. */
|
|
96
|
+
export declare function ensureDefaultTenant(context: ExtensionContext): Promise<string>;
|
package/tenants.js
ADDED
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import { randomBytes } from "node:crypto";
|
|
2
|
+
import { HttpError } from "tansy/plugin";
|
|
3
|
+
/**
|
|
4
|
+
* The tenants' data, kept by better-auth's organization plugin in this plugin's collections:
|
|
5
|
+
* one collection per model, named with the plugin's prefix, and system-owned, so only this
|
|
6
|
+
* plugin writes them. Fields keep better-auth's names, except those records reserve, which
|
|
7
|
+
* get the model's name in front (an organization's `status` is `organizationStatus`).
|
|
8
|
+
*/
|
|
9
|
+
export const PLUGIN = "multi-tenant";
|
|
10
|
+
export const ORGANIZATIONS = `${PLUGIN}-organization`;
|
|
11
|
+
export const MEMBERS = `${PLUGIN}-member`;
|
|
12
|
+
export const INVITATIONS = `${PLUGIN}-invitation`;
|
|
13
|
+
export const SESSIONS = `${PLUGIN}-session-data`;
|
|
14
|
+
export const SYSTEM_COLLECTIONS = [ORGANIZATIONS, MEMBERS, INVITATIONS, SESSIONS];
|
|
15
|
+
/** The tenant that existing records go to when a collection first becomes scoped. */
|
|
16
|
+
export const DEFAULT_TENANT = "default";
|
|
17
|
+
/** Tenant ids are row scope values, and better-auth's ids fit them too. Checked wherever one enters, so it can go in a filter. */
|
|
18
|
+
export const TENANT_ID = /^[A-Za-z0-9][A-Za-z0-9_-]{0,63}$/;
|
|
19
|
+
/** Member and invitation ids, which this plugin and better-auth make. */
|
|
20
|
+
export const RECORD_KEY = /^[A-Za-z0-9_-]{1,64}$/;
|
|
21
|
+
export const TENANT_SLUG = /^[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?$/;
|
|
22
|
+
/** Names the tenant a signed-in request is for, ahead of the one the session last switched to. */
|
|
23
|
+
export const TENANT_HEADER = "x-cms-tenant";
|
|
24
|
+
/** Staff who reach every tenant. */
|
|
25
|
+
export const EVERY_TENANT = "ext.multi-tenant:all";
|
|
26
|
+
/** Managing members within one tenant, which a tenant role may hold. */
|
|
27
|
+
export const MEMBERS_PERMISSION = "ext.multi-tenant:members";
|
|
28
|
+
export const userOf = (principal) => {
|
|
29
|
+
if (principal.kind === "anonymous")
|
|
30
|
+
throw new HttpError(401, "UNAUTHENTICATED", "Authentication required.");
|
|
31
|
+
return principal.user;
|
|
32
|
+
};
|
|
33
|
+
const str = (value) => (typeof value === "string" ? value : "");
|
|
34
|
+
/** better-auth keeps several roles as one comma-separated value. */
|
|
35
|
+
export const roleList = (value) => str(value).split(",").map((name) => name.trim()).filter(Boolean);
|
|
36
|
+
export const newId = () => randomBytes(24).toString("base64url").replace(/[-_]/g, "").slice(0, 32);
|
|
37
|
+
const quoted = (value) => {
|
|
38
|
+
if (!RECORD_KEY.test(value))
|
|
39
|
+
throw new Error(`Not an id: ${JSON.stringify(value).slice(0, 80)}`);
|
|
40
|
+
return `"${value}"`;
|
|
41
|
+
};
|
|
42
|
+
async function all(records, slug, filter) {
|
|
43
|
+
const items = [];
|
|
44
|
+
for (let page = 1;; page += 1) {
|
|
45
|
+
const result = await records.list(slug, { filter, perPage: 200, page, sort: "id" });
|
|
46
|
+
items.push(...result.items);
|
|
47
|
+
if (page >= result.totalPages)
|
|
48
|
+
return items;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
const toTenant = (row) => ({
|
|
52
|
+
id: str(row.authId),
|
|
53
|
+
name: str(row.name),
|
|
54
|
+
slug: str(row.slug),
|
|
55
|
+
status: row.organizationStatus === "suspended" ? "suspended" : "active",
|
|
56
|
+
createdAt: str(row.organizationCreatedAt) || null,
|
|
57
|
+
});
|
|
58
|
+
const toMember = (row) => ({
|
|
59
|
+
id: str(row.authId),
|
|
60
|
+
recordId: Number(row.id),
|
|
61
|
+
tenant: str(row.organizationId),
|
|
62
|
+
userId: str(row.userId),
|
|
63
|
+
roles: roleList(row.role),
|
|
64
|
+
createdAt: str(row.memberCreatedAt) || null,
|
|
65
|
+
});
|
|
66
|
+
const toInvitation = (row) => ({
|
|
67
|
+
id: str(row.authId),
|
|
68
|
+
recordId: Number(row.id),
|
|
69
|
+
tenant: str(row.organizationId),
|
|
70
|
+
email: str(row.email),
|
|
71
|
+
roles: roleList(row.role),
|
|
72
|
+
status: str(row.invitationStatus),
|
|
73
|
+
expiresAt: str(row.expiresAt) || null,
|
|
74
|
+
inviterId: str(row.inviterId) || null,
|
|
75
|
+
});
|
|
76
|
+
/** A tenant as any caller may see it: never better-auth's own row. */
|
|
77
|
+
export const publicTenant = ({ id, name, slug, status, createdAt }) => ({ id, name, slug, status, createdAt });
|
|
78
|
+
/** Tenants someone can work in: every one with the permission to read them all, else those they belong to. */
|
|
79
|
+
export async function reachable(context, userId) {
|
|
80
|
+
const { permissions } = context.principal;
|
|
81
|
+
const memberships = await membershipsOf(context, userId);
|
|
82
|
+
const everyTenant = permissions.has(EVERY_TENANT) || permissions.has("ext.multi-tenant:read");
|
|
83
|
+
const tenants = everyTenant ? await listTenants(context) : await listTenants(context, memberships.map((member) => member.tenant));
|
|
84
|
+
return { tenants, memberships };
|
|
85
|
+
}
|
|
86
|
+
/** A tenant by id, for staff surfaces that act on any of them, suspended ones included. */
|
|
87
|
+
export async function requireTenant(context, id) {
|
|
88
|
+
const tenant = TENANT_ID.test(id) ? await findTenant(context, id) : null;
|
|
89
|
+
if (!tenant)
|
|
90
|
+
throw new HttpError(404, "NOT_FOUND", "Tenant not found.");
|
|
91
|
+
return tenant;
|
|
92
|
+
}
|
|
93
|
+
export async function findTenant(context, id) {
|
|
94
|
+
if (!TENANT_ID.test(id))
|
|
95
|
+
return null;
|
|
96
|
+
const [row] = await all(context.system.records, ORGANIZATIONS, `authId = ${quoted(id)}`);
|
|
97
|
+
return row ? { ...toTenant(row), recordId: Number(row.id) } : null;
|
|
98
|
+
}
|
|
99
|
+
export async function listTenants(context, ids) {
|
|
100
|
+
if (ids && !ids.length)
|
|
101
|
+
return [];
|
|
102
|
+
const filter = ids ? ids.filter((id) => TENANT_ID.test(id)).map((id) => `authId = ${quoted(id)}`).join(" || ") : "";
|
|
103
|
+
return (await all(context.system.records, ORGANIZATIONS, filter)).map(toTenant);
|
|
104
|
+
}
|
|
105
|
+
export async function membersOf(context, tenant) {
|
|
106
|
+
return (await all(context.system.records, MEMBERS, `organizationId = ${quoted(tenant)}`)).map(toMember);
|
|
107
|
+
}
|
|
108
|
+
export async function membershipsOf(context, userId) {
|
|
109
|
+
return (await all(context.system.records, MEMBERS, `userId = ${quoted(userId)}`)).map(toMember);
|
|
110
|
+
}
|
|
111
|
+
export async function findMember(context, tenant, userId) {
|
|
112
|
+
if (!TENANT_ID.test(tenant) || !RECORD_KEY.test(userId))
|
|
113
|
+
return null;
|
|
114
|
+
const [row] = await all(context.system.records, MEMBERS, `organizationId = ${quoted(tenant)} && userId = ${quoted(userId)}`);
|
|
115
|
+
return row ? toMember(row) : null;
|
|
116
|
+
}
|
|
117
|
+
export async function findMemberById(context, tenant, id) {
|
|
118
|
+
if (!RECORD_KEY.test(id))
|
|
119
|
+
return null;
|
|
120
|
+
const [row] = await all(context.system.records, MEMBERS, `organizationId = ${quoted(tenant)} && authId = ${quoted(id)}`);
|
|
121
|
+
return row ? toMember(row) : null;
|
|
122
|
+
}
|
|
123
|
+
export async function invitationsOf(context, tenant) {
|
|
124
|
+
return (await all(context.system.records, INVITATIONS, `organizationId = ${quoted(tenant)} && invitationStatus = "pending"`)).map(toInvitation);
|
|
125
|
+
}
|
|
126
|
+
/** An invitation by its id alone, whichever tenant it's for. */
|
|
127
|
+
export async function invitationById(context, id) {
|
|
128
|
+
if (!RECORD_KEY.test(id))
|
|
129
|
+
return null;
|
|
130
|
+
const [row] = await all(context.system.records, INVITATIONS, `authId = ${quoted(id)}`);
|
|
131
|
+
return row ? toInvitation(row) : null;
|
|
132
|
+
}
|
|
133
|
+
export async function findInvitation(context, tenant, id) {
|
|
134
|
+
if (!RECORD_KEY.test(id))
|
|
135
|
+
return null;
|
|
136
|
+
const [row] = await all(context.system.records, INVITATIONS, `organizationId = ${quoted(tenant)} && authId = ${quoted(id)}`);
|
|
137
|
+
return row ? toInvitation(row) : null;
|
|
138
|
+
}
|
|
139
|
+
/** The tenant a session last switched to, kept on the session as better-auth's `activeOrganizationId`. */
|
|
140
|
+
export async function activeTenant(context, sessionId) {
|
|
141
|
+
if (!RECORD_KEY.test(sessionId))
|
|
142
|
+
return null;
|
|
143
|
+
const [row] = await all(context.system.records, SESSIONS, `coreId = ${quoted(sessionId)}`);
|
|
144
|
+
const value = str(row?.activeOrganizationId);
|
|
145
|
+
return TENANT_ID.test(value) ? value : null;
|
|
146
|
+
}
|
|
147
|
+
export async function setActiveTenant(context, sessionId, tenant) {
|
|
148
|
+
if (!RECORD_KEY.test(sessionId))
|
|
149
|
+
throw new Error("Not a session id.");
|
|
150
|
+
const records = context.system.records;
|
|
151
|
+
const [row] = await all(records, SESSIONS, `coreId = ${quoted(sessionId)}`);
|
|
152
|
+
if (row)
|
|
153
|
+
await records.update(SESSIONS, Number(row.id), { activeOrganizationId: tenant });
|
|
154
|
+
else
|
|
155
|
+
await records.create(SESSIONS, { status: "published", coreId: sessionId, activeOrganizationId: tenant });
|
|
156
|
+
}
|
|
157
|
+
/** The first tenant, made when the plugin is first needed, so a site that was one tenant keeps its records in it. */
|
|
158
|
+
export async function ensureDefaultTenant(context) {
|
|
159
|
+
if (await findTenant(context, DEFAULT_TENANT))
|
|
160
|
+
return DEFAULT_TENANT;
|
|
161
|
+
try {
|
|
162
|
+
await context.system.records.create(ORGANIZATIONS, {
|
|
163
|
+
status: "published",
|
|
164
|
+
authId: DEFAULT_TENANT,
|
|
165
|
+
name: "Default",
|
|
166
|
+
slug: DEFAULT_TENANT,
|
|
167
|
+
organizationStatus: "active",
|
|
168
|
+
organizationCreatedAt: new Date().toISOString(),
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
catch (error) {
|
|
172
|
+
// Another server may have made it first.
|
|
173
|
+
if (!(await findTenant(context, DEFAULT_TENANT)))
|
|
174
|
+
throw error;
|
|
175
|
+
}
|
|
176
|
+
return DEFAULT_TENANT;
|
|
177
|
+
}
|
package/tools.d.ts
ADDED
package/tools.js
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { defineTool, HttpError } from "tansy/plugin";
|
|
3
|
+
import { domainsOf } from "./domains";
|
|
4
|
+
import { planOf, resumeTenant, suspendTenant } from "./lifecycle";
|
|
5
|
+
import { MEMBERS_PERMISSION, membersOf, publicTenant, reachable, requireTenant, TENANT_ID, userOf, } from "./tenants";
|
|
6
|
+
/**
|
|
7
|
+
* Tenants for an agent: served over MCP at /api/mcp, and through `context.tools` for an
|
|
8
|
+
* in-dashboard assistant. Each one runs as the caller and states the permission it needs.
|
|
9
|
+
*
|
|
10
|
+
* **A tool does only what an API key may do on one of this plugin's routes**, with the same
|
|
11
|
+
* permission. A tool call shows no recent sign-in, so anything a route guards with `fresh` —
|
|
12
|
+
* making a tenant, changing its limits, asking for a purge — stays on that route, where a person
|
|
13
|
+
* signs in again first. Nor is a tool a place for the export and import, which carry everything a
|
|
14
|
+
* tenant has as a stream. `tests/unit/multi-tenant-tools.test.ts` holds the line, and
|
|
15
|
+
* MULTI_TENANT_PLAN.md says why.
|
|
16
|
+
*/
|
|
17
|
+
const tenantId = z.string().regex(TENANT_ID).describe("The tenant's id, as multi_tenant_list_tenants gives it.");
|
|
18
|
+
/** Both lists are of the tenant being worked in, which is the caller's own: no input names a tenant. */
|
|
19
|
+
async function currentTenant(context) {
|
|
20
|
+
const current = context.principal.scope;
|
|
21
|
+
if (!current)
|
|
22
|
+
throw new HttpError(400, "SCOPE_REQUIRED", "Choose a tenant first, or work in one.");
|
|
23
|
+
return requireTenant(context, current);
|
|
24
|
+
}
|
|
25
|
+
export function tenantTools() {
|
|
26
|
+
return [
|
|
27
|
+
defineTool({
|
|
28
|
+
name: "multi_tenant_list_tenants",
|
|
29
|
+
description: "List the tenants you can work in, each with its id, name, short name, status and the roles you hold there. Staff who can read every tenant get them all. Call this first: the other tools take an id from it.",
|
|
30
|
+
input: z.object({}),
|
|
31
|
+
access: { access: "authenticated" },
|
|
32
|
+
write: false,
|
|
33
|
+
handler: async (context) => {
|
|
34
|
+
const { tenants, memberships } = await reachable(context, userOf(context.principal).id);
|
|
35
|
+
const roles = new Map(memberships.map((member) => [member.tenant, member.roles]));
|
|
36
|
+
return {
|
|
37
|
+
current: context.principal.scope ?? null,
|
|
38
|
+
items: tenants.map((tenant) => ({ ...publicTenant(tenant), roles: roles.get(tenant.id) ?? [] })),
|
|
39
|
+
};
|
|
40
|
+
},
|
|
41
|
+
}),
|
|
42
|
+
defineTool({
|
|
43
|
+
name: "multi_tenant_describe_tenant",
|
|
44
|
+
description: "One tenant's plan: its status, its limits on records, file bytes, members and requests a minute, what it is using against them, and when any purge it is waiting for happens. Limits are changed by a person in the dashboard, not here.",
|
|
45
|
+
input: z.object({ tenant: tenantId }),
|
|
46
|
+
access: { access: "permission", permission: "ext.multi-tenant:read" },
|
|
47
|
+
write: false,
|
|
48
|
+
handler: async (context) => {
|
|
49
|
+
const tenant = await requireTenant(context, context.input.tenant);
|
|
50
|
+
const [plan, usage, members] = await Promise.all([
|
|
51
|
+
planOf(context, tenant.id),
|
|
52
|
+
context.scope.data.usage(tenant.id),
|
|
53
|
+
membersOf(context, tenant.id),
|
|
54
|
+
]);
|
|
55
|
+
// Domains are the domains permission's, so they are a tool of their own, not part of this.
|
|
56
|
+
return {
|
|
57
|
+
...publicTenant(tenant),
|
|
58
|
+
limits: { records: plan.records, storageBytes: plan.storageBytes, members: plan.members, requestsPerMinute: plan.requestsPerMinute },
|
|
59
|
+
usage: { ...usage, members: members.length },
|
|
60
|
+
purgeAt: plan.purgeAt,
|
|
61
|
+
};
|
|
62
|
+
},
|
|
63
|
+
}),
|
|
64
|
+
defineTool({
|
|
65
|
+
name: "multi_tenant_list_domains",
|
|
66
|
+
description: "The domains claimed for the tenant you are working in, each with whether DNS has proved it and whether it is the tenant's main one. A claim routes nothing until it is verified.",
|
|
67
|
+
input: z.object({}),
|
|
68
|
+
access: { access: "permission", permission: "ext.multi-tenant:domains" },
|
|
69
|
+
write: false,
|
|
70
|
+
handler: async (context) => {
|
|
71
|
+
const tenant = await currentTenant(context);
|
|
72
|
+
const domains = await domainsOf(context, tenant.id);
|
|
73
|
+
return {
|
|
74
|
+
tenant: publicTenant(tenant),
|
|
75
|
+
// Never the token: it is the proof, and it is a hidden field on the record for that reason.
|
|
76
|
+
items: domains.map((domain) => ({
|
|
77
|
+
id: domain.id,
|
|
78
|
+
host: domain.host,
|
|
79
|
+
verified: domain.verifiedAt !== null,
|
|
80
|
+
primary: domain.primary,
|
|
81
|
+
verifiedAt: domain.verifiedAt,
|
|
82
|
+
})),
|
|
83
|
+
};
|
|
84
|
+
},
|
|
85
|
+
}),
|
|
86
|
+
defineTool({
|
|
87
|
+
name: "multi_tenant_list_members",
|
|
88
|
+
description: "The members of the tenant you are working in, with their names, email addresses and the roles they hold there. It is always your own tenant: there is no way to ask for another's.",
|
|
89
|
+
input: z.object({}),
|
|
90
|
+
access: { access: "permission", permission: MEMBERS_PERMISSION },
|
|
91
|
+
write: false,
|
|
92
|
+
handler: async (context) => {
|
|
93
|
+
const tenant = await currentTenant(context);
|
|
94
|
+
const members = await membersOf(context, tenant.id);
|
|
95
|
+
const people = await Promise.all(members.map((member) => context.system.users.get(member.userId)));
|
|
96
|
+
return {
|
|
97
|
+
tenant: publicTenant(tenant),
|
|
98
|
+
items: members.map((member, index) => ({
|
|
99
|
+
id: member.id,
|
|
100
|
+
name: people[index]?.name ?? null,
|
|
101
|
+
email: people[index]?.email ?? null,
|
|
102
|
+
roles: member.roles,
|
|
103
|
+
createdAt: member.createdAt,
|
|
104
|
+
})),
|
|
105
|
+
};
|
|
106
|
+
},
|
|
107
|
+
}),
|
|
108
|
+
defineTool({
|
|
109
|
+
name: "multi_tenant_suspend_tenant",
|
|
110
|
+
description: "Suspend a tenant. It then resolves for nobody: its domains, members and API keys reach nothing, and everything it has stays where it is. Resume it to undo this. Removing a tenant for good is not something you can do: a person asks for that in the dashboard.",
|
|
111
|
+
input: z.object({ tenant: tenantId }),
|
|
112
|
+
access: { access: "permission", permission: "ext.multi-tenant:delete" },
|
|
113
|
+
write: true,
|
|
114
|
+
audit: "multi_tenant.suspend",
|
|
115
|
+
handler: async (context) => {
|
|
116
|
+
const tenant = await requireTenant(context, context.input.tenant);
|
|
117
|
+
if (tenant.status !== "suspended")
|
|
118
|
+
await suspendTenant(context, tenant);
|
|
119
|
+
context.auditTarget("tenant", tenant.id);
|
|
120
|
+
return { ...publicTenant(tenant), status: "suspended" };
|
|
121
|
+
},
|
|
122
|
+
}),
|
|
123
|
+
defineTool({
|
|
124
|
+
name: "multi_tenant_resume_tenant",
|
|
125
|
+
description: "Let a suspended tenant work again, and call off any purge it was waiting for.",
|
|
126
|
+
input: z.object({ tenant: tenantId }),
|
|
127
|
+
access: { access: "permission", permission: "ext.multi-tenant:delete" },
|
|
128
|
+
write: true,
|
|
129
|
+
audit: "multi_tenant.resume",
|
|
130
|
+
handler: async (context) => {
|
|
131
|
+
const tenant = await requireTenant(context, context.input.tenant);
|
|
132
|
+
await resumeTenant(context, tenant);
|
|
133
|
+
context.auditTarget("tenant", tenant.id);
|
|
134
|
+
return { ...publicTenant(tenant), status: "active" };
|
|
135
|
+
},
|
|
136
|
+
}),
|
|
137
|
+
];
|
|
138
|
+
}
|