@crombolo/api-management 0.62.0 → 0.64.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.62.0",
3
+ "version": "0.64.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.62.0",
33
- "@crombolo/contracts": "0.62.0",
34
- "@crombolo/core": "0.62.0"
32
+ "@crombolo/content": "0.64.0",
33
+ "@crombolo/contracts": "0.64.0",
34
+ "@crombolo/core": "0.64.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. */
@@ -2,7 +2,7 @@
2
2
  import type { ResponseOf } from '@crombolo/contracts'
3
3
  import type { DataTypeModel, TreeItem } from '@crombolo/core'
4
4
  import { invalidSkipTake, notFound, problemDetails, problemResponse } from '@crombolo/core'
5
- import type { DataTypePort } from '../ports-content.ts'
5
+ import type { DataTypePort, PortFailure } from '../ports-content.ts'
6
6
  import type { ManagementApiRouter, RequestContext } from '../router.ts'
7
7
  import {
8
8
  created,
@@ -94,7 +94,11 @@ export function registerDataTypeHandlers(router: ManagementApiRouter, port: Data
94
94
  router.handle('PostDataType', async (ctx) => {
95
95
  const body = (await ctx.request.json()) as Record<string, unknown>
96
96
  const key = typeof body.id === 'string' && body.id ? body.id : crypto.randomUUID()
97
- await port.save(fromRequest(key, body), ctx.principal as NonNullable<typeof ctx.principal>)
97
+ const refused = await port.save(
98
+ fromRequest(key, body),
99
+ ctx.principal as NonNullable<typeof ctx.principal>,
100
+ )
101
+ if (refused) return refusedSave(refused)
98
102
  ctx.notifications.push({ message: 'Data type created', category: 'Data Type', type: 'Success' })
99
103
  return created(`${ctx.url.origin}/umbraco/management/api/v1/data-type/${key}`, key)
100
104
  })
@@ -105,10 +109,11 @@ export function registerDataTypeHandlers(router: ManagementApiRouter, port: Data
105
109
  if (!existing) return problemResponse(notFound('The data type could not be found'))
106
110
  const body = (await ctx.request.json()) as Record<string, unknown>
107
111
  // Keep the storage type the editor was created with.
108
- await port.save(
112
+ const refused = await port.save(
109
113
  { ...fromRequest(key, body), dbType: existing.dbType },
110
114
  ctx.principal as NonNullable<typeof ctx.principal>,
111
115
  )
116
+ if (refused) return refusedSave(refused)
112
117
  ctx.notifications.push({ message: 'Data type saved', category: 'Data Type', type: 'Success' })
113
118
  return new Response(null, { status: 200 })
114
119
  })
@@ -300,3 +305,13 @@ export function registerDataTypeHandlers(router: ManagementApiRouter, port: Data
300
305
  )
301
306
  })
302
307
  }
308
+
309
+ /** A save the server refused, such as one that would leave the schema files invalid. */
310
+ const refusedSave = (failure: PortFailure) =>
311
+ problemResponse(
312
+ problemDetails({
313
+ title: 'The data type was not saved',
314
+ status: failure.status ?? 400,
315
+ detail: failure.reason,
316
+ }),
317
+ )
@@ -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 })
@@ -55,7 +55,7 @@ const USER_PROBLEMS: Partial<Record<UserStatus, [number, string, string]>> = {
55
55
  CannotInvite: [
56
56
  500,
57
57
  'Cannot send user invitation',
58
- 'This site cannot send e-mail, so users cannot be invited. Configure an e-mail provider — CROMBOLO_EMAIL_PROVIDER, or `email` in crombolo.config.ts — or create the user and give them a password instead.',
58
+ 'This site cannot send e-mail, so users cannot be invited. Set up e-mail in Settings, under E-mail, or create the user and give them a password instead.',
59
59
  ],
60
60
  InvalidPassword: [
61
61
  400,
@@ -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
 
@@ -238,7 +238,8 @@ export interface DataTypePort extends TreePort {
238
238
  ): Promise<{ valueTypeName: string; jsonSchema: Record<string, unknown> } | undefined>
239
239
  byKey(key: string): Promise<DataTypeModel | undefined>
240
240
  byKeys(keys: readonly string[]): Promise<DataTypeModel[]>
241
- save(model: DataTypeModel, principal: Principal): Promise<void>
241
+ /** A failure when the save would leave the schema files invalid (story 344). */
242
+ save(model: DataTypeModel, principal: Principal): Promise<PortFailure | undefined>
242
243
  /** 'in-use' when a property type still uses it. */
243
244
  remove(key: string): Promise<boolean | 'in-use'>
244
245
  filter(
@@ -266,6 +267,8 @@ export interface DataTypePort extends TreePort {
266
267
  }
267
268
 
268
269
  export interface TemplatePort extends TreePort {
270
+ /** Crombolo: the site keeps template files read-only in the back office (story 337). */
271
+ readOnly?: boolean
269
272
  byKey(key: string): Promise<ComponentModel | undefined>
270
273
  save(model: ComponentModel, principal: Principal): Promise<void>
271
274
  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,