@crombolo/schema 0.40.0 → 0.41.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@crombolo/schema",
3
- "version": "0.40.0",
3
+ "version": "0.41.1",
4
4
  "description": "Internal to crombolo, with no compatibility promise: use @crombolo/crombolo. Schema-as-code for crombolo: TOML document, media and member type definitions.",
5
5
  "license": "MIT",
6
6
  "author": "Crombolo",
@@ -28,7 +28,7 @@
28
28
  "access": "public"
29
29
  },
30
30
  "dependencies": {
31
- "@crombolo/core": "0.40.0",
32
- "@crombolo/data": "0.40.0"
31
+ "@crombolo/core": "0.41.1",
32
+ "@crombolo/data": "0.41.1"
33
33
  }
34
34
  }
package/src/index.ts CHANGED
@@ -16,6 +16,7 @@ export * from './reader.ts'
16
16
  export * from './scaffold.ts'
17
17
  export * from './sync.ts'
18
18
  export * from './upgrade.ts'
19
+ export * from './user-groups.ts'
19
20
  export * from './validate.ts'
20
21
  export * from './validate-forms.ts'
21
22
  export * from './value-migrations.ts'
package/src/load.ts CHANGED
@@ -28,6 +28,7 @@ import {
28
28
  parseSchemaSettings,
29
29
  } from './parse.ts'
30
30
  import { parseForm } from './parse-forms.ts'
31
+ import { parseUserGroup, type SchemaUserGroup, writeUserGroup } from './user-groups.ts'
31
32
  import { parseWorkflow, writeWorkflow } from './workflows.ts'
32
33
  import {
33
34
  writeContentType,
@@ -134,6 +135,15 @@ export function loadSchemaDirectory(
134
135
  files.set(`workflow:${parsed.value.alias}`, file)
135
136
  }
136
137
  }
138
+ const userGroups: SchemaUserGroup[] = []
139
+ for (const file of tomlFiles(join(schemaDir, 'user-groups'))) {
140
+ const parsed = parseUserGroup(rel(file), readFileSync(file, 'utf8'))
141
+ problems.push(...parsed.problems)
142
+ if (parsed.value) {
143
+ userGroups.push(parsed.value)
144
+ files.set(`user-group:${parsed.value.alias}`, file)
145
+ }
146
+ }
137
147
  const languagesFile = join(schemaDir, 'languages.toml')
138
148
  if (existsSync(languagesFile)) {
139
149
  const parsed = parseLanguages(rel(languagesFile), readFileSync(languagesFile, 'utf8'))
@@ -150,6 +160,7 @@ export function loadSchemaDirectory(
150
160
  languages,
151
161
  forms,
152
162
  ...(workflows.length > 0 ? { workflows } : {}),
163
+ ...(userGroups.length > 0 ? { userGroups } : {}),
153
164
  ...(urls ? { urls } : {}),
154
165
  },
155
166
  problems,
@@ -171,6 +182,8 @@ export function hashSchemaSet(set: SchemaSet): string {
171
182
  ...(set.forms ?? []).map((f) => writeForm(f)).sort(),
172
183
  // Absent from a set with none, so every hash taken before workflows stands.
173
184
  ...(set.workflows ?? []).map((w) => writeWorkflow(w)).sort(),
185
+ // Likewise for user groups (story 228).
186
+ ...(set.userGroups ?? []).map((g) => writeUserGroup(g)).sort(),
174
187
  ]
175
188
  return createHash('sha256').update(parts.join('\n---\n')).digest('hex')
176
189
  }
package/src/model.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import type { SchemaForm, SchemaWorkflow, SiteSearchLevel } from '@crombolo/core'
2
+ import type { SchemaUserGroup } from './user-groups.ts'
2
3
 
3
4
  /**
4
5
  * The schema-as-code model: what a site's `schema/*.toml` files describe.
@@ -165,6 +166,8 @@ export interface SchemaSet {
165
166
  urls?: SchemaUrls
166
167
  /** `schema/workflows/*.toml` — approval workflows; see `docs/workflows.md`. */
167
168
  workflows?: SchemaWorkflow[]
169
+ /** `schema/user-groups/*.toml` — the user groups the site declares (story 228). */
170
+ userGroups?: SchemaUserGroup[]
168
171
  }
169
172
 
170
173
  /** How URLs are built, as Umbraco's `Umbraco:CMS:Global` settings set it. */
package/src/sync.ts CHANGED
@@ -36,6 +36,7 @@ import {
36
36
  sameUrlOptions,
37
37
  syncWorkflows,
38
38
  type UrlOptions,
39
+ UserGroupRepository,
39
40
  updateSchemaStateHash,
40
41
  type WorkflowSyncReport,
41
42
  writeUrlOptions,
@@ -110,6 +111,8 @@ export interface SyncReport {
110
111
  urls?: { before: UrlOptions; after: UrlOptions }
111
112
  /** Set when a workflow changed, with what that did to requests in flight. */
112
113
  workflows?: WorkflowSyncReport
114
+ /** The declared user groups this sync created or changed (story 228). */
115
+ userGroups?: { created: string[]; updated: string[] }
113
116
  }
114
117
 
115
118
  export const emptySyncReport = (
@@ -385,6 +388,10 @@ export async function syncSchema(
385
388
  })),
