emailengine-app 2.80.0 → 2.80.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.
Files changed (101) hide show
  1. package/.release-please-manifest.json +1 -1
  2. package/CHANGELOG.md +24 -0
  3. package/README.md +6 -0
  4. package/data/google-crawlers.json +1 -1
  5. package/lib/api-routes/account-routes.js +81 -1
  6. package/lib/api-routes/blocklist-routes.js +24 -0
  7. package/lib/api-routes/delivery-test-routes.js +6 -0
  8. package/lib/api-routes/gateway-routes.js +30 -0
  9. package/lib/api-routes/license-routes.js +10 -0
  10. package/lib/api-routes/mcp-oauth-routes.js +3 -3
  11. package/lib/api-routes/mcp-routes.js +39 -21
  12. package/lib/api-routes/oauth2-app-routes.js +38 -1
  13. package/lib/api-routes/outbox-routes.js +6 -0
  14. package/lib/api-routes/permission-map.js +208 -43
  15. package/lib/api-routes/pubsub-routes.js +8 -0
  16. package/lib/api-routes/route-helpers.js +58 -11
  17. package/lib/api-routes/settings-routes.js +50 -4
  18. package/lib/api-routes/stats-routes.js +6 -0
  19. package/lib/api-routes/template-routes.js +21 -0
  20. package/lib/api-routes/token-routes.js +34 -2
  21. package/lib/api-routes/webhook-route-routes.js +13 -0
  22. package/lib/email-client/base-client.js +6 -3
  23. package/lib/email-client/credential-errors.js +4 -0
  24. package/lib/email-client/imap/login-failure.js +52 -0
  25. package/lib/email-client/imap/subconnection.js +2 -1
  26. package/lib/email-client/imap-client.js +3 -6
  27. package/lib/enum-descriptions.js +12 -2
  28. package/lib/imapproxy/imap-server.js +1 -1
  29. package/lib/mcp/oauth.js +46 -8
  30. package/lib/mcp/protocol.js +78 -35
  31. package/lib/mcp/tools.js +53 -17
  32. package/lib/privileged-settings.js +147 -0
  33. package/lib/schemas.js +60 -16
  34. package/lib/settings.js +13 -0
  35. package/lib/tls/catalog.js +428 -0
  36. package/lib/tls/context.js +87 -161
  37. package/lib/tls/listeners.js +62 -0
  38. package/lib/tls/status.js +128 -79
  39. package/lib/tls/store.js +3 -2
  40. package/lib/token-permission-view.js +252 -102
  41. package/lib/token-permissions.js +176 -26
  42. package/lib/tokens.js +7 -6
  43. package/lib/tools.js +4 -3
  44. package/lib/ui-routes/admin-config-routes.js +41 -21
  45. package/lib/ui-routes/admin-entities-routes.js +23 -13
  46. package/lib/ui-routes/mcp-consent-routes.js +72 -31
  47. package/lib/ui-routes/tls-config-routes.js +121 -74
  48. package/package.json +12 -12
  49. package/sbom.json +1 -1
  50. package/server.js +7 -1
  51. package/static/css/flyonui.css +1 -1
  52. package/static/js/ui.js +89 -36
  53. package/static/licenses.html +46 -28
  54. package/translations/messages.pot +74 -68
  55. package/views/account/login-redirect.hbs +1 -1
  56. package/views/account/passkeys.hbs +2 -2
  57. package/views/accounts/account.hbs +2 -2
  58. package/views/accounts/browse.hbs +1 -1
  59. package/views/accounts/edit.hbs +1 -1
  60. package/views/accounts/register/imap-server.hbs +1 -1
  61. package/views/config/ai.hbs +1 -1
  62. package/views/config/branding.hbs +2 -2
  63. package/views/config/document-store/chat.hbs +1 -1
  64. package/views/config/document-store/index.hbs +1 -1
  65. package/views/config/imap-proxy.hbs +1 -1
  66. package/views/config/license.hbs +1 -1
  67. package/views/config/mcp.hbs +88 -52
  68. package/views/config/network.hbs +1 -1
  69. package/views/config/oauth/app.hbs +2 -2
  70. package/views/config/service.hbs +1 -1
  71. package/views/config/smtp.hbs +1 -1
  72. package/views/config/tls.hbs +266 -165
  73. package/views/config/webhooks.hbs +2 -2
  74. package/views/internals/index.hbs +1 -1
  75. package/views/layout/app.hbs +6 -6
  76. package/views/layout/login.hbs +5 -5
  77. package/views/layout/main.hbs +2 -2
  78. package/views/layout/prompt.hbs +3 -3
  79. package/views/layout/public.hbs +1 -1
  80. package/views/mcp/authorize.hbs +47 -45
  81. package/views/partials/ace_assets.hbs +1 -1
  82. package/views/partials/add_account_modal.hbs +1 -1
  83. package/views/partials/filter_editor_js.hbs +2 -2
  84. package/views/partials/gateway_js.hbs +1 -1
  85. package/views/partials/mcp_access_levels.hbs +16 -9
  86. package/views/partials/mcp_tool_count.hbs +15 -14
  87. package/views/partials/oauth_form.hbs +1 -1
  88. package/views/partials/public_theme_init.hbs +1 -1
  89. package/views/partials/template_form.hbs +1 -1
  90. package/views/partials/theme_init.hbs +1 -1
  91. package/views/partials/webhooks_editor_functions.hbs +1 -1
  92. package/views/reference/index.hbs +1 -1
  93. package/views/reference/redirect.hbs +1 -1
  94. package/views/reference/tag.hbs +1 -1
  95. package/views/reference/token.hbs +1 -1
  96. package/views/templates/template.hbs +1 -1
  97. package/views/tokens/new.hbs +175 -74
  98. package/views/unsubscribe.hbs +1 -1
  99. package/views/webhooks/webhook.hbs +1 -1
  100. package/workers/api.js +9 -8
  101. package/workers/smtp.js +1 -1
