@coffre/ui 0.9.0 → 0.10.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 (109) hide show
  1. package/dist/chunks/{audit-sentence-JrQIUq6j.js → audit-sentence-DpYySuqg.js} +74 -7
  2. package/dist/chunks/audit-sentence-DpYySuqg.js.map +1 -0
  3. package/dist/chunks/{connected-apps-Dr8UgjK7.js → connected-apps-D48SFJsG.js} +4 -4
  4. package/dist/chunks/{connected-apps-Dr8UgjK7.js.map → connected-apps-D48SFJsG.js.map} +1 -1
  5. package/dist/chunks/{delete-place-CQCDHkne.js → delete-place-Cg_M6rAq.js} +4 -4
  6. package/dist/chunks/{delete-place-CQCDHkne.js.map → delete-place-Cg_M6rAq.js.map} +1 -1
  7. package/dist/chunks/{directory-XcSbDRJv.js → directory-_LTgdiIM.js} +11 -9
  8. package/dist/chunks/directory-_LTgdiIM.js.map +1 -0
  9. package/dist/chunks/{folders-Dw240WMJ.js → folders-D3A1NGpt.js} +2 -2
  10. package/dist/chunks/{folders-Dw240WMJ.js.map → folders-D3A1NGpt.js.map} +1 -1
  11. package/dist/chunks/{grants-CmdZjKYU.js → grants-CNM913EY.js} +6 -6
  12. package/dist/chunks/{grants-CmdZjKYU.js.map → grants-CNM913EY.js.map} +1 -1
  13. package/dist/chunks/{multi-combobox-CsIXihuc.js → multi-combobox-B0JzP1QN.js} +2 -2
  14. package/dist/chunks/{multi-combobox-CsIXihuc.js.map → multi-combobox-B0JzP1QN.js.map} +1 -1
  15. package/dist/chunks/{offboarding-CrgwaetA.js → offboarding-BeaNM3sf.js} +3 -3
  16. package/dist/chunks/{offboarding-CrgwaetA.js.map → offboarding-BeaNM3sf.js.map} +1 -1
  17. package/dist/chunks/{personal-tokens-Dvl1FktE.js → personal-tokens-KsXajpx4.js} +4 -4
  18. package/dist/chunks/{personal-tokens-Dvl1FktE.js.map → personal-tokens-KsXajpx4.js.map} +1 -1
  19. package/dist/chunks/{principal-page-bOaEYWJB.js → principal-page-BaiZXh6D.js} +94 -78
  20. package/dist/chunks/principal-page-BaiZXh6D.js.map +1 -0
  21. package/dist/chunks/{project-page-BI--vjt3.js → project-page-CYZ_MkfX.js} +2 -2
  22. package/dist/chunks/{project-page-BI--vjt3.js.map → project-page-CYZ_MkfX.js.map} +1 -1
  23. package/dist/chunks/protected-BL_DsZO6.js +22 -0
  24. package/dist/chunks/protected-BL_DsZO6.js.map +1 -0
  25. package/dist/chunks/{queries-epz4MIuW.js → queries-Danu0o75.js} +99 -31
  26. package/dist/chunks/queries-Danu0o75.js.map +1 -0
  27. package/dist/chunks/{references-DImfw0cz.js → references-BK2aPXae.js} +4 -4
  28. package/dist/chunks/{references-DImfw0cz.js.map → references-BK2aPXae.js.map} +1 -1
  29. package/dist/chunks/{role-field-C1KhNaIR.js → role-field-DIJiRz9B.js} +8 -5
  30. package/dist/chunks/role-field-DIJiRz9B.js.map +1 -0
  31. package/dist/chunks/{row-state-BsIHspy4.js → row-state-xtRGFnDc.js} +2 -2
  32. package/dist/chunks/{row-state-BsIHspy4.js.map → row-state-xtRGFnDc.js.map} +1 -1
  33. package/dist/chunks/{theme-3b4DnCow.js → theme-Ck45wzWB.js} +2 -2
  34. package/dist/chunks/{theme-3b4DnCow.js.map → theme-Ck45wzWB.js.map} +1 -1
  35. package/dist/chunks/{ui-2kwXXfrX.js → ui-D-blFWfx.js} +29 -5
  36. package/dist/chunks/{ui-2kwXXfrX.js.map → ui-D-blFWfx.js.map} +1 -1
  37. package/dist/chunks/{use-action-BB-RcHo1.js → use-action-BafZVwux.js} +2 -2
  38. package/dist/chunks/{use-action-BB-RcHo1.js.map → use-action-BafZVwux.js.map} +1 -1
  39. package/dist/chunks/{use-change-BY6KPmnJ.js → use-change-DhV6i_Ge.js} +218 -101
  40. package/dist/chunks/use-change-DhV6i_Ge.js.map +1 -0
  41. package/dist/chunks/use-roles-Daje0ikO.js +224 -0
  42. package/dist/chunks/use-roles-Daje0ikO.js.map +1 -0
  43. package/dist/chunks/{use-shell-C_NmvCt_.js → use-shell-BX9mzfaL.js} +2 -2
  44. package/dist/chunks/{use-shell-C_NmvCt_.js.map → use-shell-BX9mzfaL.js.map} +1 -1
  45. package/dist/index.d.ts +8 -3
  46. package/dist/index.js +29 -15
  47. package/dist/index.js.map +1 -1
  48. package/dist/pages/account.js +9 -9
  49. package/dist/pages/approval.js +1 -1
  50. package/dist/pages/audit.js +96 -34
  51. package/dist/pages/audit.js.map +1 -1
  52. package/dist/pages/device-login.js +1 -1
  53. package/dist/pages/environment.js +255 -115
  54. package/dist/pages/environment.js.map +1 -1
  55. package/dist/pages/login.js +2 -2
  56. package/dist/pages/oauth-authorize.js +1 -1
  57. package/dist/pages/project-access.js +79 -46
  58. package/dist/pages/project-access.js.map +1 -1
  59. package/dist/pages/project-environments.js +79 -30
  60. package/dist/pages/project-environments.js.map +1 -1
  61. package/dist/pages/project-settings.js +7 -7
  62. package/dist/pages/project.js +2 -2
  63. package/dist/pages/projects.js +26 -14
  64. package/dist/pages/projects.js.map +1 -1
  65. package/dist/pages/service-account.js +22 -15
  66. package/dist/pages/service-account.js.map +1 -1
  67. package/dist/pages/service-accounts.js +4 -4
  68. package/dist/pages/settings.js +580 -8
  69. package/dist/pages/settings.js.map +1 -1
  70. package/dist/pages/unregistered.js +1 -1
  71. package/dist/pages/user.js +5 -5
  72. package/dist/pages/users.js +4 -4
  73. package/package.json +3 -3
  74. package/src/components/directory.tsx +5 -2
  75. package/src/components/grants.tsx +1 -1
  76. package/src/components/principal-page.tsx +38 -23
  77. package/src/components/protected.tsx +17 -0
  78. package/src/components/role-editor.tsx +582 -0
  79. package/src/components/role-field.tsx +7 -3
  80. package/src/components/scope-picker.tsx +19 -9
  81. package/src/components/shell.tsx +7 -1
  82. package/src/components/ui.tsx +25 -2
  83. package/src/lib/access-plan.ts +35 -36
  84. package/src/lib/audit-sentences.ts +75 -9
  85. package/src/lib/changes.ts +46 -8
  86. package/src/lib/project-access.ts +4 -78
  87. package/src/lib/queries.ts +97 -29
  88. package/src/lib/roles.ts +201 -0
  89. package/src/lib/row-reads.ts +12 -0
  90. package/src/lib/secret-edit-batch.ts +147 -56
  91. package/src/lib/sensitivity.ts +50 -0
  92. package/src/lib/service-setup.ts +20 -8
  93. package/src/lib/use-roles.ts +17 -0
  94. package/src/options.ts +2 -3
  95. package/src/pages/audit.tsx +99 -26
  96. package/src/pages/environment.tsx +264 -101
  97. package/src/pages/project-access.tsx +49 -19
  98. package/src/pages/project-environments.tsx +62 -3
  99. package/src/pages/projects.tsx +3 -1
  100. package/src/pages/settings.tsx +2 -0
  101. package/src/parity.ts +42 -5
  102. package/src/shared/models.ts +2 -9
  103. package/src/styles/globals.css +322 -25
  104. package/dist/chunks/audit-sentence-JrQIUq6j.js.map +0 -1
  105. package/dist/chunks/directory-XcSbDRJv.js.map +0 -1
  106. package/dist/chunks/principal-page-bOaEYWJB.js.map +0 -1
  107. package/dist/chunks/queries-epz4MIuW.js.map +0 -1
  108. package/dist/chunks/role-field-C1KhNaIR.js.map +0 -1
  109. package/dist/chunks/use-change-BY6KPmnJ.js.map +0 -1
