@x12i/keyrox-store-pg 0.1.0 → 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/dist/database.d.ts.map +1 -1
- package/dist/database.js +2 -0
- package/dist/database.js.map +1 -1
- package/dist/directory-access-sql.d.ts +12 -0
- package/dist/directory-access-sql.d.ts.map +1 -0
- package/dist/directory-access-sql.js +253 -0
- package/dist/directory-access-sql.js.map +1 -0
- package/dist/directory-sql.d.ts +4 -2
- package/dist/directory-sql.d.ts.map +1 -1
- package/dist/directory-sql.js +808 -30
- package/dist/directory-sql.js.map +1 -1
- package/dist/hosted-human-sessions.d.ts +5 -0
- package/dist/hosted-human-sessions.d.ts.map +1 -0
- package/dist/hosted-human-sessions.js +105 -0
- package/dist/hosted-human-sessions.js.map +1 -0
- package/dist/migrate.d.ts.map +1 -1
- package/dist/migrate.js +24 -1
- package/dist/migrate.js.map +1 -1
- package/dist/notification-deliveries.d.ts +5 -0
- package/dist/notification-deliveries.d.ts.map +1 -0
- package/dist/notification-deliveries.js +81 -0
- package/dist/notification-deliveries.js.map +1 -0
- package/dist/notification-webhook-sql.d.ts +4 -0
- package/dist/notification-webhook-sql.d.ts.map +1 -0
- package/dist/notification-webhook-sql.js +109 -0
- package/dist/notification-webhook-sql.js.map +1 -0
- package/dist/rows.d.ts +19 -1
- package/dist/rows.d.ts.map +1 -1
- package/dist/rows.js +153 -3
- package/dist/rows.js.map +1 -1
- package/dist/sql-store.d.ts.map +1 -1
- package/dist/sql-store.js +796 -78
- package/dist/sql-store.js.map +1 -1
- package/migrations/007_account_sso.sql +40 -0
- package/migrations/008_app_sign_in.sql +26 -0
- package/migrations/009_account_sso_next.sql +4 -0
- package/migrations/010_account_sso_profile.sql +14 -0
- package/migrations/011_hosted_sso_profiles.sql +26 -0
- package/migrations/012_app_signup_roles.sql +2 -0
- package/migrations/013_product_role_catalog.sql +14 -0
- package/migrations/014_application_staff_access.sql +21 -0
- package/migrations/015_audit_service_actor.sql +7 -0
- package/migrations/016_app_waitlist_questions.sql +2 -0
- package/migrations/017_app_signup_policy.sql +9 -0
- package/migrations/018_managed_app_sign_in.sql +8 -0
- package/migrations/019_hosted_sso_email_proofs.sql +12 -0
- package/migrations/020_cli_login_codes.sql +15 -0
- package/migrations/021_hosted_email_codes.sql +18 -0
- package/migrations/022_sso_waitlist_callback.sql +3 -0
- package/migrations/023_app_email_code_sign_in.sql +1 -0
- package/migrations/024_hosted_auth_copy.sql +2 -0
- package/migrations/025_app_directory_workspaces.sql +55 -0
- package/migrations/027_role_features_and_configuration.sql +17 -0
- package/migrations/028_sso_only_signup_audience.sql +4 -0
- package/migrations/029_people_only_directory_model.sql +3 -0
- package/migrations/030_typed_questionnaires.sql +3 -0
- package/migrations/031_app_notifications.sql +22 -0
- package/migrations/032_agent_grant_duration.sql +10 -0
- package/migrations/033_application_lifecycle.sql +67 -0
- package/migrations/034_notification_deliveries.sql +25 -0
- package/migrations/035_app_notification_webhook_jobs.sql +28 -0
- package/migrations/036_account_notification_recovery.sql +11 -0
- package/migrations/037_waitlist_confirmation.sql +7 -0
- package/migrations/039_hosted_human_sessions.sql +45 -0
- package/migrations/040_hosted_human_clients.sql +35 -0
- package/migrations/041_app_workspace_invitations.sql +9 -0
- package/migrations/042_directory_email_proof.sql +16 -0
- package/migrations/043_sso_exchange_redirect_uri.sql +1 -0
- package/migrations/044_repair_app_sign_in_connections.sql +30 -0
- package/migrations/045_directory_integrity.sql +140 -0
- package/migrations/046_person_email_across_organizations.sql +7 -0
- package/migrations/047_owner_access.sql +35 -0
- package/migrations/048_invitation_links.sql +28 -0
- package/package.json +3 -3
package/dist/directory-sql.js
CHANGED
|
@@ -1,4 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { randomBytes } from "node:crypto";
|
|
2
|
+
import { asTime, asTimeOrNull, invitationLifecycle, invitationPreview, isExpired, StoreError } from "@x12i/keyrox-store";
|
|
3
|
+
import { directoryHasOrganizations, directoryHasWorkspaces } from "@x12i/keyrox-types";
|
|
4
|
+
import { applySqlDirectoryAccess, clearSqlDirectoryProductRole, listSqlAccessChecks, saveSqlAccessCheck } from "./directory-access-sql.js";
|
|
5
|
+
import { insertSqlNotificationDelivery } from "./notification-deliveries.js";
|
|
6
|
+
import { hostedSsoIdentityOf } from "./rows.js";
|
|
2
7
|
async function rows(sql, text, values = []) {
|
|
3
8
|
const result = await sql.query(text, values);
|
|
4
9
|
return result.rows;
|
|
@@ -63,11 +68,12 @@ function integerOrNull(row, key) {
|
|
|
63
68
|
throw new Error(`Expected number ${key}.`);
|
|
64
69
|
}
|
|
65
70
|
function identityOf(row) {
|
|
71
|
+
const emailProofAt = timeOrNull(row, "email_proof_at");
|
|
66
72
|
return {
|
|
67
73
|
id: text(row, "id"), appId: text(row, "app_id"), environmentId: text(row, "environment_id"), orgId: text(row, "org_id"),
|
|
68
74
|
kind: text(row, "kind"), email: textOrNull(row, "email"), passwordHash: textOrNull(row, "password_hash"),
|
|
69
75
|
displayName: text(row, "display_name"), status: text(row, "status"),
|
|
70
|
-
emailVerifiedAt: timeOrNull(row, "email_verified_at"), metadata: json(row, "metadata"),
|
|
76
|
+
emailVerifiedAt: timeOrNull(row, "email_verified_at"), ...(emailProofAt && { emailProofAt }), metadata: json(row, "metadata"),
|
|
71
77
|
createdAt: time(row, "created_at"), updatedAt: time(row, "updated_at"),
|
|
72
78
|
};
|
|
73
79
|
}
|
|
@@ -77,11 +83,41 @@ function membershipOf(row) {
|
|
|
77
83
|
organizationId: text(row, "organization_id"), relation: text(row, "relation"), createdAt: time(row, "created_at"),
|
|
78
84
|
};
|
|
79
85
|
}
|
|
86
|
+
function workspaceOf(row) {
|
|
87
|
+
return {
|
|
88
|
+
id: text(row, "id"), appId: text(row, "app_id"), environmentId: text(row, "environment_id"),
|
|
89
|
+
organizationId: textOrNull(row, "organization_id"), name: text(row, "name"), status: text(row, "status"),
|
|
90
|
+
createdAt: time(row, "created_at"), updatedAt: time(row, "updated_at"),
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
function workspaceMemberOf(row) {
|
|
94
|
+
const roleId = textOrNull(row, "role_id");
|
|
95
|
+
return {
|
|
96
|
+
id: text(row, "id"), environmentId: text(row, "environment_id"), workspaceId: text(row, "workspace_id"),
|
|
97
|
+
identityId: text(row, "identity_id"), relation: text(row, "relation"), createdAt: time(row, "created_at"),
|
|
98
|
+
...(roleId ? { roleId } : {}),
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
function onboardingResponseOf(row) {
|
|
102
|
+
const questions = typeof row.questions === "string" ? JSON.parse(row.questions) : row.questions;
|
|
103
|
+
return {
|
|
104
|
+
environmentId: text(row, "environment_id"), target: text(row, "target"),
|
|
105
|
+
subjectId: text(row, "subject_id"), questions: questions,
|
|
106
|
+
answers: json(row, "answers"), updatedAt: time(row, "updated_at"),
|
|
107
|
+
};
|
|
108
|
+
}
|
|
80
109
|
function permissionOf(row) {
|
|
81
110
|
return { id: text(row, "id"), environmentId: text(row, "environment_id"), key: text(row, "key"), description: text(row, "description"), createdAt: time(row, "created_at") };
|
|
82
111
|
}
|
|
83
112
|
function roleOf(row) {
|
|
84
|
-
return {
|
|
113
|
+
return {
|
|
114
|
+
id: text(row, "id"), environmentId: text(row, "environment_id"), name: text(row, "name"), permissions: texts(row, "permissions"),
|
|
115
|
+
inherits: texts(row, "inherits"), catalogRoleId: textOrNull(row, "catalog_role_id"), allowsOrganizationlessSignup: flag(row, "allows_organizationless_signup"),
|
|
116
|
+
...(flag(row, "owner_access") ? { ownerAccess: true } : {}), createdAt: time(row, "created_at"),
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
function tenantRoleOf(row) {
|
|
120
|
+
return { id: text(row, "id"), orgId: text(row, "org_id"), name: text(row, "name"), description: text(row, "description"), createdAt: time(row, "created_at") };
|
|
85
121
|
}
|
|
86
122
|
function groupOf(row) {
|
|
87
123
|
return { id: text(row, "id"), environmentId: text(row, "environment_id"), name: text(row, "name"), createdAt: time(row, "created_at") };
|
|
@@ -97,7 +133,8 @@ function bindingOf(row) {
|
|
|
97
133
|
};
|
|
98
134
|
}
|
|
99
135
|
function featureOf(row) {
|
|
100
|
-
return { id: text(row, "id"), environmentId: text(row, "environment_id"), key: text(row, "key"),
|
|
136
|
+
return { id: text(row, "id"), environmentId: text(row, "environment_id"), key: text(row, "key"), kind: text(row, "kind"),
|
|
137
|
+
enabled: flag(row, "enabled"), config: json(row, "config"), createdAt: time(row, "created_at") };
|
|
101
138
|
}
|
|
102
139
|
function assignmentOf(row) {
|
|
103
140
|
return {
|
|
@@ -107,11 +144,35 @@ function assignmentOf(row) {
|
|
|
107
144
|
};
|
|
108
145
|
}
|
|
109
146
|
function appWaitOf(row) {
|
|
147
|
+
const answers = Array.isArray(row.answers) ? row.answers : [];
|
|
148
|
+
const workspaceId = textOrNull(row, "invitation_workspace_id");
|
|
149
|
+
const roleId = textOrNull(row, "invitation_role_id");
|
|
150
|
+
const organizationId = textOrNull(row, "invitation_organization_id");
|
|
151
|
+
const delivery = textOrNull(row, "invitation_delivery");
|
|
152
|
+
const pendingWorkspaceIdentityId = textOrNull(row, "pending_workspace_identity_id");
|
|
153
|
+
const auditId = textOrNull(row, "invitation_audit_id");
|
|
154
|
+
const revokedAt = timeOrNull(row, "revoked_at");
|
|
110
155
|
return {
|
|
111
156
|
id: text(row, "id"), appId: text(row, "app_id"), environmentId: text(row, "environment_id"), email: text(row, "email"),
|
|
112
|
-
status: text(row, "status"), tokenHash: textOrNull(row, "token_hash"), createdAt: time(row, "created_at"),
|
|
157
|
+
status: text(row, "status"), tokenHash: textOrNull(row, "token_hash"), createdAt: time(row, "created_at"), answers,
|
|
158
|
+
...(roleId ? { invitation: {
|
|
159
|
+
roleId, ...(workspaceId ? { workspaceId } : {}), ...(organizationId ? { organizationId } : {}),
|
|
160
|
+
...(delivery === "email" || delivery === "link" ? { delivery } : {}),
|
|
161
|
+
} } : {}),
|
|
162
|
+
...(pendingWorkspaceIdentityId ? { pendingWorkspaceIdentityId } : {}),
|
|
163
|
+
...(auditId ? { auditId } : {}),
|
|
164
|
+
...(revokedAt ? { revokedAt } : {}),
|
|
113
165
|
};
|
|
114
166
|
}
|
|
167
|
+
function appWaitValues(row) {
|
|
168
|
+
return [row.id, row.appId, row.environmentId, row.email.toLowerCase(), row.status, row.tokenHash, asTime(row.createdAt), JSON.stringify(row.answers ?? []),
|
|
169
|
+
row.invitation?.workspaceId ?? null, row.invitation?.roleId ?? null, row.pendingWorkspaceIdentityId ?? null,
|
|
170
|
+
row.invitation?.organizationId ?? null, row.invitation?.delivery ?? null, row.auditId ?? null, asTimeOrNull(row.revokedAt ?? null)];
|
|
171
|
+
}
|
|
172
|
+
const appWaitlistInsert = `INSERT INTO app_waitlist (id, app_id, environment_id, email, status, token_hash, created_at, answers,
|
|
173
|
+
invitation_workspace_id, invitation_role_id, pending_workspace_identity_id,
|
|
174
|
+
invitation_organization_id, invitation_delivery, invitation_audit_id, revoked_at, confirmation_pending)
|
|
175
|
+
VALUES ($1, $2, $3, $4, $5, $6, $7::timestamptz, $8::jsonb, $9, $10, $11, $12, $13, $14, $15::timestamptz, $16)`;
|
|
115
176
|
function platformWaitOf(row) {
|
|
116
177
|
return { id: text(row, "id"), email: text(row, "email"), status: text(row, "status"), tokenHash: textOrNull(row, "token_hash"), createdAt: time(row, "created_at") };
|
|
117
178
|
}
|
|
@@ -127,6 +188,211 @@ function signupOf(row) {
|
|
|
127
188
|
return { mode: mode === "waiting-list" ? "waiting-list" : "open", explicit: flag(row, "explicit"), operators: texts(row, "operators") };
|
|
128
189
|
}
|
|
129
190
|
const openSignup = { mode: "open", explicit: false, operators: [] };
|
|
191
|
+
function identityValues(row) {
|
|
192
|
+
return [row.id, row.appId, row.environmentId, row.orgId, row.kind, row.email, row.passwordHash, row.displayName, row.status,
|
|
193
|
+
asTimeOrNull(row.emailVerifiedAt), JSON.stringify(row.metadata), asTime(row.createdAt), asTime(row.updatedAt), asTimeOrNull(row.emailProofAt ?? null)];
|
|
194
|
+
}
|
|
195
|
+
async function insertIdentity(sql, values) {
|
|
196
|
+
await sql.query(`INSERT INTO app_identities (
|
|
197
|
+
id, app_id, environment_id, org_id, kind, email, password_hash, display_name, status, email_verified_at, metadata, created_at, updated_at, email_proof_at
|
|
198
|
+
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10::timestamptz, $11::jsonb, $12::timestamptz, $13::timestamptz, $14::timestamptz)`, values);
|
|
199
|
+
}
|
|
200
|
+
async function insertChallenge(sql, row, delivery) {
|
|
201
|
+
if (row.environmentId !== "platform") {
|
|
202
|
+
const scope = (await rows(sql, `SELECT application.org_id, environment.key_version FROM applications application
|
|
203
|
+
JOIN app_environments environment ON environment.app_id = application.id
|
|
204
|
+
WHERE environment.id = $1 AND application.trashed_at IS NULL FOR SHARE OF application, environment`, [row.environmentId]))[0];
|
|
205
|
+
if (!scope)
|
|
206
|
+
throw new StoreError("not_found", "Application environment not found.");
|
|
207
|
+
if (delivery && (delivery.environmentId !== row.environmentId || delivery.organizationId !== scope.org_id ||
|
|
208
|
+
delivery.environmentKeyVersion !== Number(scope.key_version)))
|
|
209
|
+
throw new StoreError("conflict", "Notification scope changed.");
|
|
210
|
+
}
|
|
211
|
+
else if (delivery) {
|
|
212
|
+
throw new StoreError("conflict", "Notification scope changed.");
|
|
213
|
+
}
|
|
214
|
+
await sql.query(`INSERT INTO directory_challenges (id, environment_id, identity_id, purpose, email, token_hash, expires_at, used_at, created_at)
|
|
215
|
+
VALUES ($1, $2, $3, $4, $5, $6, $7::timestamptz, $8::timestamptz, $9::timestamptz)`, [row.id, row.environmentId, row.identityId, row.purpose, row.email, row.tokenHash, asTime(row.expiresAt), asTimeOrNull(row.usedAt), asTime(row.createdAt)]);
|
|
216
|
+
await insertSqlNotificationDelivery(sql, delivery);
|
|
217
|
+
}
|
|
218
|
+
async function assertWorkspaceScope(sql, workspace) {
|
|
219
|
+
const model = await first(sql, `SELECT application.directory_model FROM applications AS application
|
|
220
|
+
JOIN app_environments AS environment ON environment.app_id = application.id
|
|
221
|
+
WHERE application.id = $1 AND environment.id = $2 AND application.trashed_at IS NULL AND application.status = 'active' AND environment.status = 'active'
|
|
222
|
+
FOR SHARE OF application, environment`, [workspace.appId, workspace.environmentId], (row) => text(row, "directory_model"));
|
|
223
|
+
if (!model || !directoryHasWorkspaces(model) || (model === "organizations-and-workspaces") !== (workspace.organizationId !== null)) {
|
|
224
|
+
throw new StoreError("conflict", "This workspace is incompatible with the application directory model.");
|
|
225
|
+
}
|
|
226
|
+
if (workspace.organizationId) {
|
|
227
|
+
const parent = await rows(sql, `SELECT id FROM app_identities WHERE id = $1 AND environment_id = $2 AND app_id = $3
|
|
228
|
+
AND kind = 'organization' AND status = 'active' FOR SHARE`, [workspace.organizationId, workspace.environmentId, workspace.appId]);
|
|
229
|
+
if (!parent.length)
|
|
230
|
+
throw new StoreError("conflict", "The workspace organization is unavailable in this environment.");
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
async function requireOrganizationScope(sql, environmentId) {
|
|
234
|
+
const model = await first(sql, `SELECT application.directory_model FROM applications AS application
|
|
235
|
+
JOIN app_environments AS environment ON environment.app_id = application.id
|
|
236
|
+
WHERE environment.id = $1 AND application.trashed_at IS NULL FOR SHARE OF application`, [environmentId], (row) => text(row, "directory_model"));
|
|
237
|
+
if (model === "people-only")
|
|
238
|
+
throw new StoreError("conflict", "Organizations are unavailable for this application.");
|
|
239
|
+
}
|
|
240
|
+
async function lockDirectoryChallengeApplication(sql, tokenHash, allowPlatform) {
|
|
241
|
+
if (allowPlatform && (await rows(sql, "SELECT id FROM directory_challenges WHERE token_hash = $1 AND environment_id = 'platform'", [tokenHash])).length)
|
|
242
|
+
return true;
|
|
243
|
+
const scope = await rows(sql, `SELECT app.id FROM directory_challenges challenge
|
|
244
|
+
JOIN app_environments env ON env.id = challenge.environment_id JOIN applications app ON app.id = env.app_id
|
|
245
|
+
WHERE challenge.token_hash = $1 AND app.trashed_at IS NULL FOR SHARE OF app`, [tokenHash]);
|
|
246
|
+
return scope.length > 0;
|
|
247
|
+
}
|
|
248
|
+
async function assertProductRoleCatalog(sql, environmentId, catalogRoleId) {
|
|
249
|
+
const scope = await rows(sql, `SELECT app.id FROM app_environments env JOIN applications app ON app.id = env.app_id
|
|
250
|
+
JOIN tenant_product_roles catalog ON catalog.org_id = app.org_id
|
|
251
|
+
WHERE env.id = $1 AND catalog.id = $2 AND app.trashed_at IS NULL FOR SHARE OF app`, [environmentId, catalogRoleId]);
|
|
252
|
+
if (!scope.length)
|
|
253
|
+
throw new StoreError("conflict", "Catalog role must belong to the application tenant.");
|
|
254
|
+
}
|
|
255
|
+
async function insertWorkspaceMember(sql, workspace, row) {
|
|
256
|
+
if (row.workspaceId !== workspace.id || row.environmentId !== workspace.environmentId) {
|
|
257
|
+
throw new StoreError("conflict", "The workspace member is unavailable in this environment.");
|
|
258
|
+
}
|
|
259
|
+
const identity = await rows(sql, `SELECT id FROM app_identities WHERE id = $1 AND environment_id = $2 AND app_id = $3
|
|
260
|
+
AND kind <> 'organization' AND status = 'active' FOR SHARE`, [row.identityId, row.environmentId, workspace.appId]);
|
|
261
|
+
if (!identity.length)
|
|
262
|
+
throw new StoreError("conflict", "The workspace member is unavailable in this environment.");
|
|
263
|
+
if (workspace.organizationId) {
|
|
264
|
+
const parentMembership = await rows(sql, `SELECT id FROM app_identity_memberships
|
|
265
|
+
WHERE environment_id = $1 AND identity_id = $2 AND organization_id = $3 FOR SHARE`, [row.environmentId, row.identityId, workspace.organizationId]);
|
|
266
|
+
if (!parentMembership.length)
|
|
267
|
+
throw new StoreError("conflict", "The workspace member must belong to its organization.");
|
|
268
|
+
}
|
|
269
|
+
await sql.query(`INSERT INTO app_workspace_memberships (id, environment_id, workspace_id, identity_id, relation, created_at, role_id)
|
|
270
|
+
VALUES ($1, $2, $3, $4, $5, $6::timestamptz, $7)`, [row.id, row.environmentId, row.workspaceId, row.identityId, row.relation, asTime(row.createdAt), row.roleId ?? null]);
|
|
271
|
+
}
|
|
272
|
+
async function workspaceInvitationTarget(sql, invite) {
|
|
273
|
+
if (!invite.invitation?.workspaceId)
|
|
274
|
+
return null;
|
|
275
|
+
const workspace = await first(sql, `SELECT workspace.* FROM app_workspaces AS workspace
|
|
276
|
+
JOIN app_environments AS environment ON environment.id = workspace.environment_id
|
|
277
|
+
JOIN applications AS application ON application.id = environment.app_id
|
|
278
|
+
JOIN app_product_roles AS role ON role.id = $4 AND role.environment_id = environment.id
|
|
279
|
+
WHERE workspace.app_id = $1 AND workspace.environment_id = $2 AND workspace.id = $3 AND workspace.status = 'active'
|
|
280
|
+
AND environment.status = 'active' AND application.status = 'active' AND application.trashed_at IS NULL
|
|
281
|
+
AND ((application.directory_model = 'workspaces' AND workspace.organization_id IS NULL)
|
|
282
|
+
OR (application.directory_model = 'organizations-and-workspaces' AND workspace.organization_id IS NOT NULL))
|
|
283
|
+
FOR SHARE OF workspace, role, environment, application`, [invite.appId, invite.environmentId, invite.invitation.workspaceId, invite.invitation.roleId], workspaceOf);
|
|
284
|
+
if (!workspace?.organizationId)
|
|
285
|
+
return workspace;
|
|
286
|
+
const parent = await rows(sql, `SELECT id FROM app_identities WHERE id = $1 AND kind = 'organization'
|
|
287
|
+
AND app_id = $2 AND environment_id = $3 AND status = 'active' FOR SHARE`, [workspace.organizationId, invite.appId, invite.environmentId]);
|
|
288
|
+
return parent.length ? workspace : null;
|
|
289
|
+
}
|
|
290
|
+
async function applyWorkspaceInvitation(sql, invite, challengeId, identityId, at) {
|
|
291
|
+
const workspace = await workspaceInvitationTarget(sql, invite);
|
|
292
|
+
if (!workspace || !invite.invitation)
|
|
293
|
+
return false;
|
|
294
|
+
if (workspace.organizationId) {
|
|
295
|
+
await sql.query(`INSERT INTO app_identity_memberships (id, environment_id, identity_id, organization_id, relation, created_at)
|
|
296
|
+
VALUES ($1, $2, $3, $4, 'member', $5::timestamptz) ON CONFLICT (identity_id, organization_id) DO NOTHING`, [`dms_${challengeId}`, invite.environmentId, identityId, workspace.organizationId, at]);
|
|
297
|
+
}
|
|
298
|
+
await sql.query(`INSERT INTO app_workspace_memberships (id, environment_id, workspace_id, identity_id, relation, created_at, role_id)
|
|
299
|
+
VALUES ($1, $2, $3, $4, 'member', $5::timestamptz, $6)
|
|
300
|
+
ON CONFLICT (environment_id, workspace_id, identity_id) DO UPDATE SET role_id = EXCLUDED.role_id`, [`wsm_${challengeId}`, invite.environmentId, workspace.id, identityId, at, invite.invitation.roleId]);
|
|
301
|
+
await sql.query("UPDATE directory_challenges SET used_at = COALESCE(used_at, $2::timestamptz) WHERE id = $1", [challengeId, at]);
|
|
302
|
+
await sql.query("UPDATE app_waitlist SET status = 'admitted', pending_workspace_identity_id = NULL WHERE id = $1", [invite.id]);
|
|
303
|
+
return true;
|
|
304
|
+
}
|
|
305
|
+
async function invitationIsAvailable(sql, invite) {
|
|
306
|
+
if (!invite.invitation?.roleId)
|
|
307
|
+
return false;
|
|
308
|
+
const model = await first(sql, `SELECT application.directory_model FROM applications AS application
|
|
309
|
+
JOIN app_environments AS environment ON environment.app_id = application.id
|
|
310
|
+
WHERE application.id = $1 AND environment.id = $2 AND application.trashed_at IS NULL AND application.status = 'active' AND environment.status = 'active'
|
|
311
|
+
FOR SHARE OF application, environment`, [invite.appId, invite.environmentId], (row) => text(row, "directory_model"));
|
|
312
|
+
const role = await rows(sql, "SELECT id FROM app_product_roles WHERE id = $1 AND environment_id = $2", [invite.invitation.roleId, invite.environmentId]);
|
|
313
|
+
if (!model || !role.length)
|
|
314
|
+
return false;
|
|
315
|
+
if (invite.invitation.workspaceId)
|
|
316
|
+
return !invite.invitation.organizationId && Boolean(await workspaceInvitationTarget(sql, invite));
|
|
317
|
+
if (model === "people-only")
|
|
318
|
+
return !invite.invitation.organizationId;
|
|
319
|
+
if (model !== "organizations" || !invite.invitation.organizationId)
|
|
320
|
+
return false;
|
|
321
|
+
const organization = await rows(sql, `SELECT id FROM app_identities WHERE id = $1 AND app_id = $2 AND environment_id = $3
|
|
322
|
+
AND kind = 'organization' AND status = 'active'`, [invite.invitation.organizationId, invite.appId, invite.environmentId]);
|
|
323
|
+
return organization.length > 0;
|
|
324
|
+
}
|
|
325
|
+
function invitationGrantRequest(invite, identityId, at, ids, ownerRoleId) {
|
|
326
|
+
const invitation = invite.invitation;
|
|
327
|
+
return {
|
|
328
|
+
appId: invite.appId, environmentId: invite.environmentId, identityId, roleId: invitation.roleId,
|
|
329
|
+
...(invitation.workspaceId ? { workspaceId: invitation.workspaceId } : {}),
|
|
330
|
+
...(!invitation.workspaceId && invitation.organizationId ? { organizationId: invitation.organizationId } : {}),
|
|
331
|
+
ownerRoleId, at, newMembershipId: ids.membershipId, newBindingId: ids.bindingId, newOrganizationMembershipId: ids.organizationMembershipId,
|
|
332
|
+
};
|
|
333
|
+
}
|
|
334
|
+
async function applyStoredInvitation(sql, invite, challengeId, identityId, at) {
|
|
335
|
+
if (!invite.invitation || invite.invitation.delivery === "link")
|
|
336
|
+
return false;
|
|
337
|
+
if (invite.invitation.workspaceId)
|
|
338
|
+
return applyWorkspaceInvitation(sql, invite, challengeId, identityId, at);
|
|
339
|
+
if (!await invitationIsAvailable(sql, invite))
|
|
340
|
+
return false;
|
|
341
|
+
const owner = await first(sql, "SELECT id FROM app_product_roles WHERE environment_id = $1 AND owner_access", [invite.environmentId], (row) => text(row, "id"));
|
|
342
|
+
const saved = await applySqlDirectoryAccess(sql, invitationGrantRequest(invite, identityId, at, {
|
|
343
|
+
membershipId: `wsm_${challengeId}`, bindingId: `rbd_${challengeId}`, organizationMembershipId: `dms_${challengeId}`,
|
|
344
|
+
}, owner));
|
|
345
|
+
if (!saved.roleId)
|
|
346
|
+
return false;
|
|
347
|
+
await sql.query("UPDATE directory_challenges SET used_at = COALESCE(used_at, $2::timestamptz) WHERE id = $1", [challengeId, at]);
|
|
348
|
+
await sql.query("UPDATE app_waitlist SET status = 'admitted', pending_workspace_identity_id = NULL WHERE id = $1", [invite.id]);
|
|
349
|
+
return true;
|
|
350
|
+
}
|
|
351
|
+
async function savedInvitationGrant(sql, invite, identityId) {
|
|
352
|
+
const invitation = invite.invitation;
|
|
353
|
+
if (!invitation)
|
|
354
|
+
return { membershipId: null, bindingId: null, roleId: "" };
|
|
355
|
+
if (invitation.workspaceId) {
|
|
356
|
+
const membership = await first(sql, `SELECT id, role_id FROM app_workspace_memberships
|
|
357
|
+
WHERE environment_id = $1 AND workspace_id = $2 AND identity_id = $3`, [invite.environmentId, invitation.workspaceId, identityId], (row) => ({ id: text(row, "id"), roleId: textOrNull(row, "role_id") }));
|
|
358
|
+
return { membershipId: membership?.id ?? null, bindingId: null, roleId: membership?.roleId ?? invitation.roleId };
|
|
359
|
+
}
|
|
360
|
+
const binding = await first(sql, `SELECT id, role_id FROM app_role_bindings
|
|
361
|
+
WHERE environment_id = $1 AND subject_type = 'identity' AND subject_id = $2 AND resource IS NULL
|
|
362
|
+
AND organization_id IS NOT DISTINCT FROM $3::text
|
|
363
|
+
ORDER BY created_at, id LIMIT 1`, [invite.environmentId, identityId, invitation.organizationId ?? null], (row) => ({ id: text(row, "id"), roleId: text(row, "role_id") }));
|
|
364
|
+
return { membershipId: null, bindingId: binding?.id ?? null, roleId: binding?.roleId ?? invitation.roleId };
|
|
365
|
+
}
|
|
366
|
+
async function acceptWorkspaceInvitation(sql, identity, at, expectedTokenHash) {
|
|
367
|
+
if (!identity.emailVerifiedAt || identity.status !== "active" || identity.kind !== "user" || !identity.email)
|
|
368
|
+
return false;
|
|
369
|
+
const invite = await first(sql, `SELECT * FROM app_waitlist WHERE app_id = $1 AND environment_id = $2 AND lower(email) = lower($3)
|
|
370
|
+
AND status = 'invited' AND invitation_role_id IS NOT NULL AND invitation_delivery IS DISTINCT FROM 'link' FOR UPDATE`, [identity.appId, identity.environmentId, identity.email], appWaitOf);
|
|
371
|
+
if (!invite || (expectedTokenHash && (invite.tokenHash !== expectedTokenHash || invite.pendingWorkspaceIdentityId)))
|
|
372
|
+
return false;
|
|
373
|
+
const challenge = await first(sql, `SELECT * FROM directory_challenges WHERE token_hash = $1 AND environment_id = $2
|
|
374
|
+
AND lower(email) = lower($3) AND purpose = 'admit' FOR UPDATE`, [invite.tokenHash, invite.environmentId, invite.email], challengeOf);
|
|
375
|
+
if (!challenge)
|
|
376
|
+
return false;
|
|
377
|
+
if (isExpired(challenge.expiresAt, at) || (invite.pendingWorkspaceIdentityId
|
|
378
|
+
? invite.pendingWorkspaceIdentityId !== identity.id || !challenge.usedAt : Boolean(challenge.usedAt)))
|
|
379
|
+
return false;
|
|
380
|
+
return applyStoredInvitation(sql, invite, challenge.id, identity.id, at);
|
|
381
|
+
}
|
|
382
|
+
function findHostedSsoLink(sql, environmentId, authority, subject) {
|
|
383
|
+
return first(sql, `SELECT * FROM hosted_sso_identities WHERE environment_id = $1 AND authority = $2 AND subject = $3`, [environmentId, authority, subject], hostedSsoIdentityOf);
|
|
384
|
+
}
|
|
385
|
+
async function saveHostedSsoLink(sql, row) {
|
|
386
|
+
const { environmentId, authority, subject, identityId } = row;
|
|
387
|
+
const identity = await rows(sql, "SELECT id FROM app_identities WHERE id = $1 AND environment_id = $2 AND kind = 'user'", [identityId, environmentId]);
|
|
388
|
+
if (!identity.length)
|
|
389
|
+
throw new StoreError("conflict", "Hosted sign-in identity is missing.");
|
|
390
|
+
const inserted = await rows(sql, `INSERT INTO hosted_sso_identities (environment_id, authority, subject, identity_id)
|
|
391
|
+
VALUES ($1, $2, $3, $4) ON CONFLICT (environment_id, authority, subject) DO NOTHING RETURNING identity_id`, [environmentId, authority, subject, identityId]);
|
|
392
|
+
if (inserted.length)
|
|
393
|
+
return true;
|
|
394
|
+
return (await findHostedSsoLink(sql, environmentId, authority, subject))?.identityId === identityId;
|
|
395
|
+
}
|
|
130
396
|
export function createSqlDirectory(tx) {
|
|
131
397
|
return {
|
|
132
398
|
countUsers: () => tx(async (sql) => Number((await rows(sql, "SELECT count(*)::int AS count FROM users"))[0]?.count ?? 0)),
|
|
@@ -135,40 +401,237 @@ export function createSqlDirectory(tx) {
|
|
|
135
401
|
await sql.query(`INSERT INTO platform_settings (id, signup_mode, explicit, operators) VALUES ('platform', $1, $2, $3::text[])
|
|
136
402
|
ON CONFLICT (id) DO UPDATE SET signup_mode = $1, explicit = $2, operators = $3::text[]`, [row.mode, row.explicit, row.operators]);
|
|
137
403
|
}),
|
|
138
|
-
|
|
404
|
+
insertPlatformWaitlist: (row) => tx(async (sql) => (await rows(sql, `INSERT INTO platform_waitlist
|
|
405
|
+
(id, email, status, token_hash, created_at, confirmation_pending) VALUES ($1, $2, $3, $4, $5::timestamptz, true)
|
|
406
|
+
ON CONFLICT DO NOTHING RETURNING id`, [row.id, row.email.toLowerCase(), row.status, row.tokenHash, asTime(row.createdAt)])).length > 0),
|
|
407
|
+
savePlatformWaitlist: (row, challenge) => tx(async (sql) => {
|
|
139
408
|
const updated = await rows(sql, "UPDATE platform_waitlist SET email = $2, status = $3, token_hash = $4, created_at = $5::timestamptz WHERE id = $1 RETURNING id", [row.id, row.email, row.status, row.tokenHash, asTime(row.createdAt)]);
|
|
140
409
|
if (!updated.length) {
|
|
141
410
|
await sql.query("INSERT INTO platform_waitlist (id, email, status, token_hash, created_at) VALUES ($1, $2, $3, $4, $5::timestamptz)", [row.id, row.email, row.status, row.tokenHash, asTime(row.createdAt)]);
|
|
142
411
|
}
|
|
412
|
+
if (challenge)
|
|
413
|
+
await insertChallenge(sql, challenge);
|
|
143
414
|
}),
|
|
144
415
|
listPlatformWaitlist: () => tx(async (sql) => (await rows(sql, "SELECT * FROM platform_waitlist ORDER BY created_at, id")).map(platformWaitOf)),
|
|
145
416
|
findPlatformWaitlist: (email) => tx((sql) => first(sql, "SELECT * FROM platform_waitlist WHERE lower(email) = lower($1)", [email], platformWaitOf)),
|
|
146
417
|
saveDirectoryIdentity: (row) => tx(async (sql) => {
|
|
147
|
-
const
|
|
418
|
+
const model = await first(sql, `SELECT application.directory_model FROM applications application
|
|
419
|
+
JOIN app_environments environment ON application.id = environment.app_id
|
|
420
|
+
WHERE application.id = $1 AND environment.id = $2 AND application.org_id = $3 AND application.trashed_at IS NULL
|
|
421
|
+
FOR SHARE OF application`, [row.appId, row.environmentId, row.orgId], (application) => text(application, "directory_model"));
|
|
422
|
+
if (!model)
|
|
423
|
+
throw new StoreError("conflict", "Identity must match its application environment and tenant.");
|
|
424
|
+
if (row.kind === "organization" && model === "people-only")
|
|
425
|
+
throw new StoreError("conflict", "Organizations are unavailable for this application.");
|
|
426
|
+
const values = identityValues(row);
|
|
148
427
|
const updated = await rows(sql, `UPDATE app_identities SET app_id = $2, environment_id = $3, org_id = $4, kind = $5, email = $6, password_hash = $7,
|
|
149
|
-
display_name = $8, status = $9,
|
|
150
|
-
|
|
428
|
+
display_name = $8, status = $9,
|
|
429
|
+
email_verified_at = CASE WHEN lower(email) = lower($6) AND email_proof_at IS NOT NULL
|
|
430
|
+
THEN COALESCE($10::timestamptz, email_verified_at) ELSE $10::timestamptz END,
|
|
431
|
+
email_proof_at = CASE WHEN $5 = 'user' AND lower(email) = lower($6) THEN email_proof_at END,
|
|
432
|
+
metadata = $11::jsonb, created_at = $12::timestamptz, updated_at = $13::timestamptz
|
|
433
|
+
WHERE id = $1 AND app_id = $2 AND environment_id = $3 AND org_id = $4 RETURNING id`, values.slice(0, 13));
|
|
151
434
|
if (!updated.length) {
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
435
|
+
if (row.kind === "organization" && model === "workspaces")
|
|
436
|
+
throw new StoreError("conflict", "New organizations are unavailable for this application.");
|
|
437
|
+
await insertIdentity(sql, values);
|
|
155
438
|
}
|
|
156
439
|
}),
|
|
440
|
+
proveDirectoryEmail: (identityId, environmentId, email, at) => tx((sql) => first(sql, `UPDATE app_identities AS identity
|
|
441
|
+
SET email_verified_at = COALESCE(identity.email_verified_at, $4::timestamptz),
|
|
442
|
+
email_proof_at = GREATEST(identity.email_proof_at, $4::timestamptz),
|
|
443
|
+
updated_at = GREATEST(identity.updated_at, $4::timestamptz)
|
|
444
|
+
FROM applications AS application, app_environments AS environment
|
|
445
|
+
WHERE identity.id = $1 AND identity.environment_id = $2 AND lower(identity.email) = lower($3)
|
|
446
|
+
AND identity.kind = 'user' AND identity.status = 'active'
|
|
447
|
+
AND application.id = identity.app_id AND application.org_id = identity.org_id
|
|
448
|
+
AND application.trashed_at IS NULL AND application.status = 'active'
|
|
449
|
+
AND environment.id = identity.environment_id AND environment.app_id = application.id
|
|
450
|
+
AND environment.org_id = application.org_id AND environment.status = 'active'
|
|
451
|
+
RETURNING identity.*`, [identityId, environmentId, email, asTime(at)], identityOf)),
|
|
452
|
+
signupDirectoryPerson: ({ identity, organization, membership, bindingId, challenge, allowOrganizationless, admissionTokenHash, hostedSsoLink, notificationDelivery }) => tx(async (sql) => {
|
|
453
|
+
const environmentId = identity.environmentId;
|
|
454
|
+
const environment = (await rows(sql, `SELECT environment.default_signup_role_id, application.directory_model, environment.key_version
|
|
455
|
+
FROM app_environments AS environment JOIN applications AS application ON application.id = environment.app_id
|
|
456
|
+
WHERE environment.id = $1 AND application.id = $2 AND application.org_id = $3 AND application.trashed_at IS NULL
|
|
457
|
+
FOR SHARE OF application, environment`, [environmentId, identity.appId, identity.orgId]))[0];
|
|
458
|
+
if (!environment)
|
|
459
|
+
throw new StoreError("not_found", "Environment not found.");
|
|
460
|
+
let workspaceInvite;
|
|
461
|
+
if (admissionTokenHash) {
|
|
462
|
+
const invite = await first(sql, `SELECT * FROM app_waitlist WHERE token_hash = $1 AND app_id = $2 AND environment_id = $3
|
|
463
|
+
AND lower(email) = lower($4) AND status = 'invited' FOR UPDATE`, [admissionTokenHash, identity.appId, environmentId, identity.email], appWaitOf);
|
|
464
|
+
if (!invite)
|
|
465
|
+
throw new StoreError("challenge_invalid", "Waiting list invitation is no longer available.");
|
|
466
|
+
if (invite.invitation?.delivery === "link")
|
|
467
|
+
throw new StoreError("challenge_invalid", "Waiting list invitation is no longer available.");
|
|
468
|
+
const used = await rows(sql, "UPDATE directory_challenges SET used_at = $4::timestamptz WHERE token_hash = $1 AND environment_id = $2 AND lower(email) = lower($3) AND purpose = 'admit' AND used_at IS NULL AND expires_at > $4::timestamptz RETURNING id", [admissionTokenHash, environmentId, identity.email, asTime(identity.createdAt)]);
|
|
469
|
+
if (!used.length)
|
|
470
|
+
throw new StoreError("challenge_invalid", "Waiting list invitation is no longer available.");
|
|
471
|
+
if (invite.invitation) {
|
|
472
|
+
if (!await workspaceInvitationTarget(sql, invite))
|
|
473
|
+
throw new StoreError("challenge_invalid", "The invited workspace or role is no longer available.");
|
|
474
|
+
workspaceInvite = { invite, challengeId: text(used[0], "id") };
|
|
475
|
+
await sql.query("UPDATE app_waitlist SET pending_workspace_identity_id = $2 WHERE id = $1", [invite.id, identity.id]);
|
|
476
|
+
}
|
|
477
|
+
else
|
|
478
|
+
await sql.query("UPDATE app_waitlist SET status = 'admitted' WHERE id = $1", [invite.id]);
|
|
479
|
+
}
|
|
480
|
+
const roleId = workspaceInvite ? null : textOrNull(environment, "default_signup_role_id");
|
|
481
|
+
const roleAllows = allowOrganizationless && roleId ? await first(sql, "SELECT allows_organizationless_signup FROM app_product_roles WHERE id = $1 AND environment_id = $2 FOR SHARE", [roleId, environmentId], (row) => flag(row, "allows_organizationless_signup")) : false;
|
|
482
|
+
const organizationCreated = !workspaceInvite && directoryHasOrganizations(text(environment, "directory_model")) && roleAllows !== true;
|
|
483
|
+
await insertIdentity(sql, identityValues(identity));
|
|
484
|
+
if (workspaceInvite && identity.emailVerifiedAt && !await applyWorkspaceInvitation(sql, workspaceInvite.invite, workspaceInvite.challengeId, identity.id, asTime(identity.createdAt))) {
|
|
485
|
+
throw new StoreError("challenge_invalid", "The invited workspace or role is no longer available.");
|
|
486
|
+
}
|
|
487
|
+
if (hostedSsoLink && (hostedSsoLink.identityId !== identity.id || hostedSsoLink.environmentId !== environmentId ||
|
|
488
|
+
!await saveHostedSsoLink(sql, hostedSsoLink))) {
|
|
489
|
+
throw new StoreError("conflict", "This provider account is linked to another profile.");
|
|
490
|
+
}
|
|
491
|
+
if (organizationCreated) {
|
|
492
|
+
await insertIdentity(sql, identityValues(organization));
|
|
493
|
+
await sql.query("INSERT INTO app_identity_memberships (id, environment_id, identity_id, organization_id, relation, created_at) VALUES ($1, $2, $3, $4, $5, $6::timestamptz)", [membership.id, environmentId, identity.id, organization.id, membership.relation, asTime(membership.createdAt)]);
|
|
494
|
+
}
|
|
495
|
+
if (challenge)
|
|
496
|
+
await insertChallenge(sql, challenge);
|
|
497
|
+
if (roleId)
|
|
498
|
+
await sql.query(`INSERT INTO app_role_bindings (id, environment_id, role_id, subject_type, subject_id, organization_id, resource, created_at)
|
|
499
|
+
VALUES ($1, $2, $3, 'identity', $4, NULL, NULL, $5::timestamptz)`, [bindingId, environmentId, roleId, identity.id, asTime(identity.createdAt)]);
|
|
500
|
+
if (notificationDelivery && (notificationDelivery.environmentId !== environmentId || notificationDelivery.organizationId !== identity.orgId ||
|
|
501
|
+
notificationDelivery.environmentKeyVersion !== Number(environment.key_version)))
|
|
502
|
+
throw new StoreError("conflict", "Notification scope changed.");
|
|
503
|
+
await insertSqlNotificationDelivery(sql, notificationDelivery);
|
|
504
|
+
return { organizationCreated };
|
|
505
|
+
}),
|
|
506
|
+
getDirectorySignupRole: (environmentId) => tx(async (sql) => {
|
|
507
|
+
return first(sql, "SELECT default_signup_role_id FROM app_environments WHERE id = $1", [environmentId], (row) => textOrNull(row, "default_signup_role_id"));
|
|
508
|
+
}),
|
|
509
|
+
findHostedSsoIdentity: (environmentId, authority, subject) => tx((sql) => findHostedSsoLink(sql, environmentId, authority, subject)),
|
|
510
|
+
listHostedSsoIdentityIds: (environmentId) => tx(async (sql) => (await rows(sql, "SELECT DISTINCT identity_id FROM hosted_sso_identities WHERE environment_id = $1", [environmentId])).map((row) => text(row, "identity_id"))),
|
|
511
|
+
saveHostedSsoIdentity: (row) => tx((sql) => saveHostedSsoLink(sql, row)),
|
|
512
|
+
setDirectorySignupRole: (environmentId, roleId) => tx(async (sql) => {
|
|
513
|
+
const updated = await rows(sql, `UPDATE app_environments SET default_signup_role_id = $2
|
|
514
|
+
WHERE id = $1 AND ($2::text IS NULL OR EXISTS (SELECT 1 FROM app_product_roles WHERE id = $2 AND environment_id = $1))
|
|
515
|
+
RETURNING id`, [environmentId, roleId]);
|
|
516
|
+
if (!updated.length)
|
|
517
|
+
throw new StoreError("not_found", "The role is unavailable in this environment.");
|
|
518
|
+
}),
|
|
157
519
|
getDirectoryIdentity: (id) => tx((sql) => first(sql, "SELECT * FROM app_identities WHERE id = $1", [id], identityOf)),
|
|
158
520
|
listDirectoryIdentities: (environmentId) => tx(async (sql) => (await rows(sql, "SELECT * FROM app_identities WHERE environment_id = $1 ORDER BY created_at, id", [environmentId])).map(identityOf)),
|
|
159
|
-
findDirectoryIdentityByEmail: (environmentId, email) => tx((sql) => first(sql,
|
|
521
|
+
findDirectoryIdentityByEmail: (environmentId, email) => tx((sql) => first(sql, `SELECT * FROM app_identities
|
|
522
|
+
WHERE environment_id = $1 AND lower(email) = lower($2) AND kind <> 'organization'
|
|
523
|
+
ORDER BY CASE kind WHEN 'user' THEN 0 WHEN 'account' THEN 1 ELSE 2 END, id LIMIT 1`, [environmentId, email], identityOf)),
|
|
160
524
|
saveDirectoryMembership: (row) => tx(async (sql) => {
|
|
525
|
+
await requireOrganizationScope(sql, row.environmentId);
|
|
161
526
|
const updated = await rows(sql, "UPDATE app_identity_memberships SET environment_id = $2, identity_id = $3, organization_id = $4, relation = $5, created_at = $6::timestamptz WHERE id = $1 RETURNING id", [row.id, row.environmentId, row.identityId, row.organizationId, row.relation, asTime(row.createdAt)]);
|
|
162
527
|
if (!updated.length) {
|
|
163
528
|
await sql.query("INSERT INTO app_identity_memberships (id, environment_id, identity_id, organization_id, relation, created_at) VALUES ($1, $2, $3, $4, $5, $6::timestamptz)", [row.id, row.environmentId, row.identityId, row.organizationId, row.relation, asTime(row.createdAt)]);
|
|
164
529
|
}
|
|
165
530
|
}),
|
|
166
531
|
deleteDirectoryMembership: (id) => tx(async (sql) => {
|
|
167
|
-
const
|
|
168
|
-
if (!
|
|
169
|
-
throw new StoreError("not_found", "
|
|
532
|
+
const member = await first(sql, "SELECT * FROM app_identity_memberships WHERE id = $1", [id], membershipOf);
|
|
533
|
+
if (!member)
|
|
534
|
+
throw new StoreError("not_found", "Membership not found.");
|
|
535
|
+
await sql.query("SELECT id FROM app_identities WHERE id = $1 AND environment_id = $2 FOR UPDATE", [member.organizationId, member.environmentId]);
|
|
536
|
+
const current = await first(sql, "SELECT * FROM app_identity_memberships WHERE id = $1 FOR UPDATE", [id], membershipOf);
|
|
537
|
+
if (!current || current.organizationId !== member.organizationId || current.environmentId !== member.environmentId) {
|
|
538
|
+
throw new StoreError("not_found", "Membership not found.");
|
|
539
|
+
}
|
|
540
|
+
if (current.relation === "owner") {
|
|
541
|
+
const replacement = await rows(sql, `SELECT id FROM app_identity_memberships
|
|
542
|
+
WHERE organization_id = $1 AND environment_id = $2 AND relation = 'owner' AND id <> $3 LIMIT 1`, [current.organizationId, current.environmentId, id]);
|
|
543
|
+
if (!replacement.length)
|
|
544
|
+
throw new StoreError("last_owner", "An organization must keep an owner.");
|
|
545
|
+
}
|
|
546
|
+
await sql.query("DELETE FROM app_identity_memberships WHERE id = $1", [id]);
|
|
170
547
|
}),
|
|
171
548
|
listDirectoryMemberships: (environmentId) => tx(async (sql) => (await rows(sql, "SELECT * FROM app_identity_memberships WHERE environment_id = $1 ORDER BY created_at, id", [environmentId])).map(membershipOf)),
|
|
549
|
+
createDirectoryWorkspace: (workspace, owner, organizationMembership) => tx(async (sql) => {
|
|
550
|
+
if (organizationMembership) {
|
|
551
|
+
await requireOrganizationScope(sql, organizationMembership.environmentId);
|
|
552
|
+
const existing = await rows(sql, `SELECT id FROM app_identity_memberships
|
|
553
|
+
WHERE environment_id = $1 AND identity_id = $2 AND organization_id = $3`, [organizationMembership.environmentId, organizationMembership.identityId, organizationMembership.organizationId]);
|
|
554
|
+
if (existing.length)
|
|
555
|
+
throw new StoreError("duplicate", "Duplicate key.");
|
|
556
|
+
await sql.query(`INSERT INTO app_identity_memberships (id, environment_id, identity_id, organization_id, relation, created_at)
|
|
557
|
+
VALUES ($1, $2, $3, $4, $5, $6::timestamptz)`, [
|
|
558
|
+
organizationMembership.id, organizationMembership.environmentId, organizationMembership.identityId,
|
|
559
|
+
organizationMembership.organizationId, organizationMembership.relation, asTime(organizationMembership.createdAt),
|
|
560
|
+
]);
|
|
561
|
+
}
|
|
562
|
+
await assertWorkspaceScope(sql, workspace);
|
|
563
|
+
if (owner.relation !== "owner")
|
|
564
|
+
throw new StoreError("conflict", "A workspace must have an owner.");
|
|
565
|
+
await sql.query(`INSERT INTO app_workspaces (id, app_id, environment_id, organization_id, name, status, created_at, updated_at)
|
|
566
|
+
VALUES ($1, $2, $3, $4, $5, $6, $7::timestamptz, $8::timestamptz)`, [
|
|
567
|
+
workspace.id, workspace.appId, workspace.environmentId, workspace.organizationId, workspace.name, workspace.status,
|
|
568
|
+
asTime(workspace.createdAt), asTime(workspace.updatedAt),
|
|
569
|
+
]);
|
|
570
|
+
await insertWorkspaceMember(sql, workspace, owner);
|
|
571
|
+
}),
|
|
572
|
+
getDirectoryWorkspace: (environmentId, id) => tx((sql) => first(sql, "SELECT * FROM app_workspaces WHERE environment_id = $1 AND id = $2", [environmentId, id], workspaceOf)),
|
|
573
|
+
listDirectoryWorkspaces: (environmentId) => tx(async (sql) => (await rows(sql, "SELECT * FROM app_workspaces WHERE environment_id = $1 ORDER BY created_at, id", [environmentId])).map(workspaceOf)),
|
|
574
|
+
updateDirectoryWorkspace: (environmentId, id, patch) => tx(async (sql) => {
|
|
575
|
+
if (patch.status === "active") {
|
|
576
|
+
const workspace = await first(sql, "SELECT * FROM app_workspaces WHERE environment_id = $1 AND id = $2", [environmentId, id], workspaceOf);
|
|
577
|
+
if (!workspace)
|
|
578
|
+
return null;
|
|
579
|
+
await assertWorkspaceScope(sql, workspace);
|
|
580
|
+
}
|
|
581
|
+
return first(sql, `UPDATE app_workspaces SET name = COALESCE($3, name), status = COALESCE($4, status), updated_at = $5::timestamptz
|
|
582
|
+
WHERE environment_id = $1 AND id = $2 RETURNING *`, [environmentId, id, patch.name ?? null, patch.status ?? null, asTime(patch.updatedAt)], workspaceOf);
|
|
583
|
+
}),
|
|
584
|
+
addDirectoryWorkspaceMembership: (row) => tx(async (sql) => {
|
|
585
|
+
const scope = [row.environmentId, row.workspaceId];
|
|
586
|
+
const workspace = await first(sql, "SELECT * FROM app_workspaces WHERE environment_id = $1 AND id = $2 AND status = 'active'", scope, workspaceOf);
|
|
587
|
+
if (!workspace)
|
|
588
|
+
throw new StoreError("not_found", "Workspace not found.");
|
|
589
|
+
await assertWorkspaceScope(sql, workspace);
|
|
590
|
+
const locked = await rows(sql, "SELECT id FROM app_workspaces WHERE environment_id = $1 AND id = $2 AND status = 'active' FOR UPDATE", scope);
|
|
591
|
+
if (!locked.length)
|
|
592
|
+
throw new StoreError("not_found", "Workspace not found.");
|
|
593
|
+
await insertWorkspaceMember(sql, workspace, row);
|
|
594
|
+
}),
|
|
595
|
+
listDirectoryWorkspaceMemberships: (environmentId) => tx(async (sql) => (await rows(sql, "SELECT * FROM app_workspace_memberships WHERE environment_id = $1 ORDER BY created_at, id", [environmentId])).map(workspaceMemberOf)),
|
|
596
|
+
removeDirectoryWorkspaceMembership: (environmentId, workspaceId, membershipId) => tx(async (sql) => {
|
|
597
|
+
await sql.query("SELECT id FROM app_workspaces WHERE environment_id = $1 AND id = $2 FOR UPDATE", [environmentId, workspaceId]);
|
|
598
|
+
const membership = await first(sql, "SELECT * FROM app_workspace_memberships WHERE environment_id = $1 AND workspace_id = $2 AND id = $3", [environmentId, workspaceId, membershipId], workspaceMemberOf);
|
|
599
|
+
if (!membership)
|
|
600
|
+
throw new StoreError("not_found", "Workspace membership not found.");
|
|
601
|
+
if (membership.relation === "owner") {
|
|
602
|
+
const replacement = await rows(sql, `SELECT id FROM app_workspace_memberships
|
|
603
|
+
WHERE environment_id = $1 AND workspace_id = $2 AND id <> $3 AND relation = 'owner' LIMIT 1`, [environmentId, workspaceId, membershipId]);
|
|
604
|
+
if (!replacement.length)
|
|
605
|
+
throw new StoreError("last_owner", "A workspace must keep an owner.");
|
|
606
|
+
}
|
|
607
|
+
if (membership.roleId) {
|
|
608
|
+
const ownerRole = await rows(sql, "SELECT id FROM app_product_roles WHERE environment_id = $1 AND owner_access AND id = $2", [environmentId, membership.roleId]);
|
|
609
|
+
if (ownerRole.length) {
|
|
610
|
+
const replacement = await rows(sql, `SELECT id FROM app_workspace_memberships
|
|
611
|
+
WHERE environment_id = $1 AND workspace_id = $2 AND id <> $3 AND role_id = $4 LIMIT 1`, [environmentId, workspaceId, membershipId, membership.roleId]);
|
|
612
|
+
if (!replacement.length)
|
|
613
|
+
throw new StoreError("last_owner_access", "Assign owner access to another person in this scope first.");
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
await sql.query("DELETE FROM app_access_checks WHERE environment_id = $1 AND identity_id = $2 AND workspace_id = $3", [environmentId, membership.identityId, workspaceId]);
|
|
617
|
+
await sql.query("DELETE FROM app_workspace_memberships WHERE id = $1", [membership.id]);
|
|
618
|
+
}),
|
|
619
|
+
applyDirectoryAccess: (request) => tx((sql) => applySqlDirectoryAccess(sql, request)),
|
|
620
|
+
clearDirectoryProductRole: (input) => tx((sql) => clearSqlDirectoryProductRole(sql, input)),
|
|
621
|
+
saveDirectoryAccessCheck: (row) => tx((sql) => saveSqlAccessCheck(sql, row)),
|
|
622
|
+
listDirectoryAccessChecks: (environmentId, identityId) => tx((sql) => listSqlAccessChecks(sql, environmentId, identityId)),
|
|
623
|
+
getDirectoryOnboardingResponse: (environmentId, target, subjectId) => tx((sql) => first(sql, "SELECT * FROM app_onboarding_responses WHERE environment_id = $1 AND target = $2 AND subject_id = $3", [environmentId, target, subjectId], onboardingResponseOf)),
|
|
624
|
+
saveDirectoryOnboardingResponse: (row) => tx(async (sql) => {
|
|
625
|
+
const subject = row.target === "workspace"
|
|
626
|
+
? await rows(sql, "SELECT id FROM app_workspaces WHERE environment_id = $1 AND id = $2", [row.environmentId, row.subjectId])
|
|
627
|
+
: await rows(sql, "SELECT id FROM app_identities WHERE environment_id = $1 AND id = $2 AND kind = $3", [row.environmentId, row.subjectId, row.target]);
|
|
628
|
+
if (!subject.length)
|
|
629
|
+
throw new StoreError("not_found", "Onboarding subject not found.");
|
|
630
|
+
await sql.query(`INSERT INTO app_onboarding_responses (environment_id, target, subject_id, questions, answers, updated_at)
|
|
631
|
+
VALUES ($1, $2, $3, $4::jsonb, $5::jsonb, $6::timestamptz)
|
|
632
|
+
ON CONFLICT (environment_id, target, subject_id) DO UPDATE SET
|
|
633
|
+
questions = EXCLUDED.questions, answers = EXCLUDED.answers, updated_at = EXCLUDED.updated_at`, [row.environmentId, row.target, row.subjectId, JSON.stringify(row.questions), JSON.stringify(row.answers), asTime(row.updatedAt)]);
|
|
634
|
+
}),
|
|
172
635
|
saveProductPermission: (row) => tx(async (sql) => {
|
|
173
636
|
const updated = await rows(sql, "UPDATE app_permissions SET environment_id = $2, key = $3, description = $4, created_at = $5::timestamptz WHERE id = $1 RETURNING id", [row.id, row.environmentId, row.key, row.description, asTime(row.createdAt)]);
|
|
174
637
|
if (!updated.length)
|
|
@@ -181,9 +644,12 @@ export function createSqlDirectory(tx) {
|
|
|
181
644
|
}),
|
|
182
645
|
listProductPermissions: (environmentId) => tx(async (sql) => (await rows(sql, "SELECT * FROM app_permissions WHERE environment_id = $1 ORDER BY created_at, id", [environmentId])).map(permissionOf)),
|
|
183
646
|
saveProductRole: (row) => tx(async (sql) => {
|
|
184
|
-
|
|
647
|
+
if (row.catalogRoleId)
|
|
648
|
+
await assertProductRoleCatalog(sql, row.environmentId, row.catalogRoleId);
|
|
649
|
+
const fields = [row.id, row.environmentId, row.name, row.permissions, row.inherits, asTime(row.createdAt), row.allowsOrganizationlessSignup === true, row.ownerAccess === true];
|
|
650
|
+
const updated = await rows(sql, "UPDATE app_product_roles SET environment_id = $2, name = $3, permissions = $4::text[], inherits = $5::text[], created_at = $6::timestamptz, allows_organizationless_signup = $7, owner_access = $8 WHERE id = $1 RETURNING id", fields);
|
|
185
651
|
if (!updated.length)
|
|
186
|
-
await sql.query("INSERT INTO app_product_roles (id, environment_id, name, permissions, inherits, created_at) VALUES ($1, $2, $3, $4::text[], $5::text[], $6::timestamptz
|
|
652
|
+
await sql.query("INSERT INTO app_product_roles (id, environment_id, name, permissions, inherits, created_at, allows_organizationless_signup, owner_access, catalog_role_id) VALUES ($1, $2, $3, $4::text[], $5::text[], $6::timestamptz, $7, $8, $9)", [...fields, row.catalogRoleId ?? null]);
|
|
187
653
|
}),
|
|
188
654
|
deleteProductRole: (id) => tx(async (sql) => {
|
|
189
655
|
const removed = await rows(sql, "DELETE FROM app_product_roles WHERE id = $1 RETURNING id", [id]);
|
|
@@ -191,6 +657,29 @@ export function createSqlDirectory(tx) {
|
|
|
191
657
|
throw new StoreError("not_found", "Record not found.");
|
|
192
658
|
}),
|
|
193
659
|
listProductRoles: (environmentId) => tx(async (sql) => (await rows(sql, "SELECT * FROM app_product_roles WHERE environment_id = $1 ORDER BY created_at, id", [environmentId])).map(roleOf)),
|
|
660
|
+
insertTenantProductRole: (row) => tx(async (sql) => {
|
|
661
|
+
await sql.query("INSERT INTO tenant_product_roles (id, org_id, name, description, created_at) VALUES ($1, $2, $3, $4, $5::timestamptz)", [row.id, row.orgId, row.name, row.description, asTime(row.createdAt)]);
|
|
662
|
+
}),
|
|
663
|
+
listTenantProductRoles: (orgId) => tx(async (sql) => (await rows(sql, "SELECT * FROM tenant_product_roles WHERE org_id = $1 ORDER BY created_at, id", [orgId])).map(tenantRoleOf)),
|
|
664
|
+
updateTenantProductRole: (orgId, id, name, description) => tx(async (sql) => {
|
|
665
|
+
const updated = await first(sql, "UPDATE tenant_product_roles SET name = $3, description = $4 WHERE org_id = $1 AND id = $2 RETURNING *", [orgId, id, name, description], tenantRoleOf);
|
|
666
|
+
if (!updated)
|
|
667
|
+
throw new StoreError("not_found", "Catalog role not found.");
|
|
668
|
+
return updated;
|
|
669
|
+
}),
|
|
670
|
+
deleteTenantProductRole: (orgId, id) => tx(async (sql) => {
|
|
671
|
+
const removed = await rows(sql, "DELETE FROM tenant_product_roles WHERE org_id = $1 AND id = $2 RETURNING id", [orgId, id]);
|
|
672
|
+
if (!removed.length)
|
|
673
|
+
throw new StoreError("not_found", "Catalog role not found.");
|
|
674
|
+
}),
|
|
675
|
+
setProductRoleCatalogLink: (environmentId, roleId, catalogRoleId, expectedCatalogRoleId) => tx(async (sql) => {
|
|
676
|
+
if (catalogRoleId)
|
|
677
|
+
await assertProductRoleCatalog(sql, environmentId, catalogRoleId);
|
|
678
|
+
const updated = await first(sql, "UPDATE app_product_roles SET catalog_role_id = $3 WHERE environment_id = $1 AND id = $2 AND catalog_role_id IS NOT DISTINCT FROM $4::text RETURNING *", [environmentId, roleId, catalogRoleId, expectedCatalogRoleId], roleOf);
|
|
679
|
+
if (!updated)
|
|
680
|
+
throw new StoreError("conflict", "The role link changed.");
|
|
681
|
+
return updated;
|
|
682
|
+
}),
|
|
194
683
|
saveDirectoryGroup: (row) => tx(async (sql) => {
|
|
195
684
|
const updated = await rows(sql, "UPDATE app_groups SET environment_id = $2, name = $3, created_at = $4::timestamptz WHERE id = $1 RETURNING id", [row.id, row.environmentId, row.name, asTime(row.createdAt)]);
|
|
196
685
|
if (!updated.length)
|
|
@@ -214,6 +703,8 @@ export function createSqlDirectory(tx) {
|
|
|
214
703
|
}),
|
|
215
704
|
listGroupMembers: (environmentId) => tx(async (sql) => (await rows(sql, "SELECT * FROM app_group_members WHERE environment_id = $1 ORDER BY created_at, id", [environmentId])).map(groupMemberOf)),
|
|
216
705
|
saveRoleBinding: (row) => tx(async (sql) => {
|
|
706
|
+
if (row.subjectType === "organization" || row.organizationId !== null)
|
|
707
|
+
await requireOrganizationScope(sql, row.environmentId);
|
|
217
708
|
const updated = await rows(sql, `UPDATE app_role_bindings SET environment_id = $2, role_id = $3, subject_type = $4, subject_id = $5, organization_id = $6, resource = $7, created_at = $8::timestamptz
|
|
218
709
|
WHERE id = $1 RETURNING id`, [row.id, row.environmentId, row.roleId, row.subjectType, row.subjectId, row.organizationId, row.resource, asTime(row.createdAt)]);
|
|
219
710
|
if (!updated.length)
|
|
@@ -221,15 +712,40 @@ export function createSqlDirectory(tx) {
|
|
|
221
712
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8::timestamptz)`, [row.id, row.environmentId, row.roleId, row.subjectType, row.subjectId, row.organizationId, row.resource, asTime(row.createdAt)]);
|
|
222
713
|
}),
|
|
223
714
|
deleteRoleBinding: (id) => tx(async (sql) => {
|
|
224
|
-
const
|
|
225
|
-
if (!
|
|
715
|
+
const [binding] = await rows(sql, "SELECT * FROM app_role_bindings WHERE id = $1 FOR UPDATE", [id]);
|
|
716
|
+
if (!binding)
|
|
226
717
|
throw new StoreError("not_found", "Record not found.");
|
|
718
|
+
const roleId = text(binding, "role_id");
|
|
719
|
+
const environmentId = text(binding, "environment_id");
|
|
720
|
+
const ownerRole = await rows(sql, "SELECT id FROM app_product_roles WHERE environment_id = $1 AND owner_access AND id = $2", [environmentId, roleId]);
|
|
721
|
+
if (ownerRole.length && text(binding, "subject_type") === "identity" && textOrNull(binding, "resource") === null) {
|
|
722
|
+
const replacement = await rows(sql, `SELECT id FROM app_role_bindings
|
|
723
|
+
WHERE environment_id = $1 AND id <> $2 AND subject_type = 'identity' AND resource IS NULL AND role_id = $3
|
|
724
|
+
AND organization_id IS NOT DISTINCT FROM $4::text LIMIT 1`, [environmentId, id, roleId, textOrNull(binding, "organization_id")]);
|
|
725
|
+
if (!replacement.length)
|
|
726
|
+
throw new StoreError("last_owner_access", "Assign owner access to another person in this scope first.");
|
|
727
|
+
}
|
|
728
|
+
if (text(binding, "subject_type") === "identity") {
|
|
729
|
+
await sql.query(`DELETE FROM app_access_checks WHERE environment_id = $1 AND identity_id = $2 AND workspace_id IS NULL
|
|
730
|
+
AND organization_id IS NOT DISTINCT FROM $3::text`, [environmentId, text(binding, "subject_id"), textOrNull(binding, "organization_id")]);
|
|
731
|
+
}
|
|
732
|
+
await sql.query("DELETE FROM app_role_bindings WHERE id = $1", [id]);
|
|
227
733
|
}),
|
|
228
734
|
listRoleBindings: (environmentId) => tx(async (sql) => (await rows(sql, "SELECT * FROM app_role_bindings WHERE environment_id = $1 ORDER BY created_at, id", [environmentId])).map(bindingOf)),
|
|
229
735
|
saveFeatureDefinition: (row) => tx(async (sql) => {
|
|
230
|
-
const
|
|
736
|
+
const values = [row.id, row.environmentId, row.key, row.enabled, JSON.stringify(row.config), asTime(row.createdAt), row.kind ?? null];
|
|
737
|
+
const updated = await rows(sql, `UPDATE app_features SET environment_id = $2, key = $3, enabled = $4, config = $5::jsonb,
|
|
738
|
+
created_at = $6::timestamptz, kind = COALESCE($7, kind) WHERE id = $1 RETURNING id`, values);
|
|
231
739
|
if (!updated.length)
|
|
232
|
-
await sql.query(
|
|
740
|
+
await sql.query(`INSERT INTO app_features (id, environment_id, key, enabled, config, created_at, kind)
|
|
741
|
+
VALUES ($1, $2, $3, $4, $5::jsonb, $6::timestamptz, COALESCE($7, 'feature'))`, values);
|
|
742
|
+
}),
|
|
743
|
+
updateFeatureDefinition: (environmentId, id, changes) => tx(async (sql) => {
|
|
744
|
+
const updated = await first(sql, `UPDATE app_features SET enabled = COALESCE($3, enabled), config = COALESCE($4::jsonb, config)
|
|
745
|
+
WHERE environment_id = $1 AND id = $2 RETURNING *`, [environmentId, id, changes.enabled ?? null, changes.config === undefined ? null : JSON.stringify(changes.config)], featureOf);
|
|
746
|
+
if (!updated)
|
|
747
|
+
throw new StoreError("not_found", "Feature definition not found.");
|
|
748
|
+
return updated;
|
|
233
749
|
}),
|
|
234
750
|
deleteFeatureDefinition: (id) => tx(async (sql) => {
|
|
235
751
|
const removed = await rows(sql, "DELETE FROM app_features WHERE id = $1 RETURNING id", [id]);
|
|
@@ -238,6 +754,8 @@ export function createSqlDirectory(tx) {
|
|
|
238
754
|
}),
|
|
239
755
|
listFeatureDefinitions: (environmentId) => tx(async (sql) => (await rows(sql, "SELECT * FROM app_features WHERE environment_id = $1 ORDER BY created_at, id", [environmentId])).map(featureOf)),
|
|
240
756
|
saveFeatureAssignment: (row) => tx(async (sql) => {
|
|
757
|
+
if (row.targetType === "organization")
|
|
758
|
+
await requireOrganizationScope(sql, row.environmentId);
|
|
241
759
|
const updated = await rows(sql, `UPDATE app_feature_assignments SET environment_id = $2, feature_id = $3, target_type = $4, target_id = $5, enabled = $6, feature_limit = $7, config = $8::jsonb, created_at = $9::timestamptz
|
|
242
760
|
WHERE id = $1 RETURNING id`, [row.id, row.environmentId, row.featureId, row.targetType, row.targetId, row.enabled, row.limit, JSON.stringify(row.config), asTime(row.createdAt)]);
|
|
243
761
|
if (!updated.length)
|
|
@@ -250,25 +768,285 @@ export function createSqlDirectory(tx) {
|
|
|
250
768
|
throw new StoreError("not_found", "Record not found.");
|
|
251
769
|
}),
|
|
252
770
|
listFeatureAssignments: (environmentId) => tx(async (sql) => (await rows(sql, "SELECT * FROM app_feature_assignments WHERE environment_id = $1 ORDER BY created_at, id", [environmentId])).map(assignmentOf)),
|
|
253
|
-
|
|
254
|
-
|
|
771
|
+
insertAppWaitlist: (row) => tx(async (sql) => (await rows(sql, appWaitlistInsert + " ON CONFLICT DO NOTHING RETURNING id", [...appWaitValues(row), true])).length > 0),
|
|
772
|
+
saveAppWaitlist: (row, challenge) => tx(async (sql) => {
|
|
773
|
+
if (row.invitation && !await invitationIsAvailable(sql, row))
|
|
774
|
+
throw new StoreError("conflict", "The invited workspace or role is unavailable.");
|
|
775
|
+
const previous = await first(sql, "SELECT token_hash FROM app_waitlist WHERE id = $1 FOR UPDATE", [row.id], (item) => textOrNull(item, "token_hash"));
|
|
776
|
+
if (challenge && previous && previous !== challenge.tokenHash) {
|
|
777
|
+
await sql.query(`UPDATE directory_challenges SET used_at = $3::timestamptz
|
|
778
|
+
WHERE environment_id = $1 AND token_hash = $2 AND purpose = 'admit' AND used_at IS NULL`, [row.environmentId, previous, asTime(challenge.createdAt)]);
|
|
779
|
+
}
|
|
780
|
+
if (challenge)
|
|
781
|
+
await insertChallenge(sql, challenge);
|
|
782
|
+
const updated = await rows(sql, `UPDATE app_waitlist SET app_id = $2, environment_id = $3, email = $4,
|
|
783
|
+
status = $5, token_hash = $6, created_at = $7::timestamptz, answers = $8::jsonb, invitation_workspace_id = $9,
|
|
784
|
+
invitation_role_id = $10, pending_workspace_identity_id = $11, invitation_organization_id = $12,
|
|
785
|
+
invitation_delivery = $13, invitation_audit_id = $14, revoked_at = $15::timestamptz WHERE id = $1 RETURNING id`, appWaitValues(row));
|
|
786
|
+
if (!updated.length)
|
|
787
|
+
await sql.query(appWaitlistInsert, [...appWaitValues(row), false]);
|
|
788
|
+
}),
|
|
789
|
+
readDirectoryInvitation: (environmentId, tokenHash, at) => tx(async (sql) => {
|
|
790
|
+
const invite = await first(sql, "SELECT * FROM app_waitlist WHERE environment_id = $1 AND token_hash = $2", [environmentId, tokenHash], appWaitOf);
|
|
791
|
+
if (!invite)
|
|
792
|
+
return null;
|
|
793
|
+
const challenge = await first(sql, "SELECT * FROM directory_challenges WHERE environment_id = $1 AND token_hash = $2 AND purpose = 'admit'", [environmentId, tokenHash], challengeOf);
|
|
794
|
+
return invitationPreview(invite, challenge, asTime(at));
|
|
795
|
+
}),
|
|
796
|
+
listDirectoryInvitations: (environmentId, at) => tx(async (sql) => {
|
|
797
|
+
const stamp = asTime(at);
|
|
798
|
+
const invites = (await rows(sql, "SELECT * FROM app_waitlist WHERE environment_id = $1 AND (invitation_role_id IS NOT NULL OR status = 'revoked') ORDER BY created_at, id", [environmentId])).map(appWaitOf);
|
|
799
|
+
const previews = [];
|
|
800
|
+
for (const invite of invites) {
|
|
801
|
+
const challenge = invite.tokenHash
|
|
802
|
+
? await first(sql, "SELECT * FROM directory_challenges WHERE environment_id = $1 AND token_hash = $2 AND purpose = 'admit'", [environmentId, invite.tokenHash], challengeOf)
|
|
803
|
+
: null;
|
|
804
|
+
const preview = invitationPreview(invite, challenge, stamp);
|
|
805
|
+
if (preview)
|
|
806
|
+
previews.push(preview);
|
|
807
|
+
}
|
|
808
|
+
return previews;
|
|
809
|
+
}),
|
|
810
|
+
setDirectoryInvitationAudit: (environmentId, id, auditId) => tx(async (sql) => {
|
|
811
|
+
const updated = await rows(sql, "UPDATE app_waitlist SET invitation_audit_id = $3 WHERE environment_id = $1 AND id = $2 RETURNING id", [environmentId, id, auditId]);
|
|
255
812
|
if (!updated.length)
|
|
256
|
-
|
|
813
|
+
throw new StoreError("not_found", "Invitation not found.");
|
|
814
|
+
}),
|
|
815
|
+
revokeDirectoryInvitation: (environmentId, id, at) => tx(async (sql) => {
|
|
816
|
+
const invite = await first(sql, "SELECT * FROM app_waitlist WHERE environment_id = $1 AND id = $2 FOR UPDATE", [environmentId, id], appWaitOf);
|
|
817
|
+
if (!invite)
|
|
818
|
+
return "missing";
|
|
819
|
+
const challenge = invite.tokenHash
|
|
820
|
+
? await first(sql, "SELECT * FROM directory_challenges WHERE environment_id = $1 AND token_hash = $2 AND purpose = 'admit' FOR UPDATE", [environmentId, invite.tokenHash], challengeOf)
|
|
821
|
+
: null;
|
|
822
|
+
const stamp = asTime(at);
|
|
823
|
+
if (invitationLifecycle(invite.status, challenge, stamp) !== "pending")
|
|
824
|
+
return "closed";
|
|
825
|
+
await sql.query("UPDATE app_waitlist SET status = 'revoked', revoked_at = $2::timestamptz WHERE id = $1", [invite.id, stamp]);
|
|
826
|
+
if (challenge && !challenge.usedAt)
|
|
827
|
+
await sql.query("UPDATE directory_challenges SET used_at = $2::timestamptz WHERE id = $1 AND used_at IS NULL", [challenge.id, stamp]);
|
|
828
|
+
return "revoked";
|
|
829
|
+
}),
|
|
830
|
+
redeemDirectoryInvitation: (input) => tx(async (sql) => {
|
|
831
|
+
const stamp = asTime(input.at);
|
|
832
|
+
const challenge = await first(sql, `SELECT * FROM directory_challenges
|
|
833
|
+
WHERE environment_id = $1 AND token_hash = $2 AND purpose = 'admit' FOR UPDATE`, [input.environmentId, input.tokenHash], challengeOf);
|
|
834
|
+
const invite = challenge
|
|
835
|
+
? await first(sql, "SELECT * FROM app_waitlist WHERE environment_id = $1 AND token_hash = $2 FOR UPDATE", [input.environmentId, input.tokenHash], appWaitOf)
|
|
836
|
+
: null;
|
|
837
|
+
if (!invite?.invitation || invite.invitation.delivery !== "link" || !challenge)
|
|
838
|
+
return { outcome: "unavailable" };
|
|
839
|
+
const identity = await first(sql, "SELECT * FROM app_identities WHERE id = $1 FOR UPDATE", [input.identityId], identityOf);
|
|
840
|
+
if (!identity || identity.environmentId !== input.environmentId || identity.appId !== invite.appId || identity.status !== "active")
|
|
841
|
+
return { outcome: "unavailable" };
|
|
842
|
+
if ((identity.kind !== "user" && identity.kind !== "account") || !identity.email
|
|
843
|
+
|| identity.email.toLowerCase() !== invite.email.toLowerCase() || identity.email.toLowerCase() !== challenge.email.toLowerCase()) {
|
|
844
|
+
return { outcome: "recipient" };
|
|
845
|
+
}
|
|
846
|
+
const status = invitationLifecycle(invite.status, challenge, stamp);
|
|
847
|
+
if (status === "accepted") {
|
|
848
|
+
const current = await savedInvitationGrant(sql, invite, identity.id);
|
|
849
|
+
return { outcome: "existing", identityId: identity.id, roleId: current.roleId, membershipId: current.membershipId, bindingId: current.bindingId };
|
|
850
|
+
}
|
|
851
|
+
if (status !== "pending" || !await invitationIsAvailable(sql, invite))
|
|
852
|
+
return { outcome: "unavailable" };
|
|
853
|
+
if (!identity.emailVerifiedAt && !identity.emailProofAt)
|
|
854
|
+
return { outcome: "unverified" };
|
|
855
|
+
const owner = await first(sql, "SELECT id FROM app_product_roles WHERE environment_id = $1 AND owner_access", [invite.environmentId], (row) => text(row, "id"));
|
|
856
|
+
const saved = await applySqlDirectoryAccess(sql, invitationGrantRequest(invite, identity.id, stamp, {
|
|
857
|
+
membershipId: input.newMembershipId, bindingId: input.newBindingId, organizationMembershipId: input.newOrganizationMembershipId,
|
|
858
|
+
}, owner));
|
|
859
|
+
await sql.query("UPDATE directory_challenges SET used_at = $2::timestamptz WHERE id = $1 AND used_at IS NULL", [challenge.id, stamp]);
|
|
860
|
+
await sql.query("UPDATE app_waitlist SET status = 'admitted', pending_workspace_identity_id = NULL WHERE id = $1", [invite.id]);
|
|
861
|
+
return {
|
|
862
|
+
outcome: "applied", identityId: identity.id, roleId: saved.roleId ?? invite.invitation.roleId,
|
|
863
|
+
membershipId: saved.membership?.id ?? null, bindingId: saved.binding?.id ?? null,
|
|
864
|
+
};
|
|
865
|
+
}),
|
|
866
|
+
acceptDirectoryWorkspaceInvitation: (identityId, at) => tx(async (sql) => {
|
|
867
|
+
const identity = await first(sql, "SELECT * FROM app_identities WHERE id = $1 FOR UPDATE", [identityId], identityOf);
|
|
868
|
+
return identity ? acceptWorkspaceInvitation(sql, identity, asTime(at)) : false;
|
|
869
|
+
}),
|
|
870
|
+
acceptDirectoryWorkspaceInvitationByToken: (environmentId, tokenHash, at) => tx(async (sql) => {
|
|
871
|
+
const identity = await first(sql, `SELECT identity.* FROM directory_challenges AS challenge
|
|
872
|
+
JOIN app_identities AS identity ON identity.environment_id = challenge.environment_id AND lower(identity.email) = lower(challenge.email)
|
|
873
|
+
AND identity.kind IN ('user', 'account')
|
|
874
|
+
WHERE challenge.environment_id = $1 AND challenge.token_hash = $2 AND challenge.purpose = 'admit'
|
|
875
|
+
ORDER BY CASE identity.kind WHEN 'user' THEN 0 ELSE 1 END, identity.id LIMIT 1 FOR UPDATE OF identity`, [environmentId, tokenHash], identityOf);
|
|
876
|
+
const stamp = asTime(at);
|
|
877
|
+
if (!identity || !await acceptWorkspaceInvitation(sql, identity, stamp, tokenHash))
|
|
878
|
+
return false;
|
|
879
|
+
await sql.query("UPDATE app_identities SET email_proof_at = $2::timestamptz, updated_at = $2::timestamptz WHERE id = $1", [identity.id, stamp]);
|
|
880
|
+
return true;
|
|
881
|
+
}),
|
|
882
|
+
completeDirectoryPasswordSetup: (environmentId, tokenHash, passwordHash, at) => tx(async (sql) => {
|
|
883
|
+
const challenge = await first(sql, `SELECT * FROM directory_challenges
|
|
884
|
+
WHERE environment_id = $1 AND token_hash = $2 AND purpose = 'admit' FOR UPDATE`, [environmentId, tokenHash], challengeOf);
|
|
885
|
+
const stamp = asTime(at);
|
|
886
|
+
if (!challenge || challenge.usedAt || isExpired(challenge.expiresAt, stamp))
|
|
887
|
+
return false;
|
|
888
|
+
const identity = await first(sql, `SELECT * FROM app_identities
|
|
889
|
+
WHERE environment_id = $1 AND lower(email) = lower($2) AND kind IN ('user', 'account')
|
|
890
|
+
ORDER BY CASE kind WHEN 'user' THEN 0 ELSE 1 END, id LIMIT 1 FOR UPDATE`, [environmentId, challenge.email], identityOf);
|
|
891
|
+
if (!identity || identity.status !== "active" || (identity.kind !== "user" && identity.kind !== "account") || identity.passwordHash)
|
|
892
|
+
return false;
|
|
893
|
+
const invite = await first(sql, `SELECT * FROM app_waitlist
|
|
894
|
+
WHERE environment_id = $1 AND lower(email) = lower($2) AND status = 'invited' FOR UPDATE`, [environmentId, challenge.email], appWaitOf);
|
|
895
|
+
if (invite && invite.tokenHash !== tokenHash)
|
|
896
|
+
return false;
|
|
897
|
+
if (invite?.invitation?.delivery === "link")
|
|
898
|
+
return false;
|
|
899
|
+
if (invite?.invitation && !await invitationIsAvailable(sql, invite))
|
|
900
|
+
return false;
|
|
901
|
+
const saved = await first(sql, `UPDATE app_identities
|
|
902
|
+
SET password_hash = $2, email_verified_at = COALESCE(email_verified_at, $3::timestamptz),
|
|
903
|
+
email_proof_at = $3::timestamptz, updated_at = $3::timestamptz
|
|
904
|
+
WHERE id = $1 AND password_hash IS NULL RETURNING *`, [identity.id, passwordHash, stamp], identityOf);
|
|
905
|
+
if (!saved)
|
|
906
|
+
return false;
|
|
907
|
+
saved.emailVerifiedAt = saved.emailVerifiedAt ?? stamp;
|
|
908
|
+
if (invite?.invitation) {
|
|
909
|
+
if (!await applyStoredInvitation(sql, invite, challenge.id, saved.id, stamp)) {
|
|
910
|
+
throw new StoreError("conflict", "The invitation could not be applied.");
|
|
911
|
+
}
|
|
912
|
+
return true;
|
|
913
|
+
}
|
|
914
|
+
await sql.query("UPDATE directory_challenges SET used_at = $2::timestamptz WHERE id = $1 AND used_at IS NULL", [challenge.id, stamp]);
|
|
915
|
+
if (invite)
|
|
916
|
+
await sql.query("UPDATE app_waitlist SET status = 'admitted', pending_workspace_identity_id = NULL WHERE id = $1 AND status = 'invited'", [invite.id]);
|
|
917
|
+
return true;
|
|
257
918
|
}),
|
|
258
919
|
listAppWaitlist: (environmentId) => tx(async (sql) => (await rows(sql, "SELECT * FROM app_waitlist WHERE environment_id = $1 ORDER BY created_at, id", [environmentId])).map(appWaitOf)),
|
|
259
920
|
findAppWaitlist: (environmentId, email) => tx((sql) => first(sql, "SELECT * FROM app_waitlist WHERE environment_id = $1 AND lower(email) = lower($2)", [environmentId, email], appWaitOf)),
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
921
|
+
claimWaitlistConfirmation: (list, entryId, at, staleAt) => tx(async (sql) => {
|
|
922
|
+
const table = list === "app" ? "app_waitlist" : "platform_waitlist";
|
|
923
|
+
return (await rows(sql, `UPDATE ${table} SET confirmation_claimed_at = $2::timestamptz
|
|
924
|
+
WHERE id = $1 AND status = 'waiting' AND confirmation_pending
|
|
925
|
+
AND (confirmation_claimed_at IS NULL OR confirmation_claimed_at <= $3::timestamptz) RETURNING id`, [entryId, asTime(at), asTime(staleAt)])).length > 0;
|
|
263
926
|
}),
|
|
264
|
-
|
|
927
|
+
finishWaitlistConfirmation: (list, entryId, claimedAt, accepted) => tx(async (sql) => {
|
|
928
|
+
const table = list === "app" ? "app_waitlist" : "platform_waitlist";
|
|
929
|
+
await sql.query(`UPDATE ${table} SET confirmation_claimed_at = NULL, confirmation_pending = $3
|
|
930
|
+
WHERE id = $1 AND confirmation_claimed_at = $2::timestamptz`, [entryId, asTime(claimedAt), !accepted]);
|
|
931
|
+
}),
|
|
932
|
+
insertDirectoryChallenge: (row, delivery) => tx((sql) => insertChallenge(sql, row, delivery)),
|
|
933
|
+
insertDirectoryChallengeWithCooldown: (row, after, delivery) => tx(async (sql) => {
|
|
934
|
+
const identity = await rows(sql, `SELECT identity.id FROM app_identities identity
|
|
935
|
+
JOIN app_environments env ON env.id = identity.environment_id JOIN applications app ON app.id = env.app_id
|
|
936
|
+
WHERE identity.id = $1 AND identity.environment_id = $2 AND identity.email = $3 AND identity.status = 'active'
|
|
937
|
+
AND app.trashed_at IS NULL AND app.status = 'active' AND env.status = 'active' FOR SHARE OF app, env FOR UPDATE OF identity`, [row.identityId, row.environmentId, row.email]);
|
|
938
|
+
if (!identity.length)
|
|
939
|
+
return false;
|
|
940
|
+
const recent = await rows(sql, `SELECT id FROM directory_challenges WHERE environment_id = $1 AND identity_id = $2
|
|
941
|
+
AND purpose = $3 AND created_at > $4::timestamptz LIMIT 1`, [row.environmentId, row.identityId, row.purpose, asTime(after)]);
|
|
942
|
+
if (recent.length)
|
|
943
|
+
return false;
|
|
944
|
+
await insertChallenge(sql, row, delivery);
|
|
945
|
+
return true;
|
|
946
|
+
}),
|
|
947
|
+
takeDirectoryChallenge: (tokenHash, at, scope) => tx(async (sql) => {
|
|
265
948
|
const stamp = asTime(at);
|
|
949
|
+
if (!await lockDirectoryChallengeApplication(sql, tokenHash, true))
|
|
950
|
+
return null;
|
|
266
951
|
const row = await first(sql, "SELECT * FROM directory_challenges WHERE token_hash = $1 FOR UPDATE", [tokenHash], challengeOf);
|
|
267
|
-
if (!row || row.usedAt || isExpired(row.expiresAt, stamp)
|
|
952
|
+
if (!row || row.usedAt || isExpired(row.expiresAt, stamp)
|
|
953
|
+
|| (scope?.environmentId && row.environmentId !== scope.environmentId) || (scope?.purpose && row.purpose !== scope.purpose))
|
|
268
954
|
return null;
|
|
269
955
|
await sql.query("UPDATE directory_challenges SET used_at = $2::timestamptz WHERE id = $1", [row.id, stamp]);
|
|
270
956
|
return { ...row, usedAt: stamp };
|
|
271
957
|
}),
|
|
958
|
+
verifyDirectoryEmail: (tokenHash, at, environmentId) => tx(async (sql) => {
|
|
959
|
+
const stamp = asTime(at);
|
|
960
|
+
if (!await lockDirectoryChallengeApplication(sql, tokenHash, false))
|
|
961
|
+
return null;
|
|
962
|
+
const row = await first(sql, "SELECT * FROM directory_challenges WHERE token_hash = $1 AND purpose = 'verify' FOR UPDATE", [tokenHash], challengeOf);
|
|
963
|
+
if (!row || row.usedAt || isExpired(row.expiresAt, stamp) || (environmentId && row.environmentId !== environmentId))
|
|
964
|
+
return null;
|
|
965
|
+
const identity = await first(sql, `UPDATE app_identities SET
|
|
966
|
+
email_verified_at = COALESCE(email_verified_at, $4::timestamptz),
|
|
967
|
+
email_proof_at = CASE WHEN kind = 'user' THEN GREATEST(email_proof_at, $4::timestamptz) END,
|
|
968
|
+
updated_at = $4::timestamptz
|
|
969
|
+
WHERE id = $1 AND environment_id = $2 AND lower(email) = lower($3) AND status = 'active' RETURNING *`, [row.identityId, row.environmentId, row.email, stamp], identityOf);
|
|
970
|
+
if (!identity)
|
|
971
|
+
return null;
|
|
972
|
+
await sql.query("UPDATE directory_challenges SET used_at = $2::timestamptz WHERE id = $1", [row.id, stamp]);
|
|
973
|
+
await acceptWorkspaceInvitation(sql, identity, stamp);
|
|
974
|
+
return identity;
|
|
975
|
+
}),
|
|
272
976
|
};
|
|
273
977
|
}
|
|
978
|
+
function directoryRecordId(prefix) {
|
|
979
|
+
return `${prefix}_${randomBytes(16).toString("base64url")}`;
|
|
980
|
+
}
|
|
981
|
+
async function insertOrganization(sql, appId, environmentId, orgId, displayName, at) {
|
|
982
|
+
const id = directoryRecordId("idn");
|
|
983
|
+
await insertIdentity(sql, identityValues({
|
|
984
|
+
id, appId, environmentId, orgId, kind: "organization", email: null, passwordHash: null, displayName, status: "active",
|
|
985
|
+
emailVerifiedAt: null, metadata: {}, createdAt: at, updatedAt: at,
|
|
986
|
+
}));
|
|
987
|
+
return id;
|
|
988
|
+
}
|
|
989
|
+
async function insertOrganizationMember(sql, environmentId, identityId, organizationId, relation, at) {
|
|
990
|
+
const existing = await rows(sql, `SELECT id FROM app_identity_memberships
|
|
991
|
+
WHERE environment_id = $1 AND identity_id = $2 AND organization_id = $3`, [environmentId, identityId, organizationId]);
|
|
992
|
+
if (existing.length)
|
|
993
|
+
return;
|
|
994
|
+
await sql.query(`INSERT INTO app_identity_memberships (id, environment_id, identity_id, organization_id, relation, created_at)
|
|
995
|
+
VALUES ($1, $2, $3, $4, $5, $6::timestamptz)`, [directoryRecordId("dms"), environmentId, identityId, organizationId, relation, asTime(at)]);
|
|
996
|
+
}
|
|
997
|
+
export async function repairDirectoryStructure(sql, appId, orgId, model, at) {
|
|
998
|
+
const hasOrganizations = directoryHasOrganizations(model);
|
|
999
|
+
const hasWorkspaces = directoryHasWorkspaces(model);
|
|
1000
|
+
const repair = { organizations: 0, workspaces: 0 };
|
|
1001
|
+
const environments = await rows(sql, "SELECT id FROM app_environments WHERE app_id = $1 ORDER BY id FOR UPDATE", [appId]);
|
|
1002
|
+
for (const environment of environments) {
|
|
1003
|
+
const environmentId = text(environment, "id");
|
|
1004
|
+
const people = await rows(sql, `SELECT id, display_name, created_at FROM app_identities
|
|
1005
|
+
WHERE app_id = $1 AND environment_id = $2 AND kind IN ('user', 'account') ORDER BY created_at, id`, [appId, environmentId]);
|
|
1006
|
+
if (hasOrganizations) {
|
|
1007
|
+
for (const person of people) {
|
|
1008
|
+
const identityId = text(person, "id");
|
|
1009
|
+
const joined = await rows(sql, "SELECT id FROM app_identity_memberships WHERE environment_id = $1 AND identity_id = $2 LIMIT 1", [environmentId, identityId]);
|
|
1010
|
+
if (joined.length)
|
|
1011
|
+
continue;
|
|
1012
|
+
const organizationId = await insertOrganization(sql, appId, environmentId, orgId, text(person, "display_name"), at);
|
|
1013
|
+
await insertOrganizationMember(sql, environmentId, identityId, organizationId, "owner", at);
|
|
1014
|
+
repair.organizations += 1;
|
|
1015
|
+
}
|
|
1016
|
+
if (model === "organizations-and-workspaces") {
|
|
1017
|
+
const orphans = await rows(sql, `SELECT id, name FROM app_workspaces
|
|
1018
|
+
WHERE app_id = $1 AND environment_id = $2 AND organization_id IS NULL ORDER BY created_at, id FOR UPDATE`, [appId, environmentId]);
|
|
1019
|
+
for (const workspace of orphans) {
|
|
1020
|
+
const workspaceId = text(workspace, "id");
|
|
1021
|
+
const organizationId = await insertOrganization(sql, appId, environmentId, orgId, text(workspace, "name"), at);
|
|
1022
|
+
const owners = await rows(sql, `SELECT identity_id FROM app_workspace_memberships
|
|
1023
|
+
WHERE environment_id = $1 AND workspace_id = $2 AND relation = 'owner' ORDER BY created_at, id`, [environmentId, workspaceId]);
|
|
1024
|
+
for (const owner of owners)
|
|
1025
|
+
await insertOrganizationMember(sql, environmentId, text(owner, "identity_id"), organizationId, "member", at);
|
|
1026
|
+
await sql.query("UPDATE app_workspaces SET organization_id = $3, updated_at = $4::timestamptz WHERE id = $1 AND environment_id = $2", [workspaceId, environmentId, organizationId, asTime(at)]);
|
|
1027
|
+
repair.organizations += 1;
|
|
1028
|
+
}
|
|
1029
|
+
}
|
|
1030
|
+
}
|
|
1031
|
+
if (!hasWorkspaces)
|
|
1032
|
+
continue;
|
|
1033
|
+
for (const person of people) {
|
|
1034
|
+
const identityId = text(person, "id");
|
|
1035
|
+
const joined = await rows(sql, "SELECT id FROM app_workspace_memberships WHERE environment_id = $1 AND identity_id = $2 LIMIT 1", [environmentId, identityId]);
|
|
1036
|
+
if (joined.length)
|
|
1037
|
+
continue;
|
|
1038
|
+
const parent = hasOrganizations ? await first(sql, `SELECT organization_id FROM app_identity_memberships
|
|
1039
|
+
WHERE environment_id = $1 AND identity_id = $2 ORDER BY created_at, id LIMIT 1`, [environmentId, identityId], (row) => text(row, "organization_id")) : null;
|
|
1040
|
+
if (hasOrganizations && !parent)
|
|
1041
|
+
continue;
|
|
1042
|
+
const workspaceId = directoryRecordId("wsp");
|
|
1043
|
+
await sql.query(`INSERT INTO app_workspaces (id, app_id, environment_id, organization_id, name, status, created_at, updated_at)
|
|
1044
|
+
VALUES ($1, $2, $3, $4, $5, 'active', $6::timestamptz, $6::timestamptz)`, [workspaceId, appId, environmentId, parent, text(person, "display_name"), asTime(at)]);
|
|
1045
|
+
await sql.query(`INSERT INTO app_workspace_memberships (id, environment_id, workspace_id, identity_id, relation, created_at)
|
|
1046
|
+
VALUES ($1, $2, $3, $4, 'owner', $5::timestamptz)`, [directoryRecordId("wsm"), environmentId, workspaceId, identityId, asTime(at)]);
|
|
1047
|
+
repair.workspaces += 1;
|
|
1048
|
+
}
|
|
1049
|
+
}
|
|
1050
|
+
return repair;
|
|
1051
|
+
}
|
|
274
1052
|
//# sourceMappingURL=directory-sql.js.map
|