@open-pencil/cloud 0.17.0-beta.2 → 0.17.0-beta.4
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/README.md +1 -1
- package/dist/bin/{ai-relay.mjs → services.mjs} +8 -8
- package/dist/bin/services.mjs.map +1 -0
- package/dist/client.d.ts +236 -46
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +649 -400
- package/dist/client.js.map +1 -1
- package/dist/contract.d.ts +2 -2
- package/dist/contract.js +2 -2
- package/dist/contract2.js +680 -443
- package/dist/contract2.js.map +1 -1
- package/dist/email.d.ts +9 -1
- package/dist/email.d.ts.map +1 -1
- package/dist/email.js +99 -58
- package/dist/email.js.map +1 -1
- package/dist/index.d.ts +469 -65
- package/dist/index.d.ts.map +1 -1
- package/dist/listen.mjs +505 -787
- package/dist/listen.mjs.map +1 -1
- package/dist/runtime-node.d.mts +3920 -2524
- package/dist/runtime-node.d.mts.map +1 -1
- package/dist/runtime-node.mjs +6 -5
- package/dist/runtime-node.mjs.map +1 -1
- package/dist/server.d.ts +3586 -1897
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +5216 -4272
- package/dist/server.js.map +1 -1
- package/dist/server.mjs +5179 -3623
- package/dist/server.mjs.map +1 -1
- package/dist/server2.mjs +101 -79
- package/dist/server2.mjs.map +1 -1
- package/package.json +7 -6
- package/src/admin/api/routes.ts +59 -0
- package/src/admin/audit/service.ts +25 -6
- package/src/admin/documents/service.ts +361 -0
- package/src/admin/errors.ts +9 -1
- package/src/admin/index.ts +6 -0
- package/src/admin/operations/service.ts +7 -0
- package/src/admin/reports/service.ts +147 -0
- package/src/admin/schema.ts +6 -0
- package/src/client/api.ts +36 -21
- package/src/client/auth/navigation.ts +6 -5
- package/src/client/index.ts +7 -7
- package/src/client/portal/account.ts +25 -0
- package/src/client/portal/administration.ts +56 -0
- package/src/client/{ai.ts → services.ts} +44 -27
- package/src/contract/account.ts +58 -2
- package/src/contract/admin/index.ts +67 -3
- package/src/contract/collaboration.ts +6 -1
- package/src/contract/discovery.ts +22 -4
- package/src/contract/documents.ts +27 -0
- package/src/contract/entitlements.ts +2 -2
- package/src/contract/index.ts +63 -37
- package/src/contract/relay.ts +6 -0
- package/src/contract/reports.ts +42 -0
- package/src/contract/services.ts +233 -0
- package/src/contract/sharing.ts +3 -9
- package/src/email/render.ts +26 -0
- package/src/email/schema.ts +11 -1
- package/src/email/templates/brand.ts +1 -1
- package/src/runtime/cloudflare/environment.ts +22 -11
- package/src/runtime/cloudflare/index.ts +1 -1
- package/src/runtime/cloudflare/logging.ts +15 -0
- package/src/runtime/cloudflare/maintenance.ts +31 -0
- package/src/runtime/cloudflare/relay/index.ts +62 -14
- package/src/runtime/cloudflare/worker.ts +39 -13
- package/src/runtime/node/application.ts +5 -3
- package/src/runtime/node/bootstrap.ts +15 -4
- package/src/runtime/node/cleanup.ts +16 -3
- package/src/runtime/node/entitlements/main.ts +14 -11
- package/src/runtime/node/index.ts +1 -1
- package/src/runtime/node/migrate.ts +5 -1
- package/src/runtime/node/relay.ts +8 -2
- package/src/runtime/node/request-id.ts +13 -0
- package/src/runtime/node/server.ts +39 -15
- package/src/runtime/node/{ai-relay → services}/main.ts +6 -6
- package/src/runtime/node/{ai-relay → services}/server.ts +18 -18
- package/src/runtime/postgres/database.ts +20 -1
- package/src/runtime/posthog/errors.ts +15 -31
- package/src/runtime/s3/objects.ts +24 -1
- package/src/runtime/site/server.ts +32 -5
- package/src/server/account/actions.ts +5 -0
- package/src/server/account/deletion.ts +302 -0
- package/src/server/account/routes.ts +47 -3
- package/src/server/account/terms.ts +42 -0
- package/src/server/api/router.ts +9 -5
- package/src/server/api/types.ts +2 -0
- package/src/server/app.ts +90 -16
- package/src/server/auth/access-control.ts +63 -0
- package/src/server/auth/adapter.ts +11 -0
- package/src/server/auth/endpoints.ts +5 -1
- package/src/server/auth/factory.ts +30 -3
- package/src/server/auth/index.ts +10 -1
- package/src/server/cleanup/configured.ts +12 -5
- package/src/server/cleanup/documents.ts +165 -71
- package/src/server/cleanup/revisions.ts +16 -5
- package/src/server/cleanup/uploads.ts +8 -2
- package/src/server/cleanup/worker.ts +5 -2
- package/src/server/collaboration/relay/connection.ts +4 -3
- package/src/server/collaboration/relay/documents.ts +13 -10
- package/src/server/collaboration/relay/hub.ts +97 -25
- package/src/server/collaboration/relay/log.ts +5 -2
- package/src/server/collaboration/service.ts +27 -31
- package/src/server/config/deployment.ts +24 -4
- package/src/server/config/public.ts +5 -4
- package/src/server/config/schema.ts +82 -16
- package/src/server/db/expressions.ts +15 -1
- package/src/server/db/migrate.ts +34 -116
- package/src/server/db/migrations/001_accounts.ts +79 -0
- package/src/server/db/migrations/002_workspaces.ts +48 -0
- package/src/server/db/migrations/003_storage.ts +44 -0
- package/src/server/db/migrations/004_documents.ts +193 -0
- package/src/server/db/migrations/005_sharing.ts +95 -0
- package/src/server/db/migrations/006_operations.ts +110 -0
- package/src/server/db/migrations/007_trash.ts +33 -0
- package/src/server/db/migrations/008_terms.ts +18 -0
- package/src/server/db/migrations/schema.ts +71 -0
- package/src/server/db/tables.ts +52 -3
- package/src/server/documents/access.ts +66 -55
- package/src/server/documents/errors.ts +7 -0
- package/src/server/documents/images.ts +2 -1
- package/src/server/documents/limits.ts +9 -9
- package/src/server/documents/purge.ts +75 -0
- package/src/server/documents/repository.ts +2 -0
- package/src/server/documents/routes.ts +26 -0
- package/src/server/documents/service.ts +18 -4
- package/src/server/documents/summary.ts +1 -0
- package/src/server/documents/trash.ts +203 -0
- package/src/server/documents/versions/checkpoint.ts +2 -1
- package/src/server/documents/versions/history.ts +11 -7
- package/src/server/documents/versions/service.ts +13 -4
- package/src/server/errors/index.ts +59 -16
- package/src/server/errors/posthog.ts +80 -0
- package/src/server/index.ts +46 -17
- package/src/server/internal/index.ts +3 -3
- package/src/server/internal/routes.ts +7 -10
- package/src/server/internal/schema.ts +1 -1
- package/src/server/internal/service.ts +17 -23
- package/src/server/invitations/budget.ts +66 -0
- package/src/server/invitations/index.ts +1 -0
- package/src/server/logging.ts +76 -0
- package/src/server/pictures/service.ts +2 -1
- package/src/server/policy/database.ts +39 -25
- package/src/server/policy/index.ts +2 -1
- package/src/server/policy/keys.ts +5 -2
- package/src/server/policy/policy.ts +1 -0
- package/src/server/policy/schema.ts +2 -2
- package/src/server/policy/service.ts +12 -9
- package/src/server/policy/static.ts +5 -5
- package/src/server/policy/workspace.ts +41 -0
- package/src/server/quota/reconcile.ts +2 -0
- package/src/server/quota/storage.ts +69 -31
- package/src/server/rate-limit/cleanup.ts +17 -5
- package/src/server/rate-limit/middleware.ts +2 -2
- package/src/server/reports/index.ts +8 -0
- package/src/server/reports/routes.ts +37 -0
- package/src/server/reports/service.ts +123 -0
- package/src/server/services/errors.ts +7 -0
- package/src/server/services/index.ts +8 -0
- package/src/server/{ai → services}/keys.ts +11 -11
- package/src/server/services/routes.ts +47 -0
- package/src/server/{ai/service.ts → services/tokens.ts} +23 -20
- package/src/server/sharing/capabilities/service.ts +68 -22
- package/src/server/sharing/grants/service.ts +11 -18
- package/src/server/sharing/http.ts +4 -0
- package/src/server/sharing/invitations/service.ts +21 -11
- package/src/server/sharing/policy.ts +9 -14
- package/src/server/sharing/routes.ts +0 -16
- package/src/server/sharing/service.ts +1 -17
- package/src/server/validation.ts +1 -0
- package/src/server/workspaces/index.ts +1 -0
- package/src/server/workspaces/members.ts +89 -38
- package/src/server/workspaces/repository.ts +2 -1
- package/src/server/workspaces/routes.ts +4 -0
- package/src/{ai-relay → services}/app.ts +48 -44
- package/src/{ai-relay → services}/config.ts +25 -19
- package/src/services/db/index.ts +10 -0
- package/src/services/db/migrate.ts +37 -0
- package/src/{ai-relay/db/migrations/001_usage.ts → services/db/migrations/001_baseline.ts} +18 -7
- package/src/services/db/schema.ts +22 -0
- package/src/services/index.ts +18 -0
- package/src/{ai-relay → services}/metering.ts +2 -2
- package/src/{ai-relay → services}/usage.ts +43 -19
- package/dist/bin/ai-relay.mjs.map +0 -1
- package/src/ai-relay/db/index.ts +0 -10
- package/src/ai-relay/db/migrate.ts +0 -32
- package/src/ai-relay/db/schema.ts +0 -20
- package/src/ai-relay/index.ts +0 -18
- package/src/contract/ai.ts +0 -150
- package/src/server/ai/errors.ts +0 -7
- package/src/server/ai/index.ts +0 -4
- package/src/server/ai/routes.ts +0 -43
- package/src/server/auth/workspace-access.ts +0 -23
- package/src/server/db/migrations/001_foundation.ts +0 -164
- package/src/server/db/migrations/002_upload_cleanup_claims.ts +0 -34
- package/src/server/db/migrations/003_document_cleanup_claims.ts +0 -23
- package/src/server/db/migrations/004_document_sharing.ts +0 -82
- package/src/server/db/migrations/005_document_collaboration_epoch.ts +0 -13
- package/src/server/db/migrations/006_invitation_continuation.ts +0 -27
- package/src/server/db/migrations/007_collaboration_state.ts +0 -25
- package/src/server/db/migrations/008_storage_reservations.ts +0 -37
- package/src/server/db/migrations/009_workspace_entitlements.ts +0 -22
- package/src/server/db/migrations/010_upload_finalization.ts +0 -22
- package/src/server/db/migrations/011_upload_finalization_lease.ts +0 -12
- package/src/server/db/migrations/012_transactional_email.ts +0 -48
- package/src/server/db/migrations/013_admin_enrollment.ts +0 -54
- package/src/server/db/migrations/014_enrollment_email_kinds.ts +0 -30
- package/src/server/db/migrations/015_cloud_rate_limit.ts +0 -22
- package/src/server/db/migrations/016_auth_schema.ts +0 -16
- package/src/server/db/migrations/017_authentication_email_kinds.ts +0 -32
- package/src/server/db/migrations/018_authentication_mfa.ts +0 -78
- package/src/server/db/migrations/019_better_auth_account_issuer.ts +0 -42
- package/src/server/db/migrations/020_workspace_invitations.ts +0 -57
- package/src/server/db/migrations/021_document_versions.ts +0 -66
- package/src/server/db/migrations/022_entitlement_external_version.ts +0 -17
- package/src/server/db/migrations/023_document_images.ts +0 -61
- package/src/server/db/migrations/024_account_activity.ts +0 -66
- package/src/server/db/migrations/025_table_names.ts +0 -58
- package/src/server/db/migrations/026_user_picture.ts +0 -30
- package/src/server/db/migrations/027_document_kind.ts +0 -19
- package/src/server/db/migrations/028_collaboration_updates.ts +0 -84
- package/src/server/db/migrations/helpers.ts +0 -78
- /package/dist/bin/{ai-relay.d.mts → services.d.mts} +0 -0
|
@@ -10,7 +10,9 @@ import type { CloudDatabase } from '#cloud/server/db'
|
|
|
10
10
|
import { DocumentNotFoundError } from '#cloud/server/documents/errors'
|
|
11
11
|
import { CLOUD_FEATURE_KEYS } from '#cloud/server/policy/keys'
|
|
12
12
|
import type { CloudPolicy } from '#cloud/server/policy/policy'
|
|
13
|
+
import { workspacePolicyContext } from '#cloud/server/policy/workspace'
|
|
13
14
|
import type { Kysely, UpdateObject } from 'kysely'
|
|
15
|
+
import * as v from 'valibot'
|
|
14
16
|
|
|
15
17
|
import { randomHex } from '@open-pencil/scene-graph/random'
|
|
16
18
|
|
|
@@ -22,6 +24,27 @@ import { createSharingPolicy } from '../policy'
|
|
|
22
24
|
|
|
23
25
|
const SHARE_SECRET_BYTES = 24
|
|
24
26
|
|
|
27
|
+
/** An account ID as the user table keys it; other IDs in `createdBy` name no account. */
|
|
28
|
+
const accountIdSchema = v.pipe(v.string(), v.uuid())
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Whether any of these people is banned, for a ban that has not run out. A link stops working
|
|
32
|
+
* while whoever made it, or made its document, is banned, so what a banned person put up is no
|
|
33
|
+
* longer public. The IDs are matched as the user table keys them, so the lookup uses its index.
|
|
34
|
+
*/
|
|
35
|
+
async function anyBanned(database: Kysely<CloudDatabase>, userIds: string[]): Promise<boolean> {
|
|
36
|
+
const accounts = userIds.filter((id) => v.is(accountIdSchema, id))
|
|
37
|
+
if (accounts.length === 0) return false
|
|
38
|
+
const banned = await database
|
|
39
|
+
.selectFrom('user')
|
|
40
|
+
.select('id')
|
|
41
|
+
.where('id', 'in', accounts)
|
|
42
|
+
.where('banned', '=', true)
|
|
43
|
+
.where((user) => user.or([user('banExpires', 'is', null), user('banExpires', '>', new Date())]))
|
|
44
|
+
.executeTakeFirst()
|
|
45
|
+
return banned !== undefined
|
|
46
|
+
}
|
|
47
|
+
|
|
25
48
|
export type DocumentShareCapability = {
|
|
26
49
|
share: DocumentShare
|
|
27
50
|
secret: string
|
|
@@ -34,7 +57,10 @@ export type ResolvedSharePrincipal =
|
|
|
34
57
|
|
|
35
58
|
export type ResolvedDocumentShare = {
|
|
36
59
|
documentId: string
|
|
60
|
+
/** What this person may do: the link's permission, or view for a guest who may not edit. */
|
|
37
61
|
permission: DocumentPermission
|
|
62
|
+
/** What the link lets someone signed in to the server do. */
|
|
63
|
+
linkPermission: DocumentPermission
|
|
38
64
|
principal: ResolvedSharePrincipal
|
|
39
65
|
roomEpoch: number
|
|
40
66
|
}
|
|
@@ -50,6 +76,33 @@ export function createCapabilityService(
|
|
|
50
76
|
) {
|
|
51
77
|
const sharingPolicy = createSharingPolicy(database, options)
|
|
52
78
|
|
|
79
|
+
/**
|
|
80
|
+
* What a guest may do through a link: edit only where guests may edit, otherwise view where
|
|
81
|
+
* guests may view, so an edit link still shows the document to someone not signed in. `null`
|
|
82
|
+
* means the guest signs in first. Without a policy, guests get the link's own permission.
|
|
83
|
+
*/
|
|
84
|
+
async function guestPermission(
|
|
85
|
+
documentId: string,
|
|
86
|
+
linkPermission: DocumentPermission
|
|
87
|
+
): Promise<DocumentPermission | null> {
|
|
88
|
+
const policy = options.policy
|
|
89
|
+
if (!policy) return linkPermission
|
|
90
|
+
// A workspace takes its owner's plan.
|
|
91
|
+
const context = await workspacePolicyContext(
|
|
92
|
+
database,
|
|
93
|
+
{ documentId },
|
|
94
|
+
{ deploymentMode: options.deploymentMode ?? 'self-hosted' }
|
|
95
|
+
)
|
|
96
|
+
if (!context) return null
|
|
97
|
+
if (
|
|
98
|
+
linkPermission === 'edit' &&
|
|
99
|
+
(await policy.boolean(CLOUD_FEATURE_KEYS.guestEdit, false, context))
|
|
100
|
+
) {
|
|
101
|
+
return 'edit'
|
|
102
|
+
}
|
|
103
|
+
return (await policy.boolean(CLOUD_FEATURE_KEYS.guestView, false, context)) ? 'view' : null
|
|
104
|
+
}
|
|
105
|
+
|
|
53
106
|
async function updateActiveShare(
|
|
54
107
|
userId: string,
|
|
55
108
|
documentId: string,
|
|
@@ -205,38 +258,30 @@ export function createCapabilityService(
|
|
|
205
258
|
'documentShare.permission',
|
|
206
259
|
'documentShare.secretHash',
|
|
207
260
|
'documentShare.roomEpoch',
|
|
208
|
-
'documentShare.expiresAt'
|
|
261
|
+
'documentShare.expiresAt',
|
|
262
|
+
'documentShare.createdBy as linkMaker',
|
|
263
|
+
'document.createdBy as documentMaker'
|
|
209
264
|
])
|
|
210
265
|
.where('documentShare.id', '=', shareId)
|
|
211
266
|
.where('documentShare.revokedAt', 'is', null)
|
|
212
267
|
.where('document.deletedAt', 'is', null)
|
|
268
|
+
// A document an administrator took down opens for nobody.
|
|
269
|
+
.where('document.disabledAt', 'is', null)
|
|
213
270
|
.executeTakeFirst()
|
|
214
271
|
if (
|
|
215
272
|
!row ||
|
|
216
273
|
!capabilityHashMatches(row.secretHash, input.secret) ||
|
|
217
|
-
(row.expiresAt && new Date(row.expiresAt).getTime() <= Date.now())
|
|
274
|
+
(row.expiresAt && new Date(row.expiresAt).getTime() <= Date.now()) ||
|
|
275
|
+
(await anyBanned(database, [row.linkMaker, row.documentMaker]))
|
|
218
276
|
) {
|
|
219
277
|
throw new DocumentShareInvalidError()
|
|
220
278
|
}
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
const featureKey =
|
|
228
|
-
row.permission === 'edit' ? CLOUD_FEATURE_KEYS.guestEdit : CLOUD_FEATURE_KEYS.guestView
|
|
229
|
-
if (
|
|
230
|
-
!(await options.policy.boolean(featureKey, false, {
|
|
231
|
-
targetingKey: document.workspaceId,
|
|
232
|
-
workspaceId: document.workspaceId,
|
|
233
|
-
documentId: row.documentId,
|
|
234
|
-
deploymentMode: options.deploymentMode ?? 'self-hosted'
|
|
235
|
-
}))
|
|
236
|
-
) {
|
|
237
|
-
// Guests are not let in here; people signed in to the server are.
|
|
238
|
-
throw new DocumentShareSignInRequiredError()
|
|
239
|
-
}
|
|
279
|
+
const permission = actor
|
|
280
|
+
? row.permission
|
|
281
|
+
: await guestPermission(row.documentId, row.permission)
|
|
282
|
+
if (!permission) {
|
|
283
|
+
// Guests are not let in here; people signed in to the server are.
|
|
284
|
+
throw new DocumentShareSignInRequiredError()
|
|
240
285
|
}
|
|
241
286
|
await database
|
|
242
287
|
.updateTable('documentShare')
|
|
@@ -245,7 +290,8 @@ export function createCapabilityService(
|
|
|
245
290
|
.execute()
|
|
246
291
|
return {
|
|
247
292
|
documentId: row.documentId,
|
|
248
|
-
permission
|
|
293
|
+
permission,
|
|
294
|
+
linkPermission: row.permission,
|
|
249
295
|
principal: actor
|
|
250
296
|
? { kind: 'user', userId: actor.userId, name: actor.name, email: actor.email }
|
|
251
297
|
: {
|
|
@@ -20,6 +20,10 @@ export function createGrantService(database: Kysely<CloudDatabase>) {
|
|
|
20
20
|
return rows.map(grantContract)
|
|
21
21
|
},
|
|
22
22
|
|
|
23
|
+
/**
|
|
24
|
+
* Changes the permission of someone who already has access. Access itself is only given by an
|
|
25
|
+
* invitation the person accepts, so nobody can put a document in a stranger's files.
|
|
26
|
+
*/
|
|
23
27
|
async putGrant(
|
|
24
28
|
userId: string,
|
|
25
29
|
documentId: string,
|
|
@@ -28,23 +32,11 @@ export function createGrantService(database: Kysely<CloudDatabase>) {
|
|
|
28
32
|
): Promise<DocumentGrant> {
|
|
29
33
|
await requireSharingAccess(database, userId, documentId)
|
|
30
34
|
const row = await database
|
|
31
|
-
.
|
|
32
|
-
.
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
permission: input.permission,
|
|
37
|
-
createdBy: userId,
|
|
38
|
-
revokedAt: null
|
|
39
|
-
})
|
|
40
|
-
.onConflict((conflict) =>
|
|
41
|
-
conflict.columns(['documentId', 'userId']).doUpdateSet({
|
|
42
|
-
permission: input.permission,
|
|
43
|
-
createdBy: userId,
|
|
44
|
-
revokedAt: null,
|
|
45
|
-
updatedAt: new Date()
|
|
46
|
-
})
|
|
47
|
-
)
|
|
35
|
+
.updateTable('documentGrant')
|
|
36
|
+
.set({ permission: input.permission, createdBy: userId, updatedAt: new Date() })
|
|
37
|
+
.where('documentId', '=', documentId)
|
|
38
|
+
.where('userId', '=', targetUserId)
|
|
39
|
+
.where('revokedAt', 'is', null)
|
|
48
40
|
.returning([
|
|
49
41
|
'id',
|
|
50
42
|
'documentId',
|
|
@@ -54,7 +46,8 @@ export function createGrantService(database: Kysely<CloudDatabase>) {
|
|
|
54
46
|
'createdAt',
|
|
55
47
|
'updatedAt'
|
|
56
48
|
])
|
|
57
|
-
.
|
|
49
|
+
.executeTakeFirst()
|
|
50
|
+
if (!row) throw new DocumentNotFoundError()
|
|
58
51
|
return grantContract(row)
|
|
59
52
|
},
|
|
60
53
|
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { DocumentForbiddenError, DocumentNotFoundError } from '#cloud/server/documents'
|
|
2
|
+
import { InvitationLimitError } from '#cloud/server/invitations'
|
|
2
3
|
import {
|
|
3
4
|
DocumentShareInvalidError,
|
|
4
5
|
DocumentShareSignInRequiredError,
|
|
@@ -21,6 +22,9 @@ export function sharingDomainError(context: Context, error: unknown): Response |
|
|
|
21
22
|
if (error instanceof InvitationDeliveryError) {
|
|
22
23
|
return context.json({ error: { code: 'invitation_delivery_failed' as const } }, 502)
|
|
23
24
|
}
|
|
25
|
+
if (error instanceof InvitationLimitError) {
|
|
26
|
+
return context.json({ error: { code: 'rate_limited' as const } }, 429)
|
|
27
|
+
}
|
|
24
28
|
if (error instanceof DocumentForbiddenError) {
|
|
25
29
|
return context.json({ error: { code: 'forbidden' as const } }, 403)
|
|
26
30
|
}
|
|
@@ -11,7 +11,11 @@ import { cloudInvitationURL } from '#cloud/contract'
|
|
|
11
11
|
import type { CloudActor } from '#cloud/server/auth'
|
|
12
12
|
import type { CloudDatabase } from '#cloud/server/db'
|
|
13
13
|
import { DocumentNotFoundError } from '#cloud/server/documents/errors'
|
|
14
|
-
import
|
|
14
|
+
import {
|
|
15
|
+
spendInvitationBudget,
|
|
16
|
+
type InvitationDelivery,
|
|
17
|
+
type InvitationOutbox
|
|
18
|
+
} from '#cloud/server/invitations'
|
|
15
19
|
import type { Kysely } from 'kysely'
|
|
16
20
|
|
|
17
21
|
import { randomHex } from '@open-pencil/scene-graph/random'
|
|
@@ -70,6 +74,8 @@ export function createInvitationService(
|
|
|
70
74
|
'document.name as documentName'
|
|
71
75
|
])
|
|
72
76
|
.where('documentInvitation.id', '=', invitationId)
|
|
77
|
+
// A taken-down document's invitations name nothing until it is restored.
|
|
78
|
+
.where('document.disabledAt', 'is', null)
|
|
73
79
|
.executeTakeFirst()
|
|
74
80
|
if (!invitation || !invitationTokenIsActive(invitation, input.token)) {
|
|
75
81
|
throw new DocumentShareInvalidError()
|
|
@@ -161,6 +167,7 @@ export function createInvitationService(
|
|
|
161
167
|
}
|
|
162
168
|
: null
|
|
163
169
|
const row = await database.transaction().execute(async (transaction) => {
|
|
170
|
+
await spendInvitationBudget(transaction, { senderId: userId, email: input.email })
|
|
164
171
|
const inserted = await transaction
|
|
165
172
|
.insertInto('documentInvitation')
|
|
166
173
|
.values({
|
|
@@ -234,18 +241,21 @@ export function createInvitationService(
|
|
|
234
241
|
if (workspace) return { kind: 'workspace', ...workspace }
|
|
235
242
|
const invitation = await database
|
|
236
243
|
.selectFrom('documentInvitation')
|
|
244
|
+
.innerJoin('document', 'document.id', 'documentInvitation.documentId')
|
|
237
245
|
.select([
|
|
238
|
-
'id',
|
|
239
|
-
'documentId',
|
|
240
|
-
'emailNormalized',
|
|
241
|
-
'permission',
|
|
242
|
-
'invitedBy',
|
|
243
|
-
'tokenHash',
|
|
244
|
-
'expiresAt',
|
|
245
|
-
'acceptedAt',
|
|
246
|
-
'revokedAt'
|
|
246
|
+
'documentInvitation.id',
|
|
247
|
+
'documentInvitation.documentId',
|
|
248
|
+
'documentInvitation.emailNormalized',
|
|
249
|
+
'documentInvitation.permission',
|
|
250
|
+
'documentInvitation.invitedBy',
|
|
251
|
+
'documentInvitation.tokenHash',
|
|
252
|
+
'documentInvitation.expiresAt',
|
|
253
|
+
'documentInvitation.acceptedAt',
|
|
254
|
+
'documentInvitation.revokedAt'
|
|
247
255
|
])
|
|
248
|
-
.where('id', '=', invitationId)
|
|
256
|
+
.where('documentInvitation.id', '=', invitationId)
|
|
257
|
+
// Refused, not consumed, while the document is taken down, so it works again on restore.
|
|
258
|
+
.where('document.disabledAt', 'is', null)
|
|
249
259
|
.executeTakeFirst()
|
|
250
260
|
if (
|
|
251
261
|
!invitation ||
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { CloudDatabase } from '#cloud/server/db'
|
|
2
|
-
import { DocumentForbiddenError } from '#cloud/server/documents/errors'
|
|
3
|
-
import { CLOUD_FEATURE_KEYS, type CloudPolicy
|
|
2
|
+
import { DocumentForbiddenError, DocumentNotFoundError } from '#cloud/server/documents/errors'
|
|
3
|
+
import { CLOUD_FEATURE_KEYS, type CloudPolicy } from '#cloud/server/policy'
|
|
4
|
+
import { workspacePolicyContext } from '#cloud/server/policy/workspace'
|
|
4
5
|
import type { Kysely } from 'kysely'
|
|
5
6
|
|
|
6
7
|
export type SharingPolicyOptions = {
|
|
@@ -19,18 +20,12 @@ export function createSharingPolicy(
|
|
|
19
20
|
) {
|
|
20
21
|
async function requireCapabilityLink(input: CapabilityLinkPolicyInput): Promise<void> {
|
|
21
22
|
if (!options.policy) return
|
|
22
|
-
const
|
|
23
|
-
|
|
24
|
-
.
|
|
25
|
-
.
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
targetingKey: document.workspaceId,
|
|
29
|
-
actorId: input.actorId,
|
|
30
|
-
workspaceId: document.workspaceId,
|
|
31
|
-
documentId: input.documentId,
|
|
32
|
-
deploymentMode: options.deploymentMode ?? 'self-hosted'
|
|
33
|
-
}
|
|
23
|
+
const context = await workspacePolicyContext(
|
|
24
|
+
database,
|
|
25
|
+
{ documentId: input.documentId },
|
|
26
|
+
{ actorId: input.actorId, deploymentMode: options.deploymentMode ?? 'self-hosted' }
|
|
27
|
+
)
|
|
28
|
+
if (!context) throw new DocumentNotFoundError()
|
|
34
29
|
// Whether guests may follow the link is checked when they do, so edit links still work for
|
|
35
30
|
// people signed in to the server.
|
|
36
31
|
if (!(await options.policy.boolean(CLOUD_FEATURE_KEYS.links, false, context))) {
|
|
@@ -2,7 +2,6 @@ import {
|
|
|
2
2
|
parseAcceptDocumentInvitation,
|
|
3
3
|
parseCreateInvitationContinuation,
|
|
4
4
|
parseCreateDocumentInvitation,
|
|
5
|
-
parseLookupCloudUser,
|
|
6
5
|
parseCreateDocumentShare,
|
|
7
6
|
parsePutDocumentGrant,
|
|
8
7
|
parseResolveDocumentShare,
|
|
@@ -54,21 +53,6 @@ export function createDocumentSharingRoutes(
|
|
|
54
53
|
)
|
|
55
54
|
}
|
|
56
55
|
return router
|
|
57
|
-
.post(
|
|
58
|
-
'/documents/:documentId/users/lookup',
|
|
59
|
-
documentParams,
|
|
60
|
-
validatedJSON(parseLookupCloudUser),
|
|
61
|
-
(context) =>
|
|
62
|
-
sharingRoute(context, async () =>
|
|
63
|
-
context.json({
|
|
64
|
-
user: await service.lookupUser(
|
|
65
|
-
context.get('actor').userId,
|
|
66
|
-
context.req.valid('param').documentId,
|
|
67
|
-
context.req.valid('json')
|
|
68
|
-
)
|
|
69
|
-
})
|
|
70
|
-
)
|
|
71
|
-
)
|
|
72
56
|
.get('/documents/:documentId/users/:userId', documentUserParams, (context) =>
|
|
73
57
|
sharingRoute(context, async () =>
|
|
74
58
|
context.json({
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { CloudUserProfile
|
|
1
|
+
import type { CloudUserProfile } from '#cloud/contract'
|
|
2
2
|
import type { CloudDatabase } from '#cloud/server/db'
|
|
3
3
|
import type { InvitationDelivery, InvitationOutbox } from '#cloud/server/invitations'
|
|
4
4
|
import type { CloudPolicy } from '#cloud/server/policy/policy'
|
|
@@ -35,22 +35,6 @@ export function createDocumentSharingService(
|
|
|
35
35
|
const invitations = createInvitationService(database, options)
|
|
36
36
|
|
|
37
37
|
return {
|
|
38
|
-
async lookupUser(
|
|
39
|
-
userId: string,
|
|
40
|
-
documentId: string,
|
|
41
|
-
input: LookupCloudUserInput
|
|
42
|
-
): Promise<CloudUserProfile | null> {
|
|
43
|
-
await requireSharingAccess(database, userId, documentId)
|
|
44
|
-
const user = await database
|
|
45
|
-
.selectFrom('user')
|
|
46
|
-
.select(['id', 'name', 'email', 'image'])
|
|
47
|
-
.where((expression) =>
|
|
48
|
-
expression(expression.fn('lower', [expression.ref('email')]), '=', input.email)
|
|
49
|
-
)
|
|
50
|
-
.executeTakeFirst()
|
|
51
|
-
return user ?? null
|
|
52
|
-
},
|
|
53
|
-
|
|
54
38
|
async userProfile(
|
|
55
39
|
userId: string,
|
|
56
40
|
documentId: string,
|
package/src/server/validation.ts
CHANGED
|
@@ -66,6 +66,7 @@ export const workspaceInvitationParams = validatedParams(
|
|
|
66
66
|
export const workspaceMemberParams = validatedParams(
|
|
67
67
|
v.object({ workspaceId: uuidParamSchema, userId: textParamSchema })
|
|
68
68
|
)
|
|
69
|
+
export const accountParams = validatedParams(v.object({ userId: uuidParamSchema }))
|
|
69
70
|
export const shareParams = validatedParams(v.object({ shareId: uuidParamSchema }))
|
|
70
71
|
export const invitationParams = validatedParams(v.object({ invitationId: uuidParamSchema }))
|
|
71
72
|
export const invitationContinuationParams = validatedParams(
|
|
@@ -9,8 +9,11 @@ import type {
|
|
|
9
9
|
} from '#cloud/contract'
|
|
10
10
|
import { cloudInvitationURL } from '#cloud/contract'
|
|
11
11
|
import type { CloudActor } from '#cloud/server/auth'
|
|
12
|
+
import { workspaceRoleCan, type AccessRequest } from '#cloud/server/auth/access-control'
|
|
12
13
|
import type { CloudDatabase } from '#cloud/server/db'
|
|
13
14
|
import type { TransactionalEmailService } from '#cloud/server/email/types'
|
|
15
|
+
import { spendInvitationBudget } from '#cloud/server/invitations'
|
|
16
|
+
import { lockStorageAllowance } from '#cloud/server/quota/storage'
|
|
14
17
|
import {
|
|
15
18
|
invitationMatchesActor,
|
|
16
19
|
invitationTokenIsActive,
|
|
@@ -19,7 +22,7 @@ import {
|
|
|
19
22
|
import { dateString } from '#cloud/server/sharing/contracts'
|
|
20
23
|
import { hashCapability } from '#cloud/server/sharing/crypto'
|
|
21
24
|
import { findWorkspace, type WorkspaceDatabase } from '#cloud/server/workspaces/repository'
|
|
22
|
-
import type { Kysely } from 'kysely'
|
|
25
|
+
import type { Kysely, Transaction } from 'kysely'
|
|
23
26
|
|
|
24
27
|
import { randomHex } from '@open-pencil/scene-graph/random'
|
|
25
28
|
|
|
@@ -101,6 +104,45 @@ function invitationContract(row: {
|
|
|
101
104
|
}
|
|
102
105
|
}
|
|
103
106
|
|
|
107
|
+
/**
|
|
108
|
+
* Passes the workspace to its longest-standing other admin when its owner leaves or stops being
|
|
109
|
+
* an admin, so its limits come from someone still running it. The last-admin guard keeps one.
|
|
110
|
+
*/
|
|
111
|
+
export async function handOverOwnership(
|
|
112
|
+
database: Transaction<CloudDatabase>,
|
|
113
|
+
workspaceId: string,
|
|
114
|
+
leavingUserId: string
|
|
115
|
+
): Promise<void> {
|
|
116
|
+
const workspace = await database
|
|
117
|
+
.selectFrom('workspace')
|
|
118
|
+
.select('ownerId')
|
|
119
|
+
.where('id', '=', workspaceId)
|
|
120
|
+
// Changing the owner changes no key, so a reservation's foreign-key check on this row is
|
|
121
|
+
// never kept waiting behind it while the transfer waits for that reservation's lock.
|
|
122
|
+
.forNoKeyUpdate()
|
|
123
|
+
.executeTakeFirstOrThrow()
|
|
124
|
+
if (workspace.ownerId !== leavingUserId) return
|
|
125
|
+
const successor = await database
|
|
126
|
+
.selectFrom('workspaceMember')
|
|
127
|
+
.select('userId')
|
|
128
|
+
.where('workspaceId', '=', workspaceId)
|
|
129
|
+
.where('role', '=', 'admin')
|
|
130
|
+
.where('userId', '!=', leavingUserId)
|
|
131
|
+
.orderBy('createdAt')
|
|
132
|
+
.orderBy('userId')
|
|
133
|
+
.executeTakeFirst()
|
|
134
|
+
if (!successor) return
|
|
135
|
+
// Reservations count this workspace under its owner's lock: hold both owners', in one order.
|
|
136
|
+
for (const owner of [leavingUserId, successor.userId].toSorted()) {
|
|
137
|
+
await lockStorageAllowance(database, owner)
|
|
138
|
+
}
|
|
139
|
+
await database
|
|
140
|
+
.updateTable('workspace')
|
|
141
|
+
.set({ ownerId: successor.userId, updatedAt: new Date() })
|
|
142
|
+
.where('id', '=', workspaceId)
|
|
143
|
+
.execute()
|
|
144
|
+
}
|
|
145
|
+
|
|
104
146
|
async function adminCount(database: WorkspaceDatabase, workspaceId: string): Promise<number> {
|
|
105
147
|
const rows = await database
|
|
106
148
|
.selectFrom('workspaceMember')
|
|
@@ -143,9 +185,13 @@ export function createWorkspaceMemberService(
|
|
|
143
185
|
return workspace
|
|
144
186
|
}
|
|
145
187
|
|
|
146
|
-
async function
|
|
188
|
+
async function requireAccess(
|
|
189
|
+
userId: string,
|
|
190
|
+
workspaceId: string,
|
|
191
|
+
request: AccessRequest
|
|
192
|
+
): Promise<WorkspaceSummary> {
|
|
147
193
|
const workspace = await membership(userId, workspaceId)
|
|
148
|
-
if (workspace.role
|
|
194
|
+
if (!workspaceRoleCan(workspace.role, request)) throw new WorkspaceForbiddenError()
|
|
149
195
|
return workspace
|
|
150
196
|
}
|
|
151
197
|
|
|
@@ -175,7 +221,7 @@ export function createWorkspaceMemberService(
|
|
|
175
221
|
workspaceId: string,
|
|
176
222
|
input: CreateWorkspaceInvitationInput
|
|
177
223
|
): Promise<CreatedWorkspaceInvitation> {
|
|
178
|
-
const workspace = await
|
|
224
|
+
const workspace = await requireAccess(userId, workspaceId, { workspace: ['invite'] })
|
|
179
225
|
const existing = await memberUsers(workspaceId)
|
|
180
226
|
.select('workspaceMember.userId')
|
|
181
227
|
.where((expression) =>
|
|
@@ -193,6 +239,7 @@ export function createWorkspaceMemberService(
|
|
|
193
239
|
const expiresAt = new Date(Date.now() + INVITATION_LIFETIME_MS)
|
|
194
240
|
const url = acceptanceURL(id, token)
|
|
195
241
|
const row = await database.transaction().execute(async (transaction) => {
|
|
242
|
+
await spendInvitationBudget(transaction, { senderId: userId, email: input.email })
|
|
196
243
|
await transaction
|
|
197
244
|
.updateTable('workspaceInvitation')
|
|
198
245
|
.set({ revokedAt: new Date() })
|
|
@@ -241,7 +288,7 @@ export function createWorkspaceMemberService(
|
|
|
241
288
|
|
|
242
289
|
return {
|
|
243
290
|
async rename(userId: string, workspaceId: string, name: string): Promise<WorkspaceSummary> {
|
|
244
|
-
await
|
|
291
|
+
await requireAccess(userId, workspaceId, { workspace: ['update'] })
|
|
245
292
|
await database
|
|
246
293
|
.updateTable('workspace')
|
|
247
294
|
.set({ name, updatedAt: new Date() })
|
|
@@ -263,35 +310,34 @@ export function createWorkspaceMemberService(
|
|
|
263
310
|
])
|
|
264
311
|
.orderBy('workspaceMember.createdAt')
|
|
265
312
|
.execute()
|
|
266
|
-
const invitations =
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
.
|
|
271
|
-
|
|
272
|
-
expression(
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
expression.ref('workspaceInvitation.invitedBy')
|
|
276
|
-
)
|
|
313
|
+
const invitations = workspaceRoleCan(workspace.role, { workspace: ['invite'] })
|
|
314
|
+
? await database
|
|
315
|
+
.selectFrom('workspaceInvitation')
|
|
316
|
+
.leftJoin('user', (join) =>
|
|
317
|
+
join.on((expression) =>
|
|
318
|
+
expression(
|
|
319
|
+
expression.cast(expression.ref('user.id'), 'text'),
|
|
320
|
+
'=',
|
|
321
|
+
expression.ref('workspaceInvitation.invitedBy')
|
|
277
322
|
)
|
|
278
323
|
)
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
324
|
+
)
|
|
325
|
+
.select([
|
|
326
|
+
'workspaceInvitation.id',
|
|
327
|
+
'workspaceInvitation.emailNormalized',
|
|
328
|
+
'workspaceInvitation.role',
|
|
329
|
+
'workspaceInvitation.invitedAt',
|
|
330
|
+
'workspaceInvitation.expiresAt',
|
|
331
|
+
'workspaceInvitation.invitedBy',
|
|
332
|
+
'user.name as inviterName'
|
|
333
|
+
])
|
|
334
|
+
.where('workspaceInvitation.workspaceId', '=', workspaceId)
|
|
335
|
+
.where('workspaceInvitation.acceptedAt', 'is', null)
|
|
336
|
+
.where('workspaceInvitation.revokedAt', 'is', null)
|
|
337
|
+
.where('workspaceInvitation.expiresAt', '>', new Date())
|
|
338
|
+
.orderBy('workspaceInvitation.invitedAt')
|
|
339
|
+
.execute()
|
|
340
|
+
: []
|
|
295
341
|
return {
|
|
296
342
|
members: members.map((member) => ({
|
|
297
343
|
userId: member.userId,
|
|
@@ -313,7 +359,7 @@ export function createWorkspaceMemberService(
|
|
|
313
359
|
workspaceId: string,
|
|
314
360
|
invitationId: string
|
|
315
361
|
): Promise<CreatedWorkspaceInvitation> {
|
|
316
|
-
await
|
|
362
|
+
await requireAccess(userId, workspaceId, { workspace: ['invite'] })
|
|
317
363
|
const invitation = await database
|
|
318
364
|
.selectFrom('workspaceInvitation')
|
|
319
365
|
.select(['emailNormalized', 'role'])
|
|
@@ -334,7 +380,7 @@ export function createWorkspaceMemberService(
|
|
|
334
380
|
workspaceId: string,
|
|
335
381
|
invitationId: string
|
|
336
382
|
): Promise<void> {
|
|
337
|
-
await
|
|
383
|
+
await requireAccess(userId, workspaceId, { workspace: ['invite'] })
|
|
338
384
|
const result = await database
|
|
339
385
|
.updateTable('workspaceInvitation')
|
|
340
386
|
.set({ revokedAt: new Date() })
|
|
@@ -352,7 +398,7 @@ export function createWorkspaceMemberService(
|
|
|
352
398
|
memberId: string,
|
|
353
399
|
role: WorkspaceRole
|
|
354
400
|
): Promise<void> {
|
|
355
|
-
await
|
|
401
|
+
await requireAccess(userId, workspaceId, { workspace: ['manage-members'] })
|
|
356
402
|
await database.transaction().execute(async (transaction) => {
|
|
357
403
|
const member = await lockedMember(transaction, workspaceId, memberId)
|
|
358
404
|
if (member.role === 'admin' && role !== 'admin') {
|
|
@@ -364,12 +410,13 @@ export function createWorkspaceMemberService(
|
|
|
364
410
|
.where('workspaceId', '=', workspaceId)
|
|
365
411
|
.where('userId', '=', memberId)
|
|
366
412
|
.execute()
|
|
413
|
+
if (role !== 'admin') await handOverOwnership(transaction, workspaceId, memberId)
|
|
367
414
|
})
|
|
368
415
|
},
|
|
369
416
|
|
|
370
417
|
/** Deletes an empty workspace, with its members and invitations. */
|
|
371
418
|
async delete(userId: string, workspaceId: string): Promise<void> {
|
|
372
|
-
await
|
|
419
|
+
await requireAccess(userId, workspaceId, { workspace: ['delete'] })
|
|
373
420
|
await database.transaction().execute(async (transaction) => {
|
|
374
421
|
// Locked first, so a document created meanwhile waits and then finds no workspace.
|
|
375
422
|
await transaction
|
|
@@ -391,10 +438,13 @@ export function createWorkspaceMemberService(
|
|
|
391
438
|
})
|
|
392
439
|
},
|
|
393
440
|
|
|
394
|
-
/**
|
|
441
|
+
/**
|
|
442
|
+
* Removes a member, or lets someone leave; the last administrator cannot go, and an owner who
|
|
443
|
+
* goes leaves the workspace to the longest-standing other admin.
|
|
444
|
+
*/
|
|
395
445
|
async remove(userId: string, workspaceId: string, memberId: string): Promise<void> {
|
|
396
446
|
if (memberId === userId) await membership(userId, workspaceId)
|
|
397
|
-
else await
|
|
447
|
+
else await requireAccess(userId, workspaceId, { workspace: ['manage-members'] })
|
|
398
448
|
await database.transaction().execute(async (transaction) => {
|
|
399
449
|
const member = await lockedMember(transaction, workspaceId, memberId)
|
|
400
450
|
if (member.role === 'admin' && (await adminCount(transaction, workspaceId)) <= 1) {
|
|
@@ -405,6 +455,7 @@ export function createWorkspaceMemberService(
|
|
|
405
455
|
.where('workspaceId', '=', workspaceId)
|
|
406
456
|
.where('userId', '=', memberId)
|
|
407
457
|
.execute()
|
|
458
|
+
await handOverOwnership(transaction, workspaceId, memberId)
|
|
408
459
|
})
|
|
409
460
|
},
|
|
410
461
|
|
|
@@ -6,6 +6,7 @@ import {
|
|
|
6
6
|
} from '#cloud/contract'
|
|
7
7
|
import type { CloudActor } from '#cloud/server/auth'
|
|
8
8
|
import type { CloudDatabase } from '#cloud/server/db'
|
|
9
|
+
import { InvitationLimitError } from '#cloud/server/invitations'
|
|
9
10
|
import {
|
|
10
11
|
CLOUD_RATE_LIMITS,
|
|
11
12
|
createRouteRateLimiter,
|
|
@@ -58,6 +59,9 @@ async function memberRoute<T>(context: Context, action: () => Promise<T>): Promi
|
|
|
58
59
|
if (error instanceof WorkspaceAlreadyMemberError) {
|
|
59
60
|
return context.json({ error: { code: 'already_member' as const } }, 409)
|
|
60
61
|
}
|
|
62
|
+
if (error instanceof InvitationLimitError) {
|
|
63
|
+
return context.json({ error: { code: 'rate_limited' as const } }, 429)
|
|
64
|
+
}
|
|
61
65
|
throw error
|
|
62
66
|
}
|
|
63
67
|
}
|