@crombolo/api-management 0.64.0 → 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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@crombolo/api-management",
3
- "version": "0.64.0",
3
+ "version": "0.66.0",
4
4
  "description": "Internal to crombolo, with no compatibility promise: use @crombolo/crombolo. Umbraco-compatible Management API handlers for crombolo.",
5
5
  "license": "MIT",
6
6
  "author": "Crombolo",
@@ -29,8 +29,8 @@
29
29
  "access": "public"
30
30
  },
31
31
  "dependencies": {
32
- "@crombolo/content": "0.64.0",
33
- "@crombolo/contracts": "0.64.0",
34
- "@crombolo/core": "0.64.0"
32
+ "@crombolo/content": "0.66.0",
33
+ "@crombolo/contracts": "0.66.0",
34
+ "@crombolo/core": "0.66.0"
35
35
  }
36
36
  }
@@ -7,12 +7,11 @@
7
7
  * involved, which their port enforces. See docs/backoffice-hosting.md for the
8
8
  * tables' provenance.
9
9
  */
10
- import { WRITE_VERBS as W } from '@crombolo/content/rules'
10
+ import { ELEMENT_VERBS as L, WRITE_VERBS as W } from '@crombolo/content/rules'
11
11
  import type { OperationInfo } from '@crombolo/contracts'