@@ -0,0 +1,582 @@
1
+ import { useId, useRef, useState } from 'react';
2
+ import { CoffreError, type RoleImpact, type RoleView } from '@coffre/client';
3
+ import { definitionProblem, type Permission, type RoleDefinition } from '@coffre/core/access';
4
+ import { useSuspenseQuery } from '@tanstack/react-query';
5
+ import { toast } from 'sonner';
6
+
7
+ import { useCoffre } from '../lib/coffre';
8
+ import { affects, queries } from '../lib/queries';
9
+ import {
10
+ columnInWords,
11
+ GRID_PERMISSIONS,
12
+ impactExamples,
13
+ impactInWords,
14
+ ordered,
15
+ PERMISSION_WORDS,
16
+ projectWide,
17
+ roleIdFrom,
18
+ roleIdProblem,
19
+ sameDefinition,
20
+ toggle,
21
+ type Column,
22
+ type Columns,
23
+ } from '../lib/roles';
24
+ import { useAction } from '../lib/use-action';
25
+ import { Card } from './page';
26
+ import { Check, Lock, Pencil, Plus, RotateBack, Trash } from './icons';
27
+ import { ConfirmDialog, ErrorLine, Modal, Notice, Spinner } from './ui';
28
+
29
+ // Settings › Roles (docs/design/roles.md): what each role holds, in two
30
+ // columns, an environment's and a protected one's, which a person holds
31
+ // there; a service account holds the first everywhere. Any member reads
32
+ // them; an Owner of the whole instance edits them, each change weighed
33
+ // first (`?dryRun=1`) and said in words before it is confirmed.
34
+
35
+ type Kind = 'role' | 'instance-role';
36
+
37
+ /** The instance's roles and instance roles, each a card of rows that open its editor. */
38
+ export function RolesSettings() {
39
+ const { data } = useSuspenseQuery(queries.roles(useCoffre()));
40
+ if (!data.ok) {
41
+ return (
42
+ <Card labelledBy="roles" title="Roles">
43
+ <div className="card-row">
44
+ <ErrorLine error={`The roles could not be read. ${data.error}`} />
45
+ </div>
46
+ </Card>
47
+ );
48
+ }
49
+ return (
50
+ <>
51
+ <RoleList kind="role" roles={data.roles} editable={data.editable} />
52
+ <RoleList kind="instance-role" roles={data.instanceRoles} editable={data.editable} />
53
+ </>
54
+ );
55
+ }
56
+
57
+ function RoleList({ kind, roles, editable }: { kind: Kind; roles: RoleView[]; editable: boolean }) {
58
+ // The role open in the editor, by id, or one being made: never a role's id, which `new` may be.
59
+ const [open, setOpen] = useState<{ role: string } | { making: true } | null>(null);
60
+ const project = kind === 'role';
61
+ const opened = open === null || 'making' in open ? null : roles.find((role) => role.id === open.role);
62
+ return (
63
+ <Card
64
+ labelledBy={project ? 'roles' : 'instance-roles'}
65
+ title={project ? 'Roles' : 'Instance roles'}
66
+ description={
67
+ <>
68
+ {project
69
+ ? 'What a grant gives, on a project or one of its environments. In a protected environment a person holds the second column; a service account always holds the first.'
70
+ : 'What a person holds, by their instance role, in every project its scope takes in, before any grant adds to it.'}
71
+ {!editable && ' Only an Owner of the whole instance edits them.'}
72
+ </>
73
+ }
74
+ actions={
75
+ project && editable && (
76
+ <button className="btn" type="button" onClick={() => setOpen({ making: true })}>
77
+ <Plus size={14} />
78
+ New role
79
+ </button>
80
+ )
81
+ }
82
+ >
83
+ <ul className="role-list" aria-label={project ? 'Roles' : 'Instance roles'}>
84
+ {roles.map((role) => (
85
+ <li key={role.id} className="card-row role-row">
86
+ <div className="role-row-text">
87
+ <p className="card-row-title role-row-title">
88
+ {role.name}
89
+ <RoleBadges role={role} />
90
+ </p>
91
+ {role.description !== '' && <p className="card-row-desc">{role.description}</p>}
92
+ <dl className="role-columns">
93
+ <div>
94
+ <dt>Holds</dt>
95
+ <dd>{columnInWords(role.permissions)}</dd>
96
+ </div>
97
+ <div>
98
+ <dt>
99
+ <Lock size={12} />
100
+ In protected environments
101
+ </dt>
102
+ <dd>{columnInWords(role.protected)}</dd>
103
+ </div>
104
+ </dl>
105
+ </div>
106
+ <div className="role-row-side">
107
+ <span className="role-row-count">{held(kind, role.grants)}</span>
108
+ <button
109
+ className="btn btn-sm"
110
+ type="button"
111
+ aria-label={`${editable && !role.fixed ? 'Edit' : 'View'} ${role.name}`}
112
+ onClick={() => setOpen({ role: role.id })}
113
+ >
114
+ {editable && !role.fixed && <Pencil size={14} />}
115
+ {editable && !role.fixed ? 'Edit…' : 'View'}
116
+ </button>
117
+ </div>
118
+ </li>
119
+ ))}
120
+ </ul>
121
+ {open !== null && opened !== undefined && (
122
+ <RoleEditor
123
+ // Each opening starts from the role as it is.
124
+ key={'making' in open ? '' : `role:${open.role}`}
125
+ kind={kind}
126
+ role={opened}
127
+ roles={roles}
128
+ editable={editable && (opened === null || !opened.fixed)}
129
+ onClose={() => setOpen(null)}
130
+ />
131
+ )}
132
+ </Card>
133
+ );
134
+ }
135
+
136
+ /** "3 grants", "1 person": who a role reaches now. */
137
+ function held(kind: Kind, count: number): string {
138
+ if (kind === 'instance-role') return count === 0 ? 'Nobody' : count === 1 ? '1 person' : `${count} people`;
139
+ return count === 0 ? 'No grants' : count === 1 ? '1 grant' : `${count} grants`;
140
+ }
141
+
142
+ /** Where a role comes from, and whether it is as it shipped. */
143
+ function RoleBadges({ role }: { role: RoleView }) {
144
+ return (
145
+ <>
146
+ {role.builtin ? <span className="tag tag-outline">Built-in</span> : <span className="tag tag-blue">Custom</span>}
147
+ {role.edited && <span className="tag tag-amber">Edited</span>}
148
+ {role.fixed && (
149
+ <span className="tag">
150
+ <Lock size={11} />
151
+ Fixed
152
+ </span>
153
+ )}
154
+ </>
155
+ );
156
+ }
157
+
158
+ type Draft = RoleDefinition & Columns & { id: string };
159
+
160
+ /**
161
+ * A change asked for, weighed, waiting for its confirmation: the role and
162
+ * definition weighed, which the confirmation makes, whatever the grid says
163
+ * by then; `again` once the policy it was first weighed from changed.
164
+ */
165
+ type Pending = { action: 'save' | 'reset' | 'delete'; id: string; definition: RoleDefinition & Columns; impact: RoleImpact; again?: boolean };
166
+
167
+ /**
168
+ * One role's editor, or a new one's: its name, description and grid, and
169
+ * Save, Reset to default for a built-in one edited, Delete for one an Owner
170
+ * made. Each asks the server what it would do first and says it, then does
171
+ * it once confirmed. Read-only to anyone who does not edit roles, and for
172
+ * Owner and Member, which hold everything and nothing, always.
173
+ */
174
+ function RoleEditor({
175
+ kind,
176
+ role,
177
+ roles,
178
+ editable,
179
+ onClose,
180
+ }: {
181
+ kind: Kind;
182
+ /** Null for a new role. */
183
+ role: RoleView | null;
184
+ roles: RoleView[];
185
+ editable: boolean;
186
+ onClose: () => void;
187
+ }) {
188
+ const coffre = useCoffre();
189
+ const instance = kind === 'instance-role';
190
+ const [draft, setDraft] = useState<Draft>(() =>
191
+ role === null
192
+ ? { id: '', name: '', description: '', permissions: [], protected: [] }
193
+ : { id: role.id, name: role.name, description: role.description, ...ordered(role) },
194
+ );
195
+ // A new role's id follows its name until it is typed itself.
196
+ const [idTyped, setIdTyped] = useState(false);
197
+ // The change weighed, kept after its confirmation closes, so that its words stay while it fades.
198
+ const [pending, setPending] = useState<Pending | null>(null);
199
+ const [asking, setAsking] = useState(false);
200
+ const weigh = useAction();
201
+ const apply = useAction();
202
+ const ids = { name: useId(), id: useId(), description: useId(), start: useId() };
203
+
204
+ const made = role === null;
205
+ const label = role?.name ?? (draft.name.trim() || 'New role');
206
+ const idError = made ? roleIdProblem(draft.id, roles.map((each) => each.id)) : null;
207
+ const definitionError = draft.name.trim() === '' ? 'A role needs a name.' : definitionProblem(draft);
208
+ const changed = role === null || !sameDefinition(draft, role);
209
+ const definition = { name: draft.name.trim(), description: draft.description, permissions: draft.permissions, protected: draft.protected };
210
+ // What the grid says now, for a weighing that answers after an edit made while it ran.
211
+ const latest = useRef({ id: draft.id, definition });
212
+ latest.current = { id: draft.id, definition };
213
+ const busy = weigh.pending || apply.pending;
214
+
215
+ const set = (permission: Permission, column: Column, on: boolean) => setDraft((current) => ({ ...current, ...toggle(current, permission, column, on) }));
216
+ const startFrom = (id: string) => {
217
+ const source = roles.find((each) => each.id === id);
218
+ setDraft((current) => ({ ...current, ...(source === undefined ? { permissions: [], protected: [] } : ordered(source)) }));
219
+ };
220
+
221
+ /**
222
+ * Ask what an action would do; the confirmation shows it, for what was
223
+ * weighed. One the grid changed under while it was weighed is weighed
224
+ * again, as the grid is now. Busy until it has its answer, the weighing
225
+ * again included.
226
+ */
227
+ function ask(action: Pending['action'], again = false): Promise<void> {
228
+ const asked = latest.current;
229
+ return weigh.run(
230
+ () => (action === 'save' ? coffre.roles.preview(asked.id, asked.definition, { instance }) : coffre.roles.previewReset(asked.id, { instance })),
231
+ {
232
+ affects: [],
233
+ onSuccess: (impact) => {
234
+ if (JSON.stringify(latest.current) !== JSON.stringify(asked)) return ask(action, again);
235
+ setPending({ action, ...asked, impact, again });
236
+ setAsking(true);
237
+ },
238
+ },
239
+ );
240
+ }
241
+
242
+ /** Do what was confirmed, as it was weighed: the policy changed since, it is weighed again, and asked again. */
243
+ function confirm({ action, id, definition: weighed, impact }: Pending) {
244
+ const name = action === 'save' ? weighed.name : label;
245
+ const confirmed = { instance, revision: impact.revision };
246
+ void apply.run(
247
+ async () => {
248
+ try {
249
+ await (action === 'save' ? coffre.roles.set(id, weighed, confirmed) : coffre.roles.reset(id, confirmed));
250
+ return true;
251
+ } catch (failure) {
252
+ if (failure instanceof CoffreError && failure.reason === 'policy_changed') return false;
253
+ throw failure;
254
+ }
255
+ },
256
+ {
257
+ // Saved or overtaken, the roles are read again: grants and edits as they are now.
258
+ affects: affects.roles(),
259
+ onSuccess: (saved) => {
260
+ if (!saved) return ask(action, true);
261
+ toast.success(
262
+ action === 'delete'
263
+ ? `Deleted the ${name} role`
264
+ : action === 'reset'
265
+ ? `${name} is as it ships again`
266
+ : made
267
+ ? `Made the ${name} role`
268
+ : `Saved the ${name} role`,
269
+ );
270
+ onClose();
271
+ },
272
+ },
273
+ );
274
+ }
275
+
276
+ const granted = role !== null && !role.builtin && role.grants > 0;
277
+ const error = weigh.error ?? apply.error;
278
+
279
+ return (
280
+ <Modal
281
+ open
282
+ onOpenChange={(next) => !next && !busy && onClose()}
283
+ wide
284
+ title={made ? 'New role' : editable ? <>Edit {role.name}</> : role.name}
285
+ description={
286
+ !editable && role !== null
287
+ ? role.fixed
288
+ ? `${role.name} holds ${role.id === 'member' ? 'nothing' : 'everything'}, always: nobody edits it.`
289
+ : 'Only an Owner of the whole instance edits roles.'
290
+ : made
291
+ ? 'A project role, granted as any is. You can delete it while no grant names it.'
292
+ : undefined
293
+ }
294
+ >
295
+ <form
296
+ className="form"
297
+ onSubmit={(event) => {
298
+ event.preventDefault();
299
+ void ask('save');
300
+ }}
301
+ >
302
+ {editable && (
303
+ <div className="form-row">
304
+ <label className="field" htmlFor={ids.name}>
305
+ <span className="label">Name</span>
306
+ <input
307
+ id={ids.name}
308
+ className="input"
309
+ autoFocus={made}
310
+ maxLength={40}
311
+ autoComplete="off"
312
+ placeholder={made ? 'Release manager' : undefined}
313
+ value={draft.name}
314
+ onChange={(event) => {
315
+ const name = event.target.value;
316
+ setDraft((current) => ({ ...current, name, ...(made && !idTyped ? { id: roleIdFrom(name) } : {}) }));
317
+ }}
318
+ />
319
+ </label>
320
+ {made && (
321
+ <label className="field" htmlFor={ids.id}>
322
+ <span className="label">Id</span>
323
+ <input
324
+ id={ids.id}
325
+ className="input input-mono"
326
+ spellCheck={false}
327
+ autoComplete="off"
328
+ maxLength={32}
329
+ placeholder="release-manager"
330
+ value={draft.id}
331
+ aria-invalid={draft.id !== '' && idError !== null}
332
+ aria-describedby={`${ids.id}-hint`}
333
+ onChange={(event) => {
334
+ setIdTyped(true);
335
+ setDraft((current) => ({ ...current, id: event.target.value }));
336
+ }}
337
+ />
338
+ <span id={`${ids.id}-hint`} className={`hint${draft.id !== '' && idError !== null ? ' edit-note-error' : ''}`}>
339
+ {draft.id !== '' && idError !== null ? idError : 'What the CLI and grants name it. It never changes.'}
340
+ </span>
341
+ </label>
342
+ )}
343
+ </div>
344
+ )}
345
+ {editable && (
346
+ <label className="field" htmlFor={ids.description}>
347
+ <span className="label">
348
+ Description <span className="hint">(optional)</span>
349
+ </span>
350
+ <input
351
+ id={ids.description}
352
+ className="input"
353
+ maxLength={200}
354
+ autoComplete="off"
355
+ value={draft.description}
356
+ onChange={(event) => setDraft((current) => ({ ...current, description: event.target.value }))}
357
+ />
358
+ </label>
359
+ )}
360
+ {made && (
361
+ <label className="field" htmlFor={ids.start}>
362
+ <span className="label">Start from</span>
363
+ <select id={ids.start} className="select" defaultValue="" onChange={(event) => startFrom(event.target.value)}>
364
+ <option value="">Nothing</option>
365
+ {roles.map((each) => (
366
+ <option key={each.id} value={each.id}>
367
+ {each.name}
368
+ </option>
369
+ ))}
370
+ </select>
371
+ </label>
372
+ )}
373
+ {!editable && role !== null && role.description !== '' && <p className="dialog-body">{role.description}</p>}
374
+
375
+ <RoleGrid name={label} columns={draft} editable={editable} onChange={set} />
376
+
377
+ {editable && (
378
+ <p className="hint">
379
+ Reading takes using, and a protected environment holds no more than any other: the boxes keep both true.
380
+ {instance ? ' An instance role reaches every project its holder’s scope takes in.' : ''}
381
+ </p>
382
+ )}
383
+ {role !== null && role.edited && (
384
+ <Notice tone="info">
385
+ Edited. As it ships, it holds {columnInWords(role.default?.permissions ?? [])}; in protected environments,{' '}
386
+ {columnInWords(role.default?.protected ?? [])}.
387
+ </Notice>
388
+ )}
389
+ {editable && changed && definitionError !== null && draft.name.trim() !== '' && <p className="hint edit-note-error">{definitionError}</p>}
390
+ <ErrorLine error={error} />
391
+
392
+ <div className="dialog-actions role-editor-actions">
393
+ {editable && role !== null && (role.builtin ? role.edited : true) && (
394
+ <div className="role-editor-aside">
395
+ {role.builtin ? (
396
+ <button className="btn" type="button" disabled={busy} onClick={() => void ask('reset')}>
397
+ <RotateBack size={14} />
398
+ Reset to default
399
+ </button>
400
+ ) : (
401
+ <button
402
+ className="btn btn-danger-outline"
403
+ type="button"
404
+ disabled={busy || granted}
405
+ aria-describedby={granted ? `${ids.name}-granted` : undefined}
406
+ onClick={() => void ask('delete')}
407
+ >
408
+ <Trash size={14} />
409
+ Delete…
410
+ </button>
411
+ )}
412
+ {granted && (
413
+ <span id={`${ids.name}-granted`} className="hint">
414
+ {role.grants === 1 ? '1 grant names it' : `${role.grants} grants name it`}: revoke {role.grants === 1 ? 'it' : 'them'} first.
415
+ </span>
416
+ )}
417
+ </div>
418
+ )}
419
+ <button className="btn" type="button" disabled={busy} onClick={onClose}>
420
+ {editable ? 'Cancel' : 'Close'}
421
+ </button>
422
+ {editable && (
423
+ <button className="btn btn-primary" type="submit" disabled={busy || !changed || definitionError !== null || idError !== null}>
424
+ {weigh.pending && <Spinner />}
425
+ {made ? 'Make role…' : 'Save…'}
426
+ </button>
427
+ )}
428
+ </div>
429
+ </form>
430
+
431
+ {/* Inside the editor, so that it is a dialog nested in it: Escape closes it alone. */}
432
+ <ConfirmDialog
433
+ open={asking}
434
+ onOpenChange={setAsking}
435
+ title={
436
+ pending?.action === 'delete'
437
+ ? `Delete the ${label} role?`
438
+ : pending?.action === 'reset'
439
+ ? `Reset ${label} to its default?`
440
+ : made
441
+ ? `Make the ${pending?.definition.name ?? definition.name} role?`
442
+ : `Save the ${label} role?`
443
+ }
444
+ body={pending === null ? '' : impactLead(pending)}
445
+ detail={pending !== null && pending.impact.impact.length > 0 && <ImpactList impact={pending.impact} />}
446
+ confirmLabel={pending?.action === 'delete' ? 'Delete' : pending?.action === 'reset' ? 'Reset' : 'Save'}
447
+ destructive={pending?.action === 'delete'}
448
+ onConfirm={() => pending !== null && confirm(pending)}
449
+ />
450
+ </Modal>
451
+ );
452
+ }
453
+
454
+ /** What a change does, before its lines: whether it reaches anyone now, and, weighed again, why. */
455
+ function impactLead(pending: Pending): string {
456
+ const lead = impactNow(pending);
457
+ return pending.again === true ? `The roles or protected environments changed since you asked: this is what it does now. ${lead}` : lead;
458
+ }
459
+
460
+ function impactNow({ action, impact }: Pending): string {
461
+ const logged = 'It is logged, with the role as it was.';
462
+ if (impact.impact.length === 0) {
463
+ return action === 'delete'
464
+ ? `No grant names it, so nobody loses anything. ${logged}`
465
+ : `Nobody holds more or less for it today: it applies to whoever holds the role from now on. ${logged}`;
466
+ }
467
+ return `It applies at once to everyone the role reaches, people and service accounts; tokens issued before keep at most what they held. ${logged}`;
468
+ }
469
+
470
+ /** A change's impact, line by line, with whom each reaches. */
471
+ function ImpactList({ impact }: { impact: RoleImpact }) {
472
+ return (
473
+ <ul className="impact-list">
474
+ {impact.impact.map((line) => {
475
+ const who = impactExamples(line);
476
+ return (
477
+ <li key={`${line.change} ${line.permission} ${line.protected}`} className={`impact-line impact-${line.change}`}>
478
+ <span className={`tag ${line.change === 'widens' ? 'tag-amber' : 'tag-blue'}`}>{line.change === 'widens' ? 'Widens' : 'Narrows'}</span>
479
+ <span>
480
+ {impactInWords(line)}
481
+ {who !== null && <span className="impact-who">{who}</span>}
482
+ </span>
483
+ </li>
484
+ );
485
+ })}
486
+ </ul>
487
+ );
488
+ }
489
+
490
+ /**
491
+ * The grid: a row per permission, a column for environments and one for
492
+ * protected ones, real checkboxes each named in full for a screen reader
493
+ * ("Developer: Read values in protected environments"). Project-wide
494
+ * permissions have the first column alone, under a heading of their own.
495
+ */
496
+ function RoleGrid({
497
+ name,
498
+ columns,
499
+ editable,
500
+ onChange,
501
+ }: {
502
+ name: string;
503
+ columns: Columns;
504
+ editable: boolean;
505
+ onChange: (permission: Permission, column: Column, on: boolean) => void;
506
+ }) {
507
+ const cell = (permission: Permission, column: Column) => {
508
+ const on = (column === 'normal' ? columns.permissions : columns.protected).includes(permission);
509
+ const where = projectWide(permission) ? ', on the whole project' : column === 'normal' ? ' in environments' : ' in protected environments';
510
+ const words = `${name}: ${PERMISSION_WORDS[permission].label}${where}`;
511
+ if (!editable) {
512
+ return on ? (
513
+ <span className="role-grid-yes" title={words}>
514
+ <Check size={15} />
515
+ <span className="visually-hidden">{words}: yes</span>
516
+ </span>
517
+ ) : (
518
+ <span className="role-grid-no">
519
+ <span aria-hidden>–</span>
520
+ <span className="visually-hidden">{words}: no</span>
521
+ </span>
522
+ );
523
+ }
524
+ return (
525
+ <label className="role-grid-hit">
526
+ <input type="checkbox" className="role-grid-box" aria-label={words} checked={on} onChange={(event) => onChange(permission, column, event.target.checked)} />
527
+ </label>
528
+ );
529
+ };
530
+ const values = GRID_PERMISSIONS.filter((permission) => !projectWide(permission));
531
+ const wide = GRID_PERMISSIONS.filter(projectWide);
532
+ const row = (permission: Permission) => {
533
+ const { label, note } = PERMISSION_WORDS[permission];
534
+ return (
535
+ <tr key={permission}>
536
+ <th scope="row">
537
+ {label}
538
+ {note !== undefined && <span className="role-grid-note">{note}</span>}
539
+ </th>
540
+ <td>{cell(permission, 'normal')}</td>
541
+ {projectWide(permission) ? <td /> : <td>{cell(permission, 'protected')}</td>}
542
+ </tr>
543
+ );
544
+ };
545
+ return (
546
+ <div className="role-grid-wrap">
547
+ <table className="role-grid">
548
+ <caption className="visually-hidden">What {name} holds, in environments and in protected ones</caption>
549
+ <thead>
550
+ <tr>
551
+ <th scope="col">Permission</th>
552
+ {/* A phone's narrow columns take the short words; a screen reader always reads the long ones. */}
553
+ <th scope="col">
554
+ <span className="role-grid-long">Environments</span>
555
+ <span className="role-grid-short" aria-hidden>
556
+ Normal
557
+ </span>
558
+ </th>
559
+ <th scope="col">
560
+ <span className="th">
561
+ <Lock size={12} />
562
+ <span className="role-grid-long">Protected environments</span>
563
+ <span className="role-grid-short" aria-hidden>
564
+ Protected
565
+ </span>
566
+ </span>
567
+ </th>
568
+ </tr>
569
+ </thead>
570
+ <tbody>{values.map(row)}</tbody>
571
+ <tbody>
572
+ <tr className="role-grid-group">
573
+ <th scope="rowgroup" colSpan={3}>
574
+ Project-wide <span className="role-grid-note">one column: a project is never protected</span>
575
+ </th>
576
+ </tr>
577
+ {wide.map(row)}
578
+ </tbody>
579
+ </table>
580
+ </div>
581
+ );
582
+ }
@@ -1,7 +1,8 @@
1
1
  import { useId, useState } from 'react';
