@crombolo/api-management 0.61.0 → 0.63.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.61.0",
3
+ "version": "0.63.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.61.0",
33
- "@crombolo/contracts": "0.61.0",
34
- "@crombolo/core": "0.61.0"
32
+ "@crombolo/content": "0.63.0",
33
+ "@crombolo/contracts": "0.63.0",
34
+ "@crombolo/core": "0.63.0"
35
35
  }
36
36
  }
@@ -13,7 +13,6 @@ import {
13
13
  type AppAlias,
14
14
  DocumentPermissions as D,
15
15
  ElementPermissions as E,
16
- ElementContainerPermissions as EC,
17
16
  fallbackPermissions,
18
17
  hasPathAccess,
19
18
  MediaPermissions as M,
@@ -290,9 +289,7 @@ const DOCUMENT_RULES: Record<string, readonly DocumentRule[]> = {
290
289
  DeleteDocumentByIdPublicAccess: [{ verbs: W.publicAccess, node: id }],
291
290
  PutDocumentByIdDomains: [{ verbs: W.domains, node: id }],
292
291
  PostDocumentVersionByIdRollback: [{ verbs: W.rollback, node: { version: 'id' } }],
293
- PostDocumentBlueprintFromDocument: [
294
- { verbs: [D.CreateBlueprint], node: { body: ['document', 'id'] } },
295
- ],
292
+ PostDocumentBlueprintFromDocument: [{ verbs: [D.Create], node: { body: ['document', 'id'] } }],
296
293
  DeleteRecycleBinDocument: [{ verbs: W.emptyBin, node: 'bin' }],
297
294
  DeleteRecycleBinDocumentById: [{ verbs: W.delete, node: 'bin' }],
298
295
  PutRecycleBinDocumentByIdRestore: [{ verbs: W.restore, node: 'bin' }],
@@ -362,16 +359,16 @@ const ELEMENT_RULES: Record<string, readonly string[]> = {
362
359
  DeleteRecycleBinElementById: [E.Delete],
363
360
  PutRecycleBinElementByIdRestore: [E.Move],
364
361
  // Emptying the bin deletes the folders in it as well as the elements
365
- DeleteRecycleBinElement: [E.Delete, EC.Delete],
366
- GetElementFolderById: [EC.Read],
367
- GetRecycleBinElementFolderByIdOriginalParent: [EC.Read],
368
- PostElementFolder: [EC.Create],
369
- PutElementFolderById: [EC.Update],
370
- DeleteElementFolderById: [EC.Delete],
371
- PutElementFolderByIdMoveToRecycleBin: [EC.Delete],
372
- DeleteRecycleBinElementFolderById: [EC.Delete],
373
- PutElementFolderByIdMove: [EC.Move],
374
- PutRecycleBinElementFolderByIdRestore: [EC.Move],
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],
375
372
  }
376
373
 
377
374
  /** Values at `path` in `body`; `*` walks every element of an array. A missing or null step means the root. */
@@ -10,7 +10,14 @@ import {
10
10
  problemDetails,
11
11
  problemResponse,
12
12
  } from '@crombolo/core'
13
- import type { FileEntry, FileResult, FileStatus, FileSystemPort, Snippet } from '../ports-files.ts'
13
+ import {
14
+ CONTENT_FILES_READ_ONLY,
15
+ type FileEntry,
16
+ type FileResult,
17
+ type FileStatus,
18
+ type FileSystemPort,
19
+ type Snippet,
20
+ } from '../ports-files.ts'
14
21
  import type { ManagementApiRouter, RequestContext } from '../router.ts'
15
22
 
16
23
  export type FileArea = 'PartialView' | 'Stylesheet' | 'Script'
@@ -37,6 +44,15 @@ const treeItem = (entry: FileEntry) => ({
37
44
  })
38
45
 
39
46
  function failure(area: FileArea, status: FileStatus, folder = false): Response {
47
+ if (status === 'ReadOnly')
48
+ return problemResponse(
49
+ problemDetails({
50
+ title: "These files can't be changed here",
51
+ detail: CONTENT_FILES_READ_ONLY,
52
+ status: 403,
53
+ operationStatus: status,
54
+ }),
55
+ )
40
56
  const thing = folder ? 'Folder' : LABEL[area]
41
57
  const [code, title] = (
42
58
  {
@@ -6,6 +6,7 @@ import type { ResponseOf } from '@crombolo/contracts'
6
6
  import type { ComponentModel } from '@crombolo/core'
7
7
  import { invalidSkipTake, notFound, problemDetails, problemResponse } from '@crombolo/core'
8
8
  import type { TemplatePort } from '../ports-content.ts'
9
+ import { CONTENT_FILES_READ_ONLY } from '../ports-files.ts'
9
10
  import type { ManagementApiRouter } from '../router.ts'
10
11
  import { created, paging, toTreeItemResponse } from './content-type.ts'
11
12
 
@@ -21,6 +22,17 @@ function toResponse(model: ComponentModel): ResponseOf<'GetTemplateById'> {
21
22
  } as ResponseOf<'GetTemplateById'>
22
23
  }
23
24
 
25
+ /** Story 337: a site that keeps its templates read-only in the back office. */
26
+ const readOnly = () =>
27
+ problemResponse(
28
+ problemDetails({
29
+ title: "Templates can't be changed here",
30
+ detail: CONTENT_FILES_READ_ONLY,
31
+ status: 403,
32
+ operationStatus: 'ReadOnly',
33
+ }),
34
+ )
35
+
24
36
  export function registerTemplateHandlers(router: ManagementApiRouter, port: TemplatePort): void {
25
37
  // Templates are the site's components and are always on.
26
38
  router.handle('GetTemplateConfiguration', () => Response.json({ disabled: false }))
@@ -31,6 +43,7 @@ export function registerTemplateHandlers(router: ManagementApiRouter, port: Temp
31
43
  })
32
44
 
33
45
  router.handle('PostTemplate', async (ctx) => {
46
+ if (port.readOnly) return readOnly()
34
47
  const body = (await ctx.request.json()) as Record<string, unknown>
35
48
  const key = typeof body.id === 'string' && body.id ? body.id : crypto.randomUUID()
36
49
  const name = String(body.name ?? '')
@@ -49,6 +62,7 @@ export function registerTemplateHandlers(router: ManagementApiRouter, port: Temp
49
62
  })
50
63
 
51
64
  router.handle('PutTemplateById', async (ctx) => {
65
+ if (port.readOnly) return readOnly()
52
66
  const key = ctx.params.id as string
53
67
  const existing = await port.byKey(key)
54
68
  if (!existing) return problemResponse(notFound('The template could not be found'))
@@ -67,6 +81,7 @@ export function registerTemplateHandlers(router: ManagementApiRouter, port: Temp
67
81
  })
68
82
 
69
83
  router.handle('DeleteTemplateById', async (ctx) => {
84
+ if (port.readOnly) return readOnly()
70
85
  const removed = await port.remove(ctx.params.id as string)
71
86
  if (!removed) return problemResponse(notFound('The template could not be found'))
72
87
  return new Response(null, { status: 200 })
@@ -99,6 +99,11 @@ 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
+ NotGroupOwner: [
103
+ 403,
104
+ "You don't own that group",
105
+ "You can only add people to the groups you own, or take them out of them. Ask the group's owners, or an administrator.",
106
+ ],
102
107
  }
103
108
 
104
109
  /** A user status as the response Umbraco sends; undefined for success. */
@@ -121,6 +126,22 @@ const GROUP_PROBLEMS: Partial<Record<UserGroupStatus, [number, string, string]>>
121
126
  'The alias of a system user group cannot be changed.',
122
127
  ],
123
128
  IsSystemUserGroup: [400, 'System user group', 'A system user group cannot be deleted.'],
129
+ AdministratorsOnly: [
130
+ 403,
131
+ 'Only an administrator can change groups',
132
+ "Only an administrator can create, change or delete a group. A group's owners can choose who is in it.",
133
+ ],
134
+ NotGroupOwner: [
135
+ 403,
136
+ "You don't own this group",
137
+ "Only an owner of this group, or an administrator, can choose who's in it.",
138
+ ],
139
+ CannotBeOwned: [
140
+ 400,
141
+ "This group can't have owners",
142
+ "The administrators group, and a group that can move sites or install code, can't have owners: only an administrator chooses who is in them.",
143
+ ],
144
+ OwnerNotFound: [404, 'Owner not found', 'One of the owner groups could not be found.'],
124
145
  DeclaredInFiles: [
125
146
  409,
126
147
  "Kept in the site's files",
@@ -212,6 +233,7 @@ function readGroup(b: Record<string, unknown>): UserGroupInput {
212
233
  permissions: (Array.isArray(b.permissions)
213
234
  ? b.permissions
214
235
  : []) as UserGroupInput['permissions'],
236
+ ...(Array.isArray(b.owners) ? { owners: refIds(b.owners) } : {}),
215
237
  }
216
238
  }
217
239
 
@@ -266,6 +266,8 @@ export interface DataTypePort extends TreePort {
266
266
  }
267
267
 
268
268
  export interface TemplatePort extends TreePort {
269
+ /** Crombolo: the site keeps template files read-only in the back office (story 337). */
270
+ readOnly?: boolean
269
271
  byKey(key: string): Promise<ComponentModel | undefined>
270
272
  save(model: ComponentModel, principal: Principal): Promise<void>
271
273
  remove(key: string): Promise<boolean>
@@ -27,6 +27,12 @@ export type FileStatus =
27
27
  | 'InvalidName'
28
28
  | 'InvalidFileExtension'
29
29
  | 'NotEmpty'
30
+ /** Crombolo: the site keeps these files read-only in the back office (story 337). */
31
+ | 'ReadOnly'
32
+
33
+ /** Why the back office may not change a template, stylesheet or script here (story 337). */
34
+ export const CONTENT_FILES_READ_ONLY =
35
+ "This site doesn't allow templates, stylesheets and scripts to be changed in the back office."
30
36
 
31
37
  export type FileResult = { ok: true; path: string } | { ok: false; status: FileStatus }
32
38
 
@@ -4,7 +4,8 @@ import type { Page } from '@crombolo/core'
4
4
  import type { Principal } from './router.ts'
5
5
 
6
6
  export type UserResponse = ResponseOf<'GetUserById'>
7
- export type UserGroupResponse = ResponseOf<'GetUserGroupById'>
7
+ /** Crombolo adds the groups that own it (story 302), which Umbraco's contract does not have. */
8
+ export type UserGroupResponse = ResponseOf<'GetUserGroupById'> & { owners: Array<{ id: string }> }
8
9
 
9
10
  /** Umbraco's `UserOperationStatus` names, as its problem details report them. */
10
11
  export type UserStatus =
@@ -38,6 +39,8 @@ export type UserStatus =
38
39
  | 'DuplicateClientId'
39
40
  | 'InvalidClientId'
40
41
  | 'InvalidResetCode'
42
+ /** Crombolo: a group added or removed is not one the caller owns (story 302). */
43
+ | 'NotGroupOwner'
41
44
 
42
45
  export type UserOrder =
43
46
  | 'UserName'
@@ -185,6 +188,8 @@ export interface UserGroupInput {
185
188
  elementRootAccess: boolean
186
189
  fallbackPermissions: string[]
187
190
  permissions: UserGroupResponse['permissions']
191
+ /** Crombolo: the keys of the groups that own it; absent keeps what it has (story 302). */
192
+ owners?: string[]
188
193
  }
189
194
 
190
195
  /** Umbraco's `UserGroupOperationStatus` names. */
@@ -208,6 +213,13 @@ export type UserGroupStatus =
208
213
  | 'UserNotFound'
209
214
  /** Crombolo: the group comes from the site's files, and this change is the files' to make (story 228). */
210
215
  | 'DeclaredInFiles'
216
+ /** Crombolo: only an administrator creates, changes or deletes a group (story 302). */
217
+ | 'AdministratorsOnly'
218
+ /** Crombolo: the caller is not in any of the group's owners (story 302). */
219
+ | 'NotGroupOwner'
220
+ /** Crombolo: the admin group, or one holding a remote permission, has no owners (story 302). */
221
+ | 'CannotBeOwned'
222
+ | 'OwnerNotFound'
211
223
 
212
224
  export interface UserGroupPort {
213
225
  list(skip: number, take: number): Promise<Page<UserGroupResponse>>
@@ -217,7 +229,10 @@ export interface UserGroupPort {
217
229
  skip: number,
218
230
  take: number,
219
231
  ): Promise<Page<UserGroupResponse>>
220
- get(principal: Principal, key: string): Promise<UserGroupResponse | 'NotFound' | 'Forbidden'>
232
+ get(
233
+ principal: Principal,
234
+ key: string,
235
+ ): Promise<(UserGroupResponse & { canManageMembers: boolean }) | 'NotFound' | 'Forbidden'>
221
236
  items(keys: readonly string[]): Promise<ResponseOf<'GetItemUserGroup'>>
222
237
  create(
223
238
  principal: Principal,