@crombolo/data 0.36.2 → 0.38.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/package.json +3 -2
- package/src/audit.ts +163 -133
- package/src/change-report.ts +66 -43
- package/src/cluster.ts +119 -59
- package/src/content-query.ts +205 -200
- package/src/{bundle-tables → custom-tables}/builder.ts +104 -12
- package/src/custom-tables/chain.ts +150 -0
- package/src/custom-tables/ddl.ts +159 -0
- package/src/custom-tables/handle.ts +141 -0
- package/src/{bundle-tables → custom-tables}/index.ts +3 -2
- package/src/custom-tables/names.ts +94 -0
- package/src/custom-tables/owners.ts +299 -0
- package/src/{bundle-tables → custom-tables}/schema.ts +263 -90
- package/src/custom-tables/values.ts +181 -0
- package/src/database.ts +40 -5
- package/src/dialect.ts +43 -16
- package/src/index.ts +8 -1
- package/src/migrations.ts +130 -88
- package/src/query.ts +158 -0
- package/src/relations.ts +49 -28
- package/src/repositories/approvals.ts +130 -89
- package/src/repositories/bundle-grants.ts +74 -0
- package/src/repositories/bundles.ts +85 -76
- package/src/repositories/content-applied.ts +47 -28
- package/src/repositories/content-events.ts +114 -67
- package/src/repositories/content-notifications.ts +102 -74
- package/src/repositories/content-types.ts +449 -320
- package/src/repositories/data-types.ts +105 -56
- package/src/repositories/delivery-member-tokens.ts +37 -28
- package/src/repositories/dictionary.ts +77 -58
- package/src/repositories/documents.ts +956 -580
- package/src/repositories/domains.ts +28 -16
- package/src/repositories/dynamic-roots.ts +32 -17
- package/src/repositories/external-logins.ts +38 -24
- package/src/repositories/folders.ts +7 -4
- package/src/repositories/form-entries.ts +120 -103
- package/src/repositories/form-workflows.ts +87 -62
- package/src/repositories/jobs.ts +296 -0
- package/src/repositories/languages.ts +51 -38
- package/src/repositories/log-searches.ts +7 -5
- package/src/repositories/media-access.ts +173 -123
- package/src/repositories/media-listing.ts +155 -92
- package/src/repositories/members.ts +189 -162
- package/src/repositories/nodes.ts +227 -181
- package/src/repositories/notifications.ts +22 -17
- package/src/repositories/public-access.ts +77 -61
- package/src/repositories/redirects.ts +113 -70
- package/src/repositories/references.ts +55 -44
- package/src/repositories/remote-jobs.ts +45 -35
- package/src/repositories/tags.ts +11 -10
- package/src/repositories/templates.ts +24 -14
- package/src/repositories/transfer-answers.ts +58 -50
- package/src/repositories/transfer-runs.ts +93 -75
- package/src/repositories/two-factor.ts +74 -55
- package/src/repositories/user-data.ts +54 -36
- package/src/repositories/users.ts +345 -300
- package/src/repositories/webhooks.ts +196 -137
- package/src/schema/bundle-grants.ts +32 -0
- package/src/schema/jobs.ts +56 -0
- package/src/schema/plan.ts +4 -0
- package/src/schema-ops.ts +15 -12
- package/src/schema-state.ts +53 -38
- package/src/search.ts +175 -129
- package/src/setup.ts +17 -13
- package/src/site-search.ts +134 -83
- package/src/table-types.ts +152 -0
- package/src/tables.gen.ts +859 -0
- package/src/tree-invariants.ts +21 -12
- package/src/url-options.ts +17 -13
- package/src/workflow-store.ts +17 -13
- package/src/bundle-tables/capability.ts +0 -377
- package/src/bundle-tables/chain.ts +0 -105
- package/src/bundle-tables/ddl.ts +0 -124
- package/src/bundle-tables/guard.ts +0 -116
- package/src/bundle-tables/names.ts +0 -58
|
@@ -127,13 +127,42 @@ export interface UserAccessData {
|
|
|
127
127
|
userStartNodes: Record<StartNodeTree, StartNodePath[]>
|
|
128
128
|
}
|
|
129
129
|
|
|
130
|
-
const USER_COLUMNS =
|
|
131
|
-
|
|
132
|
-
|
|
130
|
+
const USER_COLUMNS = [
|
|
131
|
+
'id',
|
|
132
|
+
'key',
|
|
133
|
+
'user_name',
|
|
134
|
+
'login',
|
|
135
|
+
'email',
|
|
136
|
+
'language',
|
|
137
|
+
'avatar',
|
|
138
|
+
'kind',
|
|
139
|
+
'is_approved',
|
|
140
|
+
'is_locked_out',
|
|
141
|
+
'failed_login_attempts',
|
|
142
|
+
'last_login_date',
|
|
143
|
+
'last_lockout_date',
|
|
144
|
+
'last_password_change_date',
|
|
145
|
+
'invited_date',
|
|
146
|
+
'create_date',
|
|
147
|
+
'update_date',
|
|
148
|
+
] as const
|
|
149
|
+
|
|
150
|
+
const NODE_COLUMNS = [
|
|
151
|
+
'id',
|
|
152
|
+
'unique_id',
|
|
153
|
+
'parent_id',
|
|
154
|
+
'level',
|
|
155
|
+
'path',
|
|
156
|
+
'sort_order',
|
|
157
|
+
'trashed',
|
|
158
|
+
'text',
|
|
159
|
+
'node_object_type',
|
|
160
|
+
'create_date',
|
|
161
|
+
] as const
|
|
133
162
|
|
|
134
163
|
const TREES: StartNodeTree[] = ['document', 'media', 'element']
|
|
135
164
|
|
|
136
|
-
const
|
|
165
|
+
const users = (db: Db) => db.q.selectFrom('user_account').select(USER_COLUMNS)
|
|
137
166
|
|
|
138
167
|
export class UserRepository {
|
|
139
168
|
#db: Db
|
|
@@ -145,11 +174,11 @@ export class UserRepository {
|
|
|
145
174
|
async #nodeKeys(ids: readonly number[]): Promise<Map<number, NodeRow>> {
|
|
146
175
|
const wanted = [...new Set(ids.filter((id) => id > 0))]
|
|
147
176
|
if (wanted.length === 0) return new Map()
|
|
148
|
-
const rows = await this.#db.
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
wanted
|
|
152
|
-
|
|
177
|
+
const rows = await this.#db.q
|
|
178
|
+
.selectFrom('node')
|
|
179
|
+
.select(NODE_COLUMNS)
|
|
180
|
+
.where('id', 'in', wanted)
|
|
181
|
+
.execute()
|
|
153
182
|
return new Map(rows.map((row) => [Number(row.id), mapNode(row)]))
|
|
154
183
|
}
|
|
155
184
|
|
|
@@ -157,10 +186,11 @@ export class UserRepository {
|
|
|
157
186
|
async nodeIds(keys: readonly string[]): Promise<Map<string, number> | undefined> {
|
|
158
187
|
if (keys.length === 0) return new Map()
|
|
159
188
|
const wanted = keys.map(normaliseUuid)
|
|
160
|
-
const rows = await this.#db.
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
189
|
+
const rows = await this.#db.q
|
|
190
|
+
.selectFrom('node')
|
|
191
|
+
.select(['id', 'unique_id'])
|
|
192
|
+
.where('unique_id', 'in', wanted)
|
|
193
|
+
.execute()
|
|
164
194
|
const map = new Map(rows.map((r) => [normaliseUuid(String(r.unique_id)), Number(r.id)]))
|
|
165
195
|
return wanted.every((k) => map.has(k)) ? map : undefined
|
|
166
196
|
}
|
|
@@ -168,19 +198,18 @@ export class UserRepository {
|
|
|
168
198
|
async #hydrate(rows: Record<string, unknown>[]): Promise<UserRecord[]> {
|
|
169
199
|
if (rows.length === 0) return []
|
|
170
200
|
const ids = rows.map((r) => Number(r.id))
|
|
171
|
-
const memberships = await this.#db.
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
)
|
|
201
|
+
const memberships = await this.#db.q
|
|
202
|
+
.selectFrom('user_group_member as m')
|
|
203
|
+
.innerJoin('user_group as g', 'g.id', 'm.user_group_id')
|
|
204
|
+
.select(['m.user_id', 'g.key'])
|
|
205
|
+
.where('m.user_id', 'in', ids)
|
|
206
|
+
.orderBy('g.id')
|
|
207
|
+
.execute()
|
|
208
|
+
const starts = await this.#db.q
|
|
209
|
+
.selectFrom('user_start_node')
|
|
210
|
+
.select(['user_id', 'start_node', 'start_node_type'])
|
|
211
|
+
.where('user_id', 'in', ids)
|
|
212
|
+
.execute()
|
|
184
213
|
const nodes = await this.#nodeKeys(starts.map((s) => Number(s.start_node)))
|
|
185
214
|
return rows.map((row) => {
|
|
186
215
|
const id = Number(row.id)
|
|
@@ -228,20 +257,13 @@ export class UserRepository {
|
|
|
228
257
|
}
|
|
229
258
|
|
|
230
259
|
async all(): Promise<UserRecord[]> {
|
|
231
|
-
return this.#hydrate(
|
|
232
|
-
await this.#db.query(`SELECT ${USER_COLUMNS} FROM user_account ORDER BY id`),
|
|
233
|
-
)
|
|
260
|
+
return this.#hydrate(await users(this.#db).orderBy('id').execute())
|
|
234
261
|
}
|
|
235
262
|
|
|
236
263
|
async byKeys(keys: readonly string[]): Promise<UserRecord[]> {
|
|
237
264
|
const wanted = keys.filter((k) => /^[0-9a-f-]{32,36}$/i.test(k)).map(normaliseUuid)
|
|
238
265
|
if (wanted.length === 0) return []
|
|
239
|
-
return this.#hydrate(
|
|
240
|
-
await this.#db.query(
|
|
241
|
-
`SELECT ${USER_COLUMNS} FROM user_account WHERE key IN (${inList(wanted)}) ORDER BY id`,
|
|
242
|
-
wanted,
|
|
243
|
-
),
|
|
244
|
-
)
|
|
266
|
+
return this.#hydrate(await users(this.#db).where('key', 'in', wanted).orderBy('id').execute())
|
|
245
267
|
}
|
|
246
268
|
|
|
247
269
|
async byKey(key: string): Promise<UserRecord | undefined> {
|
|
@@ -249,48 +271,35 @@ export class UserRepository {
|
|
|
249
271
|
}
|
|
250
272
|
|
|
251
273
|
async byId(id: number): Promise<UserRecord | undefined> {
|
|
252
|
-
return (
|
|
253
|
-
await this.#hydrate(
|
|
254
|
-
await this.#db.query(`SELECT ${USER_COLUMNS} FROM user_account WHERE id = ?`, [id]),
|
|
255
|
-
)
|
|
256
|
-
)[0]
|
|
274
|
+
return (await this.#hydrate(await users(this.#db).where('id', '=', id).execute()))[0]
|
|
257
275
|
}
|
|
258
276
|
|
|
259
277
|
async byUserName(userName: string): Promise<UserRecord | undefined> {
|
|
260
|
-
return (
|
|
261
|
-
await this.#hydrate(
|
|
262
|
-
await this.#db.query(`SELECT ${USER_COLUMNS} FROM user_account WHERE login = ?`, [
|
|
263
|
-
userName,
|
|
264
|
-
]),
|
|
265
|
-
)
|
|
266
|
-
)[0]
|
|
278
|
+
return (await this.#hydrate(await users(this.#db).where('login', '=', userName).execute()))[0]
|
|
267
279
|
}
|
|
268
280
|
|
|
269
281
|
async byEmail(email: string): Promise<UserRecord | undefined> {
|
|
270
|
-
return (
|
|
271
|
-
await this.#hydrate(
|
|
272
|
-
await this.#db.query(`SELECT ${USER_COLUMNS} FROM user_account WHERE email = ?`, [email]),
|
|
273
|
-
)
|
|
274
|
-
)[0]
|
|
282
|
+
return (await this.#hydrate(await users(this.#db).where('email', '=', email).execute()))[0]
|
|
275
283
|
}
|
|
276
284
|
|
|
277
285
|
async #groupIds(keys: readonly string[]): Promise<number[] | undefined> {
|
|
278
286
|
if (keys.length === 0) return []
|
|
279
287
|
const wanted = keys.map(normaliseUuid)
|
|
280
|
-
const rows = await this.#db.
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
288
|
+
const rows = await this.#db.q
|
|
289
|
+
.selectFrom('user_group')
|
|
290
|
+
.select('id')
|
|
291
|
+
.where('key', 'in', wanted)
|
|
292
|
+
.execute()
|
|
284
293
|
return rows.length === new Set(wanted).size ? rows.map((r) => Number(r.id)) : undefined
|
|
285
294
|
}
|
|
286
295
|
|
|
287
296
|
async #setGroups(userId: number, groupIds: readonly number[]): Promise<void> {
|
|
288
|
-
await this.#db.
|
|
297
|
+
await this.#db.q.deleteFrom('user_group_member').where('user_id', '=', userId).execute()
|
|
289
298
|
for (const groupId of new Set(groupIds))
|
|
290
|
-
await this.#db.
|
|
291
|
-
|
|
292
|
-
groupId
|
|
293
|
-
|
|
299
|
+
await this.#db.q
|
|
300
|
+
.insertInto('user_group_member')
|
|
301
|
+
.values({ user_id: userId, user_group_id: groupId })
|
|
302
|
+
.execute()
|
|
294
303
|
}
|
|
295
304
|
|
|
296
305
|
/** Creates the user; undefined when a group does not exist. */
|
|
@@ -300,28 +309,25 @@ export class UserRepository {
|
|
|
300
309
|
const groupIds = await repo.#groupIds(input.groupKeys)
|
|
301
310
|
if (!groupIds) return undefined
|
|
302
311
|
const now = DbDate.toDb(new Date())
|
|
303
|
-
const bool = (v: boolean) => tx.dialect.boolValue(v)
|
|
304
|
-
await tx.
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
input.
|
|
313
|
-
input.
|
|
314
|
-
|
|
315
|
-
input.
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
now,
|
|
323
|
-
],
|
|
324
|
-
)
|
|
312
|
+
const bool = (v: boolean) => tx.dialect.boolValue(v) as boolean | number
|
|
313
|
+
await tx.q
|
|
314
|
+
.insertInto('user_account')
|
|
315
|
+
.values({
|
|
316
|
+
key: normaliseUuid(input.key),
|
|
317
|
+
user_name: input.name,
|
|
318
|
+
login: input.userName,
|
|
319
|
+
email: input.email,
|
|
320
|
+
security_stamp: crypto.randomUUID(),
|
|
321
|
+
language: input.languageIsoCode,
|
|
322
|
+
kind: input.kind === 'Api' ? 1 : 0,
|
|
323
|
+
is_locked_out: bool(false),
|
|
324
|
+
is_approved: bool(!input.invited),
|
|
325
|
+
failed_login_attempts: 0,
|
|
326
|
+
invited_date: input.invited ? now : null,
|
|
327
|
+
create_date: now,
|
|
328
|
+
update_date: now,
|
|
329
|
+
})
|
|
330
|
+
.execute()
|
|
325
331
|
const created = await repo.byKey(input.key)
|
|
326
332
|
if (!created) return undefined
|
|
327
333
|
await repo.#setGroups(created.id, groupIds)
|
|
@@ -348,24 +354,24 @@ export class UserRepository {
|
|
|
348
354
|
if (!ids) return tree
|
|
349
355
|
for (const id of ids.values()) nodeIds.push({ tree, id })
|
|
350
356
|
}
|
|
351
|
-
await tx.
|
|
352
|
-
'
|
|
353
|
-
|
|
354
|
-
change.name,
|
|
355
|
-
change.userName,
|
|
356
|
-
change.email,
|
|
357
|
-
change.languageIsoCode,
|
|
358
|
-
DbDate.toDb(new Date()),
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
357
|
+
await tx.q
|
|
358
|
+
.updateTable('user_account')
|
|
359
|
+
.set({
|
|
360
|
+
user_name: change.name,
|
|
361
|
+
login: change.userName,
|
|
362
|
+
email: change.email,
|
|
363
|
+
language: change.languageIsoCode,
|
|
364
|
+
update_date: DbDate.toDb(new Date()),
|
|
365
|
+
})
|
|
366
|
+
.where('id', '=', user.id)
|
|
367
|
+
.execute()
|
|
362
368
|
await repo.#setGroups(user.id, groupIds)
|
|
363
|
-
await tx.
|
|
369
|
+
await tx.q.deleteFrom('user_start_node').where('user_id', '=', user.id).execute()
|
|
364
370
|
for (const { tree, id } of nodeIds)
|
|
365
|
-
await tx.
|
|
366
|
-
'
|
|
367
|
-
|
|
368
|
-
|
|
371
|
+
await tx.q
|
|
372
|
+
.insertInto('user_start_node')
|
|
373
|
+
.values({ user_id: user.id, start_node: id, start_node_type: START_NODE_TYPE[tree] })
|
|
374
|
+
.execute()
|
|
369
375
|
return 'updated'
|
|
370
376
|
})
|
|
371
377
|
}
|
|
@@ -385,61 +391,71 @@ export class UserRepository {
|
|
|
385
391
|
if (groupId === undefined) return
|
|
386
392
|
for (const user of await this.byKeys(userKeys))
|
|
387
393
|
if (!(await this.#groupIds(user.groupKeys))?.includes(groupId))
|
|
388
|
-
await this.#db.
|
|
389
|
-
'
|
|
390
|
-
|
|
391
|
-
|
|
394
|
+
await this.#db.q
|
|
395
|
+
.insertInto('user_group_member')
|
|
396
|
+
.values({ user_id: user.id, user_group_id: groupId })
|
|
397
|
+
.execute()
|
|
392
398
|
}
|
|
393
399
|
|
|
394
400
|
async removeFromGroup(groupKey: string, userKeys: readonly string[]): Promise<void> {
|
|
395
401
|
const [groupId] = (await this.#groupIds([groupKey])) ?? []
|
|
396
402
|
if (groupId === undefined) return
|
|
397
403
|
for (const user of await this.byKeys(userKeys))
|
|
398
|
-
await this.#db.
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
404
|
+
await this.#db.q
|
|
405
|
+
.deleteFrom('user_group_member')
|
|
406
|
+
.where('user_id', '=', user.id)
|
|
407
|
+
.where('user_group_id', '=', groupId)
|
|
408
|
+
.execute()
|
|
402
409
|
}
|
|
403
410
|
|
|
404
411
|
async setApproved(keys: readonly string[], approved: boolean): Promise<void> {
|
|
405
412
|
for (const user of await this.byKeys(keys))
|
|
406
|
-
await this.#db.
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
413
|
+
await this.#db.q
|
|
414
|
+
.updateTable('user_account')
|
|
415
|
+
.set({
|
|
416
|
+
is_approved: this.#db.dialect.boolValue(approved) as boolean | number,
|
|
417
|
+
update_date: DbDate.toDb(new Date()),
|
|
418
|
+
})
|
|
419
|
+
.where('id', '=', user.id)
|
|
420
|
+
.execute()
|
|
411
421
|
}
|
|
412
422
|
|
|
413
423
|
async unlock(keys: readonly string[]): Promise<void> {
|
|
414
424
|
for (const user of await this.byKeys(keys))
|
|
415
|
-
await this.#db.
|
|
416
|
-
'
|
|
417
|
-
|
|
418
|
-
|
|
425
|
+
await this.#db.q
|
|
426
|
+
.updateTable('user_account')
|
|
427
|
+
.set({
|
|
428
|
+
is_locked_out: this.#db.dialect.boolValue(false) as boolean | number,
|
|
429
|
+
failed_login_attempts: 0,
|
|
430
|
+
update_date: DbDate.toDb(new Date()),
|
|
431
|
+
})
|
|
432
|
+
.where('id', '=', user.id)
|
|
433
|
+
.execute()
|
|
419
434
|
}
|
|
420
435
|
|
|
421
436
|
async setLanguage(key: string, isoCode: string): Promise<void> {
|
|
422
|
-
await this.#db.
|
|
423
|
-
|
|
424
|
-
DbDate.toDb(new Date())
|
|
425
|
-
normaliseUuid(key)
|
|
426
|
-
|
|
437
|
+
await this.#db.q
|
|
438
|
+
.updateTable('user_account')
|
|
439
|
+
.set({ language: isoCode, update_date: DbDate.toDb(new Date()) })
|
|
440
|
+
.where('key', '=', normaliseUuid(key))
|
|
441
|
+
.execute()
|
|
427
442
|
}
|
|
428
443
|
|
|
429
444
|
async setAvatar(key: string, avatar: string | null): Promise<void> {
|
|
430
|
-
await this.#db.
|
|
431
|
-
|
|
432
|
-
DbDate.toDb(new Date())
|
|
433
|
-
normaliseUuid(key)
|
|
434
|
-
|
|
445
|
+
await this.#db.q
|
|
446
|
+
.updateTable('user_account')
|
|
447
|
+
.set({ avatar, update_date: DbDate.toDb(new Date()) })
|
|
448
|
+
.where('key', '=', normaliseUuid(key))
|
|
449
|
+
.execute()
|
|
435
450
|
}
|
|
436
451
|
|
|
437
452
|
async passwordHash(key: string): Promise<string | null> {
|
|
438
|
-
const
|
|
439
|
-
'
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
453
|
+
const row = await this.#db.q
|
|
454
|
+
.selectFrom('user_account')
|
|
455
|
+
.select('password_hash')
|
|
456
|
+
.where('key', '=', normaliseUuid(key))
|
|
457
|
+
.executeTakeFirst()
|
|
458
|
+
return row?.password_hash ?? null
|
|
443
459
|
}
|
|
444
460
|
|
|
445
461
|
/** An accepted invitation: the user becomes approved and gets their password. */
|
|
@@ -462,17 +478,19 @@ export class UserRepository {
|
|
|
462
478
|
'auth_code',
|
|
463
479
|
'auth_token',
|
|
464
480
|
'user_login_session',
|
|
465
|
-
])
|
|
466
|
-
await tx.
|
|
481
|
+
] as const)
|
|
482
|
+
await tx.q.deleteFrom(table).where('user_id', '=', user.id).execute()
|
|
467
483
|
// Their provider accounts too, which are keyed by the user rather than the row.
|
|
468
|
-
await tx.
|
|
469
|
-
'
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
await tx.
|
|
475
|
-
await tx.
|
|
484
|
+
await tx.q
|
|
485
|
+
.deleteFrom('external_login_token')
|
|
486
|
+
.where('external_login_id', 'in', (eb) =>
|
|
487
|
+
eb.selectFrom('external_login').select('id').where('user_or_member_key', '=', user.key),
|
|
488
|
+
)
|
|
489
|
+
.execute()
|
|
490
|
+
await tx.q.deleteFrom('external_login').where('user_or_member_key', '=', user.key).execute()
|
|
491
|
+
await tx.q.deleteFrom('two_factor_login').where('owner_key', '=', user.key).execute()
|
|
492
|
+
await tx.q.deleteFrom('two_factor_recovery_code').where('owner_key', '=', user.key).execute()
|
|
493
|
+
await tx.q.deleteFrom('user_account').where('id', '=', user.id).execute()
|
|
476
494
|
})
|
|
477
495
|
}
|
|
478
496
|
|
|
@@ -483,14 +501,20 @@ export class UserRepository {
|
|
|
483
501
|
tokenHash: string,
|
|
484
502
|
expiresAt: Date,
|
|
485
503
|
): Promise<void> {
|
|
486
|
-
await this.#db.
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
504
|
+
await this.#db.q
|
|
505
|
+
.deleteFrom('user_token')
|
|
506
|
+
.where('user_id', '=', userId)
|
|
507
|
+
.where('purpose', '=', purpose)
|
|
508
|
+
.execute()
|
|
509
|
+
await this.#db.q
|
|
510
|
+
.insertInto('user_token')
|
|
511
|
+
.values({
|
|
512
|
+
token_hash: tokenHash,
|
|
513
|
+
user_id: userId,
|
|
514
|
+
purpose,
|
|
515
|
+
expires_at: DbDate.toDb(expiresAt),
|
|
516
|
+
})
|
|
517
|
+
.execute()
|
|
494
518
|
}
|
|
495
519
|
|
|
496
520
|
/** Whether the token is the user's live one for `purpose`; consuming removes it. */
|
|
@@ -501,34 +525,39 @@ export class UserRepository {
|
|
|
501
525
|
consume: boolean,
|
|
502
526
|
now = new Date(),
|
|
503
527
|
): Promise<boolean> {
|
|
504
|
-
const
|
|
505
|
-
'
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
528
|
+
const row = await this.#db.q
|
|
529
|
+
.selectFrom('user_token')
|
|
530
|
+
.select('expires_at')
|
|
531
|
+
.where('token_hash', '=', tokenHash)
|
|
532
|
+
.where('user_id', '=', userId)
|
|
533
|
+
.where('purpose', '=', purpose)
|
|
534
|
+
.executeTakeFirst()
|
|
535
|
+
const expires = DbDate.fromDb(row?.expires_at)
|
|
509
536
|
if (!expires || expires.getTime() < now.getTime()) return false
|
|
510
|
-
if (consume)
|
|
537
|
+
if (consume)
|
|
538
|
+
await this.#db.q.deleteFrom('user_token').where('token_hash', '=', tokenHash).execute()
|
|
511
539
|
return true
|
|
512
540
|
}
|
|
513
541
|
|
|
514
542
|
async clientCredentials(userId: number): Promise<string[]> {
|
|
515
|
-
const rows = await this.#db.
|
|
516
|
-
'
|
|
517
|
-
|
|
518
|
-
|
|
543
|
+
const rows = await this.#db.q
|
|
544
|
+
.selectFrom('user_client_credential')
|
|
545
|
+
.select('client_id')
|
|
546
|
+
.where('user_id', '=', userId)
|
|
547
|
+
.orderBy('client_id')
|
|
548
|
+
.execute()
|
|
519
549
|
return rows.map((r) => String(r.client_id))
|
|
520
550
|
}
|
|
521
551
|
|
|
522
552
|
async clientCredentialOwner(
|
|
523
553
|
clientId: string,
|
|
524
554
|
): Promise<{ userId: number; secretHash: string } | undefined> {
|
|
525
|
-
const
|
|
526
|
-
'
|
|
527
|
-
[
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
: undefined
|
|
555
|
+
const row = await this.#db.q
|
|
556
|
+
.selectFrom('user_client_credential')
|
|
557
|
+
.select(['user_id', 'secret_hash'])
|
|
558
|
+
.where('client_id', '=', clientId)
|
|
559
|
+
.executeTakeFirst()
|
|
560
|
+
return row ? { userId: Number(row.user_id), secretHash: String(row.secret_hash) } : undefined
|
|
532
561
|
}
|
|
533
562
|
|
|
534
563
|
async addClientCredential(
|
|
@@ -537,18 +566,24 @@ export class UserRepository {
|
|
|
537
566
|
secretHash: string,
|
|
538
567
|
): Promise<boolean> {
|
|
539
568
|
if (await this.clientCredentialOwner(clientId)) return false
|
|
540
|
-
await this.#db.
|
|
541
|
-
'
|
|
542
|
-
|
|
543
|
-
|
|
569
|
+
await this.#db.q
|
|
570
|
+
.insertInto('user_client_credential')
|
|
571
|
+
.values({
|
|
572
|
+
client_id: clientId,
|
|
573
|
+
user_id: userId,
|
|
574
|
+
secret_hash: secretHash,
|
|
575
|
+
create_date: DbDate.toDb(new Date()),
|
|
576
|
+
})
|
|
577
|
+
.execute()
|
|
544
578
|
return true
|
|
545
579
|
}
|
|
546
580
|
|
|
547
581
|
async removeClientCredential(userId: number, clientId: string): Promise<void> {
|
|
548
|
-
await this.#db.
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
582
|
+
await this.#db.q
|
|
583
|
+
.deleteFrom('user_client_credential')
|
|
584
|
+
.where('user_id', '=', userId)
|
|
585
|
+
.where('client_id', '=', clientId)
|
|
586
|
+
.execute()
|
|
552
587
|
}
|
|
553
588
|
|
|
554
589
|
/** The user's groups' grants, languages and start node positions, for access checks. */
|
|
@@ -565,11 +600,11 @@ export class UserRepository {
|
|
|
565
600
|
const keys = Object.keys(nodeIds)
|
|
566
601
|
const rows =
|
|
567
602
|
keys.length > 0
|
|
568
|
-
? await this.#db.
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
keys
|
|
572
|
-
|
|
603
|
+
? await this.#db.q
|
|
604
|
+
.selectFrom('node')
|
|
605
|
+
.select(NODE_COLUMNS)
|
|
606
|
+
.where('unique_id', 'in', keys)
|
|
607
|
+
.execute()
|
|
573
608
|
: []
|
|
574
609
|
const byKey = new Map(rows.map((r) => [normaliseUuid(String(r.unique_id)), mapNode(r)]))
|
|
575
610
|
const chains = await this.#chains([...byKey.values()])
|
|
@@ -586,12 +621,17 @@ export class UserRepository {
|
|
|
586
621
|
]
|
|
587
622
|
const languageRows =
|
|
588
623
|
groups.length > 0
|
|
589
|
-
? await this.#db.
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
624
|
+
? await this.#db.q
|
|
625
|
+
.selectFrom('user_group_language as gl')
|
|
626
|
+
.innerJoin('language as l', 'l.id', 'gl.language_id')
|
|
627
|
+
.select('l.iso_code')
|
|
628
|
+
.distinct()
|
|
629
|
+
.where(
|
|
630
|
+
'gl.user_group_id',
|
|
631
|
+
'in',
|
|
632
|
+
groups.map((g) => g.id),
|
|
633
|
+
)
|
|
634
|
+
.execute()
|
|
595
635
|
: []
|
|
596
636
|
return {
|
|
597
637
|
groups: groups.map((group) => {
|
|
@@ -663,8 +703,18 @@ export class UserRepository {
|
|
|
663
703
|
}
|
|
664
704
|
}
|
|
665
705
|
|
|
666
|
-
const GROUP_COLUMNS =
|
|
667
|
-
|
|
706
|
+
const GROUP_COLUMNS = [
|
|
707
|
+
'id',
|
|
708
|
+
'key',
|
|
709
|
+
'alias',
|
|
710
|
+
'name',
|
|
711
|
+
'description',
|
|
712
|
+
'icon',
|
|
713
|
+
'has_access_to_all_languages',
|
|
714
|
+
'start_content_id',
|
|
715
|
+
'start_media_id',
|
|
716
|
+
'start_element_id',
|
|
717
|
+
] as const
|
|
668
718
|
|
|
669
719
|
const GROUP_START_COLUMN: Record<StartNodeTree, string> = {
|
|
670
720
|
document: 'start_content_id',
|
|
@@ -672,6 +722,8 @@ const GROUP_START_COLUMN: Record<StartNodeTree, string> = {
|
|
|
672
722
|
element: 'start_element_id',
|
|
673
723
|
}
|
|
674
724
|
|
|
725
|
+
const groups = (db: Db) => db.q.selectFrom('user_group').select(GROUP_COLUMNS)
|
|
726
|
+
|
|
675
727
|
export class UserGroupRepository {
|
|
676
728
|
#db: Db
|
|
677
729
|
|
|
@@ -683,41 +735,41 @@ export class UserGroupRepository {
|
|
|
683
735
|
if (rows.length === 0) return []
|
|
684
736
|
const ids = rows.map((r) => Number(r.id))
|
|
685
737
|
const keys = rows.map((r) => normaliseUuid(String(r.key)))
|
|
686
|
-
const sections = await this.#db.
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
)
|
|
738
|
+
const sections = await this.#db.q
|
|
739
|
+
.selectFrom('user_group_section')
|
|
740
|
+
.select(['user_group_id', 'app_alias'])
|
|
741
|
+
.where('user_group_id', 'in', ids)
|
|
742
|
+
.execute()
|
|
743
|
+
const languages = await this.#db.q
|
|
744
|
+
.selectFrom('user_group_language as gl')
|
|
745
|
+
.innerJoin('language as l', 'l.id', 'gl.language_id')
|
|
746
|
+
.select(['gl.user_group_id', 'l.iso_code'])
|
|
747
|
+
.where('gl.user_group_id', 'in', ids)
|
|
748
|
+
.orderBy('l.iso_code')
|
|
749
|
+
.execute()
|
|
750
|
+
const permissions = await this.#db.q
|
|
751
|
+
.selectFrom('user_group_permission')
|
|
752
|
+
.select(['user_group_key', 'permission'])
|
|
753
|
+
.where('user_group_key', 'in', keys)
|
|
754
|
+
.orderBy('id')
|
|
755
|
+
.execute()
|
|
756
|
+
const granular = await this.#db.q
|
|
757
|
+
.selectFrom('user_group_granular_permission')
|
|
758
|
+
.select(['user_group_key', 'unique_id', 'permission', 'context'])
|
|
759
|
+
.where('user_group_key', 'in', keys)
|
|
760
|
+
.orderBy('id')
|
|
761
|
+
.execute()
|
|
711
762
|
const startIds = rows.flatMap((r) =>
|
|
712
763
|
TREES.map((tree) => Number(r[GROUP_START_COLUMN[tree]] ?? 0)),
|
|
713
764
|
)
|
|
714
765
|
const nodes = new Map<number, string>()
|
|
715
766
|
const positive = [...new Set(startIds.filter((id) => id > 0))]
|
|
716
767
|
if (positive.length > 0)
|
|
717
|
-
for (const row of await this.#db.
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
768
|
+
for (const row of await this.#db.q
|
|
769
|
+
.selectFrom('node')
|
|
770
|
+
.select(['id', 'unique_id'])
|
|
771
|
+
.where('id', 'in', positive)
|
|
772
|
+
.execute())
|
|
721
773
|
nodes.set(Number(row.id), normaliseUuid(String(row.unique_id)))
|
|
722
774
|
|
|
723
775
|
return rows.map((row) => {
|
|
@@ -764,19 +816,14 @@ export class UserGroupRepository {
|
|
|
764
816
|
}
|
|
765
817
|
|
|
766
818
|
async all(): Promise<UserGroupRecord[]> {
|
|
767
|
-
return this.#hydrate(
|
|
768
|
-
await this.#db.query(`SELECT ${GROUP_COLUMNS} FROM user_group ORDER BY name`),
|
|
769
|
-
)
|
|
819
|
+
return this.#hydrate(await groups(this.#db).orderBy('name').execute())
|
|
770
820
|
}
|
|
771
821
|
|
|
772
822
|
async byKeys(keys: readonly string[]): Promise<UserGroupRecord[]> {
|
|
773
823
|
const wanted = keys.filter((k) => /^[0-9a-f-]{32,36}$/i.test(k)).map(normaliseUuid)
|
|
774
824
|
if (wanted.length === 0) return []
|
|
775
825
|
return this.#hydrate(
|
|
776
|
-
await this.#db.
|
|
777
|
-
`SELECT ${GROUP_COLUMNS} FROM user_group WHERE key IN (${inList(wanted)}) ORDER BY name`,
|
|
778
|
-
wanted,
|
|
779
|
-
),
|
|
826
|
+
await groups(this.#db).where('key', 'in', wanted).orderBy('name').execute(),
|
|
780
827
|
)
|
|
781
828
|
}
|
|
782
829
|
|
|
@@ -785,19 +832,17 @@ export class UserGroupRepository {
|
|
|
785
832
|
}
|
|
786
833
|
|
|
787
834
|
async byAlias(alias: string): Promise<UserGroupRecord | undefined> {
|
|
788
|
-
return (
|
|
789
|
-
await this.#hydrate(
|
|
790
|
-
await this.#db.query(`SELECT ${GROUP_COLUMNS} FROM user_group WHERE alias = ?`, [alias]),
|
|
791
|
-
)
|
|
792
|
-
)[0]
|
|
835
|
+
return (await this.#hydrate(await groups(this.#db).where('alias', '=', alias).execute()))[0]
|
|
793
836
|
}
|
|
794
837
|
|
|
795
838
|
/** How many users each group has, by group key. */
|
|
796
839
|
async memberCounts(): Promise<Map<string, number>> {
|
|
797
|
-
const rows = await this.#db.
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
840
|
+
const rows = await this.#db.q
|
|
841
|
+
.selectFrom('user_group as g')
|
|
842
|
+
.leftJoin('user_group_member as m', 'm.user_group_id', 'g.id')
|
|
843
|
+
.select((eb) => ['g.key', eb.fn.count<number>('m.user_id').as('n')])
|
|
844
|
+
.groupBy('g.key')
|
|
845
|
+
.execute()
|
|
801
846
|
return new Map(rows.map((r) => [normaliseUuid(String(r.key)), Number(r.n)]))
|
|
802
847
|
}
|
|
803
848
|
|
|
@@ -826,80 +871,76 @@ export class UserGroupRepository {
|
|
|
826
871
|
}
|
|
827
872
|
const languageIds: number[] = []
|
|
828
873
|
if (input.languages.length > 0) {
|
|
829
|
-
const rows = await tx.
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
874
|
+
const rows = await tx.q
|
|
875
|
+
.selectFrom('language')
|
|
876
|
+
.select(['id', 'iso_code'])
|
|
877
|
+
.where('iso_code', 'in', input.languages)
|
|
878
|
+
.execute()
|
|
833
879
|
if (rows.length !== new Set(input.languages).size) return 'missing-language'
|
|
834
880
|
languageIds.push(...rows.map((r) => Number(r.id)))
|
|
835
881
|
}
|
|
836
882
|
const key = normaliseUuid(input.key)
|
|
837
883
|
const now = DbDate.toDb(new Date())
|
|
838
|
-
const
|
|
884
|
+
const values = {
|
|
885
|
+
alias: input.alias,
|
|
886
|
+
name: input.name,
|
|
887
|
+
description: input.description,
|
|
888
|
+
icon: input.icon,
|
|
889
|
+
has_access_to_all_languages: tx.dialect.boolValue(input.hasAccessToAllLanguages) as
|
|
890
|
+
| boolean
|
|
891
|
+
| number,
|
|
892
|
+
start_content_id: starts.document,
|
|
893
|
+
start_media_id: starts.media,
|
|
894
|
+
start_element_id: starts.element,
|
|
895
|
+
}
|
|
839
896
|
const existing = await new UserGroupRepository(tx).byKey(key)
|
|
840
897
|
if (existing)
|
|
841
|
-
await tx.
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
input.name,
|
|
847
|
-
input.description,
|
|
848
|
-
input.icon,
|
|
849
|
-
bool(input.hasAccessToAllLanguages),
|
|
850
|
-
starts.document,
|
|
851
|
-
starts.media,
|
|
852
|
-
starts.element,
|
|
853
|
-
now,
|
|
854
|
-
existing.id,
|
|
855
|
-
],
|
|
856
|
-
)
|
|
898
|
+
await tx.q
|
|
899
|
+
.updateTable('user_group')
|
|
900
|
+
.set({ ...values, update_date: now })
|
|
901
|
+
.where('id', '=', existing.id)
|
|
902
|
+
.execute()
|
|
857
903
|
else
|
|
858
|
-
await tx.
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
[
|
|
863
|
-
key,
|
|
864
|
-
input.alias,
|
|
865
|
-
input.name,
|
|
866
|
-
input.description,
|
|
867
|
-
input.icon,
|
|
868
|
-
bool(input.hasAccessToAllLanguages),
|
|
869
|
-
starts.document,
|
|
870
|
-
starts.media,
|
|
871
|
-
starts.element,
|
|
872
|
-
now,
|
|
873
|
-
now,
|
|
874
|
-
],
|
|
875
|
-
)
|
|
904
|
+
await tx.q
|
|
905
|
+
.insertInto('user_group')
|
|
906
|
+
.values({ key, ...values, create_date: now, update_date: now })
|
|
907
|
+
.execute()
|
|
876
908
|
const id = Number(
|
|
877
|
-
(await tx.
|
|
909
|
+
(await tx.q.selectFrom('user_group').select('id').where('key', '=', key).executeTakeFirst())
|
|
910
|
+
?.id,
|
|
878
911
|
)
|
|
879
|
-
await tx.
|
|
912
|
+
await tx.q.deleteFrom('user_group_section').where('user_group_id', '=', id).execute()
|
|
880
913
|
for (const section of new Set(input.sections))
|
|
881
|
-
await tx.
|
|
882
|
-
|
|
883
|
-
section
|
|
884
|
-
|
|
885
|
-
await tx.
|
|
914
|
+
await tx.q
|
|
915
|
+
.insertInto('user_group_section')
|
|
916
|
+
.values({ user_group_id: id, app_alias: section })
|
|
917
|
+
.execute()
|
|
918
|
+
await tx.q.deleteFrom('user_group_language').where('user_group_id', '=', id).execute()
|
|
886
919
|
for (const languageId of languageIds)
|
|
887
|
-
await tx.
|
|
888
|
-
'
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
await tx.
|
|
920
|
+
await tx.q
|
|
921
|
+
.insertInto('user_group_language')
|
|
922
|
+
.values({ user_group_id: id, language_id: languageId })
|
|
923
|
+
.execute()
|
|
924
|
+
await tx.q.deleteFrom('user_group_permission').where('user_group_key', '=', key).execute()
|
|
892
925
|
for (const permission of new Set(input.permissions))
|
|
893
|
-
await tx.
|
|
894
|
-
'
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
await tx.
|
|
926
|
+
await tx.q
|
|
927
|
+
.insertInto('user_group_permission')
|
|
928
|
+
.values({ user_group_key: key, permission })
|
|
929
|
+
.execute()
|
|
930
|
+
await tx.q
|
|
931
|
+
.deleteFrom('user_group_granular_permission')
|
|
932
|
+
.where('user_group_key', '=', key)
|
|
933
|
+
.execute()
|
|
898
934
|
for (const row of input.granular)
|
|
899
|
-
await tx.
|
|
900
|
-
'
|
|
901
|
-
|
|
902
|
-
|
|
935
|
+
await tx.q
|
|
936
|
+
.insertInto('user_group_granular_permission')
|
|
937
|
+
.values({
|
|
938
|
+
user_group_key: key,
|
|
939
|
+
unique_id: row.key ? normaliseUuid(row.key) : null,
|
|
940
|
+
permission: row.permission,
|
|
941
|
+
context: row.context,
|
|
942
|
+
})
|
|
943
|
+
.execute()
|
|
903
944
|
return 'saved'
|
|
904
945
|
})
|
|
905
946
|
}
|
|
@@ -908,14 +949,18 @@ export class UserGroupRepository {
|
|
|
908
949
|
const group = await this.byKey(key)
|
|
909
950
|
if (!group) return
|
|
910
951
|
await this.#db.transaction(async (tx) => {
|
|
911
|
-
await tx.
|
|
912
|
-
await tx.
|
|
913
|
-
await tx.
|
|
914
|
-
await tx.
|
|
915
|
-
|
|
916
|
-
group.key
|
|
917
|
-
|
|
918
|
-
await tx.
|
|
952
|
+
await tx.q.deleteFrom('user_group_member').where('user_group_id', '=', group.id).execute()
|
|
953
|
+
await tx.q.deleteFrom('user_group_section').where('user_group_id', '=', group.id).execute()
|
|
954
|
+
await tx.q.deleteFrom('user_group_language').where('user_group_id', '=', group.id).execute()
|
|
955
|
+
await tx.q
|
|
956
|
+
.deleteFrom('user_group_permission')
|
|
957
|
+
.where('user_group_key', '=', group.key)
|
|
958
|
+
.execute()
|
|
959
|
+
await tx.q
|
|
960
|
+
.deleteFrom('user_group_granular_permission')
|
|
961
|
+
.where('user_group_key', '=', group.key)
|
|
962
|
+
.execute()
|
|
963
|
+
await tx.q.deleteFrom('user_group').where('id', '=', group.id).execute()
|
|
919
964
|
})
|
|
920
965
|
}
|
|
921
966
|
}
|