@crombolo/data 0.65.1 → 0.66.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -2
- package/src/audit.ts +27 -7
- package/src/content-query.ts +21 -6
- package/src/index.ts +1 -0
- package/src/repositories/documents.ts +3 -1
- package/src/repositories/jobs.ts +14 -0
- package/src/schema/cli-tokens.ts +46 -0
- package/src/schema/plan.ts +2 -0
- package/src/schema/seed.ts +8 -1
- package/src/tables.gen.ts +12 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@crombolo/data",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.66.0",
|
|
4
4
|
"description": "Internal to crombolo, with no compatibility promise: use @crombolo/crombolo. SQLite and Postgres storage for crombolo: schema, migrations and repositories.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Crombolo",
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
"access": "public"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@crombolo/core": "0.
|
|
32
|
+
"@crombolo/core": "0.66.0",
|
|
33
33
|
"kysely": "0.29.6"
|
|
34
34
|
}
|
|
35
35
|
}
|
package/src/audit.ts
CHANGED
|
@@ -181,6 +181,8 @@ function toEntry(row: Record<string, unknown>): AuditEntry {
|
|
|
181
181
|
*/
|
|
182
182
|
export interface AuditContext {
|
|
183
183
|
viewing?: { userKey: string; groups: string }
|
|
184
|
+
/** A person acting through one of their command-line tokens (story 351), by its name. */
|
|
185
|
+
token?: { userKey: string; name: string }
|
|
184
186
|
}
|
|
185
187
|
|
|
186
188
|
const contexts = new AsyncLocalStorage<AuditContext>()
|
|
@@ -188,18 +190,36 @@ const contexts = new AsyncLocalStorage<AuditContext>()
|
|
|
188
190
|
/** Runs `work` with an audit context of its own, which it fills in as it learns who is asking. */
|
|
189
191
|
export const withAuditContext = <T>(work: () => T): T => contexts.run({}, work)
|
|
190
192
|
|
|
191
|
-
|
|
192
|
-
export const currentAuditContext = (): AuditContext | undefined => contexts.getStore()
|
|
193
|
+
let processContext: AuditContext | undefined
|
|
193
194
|
|
|
194
|
-
/**
|
|
195
|
+
/**
|
|
196
|
+
* The context for a whole process that answers no requests, such as one
|
|
197
|
+
* command (story 351); a request's own context always comes first.
|
|
198
|
+
*/
|
|
199
|
+
export const setProcessAuditContext = (context: AuditContext | undefined): void => {
|
|
200
|
+
processContext = context
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/** The context of the request being answered, or of the process; undefined outside both. */
|
|
204
|
+
export const currentAuditContext = (): AuditContext | undefined =>
|
|
205
|
+
contexts.getStore() ?? processContext
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* The detail an entry is written with: "while viewing as Editors", or "through
|
|
209
|
+
* the token deploy", added to the person's own.
|
|
210
|
+
*/
|
|
195
211
|
export function detailInContext(
|
|
196
212
|
entry: Pick<AuditEntryInput, 'userKey' | 'detail'>,
|
|
197
213
|
context = currentAuditContext(),
|
|
198
214
|
): string | null | undefined {
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
215
|
+
if (!context || !entry.userKey || !UUID.test(entry.userKey)) return entry.detail
|
|
216
|
+
const who = normaliseUuid(entry.userKey)
|
|
217
|
+
const notes = [
|
|
218
|
+
context.viewing?.userKey === who ? `while viewing as ${context.viewing.groups}` : undefined,
|
|
219
|
+
context.token?.userKey === who ? `through the token ${context.token.name}` : undefined,
|
|
220
|
+
].filter((note): note is string => note !== undefined)
|
|
221
|
+
if (notes.length === 0) return entry.detail
|
|
222
|
+
const note = notes.join(', ')
|
|
203
223
|
return entry.detail ? `${entry.detail}, ${note}` : `${note[0]?.toUpperCase()}${note.slice(1)}`
|
|
204
224
|
}
|
|
205
225
|
|
package/src/content-query.ts
CHANGED
|
@@ -274,22 +274,34 @@ interface PropertyTarget {
|
|
|
274
274
|
storage: ValueStorageTypeName
|
|
275
275
|
}
|
|
276
276
|
|
|
277
|
-
/**
|
|
277
|
+
/**
|
|
278
|
+
* The documents a query matches, as the scope may see them; with `library`, the
|
|
279
|
+
* Library's elements instead (story 361), placed under its folders.
|
|
280
|
+
*/
|
|
278
281
|
export async function queryDocuments(
|
|
279
282
|
db: Db,
|
|
280
283
|
query: ContentQuery,
|
|
281
284
|
scope: QueryScope,
|
|
285
|
+
options: { library?: boolean } = {},
|
|
282
286
|
): Promise<QueryResult> {
|
|
283
287
|
const run = async (tx: Db) => {
|
|
284
288
|
if (tx.dialect.name === 'postgres')
|
|
285
289
|
await tx.exec(`SET LOCAL statement_timeout = ${QUERY_TIMEOUT_MS}`)
|
|
286
|
-
return runQuery(tx, query, scope)
|
|
290
|
+
return runQuery(tx, query, scope, options.library === true)
|
|
287
291
|
}
|
|
288
292
|
// Postgres's timeout is per transaction; SQLite has none, so the window bounds it.
|
|
289
293
|
return db.dialect.name === 'postgres' ? db.transaction(run) : run(db)
|
|
290
294
|
}
|
|
291
295
|
|
|
292
|
-
async function runQuery(
|
|
296
|
+
async function runQuery(
|
|
297
|
+
db: Db,
|
|
298
|
+
query: ContentQuery,
|
|
299
|
+
scope: QueryScope,
|
|
300
|
+
library: boolean,
|
|
301
|
+
): Promise<QueryResult> {
|
|
302
|
+
const objectType = library ? ObjectTypes.Element : ObjectTypes.Document
|
|
303
|
+
/** What `under` and `parent` may name: a page, or an element or its folder. */
|
|
304
|
+
const places = library ? [ObjectTypes.Element, ObjectTypes.ElementContainer] : [objectType]
|
|
293
305
|
const bool = (value: boolean) => db.dialect.boolValue(value) as boolean
|
|
294
306
|
|
|
295
307
|
// ---- culture
|
|
@@ -326,7 +338,7 @@ async function runQuery(db: Db, query: ContentQuery, scope: QueryScope): Promise
|
|
|
326
338
|
join.onRef('dcv.node_id', '=', 'n.id').on('dcv.language_id', '=', languageId as number),
|
|
327
339
|
),
|
|
328
340
|
)
|
|
329
|
-
.where('n.node_object_type', '=',
|
|
341
|
+
.where('n.node_object_type', '=', objectType)
|
|
330
342
|
|
|
331
343
|
// ---- types
|
|
332
344
|
let typeIds: number[] | undefined
|
|
@@ -356,9 +368,12 @@ async function runQuery(db: Db, query: ContentQuery, scope: QueryScope): Promise
|
|
|
356
368
|
.selectFrom('node')
|
|
357
369
|
.select(['id', 'path', 'level'])
|
|
358
370
|
.where('unique_id', '=', key.toLowerCase())
|
|
359
|
-
.where('node_object_type', '
|
|
371
|
+
.where('node_object_type', 'in', places)
|
|
360
372
|
.executeTakeFirst()
|
|
361
|
-
if (!row)
|
|
373
|
+
if (!row)
|
|
374
|
+
throw new ContentQueryRefused(
|
|
375
|
+
`"${field}" names no ${library ? 'Library folder' : 'page'}: ${key}.`,
|
|
376
|
+
)
|
|
362
377
|
return { id: Number(row.id), path: String(row.path), level: Number(row.level) }
|
|
363
378
|
}
|
|
364
379
|
const under = query.under ? await nodeByKey(query.under, 'under') : undefined
|
package/src/index.ts
CHANGED
|
@@ -75,6 +75,7 @@ export * from './schema/bundles-disabled.ts'
|
|
|
75
75
|
export * from './schema/bundles-in-settings.ts'
|
|
76
76
|
export * from './schema/bundles-rename.ts'
|
|
77
77
|
export * from './schema/change-report.ts'
|
|
78
|
+
export * from './schema/cli-tokens.ts'
|
|
78
79
|
export * from './schema/client-id-prefix.ts'
|
|
79
80
|
export * from './schema/content.ts'
|
|
80
81
|
export * from './schema/content-applied.ts'
|
|
@@ -1566,7 +1566,9 @@ export class DocumentRepository {
|
|
|
1566
1566
|
if (!source) return undefined
|
|
1567
1567
|
if (targetKey) {
|
|
1568
1568
|
const target = await this.#nodes.byKey(targetKey)
|
|
1569
|
-
|
|
1569
|
+
const container = KINDS[this.kind].container
|
|
1570
|
+
if (!target || (target.objectType !== this.objectType && target.objectType !== container))
|
|
1571
|
+
return undefined
|
|
1570
1572
|
}
|
|
1571
1573
|
const copyOne = async (from: DocumentAggregate, parentKey: string | null): Promise<string> => {
|
|
1572
1574
|
const siblings = await this.#siblingNames(parentKey)
|
package/src/repositories/jobs.ts
CHANGED
|
@@ -111,6 +111,20 @@ export class JobRepository {
|
|
|
111
111
|
return row ? rowOf(row) : undefined
|
|
112
112
|
}
|
|
113
113
|
|
|
114
|
+
/** The jobs whose id starts with `prefix`, at most `take` of them, newest first. */
|
|
115
|
+
async byPrefix(prefix: string, take = 10): Promise<JobRow[]> {
|
|
116
|
+
const wanted = prefix.toLowerCase().replace(/[^0-9a-f-]/g, '')
|
|
117
|
+
if (!wanted) return []
|
|
118
|
+
const rows = await this.db.q
|
|
119
|
+
.selectFrom('job')
|
|
120
|
+
.selectAll()
|
|
121
|
+
.where((eb) => eb(eb.cast('id', 'text'), 'like', `${wanted}%`))
|
|
122
|
+
.orderBy('created_at', 'desc')
|
|
123
|
+
.limit(take)
|
|
124
|
+
.execute()
|
|
125
|
+
return rows.map(rowOf)
|
|
126
|
+
}
|
|
127
|
+
|
|
114
128
|
/** The newest first, for the administrators' view; `owner` and `state` narrow it. */
|
|
115
129
|
async list(
|
|
116
130
|
options: { owner?: string; state?: JobState; skip?: number; take?: number } = {},
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Migration 078 — command-line tokens (story 351): each one named, its
|
|
3
|
+
* person's, kept only as a hash, with when it expires, was last used and was
|
|
4
|
+
* revoked. A name is used once per person among those not revoked.
|
|
5
|
+
* Administrators are given `Crombolo.CliTokens.Create` as the seed gives it,
|
|
6
|
+
* through `grantBuiltInPermissions`, as migration 045 gave the media verbs.
|
|
7
|
+
*/
|
|
8
|
+
import type { Db } from '../database.ts'
|
|
9
|
+
import type { Migration } from '../migrations.ts'
|
|
10
|
+
import { sql } from '../query.ts'
|
|
11
|
+
import { grantBuiltInPermissions } from './seed.ts'
|
|
12
|
+
import { STATE_SESSION_VIEW_AS } from './session-view-as.ts'
|
|
13
|
+
|
|
14
|
+
export const STATE_CLI_TOKENS = '{3c8e1f57-0078-4d2a-b6e9-1a5f7c3d0078}'
|
|
15
|
+
|
|
16
|
+
export const cliTokensMigration: Migration = {
|
|
17
|
+
from: STATE_SESSION_VIEW_AS,
|
|
18
|
+
to: STATE_CLI_TOKENS,
|
|
19
|
+
name: 'AddCliTokens',
|
|
20
|
+
kind: 'expand',
|
|
21
|
+
release: '0.66.0',
|
|
22
|
+
async up(db: Db) {
|
|
23
|
+
const t = db.dialect.types
|
|
24
|
+
await db.q.schema
|
|
25
|
+
.createTable('cli_token')
|
|
26
|
+
.addColumn('id', sql.raw(t.identity))
|
|
27
|
+
.addColumn('key', sql.raw(t.uuid), (c) => c.notNull().unique())
|
|
28
|
+
.addColumn('user_id', sql.raw(t.integer), (c) => c.notNull())
|
|
29
|
+
.addColumn('name', sql.raw(t.varchar(100)), (c) => c.notNull())
|
|
30
|
+
.addColumn('token_hash', sql.raw(t.varchar(64)), (c) => c.notNull().unique())
|
|
31
|
+
.addColumn('created_at', sql.raw(t.timestamp), (c) => c.notNull())
|
|
32
|
+
.addColumn('expires_at', sql.raw(t.timestamp), (c) => c.notNull())
|
|
33
|
+
.addColumn('last_used_at', sql.raw(t.timestamp))
|
|
34
|
+
.addColumn('revoked_at', sql.raw(t.timestamp))
|
|
35
|
+
.addColumn('revoked_by', sql.raw(t.varchar(64)))
|
|
36
|
+
.execute()
|
|
37
|
+
await db.q.schema
|
|
38
|
+
.createIndex('ux_cli_token_name')
|
|
39
|
+
.unique()
|
|
40
|
+
.on('cli_token')
|
|
41
|
+
.columns(['user_id', 'name'])
|
|
42
|
+
.where(sql.ref('revoked_at'), 'is', null)
|
|
43
|
+
.execute()
|
|
44
|
+
await grantBuiltInPermissions(db)
|
|
45
|
+
},
|
|
46
|
+
}
|
package/src/schema/plan.ts
CHANGED
|
@@ -28,6 +28,7 @@ import { bundlesDisabledMigration } from './bundles-disabled.ts'
|
|
|
28
28
|
import { bundlesInSettingsMigration } from './bundles-in-settings.ts'
|
|
29
29
|
import { bundlesRenameMigration } from './bundles-rename.ts'
|
|
30
30
|
import { changeReportMigration } from './change-report.ts'
|
|
31
|
+
import { cliTokensMigration } from './cli-tokens.ts'
|
|
31
32
|
import { clientIdPrefixMigration } from './client-id-prefix.ts'
|
|
32
33
|
import { contentMigration } from './content.ts'
|
|
33
34
|
import { contentAppliedMigration } from './content-applied.ts'
|
|
@@ -169,4 +170,5 @@ export const cromboloPlan = new MigrationPlan([
|
|
|
169
170
|
prunedPermissionsMigration,
|
|
170
171
|
userStartNodesDroppedMigration,
|
|
171
172
|
sessionViewAsMigration,
|
|
173
|
+
cliTokensMigration,
|
|
172
174
|
])
|
package/src/schema/seed.ts
CHANGED
|
@@ -4,7 +4,12 @@
|
|
|
4
4
|
* Password hashing is injected rather than imported so this package stays free of
|
|
5
5
|
* a dependency on @crombolo/auth.
|
|
6
6
|
*/
|
|
7
|
-
import {
|
|
7
|
+
import {
|
|
8
|
+
CliTokenPermissions,
|
|
9
|
+
EXPLICIT_PERMISSIONS,
|
|
10
|
+
MediaPermissions,
|
|
11
|
+
RemotePermissions,
|
|
12
|
+
} from '@crombolo/core'
|
|
8
13
|
import type { Db } from '../database.ts'
|
|
9
14
|
import { DbDate } from '../dialect.ts'
|
|
10
15
|
|
|
@@ -64,6 +69,8 @@ export const BUILT_IN_GROUP_PERMISSIONS: Record<string, readonly string[]> = {
|
|
|
64
69
|
// Never one a group has to name (story 241): administrators are not given it.
|
|
65
70
|
...Object.values(RemotePermissions).filter((verb) => !EXPLICIT_PERMISSIONS.has(verb)),
|
|
66
71
|
...MEDIA_FULL,
|
|
72
|
+
// Explicit, so given by name (story 351): taking it away turns tokens off for administrators.
|
|
73
|
+
CliTokenPermissions.Create,
|
|
67
74
|
],
|
|
68
75
|
editor: [
|
|
69
76
|
'Umb.Document.Create',
|
package/src/tables.gen.ts
CHANGED
|
@@ -153,6 +153,18 @@ export interface CoreTables {
|
|
|
153
153
|
subject_name: string | null
|
|
154
154
|
subject_type: string
|
|
155
155
|
}
|
|
156
|
+
cli_token: {
|
|
157
|
+
created_at: Timestamp
|
|
158
|
+
expires_at: Timestamp
|
|
159
|
+
id: Generated<number>
|
|
160
|
+
key: string
|
|
161
|
+
last_used_at: Timestamp | null
|
|
162
|
+
name: string
|
|
163
|
+
revoked_at: Timestamp | null
|
|
164
|
+
revoked_by: string | null
|
|
165
|
+
token_hash: string
|
|
166
|
+
user_id: number
|
|
167
|
+
}
|
|
156
168
|
content: {
|
|
157
169
|
content_type_id: number
|
|
158
170
|
creator_member_key: string | null
|