2
- import { INSTANCE_ROLE_NAMES, INSTANCE_ROLES, type Filter, type InstanceRole, type Scope } from '@coffre/core/access';
2
+ import { INSTANCE_ROLE_NAMES, type Filter, type InstanceRole, type Scope } from '@coffre/core/access';
3
3
  import type { RoleVars } from '../lib/changes';
4
4
  import { useShell } from '../lib/use-shell';
5
+ import { useRoles } from '../lib/use-roles';
5
6
  import { slugProblem } from '../lib/validation';
6
7
  import { X } from './icons';
7
8
 
@@ -15,6 +16,9 @@ import { X } from './icons';
15
16
  export function RoleField({ value, onChange }: { value: RoleVars; onChange: (value: RoleVars) => void }) {
16
17
  const { role, scope } = value;
17
18
  const owners = useShell().capabilities.ownsInstance;
19
+ // As the instance defines them: an Owner may have renamed or redescribed one.
20
+ const { instanceRoles } = useRoles();
21
+ const shown = (name: InstanceRole) => instanceRoles.find((each) => each.id === name);
18
22
  return (
19
23
  <>
20
24
  <label className="field">
@@ -26,12 +30,12 @@ export function RoleField({ value, onChange }: { value: RoleVars; onChange: (val
26
30
  >
27
31
  {INSTANCE_ROLE_NAMES.filter((name) => owners || name !== 'owner').map((name) => (
28
32
  <option key={name} value={name}>
29
- {INSTANCE_ROLES[name].name}
33
+ {shown(name)?.name ?? name}
30
34
  </option>
31
35
  ))}
32
36
  </select>
33
37
  <span className="hint">
34
- Default permissions: {INSTANCE_ROLES[role].description} Project grants add more.
38
+ Default permissions: {shown(role)?.description} Project grants add more.
35
39
  {!owners && ' Only an Owner makes someone an Owner.'}
36
40
  </span>
37
41
  </label>
@@ -1,4 +1,8 @@
1
- import { assignableToEnvironment, credentialScopeInWords, ROLE_NAMES, ROLES, type Role } from '@coffre/core/access';
1
+ import type { RoleView } from '@coffre/client';
2
+ import { credentialScopeInWords, type Role } from '@coffre/core/access';
3
+
4
+ import { roleNameOf } from '../lib/roles';
5
+ import { useRoles } from '../lib/use-roles';
2
6
 
3
7
  /** A grant of the account's, as its Access tab lists it. */
4
8
  export type HeldGrant = { project: string; environment: string | null; role: Role };
@@ -13,12 +17,17 @@ export function wholeScope(grants: readonly HeldGrant[]): ScopeByPath {
13
17
  return Object.fromEntries(grants.map((grant) => [pathOf(grant), grant.role]));
14
18
  }
15
19
 
16
- /** The roles a credential may hold where its account holds `grant`: those it holds all of, and that the place takes. */
17
- function within(grant: HeldGrant): Role[] {
18
- const held: readonly string[] = ROLES[grant.role].permissions;
19
- return ROLE_NAMES.filter(
20
- (role) => (grant.environment === null || assignableToEnvironment(role)) && ROLES[role].permissions.every((permission) => held.includes(permission)),
21
- );
20
+ /**
21
+ * The roles a credential may hold where its account holds `grant`: those it
22
+ * holds all of, and that the place takes. Its own role always, whatever the
23
+ * policy says of it now.
24
+ */
25
+ function within(grant: HeldGrant, roles: readonly RoleView[]): Role[] {
26
+ const held: readonly string[] = roles.find((role) => role.id === grant.role)?.permissions ?? [];
27
+ const smaller = roles
28
+ .filter((role) => (grant.environment === null || role.assignableToEnvironment) && role.permissions.every((permission) => held.includes(permission)))
29
+ .map((role) => role.id);
30
+ return smaller.includes(grant.role) ? smaller : [grant.role, ...smaller];
22
31
  }
23
32
 
24
33
  /**
@@ -27,6 +36,7 @@ function within(grant: HeldGrant): Role[] {
27
36
  * given later reaches it, and it never changes after.
28
37
  */
29
38
  export function ScopePicker({ grants, value, onChange }: { grants: readonly HeldGrant[]; value: ScopeByPath; onChange: (scope: ScopeByPath) => void }) {
39
+ const { roles } = useRoles();
30
40
  if (grants.length === 0) {
31
41
  return <p className="hint">The account holds nothing yet: give it a grant first, since nothing it is given later reaches a token or binding made before.</p>;
32
42
  }
@@ -55,9 +65,9 @@ export function ScopePicker({ grants, value, onChange }: { grants: readonly Held
55
65
  disabled={chosen === undefined}
56
66
  onChange={(event) => set(path, event.target.value as Role)}
57
67
  >
58
- {within(grant).map((role) => (
68
+ {within(grant, roles).map((role) => (
59
69
  <option key={role} value={role}>
60
- {ROLES[role].name}
70
+ {roleNameOf(roles, role)}
61
71
  </option>
62
72
  ))}
63
73
  </select>