386
389
  )
387
390
  if (workflows.changed.length > 0) report.workflows = workflows
391
+ // The user groups the files declare (story 228): what each is; where
392
+ // it starts and who is in it stay this environment's.
393
+ const groups = await new UserGroupRepository(tx).syncDeclared(set.userGroups ?? [])
394
+ if (groups.created.length > 0 || groups.updated.length > 0) report.userGroups = groups
388
395
  }
389
396
 
390
397
  // ---- templates referenced by types must have a row
@@ -0,0 +1,89 @@
1
+ /**
2
+ * `schema/user-groups/*.toml` — user groups a site declares (story 228): what a
3
+ * group is (its name, sections, permission verbs and languages), never where it
4
+ * starts or who is in it, which each environment's Users section keeps. Strict
5
+ * as the type parser is, and canonical on the way out, so
6
+ * `parseUserGroup(writeUserGroup(x))` equals `x`. `docs/schema-as-code.md`.
7
+ */
8
+ import {
9
+ ADMIN_GROUP_ALIAS,
10
+ isExtensionSectionAlias,
11
+ KNOWN_PERMISSIONS,
12
+ SECTION_ALIASES,
13
+ } from '@crombolo/core'
14
+ import { type ParseResult, Reader, toml } from './reader.ts'
15
+
16
+ export interface SchemaUserGroup {
17
+ alias: string
18
+ name: string
19
+ description?: string
20
+ icon?: string
21
+ /** Application aliases (`content`), or an extension section's manifest alias. */
22
+ sections: string[]
23
+ permissions: string[]
24
+ /** ISO codes, or every language. */
25
+ languages: string[] | 'all'
26
+ }
27
+
28
+ const KEYS = ['alias', 'name', 'description', 'icon', 'sections', 'permissions', 'languages']
29
+
30
+ export function parseUserGroup(file: string, source: string): ParseResult<SchemaUserGroup> {
31
+ const r = new Reader(file)
32
+ const root = toml(source, r)
33
+ if (!root) return { value: undefined, problems: r.problems }
34
+ r.unknown(root, ['user-group'], '')
35
+ const t = r.table(root['user-group'], 'user-group')
36
+ if (!t) {
37
+ r.problem('user-group', 'a [user-group] table is required')
38
+ return { value: undefined, problems: r.problems }
39
+ }
40
+ const path = 'user-group'
41
+ r.unknown(t, KEYS, path)
42
+ const alias = r.str(t, 'alias', path, true) ?? ''
43
+ if (alias.toLowerCase() === ADMIN_GROUP_ALIAS)
44
+ r.problem(
45
+ `${path}.alias`,
46
+ 'the administrators group is built in, and a file cannot redefine it',
47
+ )
48
+ const sections = r.strs(t, 'sections', path)
49
+ for (const section of sections)
50
+ if (!(section in SECTION_ALIASES) && !isExtensionSectionAlias(section))
51
+ r.problem(
52
+ `${path}.sections`,
53
+ `"${section}" is not a section: one of ${Object.keys(SECTION_ALIASES).join(', ')}, or an extension's section alias`,
54
+ )
55
+ const permissions = r.strs(t, 'permissions', path)
56
+ for (const permission of permissions)
57
+ if (!KNOWN_PERMISSIONS.has(permission))
58
+ r.problem(`${path}.permissions`, `"${permission}" is not a permission verb`)
59
+ let languages: string[] | 'all' = []
60
+ if (t.languages === 'all') languages = 'all'
61
+ else if (t.languages !== undefined) languages = r.strs(t, 'languages', path)
62
+ const description = r.str(t, 'description', path)
63
+ const icon = r.str(t, 'icon', path)
64
+ const value: SchemaUserGroup = {
65
+ alias,
66
+ name: r.str(t, 'name', path, true) ?? '',
67
+ ...(description !== undefined ? { description } : {}),
68
+ ...(icon !== undefined ? { icon } : {}),
69
+ sections,
70
+ permissions,
71
+ languages,
72
+ }
73
+ return { value: r.problems.length === 0 ? value : undefined, problems: r.problems }
74
+ }
75
+
76
+ const str = (value: string) => JSON.stringify(value)
77
+ const list = (values: readonly string[]) => `[${values.map(str).join(', ')}]`
78
+
79
+ export function writeUserGroup(group: SchemaUserGroup): string {
80
+ const out = ['[user-group]', `alias = ${str(group.alias)}`, `name = ${str(group.name)}`]
81
+ if (group.description !== undefined) out.push(`description = ${str(group.description)}`)
82
+ if (group.icon !== undefined) out.push(`icon = ${str(group.icon)}`)
83
+ out.push(
84
+ `sections = ${list(group.sections)}`,
85
+ `permissions = ${list(group.permissions)}`,
86
+ `languages = ${group.languages === 'all' ? '"all"' : list(group.languages)}`,
87
+ )
88
+ return `${out.join('\n')}\n`
89
+ }