12
12
  import {
13
13
  type AppAlias,
14
14
  DocumentPermissions as D,
15
- ElementPermissions as E,
16
15
  fallbackPermissions,
17
16
  hasPathAccess,
18
17
  MediaPermissions as M,
@@ -333,42 +332,41 @@ const MEDIA_RULES: Record<string, MediaRule> = {
333
332
 
334
333
  /**
335
334
  * The element and element folder verbs each Library operation needs, held in
336
- * the groups' fallback permissions. Mirrors DOCUMENT_RULES verb for verb.
335
+ * the groups' fallback permissions: the Library operations' own table (story 361).
337
336
  */
338
337
  const ELEMENT_RULES: Record<string, readonly string[]> = {
339
- GetElementById: [E.Read],
340
- GetElementByIdPublished: [E.Read],
341
- GetElementByIdAuditLog: [E.Read],
342
- GetElementVersion: [E.Read],
343
- GetElementVersionById: [E.Read],
344
- GetRecycleBinElementByIdOriginalParent: [E.Read],
345
- PostElement: [E.Create],
346
- PostElementValidate: [E.Create],
347
- PutElementById: [E.Update],
348
- PutElementByIdValidate: [E.Update],
349
- PutElementVersionByIdPreventCleanup: [E.Update],
350
- PostElementCreateAndPublish: [E.Publish],
351
- PutElementByIdUpdateAndPublish: [E.Publish],
352
- PutElementByIdPublish: [E.Publish],
353
- PutElementByIdUnpublish: [E.Unpublish],
354
- DeleteElementById: [E.Delete],
355
- PutElementByIdMoveToRecycleBin: [E.Delete],
356
- PutElementByIdMove: [E.Move],
357
- PostElementByIdCopy: [E.Duplicate, E.Create],
358
- PostElementVersionByIdRollback: [E.Rollback],
359
- DeleteRecycleBinElementById: [E.Delete],
360
- PutRecycleBinElementByIdRestore: [E.Move],
361
- // Emptying the bin deletes the folders in it as well as the elements
362
- DeleteRecycleBinElement: [E.Delete],
363
- GetElementFolderById: [E.Read],
364
- GetRecycleBinElementFolderByIdOriginalParent: [E.Read],
365
- PostElementFolder: [E.Create],
366
- PutElementFolderById: [E.Update],
367
- DeleteElementFolderById: [E.Delete],
368
- PutElementFolderByIdMoveToRecycleBin: [E.Delete],
369
- DeleteRecycleBinElementFolderById: [E.Delete],
370
- PutElementFolderByIdMove: [E.Move],
371
- PutRecycleBinElementFolderByIdRestore: [E.Move],
338
+ GetElementById: L.read,
339
+ GetElementByIdPublished: L.read,
340
+ GetElementByIdAuditLog: L.read,
341
+ GetElementVersion: L.read,
342
+ GetElementVersionById: L.read,
343
+ GetRecycleBinElementByIdOriginalParent: L.read,
344
+ PostElement: L.create,
345
+ PostElementValidate: L.create,
346
+ PutElementById: L.save,
347
+ PutElementByIdValidate: L.save,
348
+ PutElementVersionByIdPreventCleanup: L.save,
349
+ PostElementCreateAndPublish: L.createAndPublish,
350
+ PutElementByIdUpdateAndPublish: L.saveAndPublish,
351
+ PutElementByIdPublish: L.publish,
352
+ PutElementByIdUnpublish: L.unpublish,
353
+ DeleteElementById: L.delete,
354
+ PutElementByIdMoveToRecycleBin: L.trash,
355
+ PutElementByIdMove: L.move,
356
+ PostElementByIdCopy: L.copy,
357
+ PostElementVersionByIdRollback: L.rollback,
358
+ DeleteRecycleBinElementById: L.delete,
359
+ PutRecycleBinElementByIdRestore: L.restore,
360
+ DeleteRecycleBinElement: L.emptyBin,
361
+ GetElementFolderById: L.read,
362
+ GetRecycleBinElementFolderByIdOriginalParent: L.read,
363
+ PostElementFolder: L.createFolder,
364
+ PutElementFolderById: L.renameFolder,
365
+ DeleteElementFolderById: L.deleteFolder,
366
+ PutElementFolderByIdMoveToRecycleBin: L.trashFolder,
367
+ DeleteRecycleBinElementFolderById: L.delete,
368
+ PutElementFolderByIdMove: L.moveFolder,
369
+ PutRecycleBinElementFolderByIdRestore: L.restore,
372
370
  }
373
371
 
374
372
  /** Values at `path` in `body`; `*` walks every element of an array. A missing or null step means the root. */
@@ -48,3 +48,24 @@ export const MEDIA_WRITE_OPERATIONS: ReadonlySet<string> = new Set([
48
48
  'DeleteRecycleBinMediaById',
49
49
  'DeleteRecycleBinMedia',
50
50
  ])
51
+
52
+ /**
53
+ * The Library writes that go through the Library operations (story 361). Each
54
+ * writes its own audit entry in the change's transaction; the router still
55
+ * raises their live events and change notices.
56
+ */
57
+ export const ELEMENT_WRITE_OPERATIONS: ReadonlySet<string> = new Set([
58
+ 'PostElement',
59
+ 'PutElementById',
60
+ 'PostElementCreateAndPublish',
61
+ 'PutElementByIdUpdateAndPublish',
62
+ 'PutElementByIdPublish',
63
+ 'PutElementByIdUnpublish',
64
+ 'PutElementByIdMove',
65
+ 'PostElementByIdCopy',
66
+ 'PutElementByIdMoveToRecycleBin',
67
+ 'PostElementFolder',
68
+ 'PutElementFolderById',
69
+ 'PutElementFolderByIdMove',
70
+ 'PutElementFolderByIdMoveToRecycleBin',
71
+ ])
package/src/domain.ts CHANGED
@@ -19,6 +19,7 @@ export * from './ports-models-builder.ts'
19
19
  export * from './ports-packages.ts'
20
20
  export * from './ports-users.ts'
21
21
  export * from './ports-webhooks.ts'
22
+ export * from './public-access-rules.ts'
22
23
  export type * from './router.ts'
23
24
  export * from './server-events.ts'
24
25
  export * from './webhook-events.ts'
@@ -11,12 +11,17 @@
11
11
  * The tree mixes folders and elements. A folder reports `documentType: null` and a
12
12
  * single `NotCreated` variant, which is the contract's way of saying there is no
13
13
  * content at that row.
14
+ *
15
+ * Every write goes through the Library operations (story 361) as the signed-in
16
+ * user, so it is checked and audited as one from the command line is.
14
17
  */
18
+ import type { ElementOperations } from '@crombolo/content'
19
+ import { userActor } from '@crombolo/content/rules'
15
20
  import type { DocumentAggregate, ElementTreeItem } from '@crombolo/core'
16
21
  import { invalidSkipTake, notFound, problemDetails, problemResponse } from '@crombolo/core'
17
22
  import type { ElementPort, PortFailure, ReferencePort } from '../ports-content.ts'
18
23
  import type { ManagementApiRouter, RequestContext } from '../router.ts'
19
- import { created, paging, registerFolderHandlers } from './content-type.ts'
24
+ import { created, paging, refOf, registerFolderHandlers } from './content-type.ts'
20
25
  import { failureResponse, readBody, readSchedules, toSaveDocument } from './document.ts'
21
26
  import { itemFlags, variantFlags } from './flags.ts'
22
27
 
@@ -123,6 +128,7 @@ const refId = (value: unknown): string | null => {
123
128
  export function registerElementHandlers(
124
129
  router: ManagementApiRouter,
125
130
  port: ElementPort,
131
+ operations: ElementOperations,
126
132
  /** For the warning a folder delete shows: what points into the branch. */
127
133
  references?: ReferencePort,
128
134
  ): void {
@@ -138,7 +144,36 @@ export function registerElementHandlers(
138
144
  return new Response(null, { status: 200 })
139
145
  }
140
146
 
147
+ /** The signed-in user, as the operations know them: their Library verbs apply. */
148
+ const actorOf = (ctx: RequestContext) =>
149
+ userActor(ctx.principal as NonNullable<typeof ctx.principal>, ctx.client ?? {})
150
+ /** A write the operations found nothing to do it to, as the 404 the back office reads. */
151
+ const missing = (title: string, detail: string, result: PortFailure & { kind?: string }) =>
152
+ result.kind === 'notFound' ? problemResponse(notFound(detail)) : failed(title, result)
153
+
141
154
  registerFolderHandlers(router, 'Element', port.folders)
155
+ // A folder made or renamed is a Library write too, so these replace the shared ones.
156
+ handle('PostElementFolder', async (ctx) => {
157
+ const body = (await ctx.request.json()) as Record<string, unknown>
158
+ const result = await operations.createFolder(actorOf(ctx), {
159
+ key: typeof body.id === 'string' && body.id ? body.id : undefined,
160
+ name: String(body.name ?? ''),
161
+ parentKey: refOf(body.parent),
162
+ })
163
+ if (!result.ok) return failed('Could not create the folder', result)
164
+ return created(`${ctx.url.origin}${API}/element/folder/${result.key}`, result.key)
165
+ })
166
+ handle('PutElementFolderById', async (ctx) => {
167
+ const body = (await ctx.request.json()) as Record<string, unknown>
168
+ const result = await operations.renameFolder(
169
+ actorOf(ctx),
170
+ ctx.params.id as string,
171
+ String(body.name ?? ''),
172
+ )
173
+ if (!result.ok)
174
+ return missing('Could not rename the folder', 'The folder could not be found', result)
175
+ return new Response(null, { status: 200 })
176
+ })
142
177
 
143
178
  // ── the tree ──────────────────────────────────────────────────────────────
144
179
  handle('GetTreeElementRoot', async (ctx) => {
@@ -232,7 +267,7 @@ export function registerElementHandlers(
232
267
  const body = await readBody(ctx)
233
268
  const key = typeof body.id === 'string' && body.id ? body.id : crypto.randomUUID()
234
269
  const input = toSaveDocument(key, body)
235
- const result = await port.create(input, ctx.principal as NonNullable<typeof ctx.principal>)
270
+ const result = await operations.create(actorOf(ctx), input)
236
271
  if (!result.ok) return failureResponse('Could not create the element', result, input.values)
237
272
  ctx.notifications.push({ message: 'Element created', category: 'Element', type: 'Success' })
238
273
  return created(`${ctx.url.origin}${API}/element/${result.key}`, result.key)
@@ -244,7 +279,7 @@ export function registerElementHandlers(
244
279
  if (!existing) return problemResponse(notFound('The element could not be found'))
245
280
  const body = await readBody(ctx)
246
281
  const input = { ...toSaveDocument(key, body), contentTypeKey: existing.contentTypeKey }
247
- const result = await port.update(key, input, ctx.principal as NonNullable<typeof ctx.principal>)
282
+ const result = await operations.save(actorOf(ctx), key, input)
248
283
  if (!result.ok) return failureResponse('Could not save the element', result, input.values)
249
284
  return done(ctx, 'Element saved')
250
285
  })
@@ -253,11 +288,7 @@ export function registerElementHandlers(
253
288
  const body = await readBody(ctx)
254
289
  const key = typeof body.id === 'string' && body.id ? body.id : crypto.randomUUID()
255
290
  const input = toSaveDocument(key, body)
256
- const result = await port.createAndPublish(
257
- input,
258
- readCultures(body),
259
- ctx.principal as NonNullable<typeof ctx.principal>,
260
- )
291
+ const result = await operations.createAndPublish(actorOf(ctx), input, readCultures(body))
261
292
  if (!result.ok) return failureResponse('Could not create the element', result, input.values)
262
293
  ctx.notifications.push({ message: 'Element published', category: 'Element', type: 'Success' })
263
294
  return created(`${ctx.url.origin}${API}/element/${result.key}`, result.key)
@@ -269,12 +300,7 @@ export function registerElementHandlers(
269
300
  const existing = await port.byKey(key)
270
301
  if (!existing) return problemResponse(notFound('The element could not be found'))
271
302
  const input = { ...toSaveDocument(key, body), contentTypeKey: existing.contentTypeKey }
272
- const result = await port.updateAndPublish(
273
- key,
274
- input,
275
- readCultures(body),
276
- ctx.principal as NonNullable<typeof ctx.principal>,
277
- )
303
+ const result = await operations.saveAndPublish(actorOf(ctx), key, input, readCultures(body))
278
304
  if (!result.ok) return failureResponse('Could not publish the element', result, input.values)
279
305
  return done(ctx, 'Element published')
280
306
  })
@@ -303,7 +329,7 @@ export function registerElementHandlers(
303
329
  return problemResponse(
304
330
  problemDetails({ title: 'A schedule date is not a valid date', status: 400 }),
305
331
  )
306
- const result = await port.publish(key, readCultures(body), schedules)
332
+ const result = await operations.publish(actorOf(ctx), key, readCultures(body), schedules)
307
333
  if (!result.ok) return failed('Could not publish the element', result)
308
334
  return done(ctx, result.scheduledOnly ? 'Publishing scheduled' : 'Element published')
309
335
  })
@@ -311,7 +337,7 @@ export function registerElementHandlers(
311
337
  handle('PutElementByIdUnpublish', async (ctx) => {
312
338
  const key = ctx.params.id as string
313
339
  if (!(await port.byKey(key))) return problemResponse(notFound('The element could not be found'))
314
- const result = await port.unpublish(key, readCultures(await readBody(ctx)))
340
+ const result = await operations.unpublish(actorOf(ctx), key, readCultures(await readBody(ctx)))
315
341
  return result.ok
316
342
  ? done(ctx, 'Element unpublished')
317
343
  : failed('Could not unpublish the element', result)
@@ -319,17 +345,23 @@ export function registerElementHandlers(
319
345
 
320
346
  // ── moving, copying and the bin ───────────────────────────────────────────
321
347
  handle('PutElementByIdMove', async (ctx) => {
322
- const result = await port.move(ctx.params.id as string, refId((await readBody(ctx)).target))
348
+ const result = await operations.move(
349
+ actorOf(ctx),
350
+ ctx.params.id as string,
351
+ refId((await readBody(ctx)).target),
352
+ )
323
353
  return result.ok ? done(ctx, 'Element moved') : failed('Could not move the element', result)
324
354
  })
325
355
 
326
356
  handle('PostElementByIdCopy', async (ctx) => {
327
357
  const body = await readBody(ctx)
328
- const result = await port.copy(
358
+ const result = await operations.copy(
359
+ actorOf(ctx),
329
360
  ctx.params.id as string,
330
361
  refId(body.target),
331
- { includeDescendants: body.includeDescendants === true },
332
- ctx.principal as NonNullable<typeof ctx.principal>,
362
+ {
363
+ includeDescendants: body.includeDescendants === true,
364
+ },
333
365
  )
334
366
  if (!result.ok) return failed('Could not copy the element', result)
335
367
  ctx.notifications.push({ message: 'Element copied', category: 'Element', type: 'Success' })
@@ -337,8 +369,9 @@ export function registerElementHandlers(
337
369
  })
338
370
 
339
371
  handle('PutElementByIdMoveToRecycleBin', async (ctx) => {
340
- const moved = await port.moveToRecycleBin(ctx.params.id as string)
341
- if (!moved) return problemResponse(notFound('The element could not be found'))
372
+ const result = await operations.trash(actorOf(ctx), ctx.params.id as string)
373
+ if (!result.ok)
374
+ return missing('Could not delete the element', 'The element could not be found', result)
342
375
  return done(ctx, 'Moved to the recycle bin')
343
376
  })
344
377
 
@@ -349,7 +382,8 @@ export function registerElementHandlers(
349
382
  })
350
383
 
351
384
  handle('PutElementFolderByIdMove', async (ctx) => {
352
- const result = await port.moveFolder(
385
+ const result = await operations.moveFolder(
386
+ actorOf(ctx),
353
387
  ctx.params.id as string,
354
388
  refId((await readBody(ctx)).target),
355
389
  )
@@ -357,8 +391,9 @@ export function registerElementHandlers(
357
391
  })
358
392
 
359
393
  handle('PutElementFolderByIdMoveToRecycleBin', async (ctx) => {
360
- const moved = await port.folderToRecycleBin(ctx.params.id as string)
361
- if (!moved) return problemResponse(notFound('The folder could not be found'))
394
+ const result = await operations.trashFolder(actorOf(ctx), ctx.params.id as string)
395
+ if (!result.ok)
396
+ return missing('Could not delete the folder', 'The folder could not be found', result)
362
397
  return done(ctx, 'Moved to the recycle bin')
363
398
  })
364
399
 
@@ -3,12 +3,14 @@
3
3
  *
4
4
  * A rule names member groups **or** individual members, never both — Umbraco
5
5
  * calls the mixture ambiguous and refuses it, and so do we, because the two are
6
- * different intentions and the editor offers them as a choice.
6
+ * different intentions and the editor offers them as a choice. The rules are
7
+ * `publicAccessRuleBreak`'s, which the command line keeps too.
7
8
  */
8
9
  import type { DocumentOperations } from '@crombolo/content'
9
10
  import { userActor } from '@crombolo/content/rules'
10
11
  import { notFound, problemDetails, problemResponse } from '@crombolo/core'
11
12
  import type { PublicAccessPort, PublicAccessStatus } from '../ports-members.ts'
13
+ import { publicAccessRuleBreak } from '../public-access-rules.ts'
12
14
  import type { ManagementApiRouter, RequestContext } from '../router.ts'
13
15
  import { readBody } from './document.ts'
14
16
 
@@ -71,30 +73,41 @@ export function registerPublicAccessHandlers(
71
73
  const body = await readBody(ctx)
72
74
  const login = refId(body.loginDocument)
73
75
  const error = refId(body.errorDocument)
74
- if (!login) return problemResponse(notFound('The login page could not be found'))
75
- if (!error) return problemResponse(notFound('The error page could not be found'))
76
76
  const memberGroupNames = names(body.memberGroupNames)
77
77
  const memberUserNames = names(body.memberUserNames)
78
- if (memberGroupNames.length === 0 && memberUserNames.length === 0)
79
- return problemResponse(
80
- problemDetails({
81
- title: 'No allowed entities given',
82
- detail: 'Both MemberGroups and Members were empty, thus no entities can be allowed.',
83
- status: 400,
84
- }),
85
- )
86
- if (memberGroupNames.length > 0 && memberUserNames.length > 0)
87
- return problemResponse(
88
- problemDetails({
89
- title: 'Ambiguous Rule',
90
- detail:
91
- 'The specified rule is ambiguous, because both member groups and member names were given.',
92
- status: 400,
93
- }),
94
- )
78
+ switch (
79
+ publicAccessRuleBreak({
80
+ loginDocumentKey: login,
81
+ errorDocumentKey: error,
82
+ memberGroupNames,
83
+ memberUserNames,
84
+ })
85
+ ) {
86
+ case 'no-login-page':
87
+ return problemResponse(notFound('The login page could not be found'))
88
+ case 'no-error-page':
89
+ return problemResponse(notFound('The error page could not be found'))
90
+ case 'no-one':
91
+ return problemResponse(
92
+ problemDetails({
93
+ title: 'No allowed entities given',
94
+ detail: 'Both MemberGroups and Members were empty, thus no entities can be allowed.',
95
+ status: 400,
96
+ }),
97
+ )
98
+ case 'ambiguous':
99
+ return problemResponse(
100
+ problemDetails({
101
+ title: 'Ambiguous Rule',
102
+ detail:
103
+ 'The specified rule is ambiguous, because both member groups and member names were given.',
104
+ status: 400,
105
+ }),
106
+ )
107
+ }
95
108
  const input = {
96
- loginDocumentKey: login,
97
- errorDocumentKey: error,
109
+ loginDocumentKey: login as string,
110
+ errorDocumentKey: error as string,
98
111
  memberGroupNames,
99
112
  memberUserNames,
100
113
  }
@@ -99,6 +99,12 @@ const USER_PROBLEMS: Partial<Record<UserStatus, [number, string, string]>> = {
99
99
  'Invalid client ID',
100
100
  'The client ID must be prefixed with "crombolo-back-office-".',
101
101
  ],
102
+ AdminGroupCannotBeEmpty: [
103
+ 400,
104
+ 'Admin group cannot be empty',
105
+ 'The admin group must have at least one user.',
106
+ ],
107
+ CannotDelete: [400, 'Cannot delete', "The site's built-in administrator cannot be deleted."],
102
108
  NotGroupOwner: [
103
109
  403,
104
110
  "You don't own that group",
@@ -66,6 +66,9 @@ export type TreeItemKind = 'All' | 'Item' | 'Folder'
66
66
  * elements are gathered in. `folders` and the four tree reads stay as they were,
67
67
  * because a folder is a plain node and the generic folder port already carries it.
68
68
  */
69
+ /** Whom an element version names as its author: a user, by key. */
70
+ export type ElementAuthor = { id: string } | null
71
+
69
72
  export interface ElementPort {
70
73
  folders: FolderPort
71
74
  treeRoot(paging: TreePage): Promise<Page<ElementTreeItem>>
@@ -84,23 +87,23 @@ export interface ElementPort {
84
87
  validate(input: SaveDocument, cultures: string[] | null): Promise<DocumentValidationError[]>
85
88
  create(
86
89
  input: SaveDocument,
87
- principal: Principal,
90
+ author: ElementAuthor,
88
91
  ): Promise<{ ok: true; key: string } | PortFailure>
89
92
  update(
90
93
  key: string,
91
94
  input: SaveDocument,
92
- principal: Principal,
95
+ author: ElementAuthor,
93
96
  ): Promise<{ ok: true } | PortFailure>
94
97
  createAndPublish(
95
98
  input: SaveDocument,
96
99
  cultures: string[] | null,
97
- principal: Principal,
100
+ author: ElementAuthor,
98
101
  ): Promise<{ ok: true; key: string } | PortFailure>
99
102
  updateAndPublish(
100
103
  key: string,
101
104
  input: SaveDocument,
102
105
  cultures: string[] | null,
103
- principal: Principal,
106
+ author: ElementAuthor,
104
107
  ): Promise<{ ok: true } | PortFailure>
105
108
  /**
106
109
  * Publishes now, or schedules: an entry with a publish time still to come
@@ -116,7 +119,7 @@ export interface ElementPort {
116
119
  key: string,
117
120
  targetKey: string | null,
118
121
  options: { includeDescendants: boolean },
119
- principal: Principal,
122
+ author: ElementAuthor,
120
123
  ): Promise<{ ok: true; key: string } | PortFailure>
121
124
  move(key: string, targetKey: string | null): Promise<{ ok: true } | PortFailure>
122
125
  moveToRecycleBin(key: string): Promise<boolean>
@@ -41,6 +41,10 @@ export type UserStatus =
41
41
  | 'InvalidResetCode'
42
42
  /** Crombolo: a group added or removed is not one the caller owns (story 302). */
43
43
  | 'NotGroupOwner'
44
+ /** Crombolo: the change would leave the admin group with nobody in it. */
45
+ | 'AdminGroupCannotBeEmpty'
46
+ /** The site's built-in administrator, who is never deleted. */
47
+ | 'CannotDelete'
44
48
 
45
49
  export type UserOrder =
46
50
  | 'UserName'
package/src/ports.ts CHANGED
@@ -7,7 +7,7 @@
7
7
  * drifts from the wire shape fails to compile.
8
8
  */
9
9
 
10
- import type { DocumentOperations, MediaOperations } from '@crombolo/content'
10
+ import type { DocumentOperations, ElementOperations, MediaOperations } from '@crombolo/content'
11
11
  import type { ResponseOf } from '@crombolo/contracts'
12
12
  import type { UploadSettings } from '@crombolo/core'
13
13
  import type {
@@ -286,6 +286,8 @@ export interface ManagementApiDeps {
286
286
  approvals?: ApprovalGatePort
287
287
  /** Every document write, with its rules and its events (story 170); required alongside `documents`. */
288
288
  documentOperations?: DocumentOperations
289
+ /** Every Library write, with its rules and its audit entries (story 361); required alongside `elements`. */
290
+ elementOperations?: ElementOperations
289
291
  dictionary?: DictionaryPort
290
292
  userManagement?: UserPort
291
293
  userGroups?: UserGroupPort
@@ -0,0 +1,20 @@
1
+ /**
2
+ * The public access dialog's two rules, for every caller that writes one: a
3
+ * rule names member groups or members, never both and at least one, and has
4
+ * both a sign-in page and a no-access page.
5
+ */
6
+
7
+ export type PublicAccessRuleBreak = 'no-login-page' | 'no-error-page' | 'no-one' | 'ambiguous'
8
+
9
+ export function publicAccessRuleBreak(input: {
10
+ loginDocumentKey: string | null | undefined
11
+ errorDocumentKey: string | null | undefined
12
+ memberGroupNames: readonly string[]
13
+ memberUserNames: readonly string[]
14
+ }): PublicAccessRuleBreak | undefined {
15
+ if (!input.loginDocumentKey) return 'no-login-page'
16
+ if (!input.errorDocumentKey) return 'no-error-page'
17
+ if (input.memberGroupNames.length === 0 && input.memberUserNames.length === 0) return 'no-one'
18
+ if (input.memberGroupNames.length > 0 && input.memberUserNames.length > 0) return 'ambiguous'
19
+ return undefined
20
+ }
package/src/router.ts CHANGED
@@ -26,7 +26,11 @@ import {
26
26
  auditReadsBody,
27
27
  } from './audit.ts'
28
28
  import { authorize, LOCAL_LOGIN_OPERATIONS, type NodeLookup } from './authorization.ts'
29
- import { DOCUMENT_WRITE_OPERATIONS, MEDIA_WRITE_OPERATIONS } from './document-writes.ts'
29
+ import {
30
+ DOCUMENT_WRITE_OPERATIONS,
31
+ ELEMENT_WRITE_OPERATIONS,
32
+ MEDIA_WRITE_OPERATIONS,
33
+ } from './document-writes.ts'
30
34
  import {
31
35
  changeReadsBody,
32
36
  contentChangeFor,
@@ -66,6 +70,16 @@ export interface Principal {
66
70
  * the remote protocol's routes.
67
71
  */
68
72
  ci?: { rule: string; claims: Record<string, string> }
73
+ /**
74
+ * Set when the person acts through one of their command-line tokens (story
75
+ * 351): it is them, but like CI it never decides an approval.
76
+ */
77
+ viaToken?: { key: string; name: string }
78
+ /**
79
+ * Set while an administrator's session views as user groups (story 314):
80
+ * everything above is the groups', and they are who this really is.
81
+ */
82
+ viewingAs?: { groups: Array<{ key: string; name: string }>; until: Date }
69
83
  }
70
84
 
71
85
  export interface RequestContext {
@@ -301,7 +315,10 @@ export class ManagementApiRouter {
301
315
  if (!handler) this.#onNotImplemented?.(operation, request)
302
316
  // A document write announces itself from its operation (story 170).
303
317
  const announces = !DOCUMENT_WRITE_OPERATIONS.has(operation.operationId)
304
- const audits = announces && !MEDIA_WRITE_OPERATIONS.has(operation.operationId)
318
+ const audits =
319
+ announces &&
320
+ !MEDIA_WRITE_OPERATIONS.has(operation.operationId) &&
321
+ !ELEMENT_WRITE_OPERATIONS.has(operation.operationId)
305
322
  const plan = handler && this.#audit && audits ? auditPlanFor(operation.operationId) : undefined
306
323
  const audit = plan ? await this.#beforeAudit(plan, params, request) : undefined
307
324
  const changeBody =
package/src/routes.ts CHANGED
@@ -128,7 +128,8 @@ export function createManagementApiRouter(options: ManagementApiOptions = {}): M
128
128
  if (deps.members) registerMemberHandlers(router, deps.members)
129
129
  if (deps.publicAccess)
130
130
  registerPublicAccessHandlers(router, deps.publicAccess, deps.documentOperations)
131
- if (deps.elements) registerElementHandlers(router, deps.elements, deps.references)
131
+ if (deps.elements && deps.elementOperations)
132
+ registerElementHandlers(router, deps.elements, deps.elementOperations, deps.references)
132
133
  registerAuditLogHandlers(
133
134
  router,
134
135
  {