Sending method, review M-1 and M-2: the plugin's own status write never stores a choice; a non-method is refused

- M-1: the save rule of `emailMethod` ran after the early return for the plugin's status writes, so the first
  failed (or recovered) send stored the method the read showed as if an admin had chosen it. The rule is now a
  hook of its own that runs on every write: a write that does not name the method leaves what is stored, empty
  included.
- M-2: a sent value that is not null or one of the three methods (a number, a list, another word) is refused
  with a ValidationError (400) before it reaches the database.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
This commit is contained in:
Radosław Smoliński
2026-11-26 23:08:34 +01:00
co-authored by Claude Fable 5.2
parent f2da28e7cb
commit 8c19391590
2 changed files with 69 additions and 17 deletions
+27 -17
View File
@@ -92,36 +92,46 @@ export function buildSiteIntegrations({
...(writeOnly ? { afterRead: [maskSecretsOnRestore] } : {}),
// What the caller sent for the sending method, before Payload merges the stored document into the data.
beforeChange: [
// Keys edited in the panel → clear that integration's old status, so a stale
// "not working" banner doesn't linger until the next form submission.
// "Sending method" (`emailMethod`) — on EVERY write, the plugin's own included (the status written after a
// send): `data` is merged with the document as it was READ by now, and that read shows the method in effect
// as selected — which must never be stored as a choice nobody made. So: what the caller really sent was
// noted before the merge (beforeOperation below; without that note, a value that differs from the one read
// was sent), and what is stored was noted by the field's own read.
({ context, data, originalDoc, req }) => {
if (context?.ipalIntegrationStatus || !data) {return data}
const changed = (k: string) => k in data && data[k] !== originalDoc?.[k]
const cleared = { checkedAt: null, code: null, message: null, since: null, state: null }
// "Sending method" (`emailMethod`). `data` is merged with the document as it was READ by now, and that read
// shows the method in effect as selected — which must not be stored as a choice nobody made. So: what the
// caller really sent was noted before the merge (beforeOperation below; without that note, a value that
// differs from the one read was sent), and what is stored was noted by the field's own read.
const noted = context?.iaplMailSent as { method?: unknown; sent?: boolean; transport?: boolean } | undefined
if (!data) {return data}
const noted = context?.iaplMailSent as { sent?: boolean; transport?: boolean } | undefined
const sent = noted ? noted.sent : data.emailMethod !== originalDoc?.emailMethod
const stored = isMailMethod(context?.iaplMailMethodStored) ? context.iaplMailMethodStored : null
if (sent && isMailMethod(data.emailMethod)) {
if (data.emailMethod === 'relay' && !mailRelay) {
throw new ValidationError({ errors: [{ message: 'This sending method is switched off for this site.', path: 'emailMethod' }], global: 'site-integrations' }, req.t)
const refuse = (message: string): never => {
throw new ValidationError({ errors: [{ message, path: 'emailMethod' }], global: 'site-integrations' }, req.t)
}
if (sent && data.emailMethod !== null && data.emailMethod !== undefined) {
// anything but one of the three values (a number, a list, another word) is refused here: the database
// would answer with an error of its own, or take it
if (!isMailMethod(data.emailMethod)) {return refuse('This is not a sending method.')}
if (data.emailMethod === 'relay' && !mailRelay) {return refuse('This sending method is switched off for this site.')}
if (shownMailMethod({ ...(originalDoc ?? {}), emailMethod: stored } as never, mailRelay) !== data.emailMethod) {
data.emailStatus = { checkedAt: null, code: null, message: null, since: null, state: null }
}
if (shownMailMethod({ ...(originalDoc ?? {}), emailMethod: stored } as never, mailRelay) !== data.emailMethod) {data.emailStatus = cleared}
// the stored field of 1.6 – 1.17.0 follows where it can say it
data.emailTransport = data.emailMethod === 'graph' ? 'graph' : 'smtp'
} else if (sent && data.emailMethod !== null && data.emailMethod !== undefined) {
// not a method: left as it is — the field's own validation refuses it
} else if (sent) {
data.emailMethod = null // un-chosen on purpose (`setMailMethod(payload, null)`): the order of 1.17.0 again
} else if ((noted ? noted.transport : true) && changed('emailTransport')) {
} else if ((noted ? noted.transport : true) && 'emailTransport' in data && data.emailTransport !== originalDoc?.emailTransport) {
// written the old way (a script from before 1.17.1): it means what it meant — the order of 1.17.0
data.emailMethod = null
} else {
// a write that does not name the method leaves what is stored — empty included
data.emailMethod = stored
}
return data
},
// Keys edited in the panel → clear that integration's old status, so a stale
// "not working" banner doesn't linger until the next form submission.
({ context, data, originalDoc }) => {
if (context?.ipalIntegrationStatus || !data) {return data}
const changed = (k: string) => k in data && data[k] !== originalDoc?.[k]
const cleared = { checkedAt: null, code: null, message: null, since: null, state: null }
if (changed('turnstileSecretKey') || changed('turnstileSiteKey')) {data.turnstileStatus = cleared}
if (Object.keys(data).some((k) => (k === 'emailTransport' || k.startsWith('smtp')) && changed(k))) {
data.emailStatus = cleared