@@ -8,13 +8,13 @@
8
8
  // label here would render as a blank checkbox, and one removed would leave a control that posts a
9
9
  // value the API refuses.
10
10
 
11
- const { ACTION, GROUP, GRANTABLE_GROUPS, SURFACE_GRANTS, PER_REQUEST_SURFACES } = require('./api-routes/permission-map');
11
+ const { ACTION, GROUP, GRANTABLE_GROUPS, IMPLICIT_GROUPS, SURFACE_GRANTS, PER_REQUEST_SURFACES, MCP_SCOPES } = require('./api-routes/permission-map');
12
12
  const tokenPermissions = require('./token-permissions');
13
13
  const { ENUM_DESCRIPTIONS } = require('./enum-descriptions');
14
14
 
15
15
  // Sentence-case labels for the checkbox rows. The slug itself is shown beside each one, so this only
16
16
  // has to read well - it is not the identifier. The same labels are also lowercased into running
17
- // prose (see MCP_GRANT_WORDING below), so each one has to work as a sentence fragment too.
17
+ // prose (the grant wording of the MCP access levels below), so each one has to work as a sentence fragment too.
18
18
  const ACTION_LABELS = {
19
19
  [ACTION.READ]: 'Read',
20
20
  [ACTION.WRITE]: 'Create and modify',
@@ -35,27 +35,47 @@ const GROUP_LABELS = {
35
35
  [GROUP.GATEWAY]: 'SMTP gateways',
36
36
  [GROUP.EVENTS]: 'Change stream',
37
37
  [GROUP.DIAGNOSTICS]: 'Statistics and status',
38
- [GROUP.LOGS]: 'Connection logs'
38
+ [GROUP.LOGS]: 'Connection logs',
39
+ [GROUP.SETTINGS]: 'Instance settings',
40
+ [GROUP.OAUTH2]: 'OAuth2 applications',
41
+ [GROUP.LICENSE]: 'License',
42
+ [GROUP.TOKEN]: 'Access tokens',
43
+ [GROUP.PROVISIONING]: 'Account and gateway setup'
39
44
  };
40
45
 
41
46
  // The one spelling of the slug-to-label lookup. Object.hasOwn rather than a bare lookup: a slug
42
47
  // of `constructor` would otherwise render an Object.prototype member.
43
48
  const mapLabels = (values, labels) => values.map(value => (Object.hasOwn(labels, value) ? labels[value] : value));
44
49
 
50
+ // The `action:group` spelling of a pair, for keyed lookups and the browser-side lists
51
+ const grantKey = grant => `${grant.action}:${grant.group}`;
52
+
45
53
  // The same vocabulary as a sentence fragment: lowercased and joined as an English list, for
46
54
  // prose like the consent screen's grant sentences ("read, create and modify, and send email").
47
55
  const GRANT_LIST_FORMAT = new Intl.ListFormat('en', { style: 'long', type: 'conjunction' });
48
56
  const humanGrantList = (values, labels) => GRANT_LIST_FORMAT.format(mapLabels(values, labels).map(word => word.toLowerCase()));
49
57
 
50
- // Clusters for the section list. Thirteen rows at identical weight is a scan rather than a choice,
51
- // and the split is the one a reader already has: the mail itself, the things that act on it, and the
52
- // things that only watch.
58
+ // Clusters for the section list. Eighteen rows at identical weight is a scan rather than a choice,
59
+ // and the split is the one a reader already has: the mail itself, the things that act on it, the
60
+ // things that only watch, and the instance underneath all of it.
53
61
  const GROUP_CLUSTERS = [
54
62
  { label: 'Mail', groups: [GROUP.ACCOUNT, GROUP.MAILBOX, GROUP.MESSAGE, GROUP.SUBMIT, GROUP.OUTBOX, GROUP.EXPORT] },
55
63
  { label: 'Configuration', groups: [GROUP.TEMPLATE, GROUP.BLOCKLIST, GROUP.WEBHOOK, GROUP.GATEWAY] },
56
- { label: 'Monitoring', groups: [GROUP.EVENTS, GROUP.DIAGNOSTICS, GROUP.LOGS] }
64
+ { label: 'Monitoring', groups: [GROUP.EVENTS, GROUP.DIAGNOSTICS, GROUP.LOGS] },
65
+ { label: 'Instance', groups: [GROUP.SETTINGS, GROUP.PROVISIONING, GROUP.OAUTH2, GROUP.TOKEN, GROUP.LICENSE] }
57
66
  ];
58
67
 
68
+ // How the token list names the sections an absent `groups` axis reaches (IMPLICIT_GROUPS): by
69
+ // what it leaves out, because that is the interesting part. The difference is rendered as the
70
+ // cluster heading the token form shows when it is exactly one cluster, and as the section labels
71
+ // otherwise, so a section added outside the implicit set changes the wording rather than hiding
72
+ // behind a heading that no longer covers it.
73
+ const IMPLICIT_GROUPS_LABEL = (() => {
74
+ const left = GRANTABLE_GROUPS.filter(group => !IMPLICIT_GROUPS.includes(group));
75
+ const cluster = GROUP_CLUSTERS.find(entry => entry.groups.length === left.length && entry.groups.every(group => left.includes(group)));
76
+ return `every section except ${cluster ? cluster.label : GRANT_LIST_FORMAT.format(mapLabels(left, GROUP_LABELS))}`;
77
+ })();
78
+
59
79
  // Starting points, because a fourteen-row matrix is not how anyone begins. Each is a real shape
60
80
  // somebody asks for rather than an illustration of the axes.
61
81
  const PRESETS = [
@@ -87,7 +107,7 @@ const PRESETS = [
87
107
  },
88
108
  {
89
109
  label: 'Everything allowed',
90
- title: 'Every action on every section that can be granted. Still cannot reach credentials or settings',
110
+ title: 'Every action on every section that can be granted. Still cannot create tokens or read stored credentials',
91
111
  actions: Object.values(ACTION),
92
112
  groups: GRANTABLE_GROUPS
93
113
  }
@@ -99,78 +119,171 @@ const SURFACE_LABELS = {
99
119
  smtp: 'send over SMTP',
100
120
  'imap-proxy': 'connect through the IMAP proxy',
101
121
  metrics: 'read the metrics endpoint',
102
- mcp: 'call MCP tools'
122
+ 'mcp-manage': 'call the MCP management tools',
123
+ mcp: 'call the MCP mail tools'
103
124
  };
104
125
 
105
- // The narrowings behind the MCP access-level choice, offered identically by the OAuth consent
106
- // page and the admin generator. Derived once at load from the same table the scope enforcement
107
- // reads, nothing else. Lives here rather than on the pages that post it because this module owns
108
- // how permission records are presented, and a second derivation had already drifted (all groups
109
- // instead of the readable ones).
126
+ // The MCP access levels: what the OAuth consent prompt, the MCP config page's generator and the
127
+ // token form offer, and what each choice mints. Two sections, because the endpoint has two tool
128
+ // sets behind two scopes and a person approving an agent decides about them separately -
129
+ // managing the instance is the default question, mailbox contents the opt-in one. Each level is
130
+ // a pair list rather than a two-axis record: one token can hold a management level and a mail
131
+ // level at once, and the cross product of two axes cannot keep "operate the instance" from
132
+ // leaking write access onto the messages. Derived once at load from the surface tables the
133
+ // enforcement reads, nothing else; lives here because this module owns how permission records
134
+ // are presented, and a second derivation had already drifted once.
110
135
  //
111
- // Three levels, because that is where consent-screen comprehension and real agent shapes meet:
112
- // read-only for browsing, "mail agent" for the triage-and-reply agent that must never reach the
113
- // delete tool, and full for everything the surface exposes. Full is the absence of a permissions
114
- // record - the mcp scope itself is the bound.
115
- const MCP_READ_ONLY_PERMISSIONS = {
116
- actions: [ACTION.READ],
117
- groups: SURFACE_GRANTS.mcp.filter(grant => grant.action === ACTION.READ).map(grant => grant.group)
118
- };
136
+ // A section is declined with the `none` level, which every section has and which mints nothing:
137
+ // one value per section, so no page has to carry "is this section on" beside the level. The
138
+ // consent prompt and the generator offer it as a radio; the token form declines a section by
139
+ // leaving its scope unticked and offers the other levels only.
140
+ //
141
+ // A minted token always carries the pair list of the levels approved, never an absent record:
142
+ // a consent given for the tools of today must not grow to include a tool shipped next release.
143
+ const pairsOf = (scope, predicate) => SURFACE_GRANTS[scope].filter(predicate).map(grant => ({ action: grant.action, group: grant.group }));
119
144
 
120
- // The permission record's two axes are independent allowlists, so deriving both from a pair list
121
- // widens it (read x submit, write x template, ...). Inert here: an mcp-scoped token is usable
122
- // only through /mcp, where surfaceAdmits() re-checks every request against the exact grant pairs.
123
- const mailAgentGrants = SURFACE_GRANTS.mcp.filter(grant => grant.action !== ACTION.DESTRUCTIVE);
124
- const MCP_MAIL_AGENT_PERMISSIONS = {
125
- actions: [...new Set(mailAgentGrants.map(grant => grant.action))],
126
- groups: [...new Set(mailAgentGrants.map(grant => grant.group))]
145
+ // Everything a level carries: `caveat` is the sentence the consent prompt shows under the derived
146
+ // grant list - what the level still cannot do, and what it can that a reader might not expect -
147
+ // and `lead` on the section opens that sentence. The wording of the pairs themselves, and the
148
+ // alert severity, are computed onto each level below.
149
+ const MCP_SECTIONS = {
150
+ manage: {
151
+ scope: MCP_SCOPES.manage,
152
+ label: 'Instance management',
153
+ lead: 'Through the management tools the client will be able to',
154
+ defaultLevel: 'observe',
155
+ none: { label: 'No management access', hint: 'The client gets only what is granted below.' },
156
+ levels: [
157
+ {
158
+ value: 'observe',
159
+ label: 'Observe',
160
+ hint: 'Recommended. Read accounts, settings, queues, OAuth2 applications, gateways, tokens and logs. Cannot change anything.',
161
+ caveat: 'It cannot change anything, create tokens or read stored credentials. Connection logs name folders and subjects, and the token audit log records who read which account.',
162
+ // Less read/provisioning, which is only POST /v1/verifyAccount: a connection to a
163
+ // host the caller names, authenticated with credentials the caller supplies. The
164
+ // other reads that leave the instance (autodiscovery, the OAuth2 application
165
+ // check) go where the caller's own domain or a stored record points; this one is
166
+ // a port and password probe for whatever network the instance sits on, which a
167
+ // level sold as unable to change anything must not be. test/mcp-tools-test.js
168
+ // pins the pair to that one route, so the group is the whole of the exclusion.
169
+ pairs: pairsOf(MCP_SCOPES.manage, grant => grant.action === ACTION.READ && grant.group !== GROUP.PROVISIONING)
170
+ },
171
+ {
172
+ value: 'operate',
173
+ label: 'Operate',
174
+ hint: 'Also reconnect, add and test accounts, change settings, and manage OAuth2 applications, gateways and templates.',
175
+ caveat: 'Settings changes take effect at once, and moving the webhook target sends future message notifications, message text included, elsewhere. Reconfiguring an account or gateway can send its stored credential to a new host, and reconfiguring an OAuth2 application can redirect where future sign-ins deliver their authorization codes. It cannot delete anything, create tokens or read stored credentials.',
176
+ pairs: pairsOf(MCP_SCOPES.manage, grant => grant.action !== ACTION.DESTRUCTIVE)
177
+ },
178
+ {
179
+ value: 'administer',
180
+ label: 'Administer',
181
+ hint: 'Also delete accounts, applications, gateways and templates, revoke tokens and flush accounts.',
182
+ caveat: 'Everything Operate allows, plus deletion. It still cannot create tokens or read stored credentials.',
183
+ pairs: pairsOf(MCP_SCOPES.manage, () => true)
184
+ }
185
+ ]
186
+ },
187
+ mail: {
188
+ scope: MCP_SCOPES.mail,
189
+ label: 'Email access',
190
+ lead: 'Through the mail tools the client will be able to',
191
+ defaultLevel: 'none',
192
+ none: {
193
+ label: 'No mail access',
194
+ hint: 'Recommended unless the client should read mail. Email content then flows into whatever model the connected client runs.'
195
+ },
196
+ levels: [
197
+ {
198
+ value: 'read',
199
+ label: 'Read-only',
200
+ hint: 'List, search and read mail - cannot send, delete or change anything.',
201
+ caveat: 'It cannot send email, delete anything or change anything in the mailboxes.',
202
+ pairs: pairsOf(MCP_SCOPES.mail, grant => grant.action === ACTION.READ)
203
+ },
204
+ {
205
+ value: 'mail',
206
+ label: 'Mail agent',
207
+ hint: 'Read, organize, draft and send mail - everything except the delete tool.',
208
+ caveat: 'Everything except the delete tool. Instructions inside received mail are a real prompt-injection risk once sending is granted.',
209
+ pairs: pairsOf(MCP_SCOPES.mail, grant => grant.action !== ACTION.DESTRUCTIVE)
210
+ },
211
+ {
212
+ value: 'full',
213
+ label: 'Full access',
214
+ hint: 'Every mail tool, including deleting messages.',
215
+ caveat: 'Instructions inside received mail are a real prompt-injection risk once sending is granted.',
216
+ pairs: pairsOf(MCP_SCOPES.mail, () => true)
217
+ }
218
+ ]
219
+ }
127
220
  };
128
221
 
129
- // What each posted access-level slug mints. The consent route validates against these keys, so a
130
- // level added here is automatically accepted there - and the fail-safe direction is fixed by the
131
- // route defaulting to 'read' when no level is posted at all.
132
- const MCP_ACCESS_LEVELS = {
133
- read: MCP_READ_ONLY_PERMISSIONS,
134
- mail: MCP_MAIL_AGENT_PERMISSIONS,
135
- full: null
136
- };
222
+ // What each level's grants mean in plain words, and how the consent prompt colours it: the
223
+ // narrowest level of a section is information, every wider one a warning. Computed once at load
224
+ // onto the level itself, so a level cannot be offered without wording.
225
+ for (const section of Object.values(MCP_SECTIONS)) {
226
+ section.levels.forEach((level, index) => {
227
+ level.actions = humanGrantList([...new Set(level.pairs.map(grant => grant.action))], ACTION_LABELS);
228
+ level.groups = humanGrantList([...new Set(level.pairs.map(grant => grant.group))], GROUP_LABELS);
229
+ level.variant = index ? 'warning' : 'info';
230
+ });
231
+ }
137
232
 
138
- // How the levels are offered on screen, in the order they are listed. The consent prompt and
139
- // the admin generator render the same radio group from this table (the mcp_access_levels
140
- // partial), because two hand-written fieldsets had already drifted on the read-only wording.
141
- // A level added to MCP_ACCESS_LEVELS needs an entry here to be offered anywhere.
142
- // What each level's grants mean in plain words, computed once at load and shown wherever a level
143
- // is approved (today the OAuth consent prompt). Keyed off MCP_ACCESS_LEVELS itself so a level
144
- // cannot exist without wording; the full level stores null - no permissions record, the mcp
145
- // scope is the bound - so its wording derives from the surface grant table directly. The mail
146
- // and full group lists currently come out identical (the destructive grants add no group of
147
- // their own), but each level derives its own list on purpose - they diverge the moment a
148
- // destructive-only group joins the surface.
149
- const MCP_GRANT_WORDING = Object.fromEntries(
150
- Object.entries(MCP_ACCESS_LEVELS).map(([level, permissions]) => {
151
- const actions = permissions ? permissions.actions : [...new Set(SURFACE_GRANTS.mcp.map(grant => grant.action))];
152
- const groups = permissions ? permissions.groups : [...new Set(SURFACE_GRANTS.mcp.map(grant => grant.group))];
153
- return [level, { actions: humanGrantList(actions, ACTION_LABELS), groups: humanGrantList(groups, GROUP_LABELS) }];
154
- })
155
- );
156
-
157
- const MCP_ACCESS_LEVEL_OPTIONS = [
158
- {
159
- value: 'read',
160
- label: 'Read-only',
161
- hint: 'Recommended. List, search and read mail - cannot send, delete or change anything.'
162
- },
163
- {
164
- value: 'mail',
165
- label: 'Mail agent',
166
- hint: 'Read, organize, draft and send mail - everything except the delete tool.'
167
- },
168
- {
169
- value: 'full',
170
- label: 'Full access',
171
- hint: 'Every MCP tool, including deleting messages.'
233
+ /**
234
+ * The level record a section offers under a name, or null for `none` and for a name it does not
235
+ * have. Callers that need to tell those two apart check the name first; declining is not a level.
236
+ */
237
+ function mcpLevel(section, value) {
238
+ return section.levels.find(level => level.value === value) || null;
239
+ }
240
+
241
+ /**
242
+ * What a choice of levels mints: the scopes of the sections that were not declined, management
243
+ * first, and the union of their pair lists as a permissions record.
244
+ *
245
+ * The single spelling for the three pages that mint an MCP token, so the same choice means the
246
+ * same credential on all of them. An unknown level throws rather than minting something narrower
247
+ * or wider than asked - the routes validate the level names against MCP_SECTIONS before calling
248
+ * this, so a throw here is a page posting a vocabulary this table does not have.
249
+ *
250
+ * @param {Object} choice - a level per section key (`manage`, `mail`); absent or 'none' declines it
251
+ * @returns {{scopes: Array<String>, permissions: {grants: Array}}} empty scopes when everything was declined
252
+ */
253
+ function mcpGrantsFor(choice) {
254
+ const scopes = [];
255
+ const grants = [];
256
+ const seen = new Set();
257
+
258
+ for (const [key, section] of Object.entries(MCP_SECTIONS)) {
259
+ const value = choice && choice[key];
260
+ if (!value || value === 'none') {
261
+ continue;
262
+ }
263
+ const level = mcpLevel(section, value);
264
+ if (!level) {
265
+ throw new Error(`Unknown ${key} access level: ${value}`);
266
+ }
267
+ scopes.push(section.scope);
268
+ for (const grant of level.pairs) {
269
+ const pairKey = grantKey(grant);
270
+ if (!seen.has(pairKey)) {
271
+ seen.add(pairKey);
272
+ grants.push({ action: grant.action, group: grant.group });
273
+ }
274
+ }
172
275
  }
173
- ];
276
+
277
+ return { scopes, permissions: { grants } };
278
+ }
279
+
280
+ /**
281
+ * The level names a section's form control may post: its levels plus `none`. The one spelling
282
+ * of the vocabulary the consent route validates against.
283
+ */
284
+ function mcpLevelNames(section) {
285
+ return ['none'].concat(section.levels.map(level => level.value));
286
+ }
174
287
 
175
288
  /**
176
289
  * View model for the permission controls on the token form.
@@ -213,7 +326,7 @@ function formModel() {
213
326
  mode: PER_REQUEST_SURFACES.has(scope) ? 'any' : 'all',
214
327
  actionList: [...new Set(SURFACE_GRANTS[scope].map(grant => grant.action))].join(','),
215
328
  groupList: [...new Set(SURFACE_GRANTS[scope].map(grant => grant.group))].join(','),
216
- grantList: SURFACE_GRANTS[scope].map(grant => `${grant.action}:${grant.group}`).join(',')
329
+ grantList: SURFACE_GRANTS[scope].map(grantKey).join(',')
217
330
  })),
218
331
 
219
332
  // data-actions/data-groups are read by the preset buttons in views/tokens/new.hbs
@@ -226,9 +339,10 @@ function formModel() {
226
339
  };
227
340
  }
228
341
 
229
- // Phrases the two axes as something a person reads rather than as two labelled fields. An axis that
230
- // is absent is not a restriction, so it is left out of the sentence entirely - saying "all sections"
231
- // would imply a grant the record does not make.
342
+ // Phrases the two axes as something a person reads rather than as two labelled fields. An absent
343
+ // actions axis is not a restriction, so it is left out of the sentence entirely - saying "every
344
+ // action" would imply a grant the record does not make. (An absent groups axis IS one, and arrives
345
+ // here already labelled by what it leaves out.)
232
346
  //
233
347
  // An axis that names everything is collapsed rather than enumerated. Spelling out all thirteen
234
348
  // sections produced a line long enough to push the table sideways, and it read as a detailed
@@ -244,7 +358,7 @@ function sentenceFor(actions, groups, coversAllActions, coversAllGroups) {
244
358
  const where = coversAllGroups ? 'every section' : groups;
245
359
 
246
360
  if (verb && where) {
247
- return coversAllActions && coversAllGroups ? 'Full access, except settings, credentials and tokens' : `${verb} in ${where}`;
361
+ return coversAllActions && coversAllGroups ? 'Full access, except creating tokens and reading credentials' : `${verb} in ${where}`;
248
362
  }
249
363
  if (verb) {
250
364
  return coversAllActions ? 'Full access' : `${verb} only`;
@@ -255,11 +369,25 @@ function sentenceFor(actions, groups, coversAllActions, coversAllGroups) {
255
369
  return 'Restricted';
256
370
  }
257
371
 
372
+ // An axis as labels: null for an absent axis (not a restriction), 'none' for an empty allowlist
373
+ // (which allows nothing, the opposite of absent), otherwise the labels in the record's order
374
+ const labelAxis = (values, labels) => {
375
+ if (!Array.isArray(values)) {
376
+ return null;
377
+ }
378
+ if (!values.length) {
379
+ return 'none';
380
+ }
381
+ return mapLabels(values, labels).join(', ');
382
+ };
383
+
384
+ const coversAll = (values, whole) => Array.isArray(values) && values.length === whole.length && whole.every(entry => values.includes(entry));
385
+
258
386
  /**
259
387
  * One-line description of what a stored `permissions` record allows, for the token list.
260
388
  *
261
389
  * @param {*} permissions - the record's `permissions` field
262
- * @returns {Object|null} { actions, groups, unreadable } or null when the token is not narrowed
390
+ * @returns {Object|null} { actions, groups, sentence, unreadable } or null when the token is not narrowed
263
391
  */
264
392
  function summarize(permissions) {
265
393
  // Asked of the module that enforces it rather than re-derived here. `{}`, an unknown axis and a
@@ -276,30 +404,53 @@ function summarize(permissions) {
276
404
  return { actions: null, groups: null, unreadable: true };
277
405
  }
278
406
 
279
- const label = (values, labels) => {
280
- if (!Array.isArray(values)) {
281
- return null;
282
- }
283
- if (!values.length) {
284
- // An empty allowlist allows nothing, which is the opposite of the axis being absent
285
- return 'none';
286
- }
287
- return mapLabels(values, labels).join(', ');
288
- };
407
+ if (!Array.isArray(permissions.grants)) {
408
+ // The two-axis form, read out as written rather than through effectiveGrants(): an absent
409
+ // actions axis is not a restriction and stays out of the sentence, and an absent groups
410
+ // axis is what the enforcement makes of it, named by what it leaves out
411
+ const actions = labelAxis(permissions.actions, ACTION_LABELS);
412
+ const groups = labelAxis(permissions.groups, GROUP_LABELS) ?? IMPLICIT_GROUPS_LABEL;
289
413
 
290
- const actions = label(permissions.actions, ACTION_LABELS);
291
- const groups = label(permissions.groups, GROUP_LABELS);
414
+ return {
415
+ actions,
416
+ groups,
417
+ // One line a person can read, rather than two labelled fragments. "Can: Read / In:
418
+ // Messages" is the record read out loud; this is what the record means.
419
+ sentence: sentenceFor(actions, groups, coversAll(permissions.actions, Object.values(ACTION)), coversAll(permissions.groups, GRANTABLE_GROUPS)),
420
+ unreadable: false
421
+ };
422
+ }
292
423
 
293
- const covers = (values, whole) => Array.isArray(values) && values.length === whole.length && whole.every(entry => values.includes(entry));
424
+ // The pair-list form, through the reader the enforcement shares. A list that happens to be a
425
+ // full cross product is the two-axis record in another spelling and gets that record's
426
+ // sentence; anything else is described section by section, in declaration order rather than
427
+ // list order so two records granting the same pairs read the same however they were written,
428
+ // because "read on Messages, write on Templates" is the shape the form exists for and it has
429
+ // no two-axis sentence.
430
+ const grants = tokenPermissions.effectiveGrants(permissions);
431
+ const actionOrder = Object.values(ACTION);
432
+ const actions = actionOrder.filter(action => grants.some(grant => grant.action === action));
433
+ const groups = Object.values(GROUP).filter(group => grants.some(grant => grant.group === group));
294
434
 
295
- return {
296
- actions,
297
- groups,
298
- // One line a person can read, rather than two labelled fragments. "Can: Read / In: Messages"
299
- // is the record read out loud; this is what the record means.
300
- sentence: sentenceFor(actions, groups, covers(permissions.actions, Object.values(ACTION)), covers(permissions.groups, GRANTABLE_GROUPS)),
435
+ const summary = {
436
+ actions: labelAxis(actions, ACTION_LABELS),
437
+ groups: labelAxis(groups, GROUP_LABELS),
301
438
  unreadable: false
302
439
  };
440
+
441
+ if (!grants.length || grants.length === actions.length * groups.length) {
442
+ summary.sentence = sentenceFor(summary.actions, summary.groups, coversAll(actions, actionOrder), coversAll(groups, GRANTABLE_GROUPS));
443
+ return summary;
444
+ }
445
+
446
+ summary.sentence = groups
447
+ .map(group => {
448
+ const granted = actions.filter(action => grants.some(grant => grant.action === action && grant.group === group));
449
+ return `${mapLabels([group], GROUP_LABELS)[0]}: ${humanGrantList(granted, ACTION_LABELS)}`;
450
+ })
451
+ .join('; ');
452
+
453
+ return summary;
303
454
  }
304
455
 
305
456
  module.exports = {
@@ -307,9 +458,8 @@ module.exports = {
307
458
  summarize,
308
459
  ACTION_LABELS,
309
460
  GROUP_LABELS,
310
- MCP_READ_ONLY_PERMISSIONS,
311
- MCP_MAIL_AGENT_PERMISSIONS,
312
- MCP_ACCESS_LEVELS,
313
- MCP_ACCESS_LEVEL_OPTIONS,
314
- MCP_GRANT_WORDING
461
+ MCP_SECTIONS,
462
+ mcpLevel,
463
+ mcpLevelNames,
464
+ mcpGrantsFor
315
465
  };