@crombolo/backoffice-host 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.
@@ -0,0 +1,249 @@
1
+ /**
2
+ * Settings → E-mail (story 340): the provider the site sends through, its key
3
+ * and the sender. Set by the site's configuration, it is shown read-only.
4
+ * Administrators only; every change is audited.
5
+ */
6
+
7
+ import { css, html, nothing } from '@umbraco-cms/backoffice/external/lit'
8
+ import { UmbLitElement } from '@umbraco-cms/backoffice/lit-element'
9
+ import { umbConfirmModal } from '@umbraco-cms/backoffice/modal'
10
+ import { bodyOf, draftOf, fieldsFor, PROVIDERS, summaryOf } from './email-settings-words.js'
11
+
12
+ const base = () => {
13
+ const href = document.querySelector('base')?.getAttribute('href') ?? '/umbraco/'
14
+ return `${href.replace(/\/$/, '')}/crombolo/api/email`
15
+ }
16
+
17
+ /** The answer and its body; an outcome the server refused carries its message. */
18
+ async function call(path, method = 'GET', body) {
19
+ const response = await fetch(`${base()}${path}`, {
20
+ method,
21
+ credentials: 'include',
22
+ headers: {
23
+ accept: 'application/json',
24
+ ...(body ? { 'content-type': 'application/json' } : {}),
25
+ },
26
+ ...(body ? { body: JSON.stringify(body) } : {}),
27
+ })
28
+ const json = await response.json().catch(() => ({}))
29
+ if (!response.ok) throw new Error(json.message ?? `E-mail settings answered ${response.status}.`)
30
+ return json
31
+ }
32
+
33
+ export default class CromboloEmailSettingsElement extends UmbLitElement {
34
+ static properties = {
35
+ _view: { state: true },
36
+ _draft: { state: true },
37
+ _problem: { state: true },
38
+ _done: { state: true },
39
+ _busy: { state: true },
40
+ }
41
+
42
+ constructor() {
43
+ super()
44
+ this._view = undefined
45
+ this._draft = draftOf(undefined)
46
+ this._problem = ''
47
+ this._done = ''
48
+ this._busy = false
49
+ }
50
+
51
+ connectedCallback() {
52
+ super.connectedCallback()
53
+ this.#load()
54
+ }
55
+
56
+ async #load() {
57
+ try {
58
+ this._view = await call('/settings')
59
+ this._draft = draftOf(this._view)
60
+ } catch (error) {
61
+ this._problem = error.message
62
+ this._view ??= { source: 'none', status: {} }
63
+ }
64
+ }
65
+
66
+ async #run(work, done) {
67
+ this._busy = true
68
+ this._problem = ''
69
+ this._done = ''
70
+ try {
71
+ await work()
72
+ await this.#load()
73
+ this._done = done
74
+ } catch (error) {
75
+ this._problem = error.message
76
+ } finally {
77
+ this._busy = false
78
+ }
79
+ }
80
+
81
+ #save() {
82
+ return this.#run(() => call('/settings', 'PUT', bodyOf(this._draft)), 'Saved.')
83
+ }
84
+
85
+ #test() {
86
+ return this.#run(() => call('/test', 'POST', {}), 'A test e-mail is on its way to you.')
87
+ }
88
+
89
+ async #clear() {
90
+ await umbConfirmModal(this, {
91
+ color: 'danger',
92
+ headline: 'Remove the e-mail settings?',
93
+ content:
94
+ 'Invitations, password resets, notifications and form e-mails stop until it is set up again.',
95
+ confirmLabel: 'Remove',
96
+ })
97
+ await this.#run(() => call('/settings', 'DELETE'), 'Removed.')
98
+ }
99
+
100
+ #input(name, label, options = {}) {
101
+ return html`<uui-form-layout-item>
102
+ <uui-label slot="label" for=${name}>${label}</uui-label>
103
+ <uui-input
104
+ id=${name}
105
+ label=${label}
106
+ type=${options.type ?? 'text'}
107
+ placeholder=${options.placeholder ?? ''}
108
+ ?readonly=${options.readonly}
109
+ .value=${this._draft[name] ?? ''}
110
+ @input=${(event) => (this._draft = { ...this._draft, [name]: event.target.value })}></uui-input>
111
+ ${options.hint ? html`<div slot="description">${options.hint}</div>` : nothing}
112
+ </uui-form-layout-item>`
113
+ }
114
+
115
+ #form() {
116
+ const view = this._view
117
+ const locked = view.source === 'config' || view.source === 'off'
118
+ if (view.source === 'off') return nothing
119
+ const draft = this._draft
120
+ const provider = PROVIDERS[draft.provider]
121
+ const keyHint = locked
122
+ ? undefined
123
+ : !view.canKeepKeys
124
+ ? "This site can't keep keys until whoever hosts it gives it a secret key."
125
+ : view.keyKept
126
+ ? 'A key is kept. Leave this empty to keep it.'
127
+ : undefined
128
+ return html`<div class="form">
129
+ <uui-form-layout-item>
130
+ <uui-label slot="label" for="provider">Provider</uui-label>
131
+ <uui-select
132
+ id="provider"
133
+ label="Provider"
134
+ ?disabled=${locked}
135
+ .options=${Object.entries(PROVIDERS).map(([value, { label }]) => ({
136
+ name: label,
137
+ value,
138
+ selected: draft.provider === value,
139
+ }))}
140
+ @change=${(event) => (this._draft = { ...this._draft, provider: event.target.value })}></uui-select>
141
+ </uui-form-layout-item>
142
+ ${this.#input('from', 'Send from', { placeholder: 'no-reply@example.com', readonly: locked })}
143
+ ${this.#input('fromName', 'Sender name (optional)', { placeholder: 'Example', readonly: locked })}
144
+ ${fieldsFor(draft.provider).map((field) =>
145
+ this.#input(field.name, field.label, { placeholder: field.placeholder, readonly: locked }),
146
+ )}
147
+ ${
148
+ locked
149
+ ? nothing
150
+ : this.#input('key', provider?.key ?? 'Key', {
151
+ type: 'password',
152
+ hint: keyHint,
153
+ placeholder: view.keyKept ? '••••••••' : '',
154
+ })
155
+ }
156
+ <div class="actions">
157
+ ${
158
+ locked
159
+ ? nothing
160
+ : html`<uui-button look="primary" label="Save" ?disabled=${this._busy} @click=${() => this.#save()}></uui-button>`
161
+ }
162
+ <uui-button
163
+ look="secondary"
164
+ label="Send me a test e-mail"
165
+ ?disabled=${this._busy || !view.status?.available}
166
+ @click=${() => this.#test()}></uui-button>
167
+ ${
168
+ view.source === 'settings'
169
+ ? html`<uui-button
170
+ look="secondary"
171
+ color="danger"
172
+ label="Remove"
173
+ ?disabled=${this._busy}
174
+ @click=${() => this.#clear()}></uui-button>`
175
+ : nothing
176
+ }
177
+ </div>
178
+ </div>`
179
+ }
180
+
181
+ render() {
182
+ const view = this._view
183
+ if (!view) return html`<uui-loader></uui-loader>`
184
+ const locked = view.source === 'config' || view.source === 'off'
185
+ const affects = view.status?.affects ?? []
186
+ return html`<uui-box headline="E-mail">
187
+ <p class=${view.status?.available ? 'summary' : 'summary warning'}>
188
+ ${view.status?.available ? nothing : html`<uui-icon name="icon-alert"></uui-icon>`}
189
+ ${summaryOf(view)}
190
+ </p>
191
+ ${
192
+ affects.length > 0
193
+ ? html`<p>Until it is, these are unavailable:</p>
194
+ <ul class="affected">
195
+ ${affects.map((feature) => html`<li>${feature}</li>`)}
196
+ </ul>`
197
+ : nothing
198
+ }
199
+ ${locked ? html`<p class="note">Whoever hosts the site changes it in the server's configuration.</p>` : nothing}
200
+ ${this._problem ? html`<p class="problem" role="alert">${this._problem}</p>` : nothing}
201
+ ${this._done ? html`<p class="done" role="status">${this._done}</p>` : nothing}
202
+ ${this.#form()}
203
+ </uui-box>`
204
+ }
205
+
206
+ static styles = css`
207
+ :host {
208
+ display: block;
209
+ padding: var(--uui-size-layout-1);
210
+ max-width: 72rem;
211
+ }
212
+ .summary {
213
+ display: flex;
214
+ align-items: center;
215
+ gap: var(--uui-size-space-2);
216
+ margin-top: 0;
217
+ font-weight: 600;
218
+ }
219
+ .warning uui-icon {
220
+ color: var(--uui-color-warning-standalone);
221
+ }
222
+ .note {
223
+ color: var(--uui-color-text-alt);
224
+ }
225
+ .affected {
226
+ margin-top: 0;
227
+ }
228
+ .form {
229
+ max-width: 40rem;
230
+ }
231
+ uui-input,
232
+ uui-select {
233
+ width: 100%;
234
+ }
235
+ .actions {
236
+ display: flex;
237
+ gap: var(--uui-size-space-2);
238
+ margin-top: var(--uui-size-space-5);
239
+ }
240
+ .problem {
241
+ color: var(--uui-color-danger);
242
+ }
243
+ .done {
244
+ color: var(--uui-color-positive);
245
+ }
246
+ `
247
+ }
248
+
249
+ customElements.define('crombolo-email-settings', CromboloEmailSettingsElement)
@@ -0,0 +1,307 @@
1
+ /**
2
+ * What the user group editor says and how its choices become verbs (story 338).
3
+ * Pure, so the unit tests read it outside a browser and hold its levels to the
4
+ * server's (`@crombolo/core`, `permission-levels.ts`).
5
+ */
6
+
7
+ export const TABS = [
8
+ { id: 'access', label: 'Access' },
9
+ { id: 'permissions', label: 'Permissions' },
10
+ { id: 'exceptions', label: 'Exceptions' },
11
+ { id: 'transfer', label: 'Data Transfer', adminOnly: true },
12
+ { id: 'people', label: 'People' },
13
+ ]
14
+
15
+ /** The areas of the back office, as Access lists them, by section alias. */
16
+ export const AREAS = [
17
+ { section: 'Umb.Section.Content', label: 'Content', line: "The site's pages." },
18
+ {
19
+ section: 'Umb.Section.Library',
20
+ label: 'Library',
21
+ line: 'Shared pieces pages are built from: blocks, menus and promos.',
22
+ },
23
+ { section: 'Umb.Section.Media', label: 'Media', line: 'Images, documents and other files.' },
24
+ {
25
+ section: 'Crombolo.Section.Forms',
26
+ label: 'Forms',
27
+ line: 'Forms visitors fill in, and their answers.',
28
+ },
29
+ {
30
+ section: 'Umb.Section.Members',
31
+ label: 'Members',
32
+ line: 'People who sign in to the site itself.',
33
+ },
34
+ {
35
+ section: 'Umb.Section.Settings',
36
+ label: 'Settings',
37
+ line: "Content types, templates, redirects and the site's tools.",
38
+ },
39
+ {
40
+ section: 'Umb.Section.Users',
41
+ label: 'Users',
42
+ line: 'The people who use the back office, and their groups.',
43
+ },
44
+ {
45
+ section: 'Umb.Section.Translation',
46
+ label: 'Translation',
47
+ line: 'The dictionary of words templates show in each language.',
48
+ },
49
+ {
50
+ section: 'Crombolo.Section.Workflow',
51
+ label: 'Workflow',
52
+ line: 'The approval steps a page goes through before it is published.',
53
+ },
54
+ ]
55
+
56
+ const D = (verb) => `Umb.Document.${verb}`
57
+ const E = (verb) => `Umb.Element.${verb}`
58
+
59
+ const cumulative = (steps) => steps.map((_, i) => steps.slice(0, i + 1).flat())
60
+
61
+ /** Each kind's levels: an id, a label, a line, and its verbs, each level adding to the one before. */
62
+ export const LEVELS = {
63
+ pages: zip(
64
+ [
65
+ ['none', 'None', "Can't open pages"],
66
+ ['see', 'See', 'Read, no changes'],
67
+ ['edit', 'Edit', 'Write and organise drafts'],
68
+ ['publish', 'Edit and publish', 'Also publish, unpublish, roll back'],
69
+ ['full', 'Full control', 'Also delete, protect, set hostnames'],
70
+ ],
71
+ cumulative([
72
+ [],
73
+ [D('Read')],
74
+ [D('Create'), D('Update'), D('Move'), D('Duplicate'), D('Sort'), D('PropertyValue.Write')],
75
+ [D('Publish'), D('Unpublish'), D('Rollback')],
76
+ [
77
+ D('Delete'),
78
+ 'Umb.DocumentRecycleBin.Restore',
79
+ D('PublicAccess'),
80
+ D('CultureAndHostnames'),
81
+ D('Permissions'),
82
+ ],
83
+ ]),
84
+ ),
85
+ library: zip(
86
+ [
87
+ ['none', 'None', "Can't open the Library"],
88
+ ['see', 'See', 'Read, no changes'],
89
+ ['edit', 'Edit', 'Write and organise drafts'],
90
+ ['publish', 'Edit and publish', 'Also publish, unpublish, roll back'],
91
+ ['full', 'Full control', 'Also delete'],
92
+ ],
93
+ cumulative([
94
+ [],
95
+ [E('Read')],
96
+ [E('Create'), E('Update'), E('Move'), E('Duplicate')],
97
+ [E('Publish'), E('Unpublish'), E('Rollback')],
98
+ [E('Delete')],
99
+ ]),
100
+ ),
101
+ media: zip(
102
+ [
103
+ ['none', 'None', "Can't open media"],
104
+ ['see', 'See', 'Look, no changes'],
105
+ ['edit', 'Upload and edit', 'Add, change and remove files'],
106
+ ],
107
+ cumulative([[], ['Crombolo.Media.Read'], ['Crombolo.Media.Update']]),
108
+ ),
109
+ forms: zip(
110
+ [
111
+ ['none', 'None', "Can't open forms"],
112
+ ['see', 'See forms and answers', 'Read, no changes'],
113
+ ['manage', 'Manage forms', 'Design forms, change and delete answers'],
114
+ ],
115
+ cumulative([
116
+ [],
117
+ ['Crombolo.Form.Read', 'Crombolo.FormEntry.Read'],
118
+ ['Crombolo.Form.Manage', 'Crombolo.FormEntry.Manage'],
119
+ ]),
120
+ ),
121
+ }
122
+
123
+ function zip(words, verbs) {
124
+ return words.map(([id, label, line], i) => ({ id, label, line, verbs: verbs[i] }))
125
+ }
126
+
127
+ /** Forms' sensitive answers: a toggle beside the level. */
128
+ export const SENSITIVE_ANSWERS = 'Crombolo.FormEntry.Sensitive'
129
+
130
+ /** Which area each kind belongs to, for "Access is off". */
131
+ export const KIND_AREA = {
132
+ pages: 'Umb.Section.Content',
133
+ library: 'Umb.Section.Library',
134
+ media: 'Umb.Section.Media',
135
+ forms: 'Crombolo.Section.Forms',
136
+ }
137
+
138
+ export const KIND_LABEL = { pages: 'Pages', library: 'Library', media: 'Media', forms: 'Forms' }
139
+
140
+ /** Every verb a kind's levels speak for. */
141
+ export const kindVerbs = (kind) => LEVELS[kind].at(-1).verbs
142
+
143
+ /** The level a group's verbs are for one kind, or 'custom'. */
144
+ export function levelOf(kind, verbs) {
145
+ const own = new Set(kindVerbs(kind))
146
+ const held = new Set(verbs.filter((verb) => own.has(verb)))
147
+ const found = LEVELS[kind].find(
148
+ (level) => level.verbs.length === held.size && level.verbs.every((verb) => held.has(verb)),
149
+ )
150
+ return found ? found.id : 'custom'
151
+ }
152
+
153
+ /** A group's verbs with one kind set to a level; nothing else touched. */
154
+ export function withLevel(verbs, kind, levelId) {
155
+ const own = new Set(kindVerbs(kind))
156
+ const level = LEVELS[kind].find((l) => l.id === levelId)
157
+ if (!level) throw new Error(`${kind} has no level "${levelId}"`)
158
+ return [...new Set([...verbs.filter((verb) => !own.has(verb)), ...level.verbs])]
159
+ }
160
+
161
+ /** A group's verbs with one verb turned on or off, for Customise. */
162
+ export const withVerb = (verbs, verb, on) =>
163
+ on ? [...new Set([...verbs, verb])] : verbs.filter((v) => v !== verb)
164
+
165
+ /** Whether the Library follows Pages: the same level, translated. */
166
+ export function libraryFollowsPages(verbs) {
167
+ const pages = levelOf('pages', verbs)
168
+ return pages !== 'custom' && levelOf('library', verbs) === pages
169
+ }
170
+
171
+ /** What Customise shows for each verb, in plain words. */
172
+ export const VERB_WORDS = {
173
+ [D('Read')]: 'See pages',
174
+ [D('Create')]: 'Create pages and blueprints',
175
+ [D('Update')]: 'Change pages',
176
+ [D('Move')]: 'Move pages',
177
+ [D('Duplicate')]: 'Copy pages',
178
+ [D('Sort')]: 'Reorder pages',
179
+ [D('PropertyValue.Write')]: 'Change any field',
180
+ [D('Publish')]: 'Publish pages',
181
+ [D('Unpublish')]: 'Unpublish pages',
182
+ [D('Rollback')]: 'Roll back to an earlier version',
183
+ [D('Delete')]: 'Delete pages',
184
+ 'Umb.DocumentRecycleBin.Restore': 'Restore pages from the recycle bin',
185
+ [D('PublicAccess')]: 'Make a page members-only',
186
+ [D('CultureAndHostnames')]: 'Set languages and hostnames',
187
+ [D('Permissions')]: 'Change who can edit a page',
188
+ [E('Read')]: 'See Library items',
189
+ [E('Create')]: 'Create Library items and folders',
190
+ [E('Update')]: 'Change Library items and folders',
191
+ [E('Move')]: 'Move Library items and folders',
192
+ [E('Duplicate')]: 'Copy Library items',
193
+ [E('Publish')]: 'Publish Library items',
194
+ [E('Unpublish')]: 'Unpublish Library items',
195
+ [E('Rollback')]: "Roll back a Library item's earlier version",
196
+ [E('Delete')]: 'Delete Library items and folders',
197
+ 'Crombolo.Media.Read': 'See media',
198
+ 'Crombolo.Media.Update': 'Upload, change and remove media',
199
+ 'Crombolo.Form.Read': 'See forms',
200
+ 'Crombolo.FormEntry.Read': 'Read answers',
201
+ 'Crombolo.Form.Manage': 'Design forms',
202
+ 'Crombolo.FormEntry.Manage': 'Change and delete answers',
203
+ }
204
+
205
+ /** Data Transfer's permissions, as sentences, in order; `under` indents one beneath another. */
206
+ export const TRANSFER = [
207
+ {
208
+ group: 'Content',
209
+ verb: 'Crombolo.Remote.Pull',
210
+ label: "Copy this site's content to their own machine",
211
+ line: 'A pull: pages, media and settings, to work on locally.',
212
+ },
213
+ {
214
+ group: 'Content',
215
+ verb: 'Crombolo.Remote.PullFull',
216
+ under: 'Crombolo.Remote.Pull',
217
+ label: '…including members and users',
218
+ line: "People's personal data, sealed with a passphrase.",
219
+ },
220
+ {
221
+ group: 'Content',
222
+ verb: 'Crombolo.Remote.Push',
223
+ label: 'Send content to this site',
224
+ line: 'A push or a content bundle: replaces pages here with theirs.',
225
+ },
226
+ {
227
+ group: 'Content',
228
+ verb: 'Crombolo.Remote.PushProtected',
229
+ under: 'Crombolo.Remote.Push',
230
+ label: '…even when this site is protected, with nobody approving',
231
+ line: 'Production is usually protected.',
232
+ },
233
+ {
234
+ group: 'Structure and code',
235
+ verb: 'Crombolo.Remote.ApplyStructure',
236
+ label: "Change this site's structure",
237
+ line: 'Content types, stylesheets and scripts arriving in a bundle.',
238
+ },
239
+ {
240
+ group: 'Structure and code',
241
+ verb: 'Crombolo.Remote.InstallCode',
242
+ warning: true,
243
+ label: 'Install code on this site',
244
+ line: 'Templates, server code and whole-site pushes: all of these run on the server. Nobody has this unless a group gives it, administrators included. Keep it on a group of its own, such as "Deployers".',
245
+ },
246
+ ]
247
+
248
+ /** Verbs the editor speaks for; anything else a group holds is an extension's, kept as it is. */
249
+ export const OWNED_VERBS = new Set([
250
+ ...Object.keys(LEVELS).flatMap((kind) => kindVerbs(kind)),
251
+ SENSITIVE_ANSWERS,
252
+ ...TRANSFER.map((entry) => entry.verb),
253
+ ])
254
+
255
+ const levelLabel = (kind, id) => LEVELS[kind].find((level) => level.id === id)?.label ?? 'Custom'
256
+
257
+ /** An exception as the list says it: where, and what is different there. */
258
+ export function exceptionSentence(exception, defaults) {
259
+ if (exception.kind === 'field')
260
+ return {
261
+ where: `${exception.typeName} › ${exception.fieldName}`,
262
+ whereNote: 'a field, on every page of that type',
263
+ what: exception.verbs.includes(D('PropertyValue.Write'))
264
+ ? 'Can change it'
265
+ : "Can see it but can't change it",
266
+ }
267
+ const kind = exception.kind === 'library' ? 'library' : 'pages'
268
+ const here = levelOf(kind, exception.verbs)
269
+ const usual = levelOf(kind, defaults)
270
+ return {
271
+ where: exception.name,
272
+ whereNote: 'and everything below it',
273
+ what: `${KIND_LABEL[kind]}: ${levelLabel(kind, here)}${here === usual ? '' : `, instead of ${levelLabel(kind, usual)}`}`,
274
+ }
275
+ }
276
+
277
+ /** The read-back before an exception is added. */
278
+ export function exceptionSummary(groupName, kind, placeName, levelId) {
279
+ if (kind === 'field')
280
+ return levelId === 'edit'
281
+ ? `${groupName} will be able to change ${placeName}.`
282
+ : `${groupName} will see ${placeName} but not change it.`
283
+ const what = {
284
+ none: 'not open',
285
+ see: 'see, but not change,',
286
+ edit: 'see and change, but not publish,',
287
+ publish: 'see, change and publish',
288
+ full: 'do anything with',
289
+ }[levelId]
290
+ return `${groupName} will ${what} ${kind === 'library' ? 'Library items' : 'pages'} under ${placeName}.`
291
+ }
292
+
293
+ /** The site's built-in groups, whose permissions the editor shows but never changes. */
294
+ export const SYSTEM_GROUPS = new Set(['admin', 'sensitiveData', 'translator'])
295
+
296
+ /** Why a built-in group's access and permissions can't be changed here. */
297
+ export const systemGroupNote = (alias) =>
298
+ alias.toLowerCase() === 'admin'
299
+ ? 'This is a system group - settings are locked'
300
+ : "This is one of the site's built-in groups, so its access and permissions can't be changed here."
301
+
302
+ /** What a level shows for a group: an administrator's is always the top one, as they can do everything. */
303
+ export const shownLevel = (kind, verbs, alias) =>
304
+ (alias ?? '').toLowerCase() === 'admin' ? LEVELS[kind].at(-1).id : levelOf(kind, verbs)
305
+
306
+ export const ACCESS_OFF = (label) =>
307
+ `This group can't open ${label}. Turn it on under Access to choose what they can do with it.`