@coffre/ui 0.4.5 → 0.5.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.
Files changed (117) hide show
  1. package/dist/chunks/{affordances-DAvsVz7u.js → affordances-Bqdevf7w.js} +10 -5
  2. package/dist/chunks/affordances-Bqdevf7w.js.map +1 -0
  3. package/dist/chunks/{audit-sentence-BhHmvQX_.js → audit-sentence-Cm5nby5k.js} +115 -8
  4. package/dist/chunks/audit-sentence-Cm5nby5k.js.map +1 -0
  5. package/dist/chunks/{connected-apps-AZsX9Zvv.js → connected-apps-DTNS7YmY.js} +2 -2
  6. package/dist/chunks/{connected-apps-AZsX9Zvv.js.map → connected-apps-DTNS7YmY.js.map} +1 -1
  7. package/dist/chunks/{delete-place-D6LIZkg9.js → delete-place-CIaOq0mt.js} +3 -3
  8. package/dist/chunks/{delete-place-D6LIZkg9.js.map → delete-place-CIaOq0mt.js.map} +1 -1
  9. package/dist/chunks/{directory-B6FuiCir.js → directory-DSVt5Tya.js} +54 -60
  10. package/dist/chunks/directory-DSVt5Tya.js.map +1 -0
  11. package/dist/chunks/{folders-DvSqBhuu.js → folders-CoGK9oqg.js} +2 -2
  12. package/dist/chunks/{folders-DvSqBhuu.js.map → folders-CoGK9oqg.js.map} +1 -1
  13. package/dist/chunks/grants-CRU7yZhL.js +164 -0
  14. package/dist/chunks/grants-CRU7yZhL.js.map +1 -0
  15. package/dist/chunks/{offboarding-CLJaNTcD.js → offboarding-DVBSiJRZ.js} +7 -3
  16. package/dist/chunks/offboarding-DVBSiJRZ.js.map +1 -0
  17. package/dist/chunks/{principal-page-UtNWKCf8.js → principal-page-BEBr_zW7.js} +350 -91
  18. package/dist/chunks/principal-page-BEBr_zW7.js.map +1 -0
  19. package/dist/chunks/{project-page-C4cEaRFa.js → project-page-B3TZ5_ig.js} +2 -2
  20. package/dist/chunks/{project-page-C4cEaRFa.js.map → project-page-B3TZ5_ig.js.map} +1 -1
  21. package/dist/chunks/{queries-Dew8mIeo.js → queries-DE5nK2y4.js} +132 -54
  22. package/dist/chunks/queries-DE5nK2y4.js.map +1 -0
  23. package/dist/chunks/{references-DWDnXNoW.js → references-uv4Mli9z.js} +4 -3
  24. package/dist/chunks/{references-DWDnXNoW.js.map → references-uv4Mli9z.js.map} +1 -1
  25. package/dist/chunks/role-field-DWhkRY6n.js +192 -0
  26. package/dist/chunks/role-field-DWhkRY6n.js.map +1 -0
  27. package/dist/chunks/use-action-BRgvbUWt.js +49 -0
  28. package/dist/chunks/use-action-BRgvbUWt.js.map +1 -0
  29. package/dist/chunks/{use-change-C4bGRhHY.js → use-change-BJ3dCFHb.js} +40 -14
  30. package/dist/chunks/use-change-BJ3dCFHb.js.map +1 -0
  31. package/dist/chunks/{use-shell-Dkv89nW1.js → use-shell-BO4ZTr1y.js} +2 -2
  32. package/dist/chunks/{use-shell-Dkv89nW1.js.map → use-shell-BO4ZTr1y.js.map} +1 -1
  33. package/dist/chunks/validation-DiHIwZRA.js +37 -0
  34. package/dist/chunks/validation-DiHIwZRA.js.map +1 -0
  35. package/dist/index.d.ts +116 -88
  36. package/dist/index.js +40 -21
  37. package/dist/index.js.map +1 -1
  38. package/dist/pages/account.js +8 -7
  39. package/dist/pages/account.js.map +1 -1
  40. package/dist/pages/approval.js +53 -21
  41. package/dist/pages/approval.js.map +1 -1
  42. package/dist/pages/audit.js +2 -2
  43. package/dist/pages/environment.js +8 -7
  44. package/dist/pages/environment.js.map +1 -1
  45. package/dist/pages/login.js +2 -2
  46. package/dist/pages/login.js.map +1 -1
  47. package/dist/pages/project-access.js +7 -7
  48. package/dist/pages/project-environments.js +26 -21
  49. package/dist/pages/project-environments.js.map +1 -1
  50. package/dist/pages/project-settings.js +9 -8
  51. package/dist/pages/project-settings.js.map +1 -1
  52. package/dist/pages/project.js +1 -1
  53. package/dist/pages/projects.js +8 -13
  54. package/dist/pages/projects.js.map +1 -1
  55. package/dist/pages/service-account.js +294 -88
  56. package/dist/pages/service-account.js.map +1 -1
  57. package/dist/pages/service-accounts.js +7 -7
  58. package/dist/pages/service-accounts.js.map +1 -1
  59. package/dist/pages/settings.js +91 -9
  60. package/dist/pages/settings.js.map +1 -1
  61. package/dist/pages/unregistered.d.ts +1 -1
  62. package/dist/pages/unregistered.js +4 -4
  63. package/dist/pages/unregistered.js.map +1 -1
  64. package/dist/pages/user.d.ts +2 -0
  65. package/dist/pages/user.js +21 -3
  66. package/dist/pages/user.js.map +1 -1
  67. package/dist/pages/users.js +4 -4
  68. package/package.json +3 -3
  69. package/src/components/affordances.ts +7 -3
  70. package/src/components/command-palette.tsx +2 -0
  71. package/src/components/directory.tsx +58 -63
  72. package/src/components/grants.tsx +9 -9
  73. package/src/components/offboarding.tsx +3 -1
  74. package/src/components/principal-page.tsx +124 -74
  75. package/src/components/role-field.tsx +183 -0
  76. package/src/components/scope-picker.tsx +73 -0
  77. package/src/components/service-tokens.tsx +20 -9
  78. package/src/components/shell.tsx +6 -10
  79. package/src/components/trusted-workloads.tsx +121 -42
  80. package/src/index.ts +1 -0
  81. package/src/lib/approval.ts +23 -0
  82. package/src/lib/audit-sentences.ts +109 -3
  83. package/src/lib/bindings.ts +33 -10
  84. package/src/lib/capabilities.ts +28 -8
  85. package/src/lib/changes.ts +22 -13
  86. package/src/lib/project-access.ts +21 -0
  87. package/src/lib/queries.ts +121 -44
  88. package/src/lib/service-setup.ts +41 -0
  89. package/src/lib/validation.ts +7 -0
  90. package/src/options.ts +31 -11
  91. package/src/pages/account.tsx +3 -2
  92. package/src/pages/approval.tsx +18 -17
  93. package/src/pages/login.tsx +1 -1
  94. package/src/pages/project-environments.tsx +12 -15
  95. package/src/pages/project-settings.tsx +3 -3
  96. package/src/pages/projects.tsx +1 -5
  97. package/src/pages/service-account.tsx +7 -5
  98. package/src/pages/service-accounts.tsx +8 -4
  99. package/src/pages/settings.tsx +80 -4
  100. package/src/pages/unregistered.tsx +3 -3
  101. package/src/pages/user.tsx +24 -1
  102. package/src/parity.ts +5 -1
  103. package/src/register.ts +3 -1
  104. package/src/shared/models.ts +8 -4
  105. package/src/styles/globals.css +111 -28
  106. package/dist/chunks/affordances-DAvsVz7u.js.map +0 -1
  107. package/dist/chunks/audit-sentence-BhHmvQX_.js.map +0 -1
  108. package/dist/chunks/directory-B6FuiCir.js.map +0 -1
  109. package/dist/chunks/grants-9kIV6uZ7.js +0 -669
  110. package/dist/chunks/grants-9kIV6uZ7.js.map +0 -1
  111. package/dist/chunks/offboarding-CLJaNTcD.js.map +0 -1
  112. package/dist/chunks/principal-page-UtNWKCf8.js.map +0 -1
  113. package/dist/chunks/queries-Dew8mIeo.js.map +0 -1
  114. package/dist/chunks/use-change-C4bGRhHY.js.map +0 -1
  115. package/dist/chunks/validation-DvPinn8V.js +0 -87
  116. package/dist/chunks/validation-DvPinn8V.js.map +0 -1
  117. package/src/components/every-project.tsx +0 -287
@@ -8,6 +8,7 @@
8
8
  * and places in it links. `plain` flattens it, for tests and titles.
9
9
  */
10
10
  import { shownMember, type AuditEntryView } from '@coffre/client';
11
+ import { INSTANCE_ROLES, isInstanceRole, isScope, scopeInWords, type Scope } from '@coffre/core/access';
11
12
 
12
13
  /** What a sentence is made from: an entry of the log, as `GET /api/audit` gives it. */
13
14
  export type AuditEntry = Pick<
@@ -261,6 +262,7 @@ const TEMPLATES: Record<string, Template> = {
261
262
  then: ({ entry }) => [
262
263
  ...(text(entry.metadata.previousRole) === null ? [] : [`, was ${text(entry.metadata.previousRole)}`]),
263
264
  ...(text(entry.metadata.expiresAt) === null ? [] : [`, until ${text(entry.metadata.expiresAt)!.slice(0, 10)}`]),
265
+ ...(entry.metadata.reason === 'every-project' ? [', for a grant on every project'] : []),
264
266
  ],
265
267
  },
266
268
  'access.revoke': {
@@ -272,13 +274,20 @@ const TEMPLATES: Record<string, Template> = {
272
274
  ' from ',
273
275
  ...subject(entry),
274
276
  ],
277
+ // The vault replacing a grant on every project of 0.4: by what, an instance role or project grants.
278
+ then: ({ entry }) => {
279
+ const by = entry.metadata.replacedBy as { role?: unknown } | undefined;
280
+ if (by === undefined) return [];
281
+ const role = typeof by.role === 'string' ? roleName(by.role) : null;
282
+ return [role === null || role === 'Member' ? ', replaced by project grants' : `, replaced by the instance role ${role}`];
283
+ },
275
284
  },
276
285
  'member.add': {
277
286
  did: 'added',
278
287
  tried: 'add',
279
288
  what: ({ entry }) => [
280
289
  ...subject(entry),
281
- ...(entry.metadata.rootAdmin === true ? [' as a root admin'] : entry.metadata.owner === true ? [' as an owner'] : []),
290
+ ...(entry.metadata.rootAdmin === true ? [' as a root admin'] : entry.metadata.owner === true ? [' as an owner'] : asRole(entry)),
282
291
  ],
283
292
  },
284
293
  'member.remove': {
@@ -287,17 +296,44 @@ const TEMPLATES: Record<string, Template> = {
287
296
  what: (facts) => subject(facts.entry),
288
297
  then: ({ entry }) => (number(entry.metadata.revoked) === null ? [] : [`, who held ${plural(number(entry.metadata.revoked)!, 'grant')}`]),
289
298
  },
290
- 'member.restore': { did: 'brought back', tried: 'bring back', what: (facts) => subject(facts.entry) },
299
+ 'member.restore': { did: 'brought back', tried: 'bring back', what: ({ entry }) => [...subject(entry), ...asRole(entry)] },
300
+ 'member.role': {
301
+ did: 'made',
302
+ tried: 'make',
303
+ what: ({ entry }) => [...subject(entry), ` ${article(roleName(text(entry.metadata.role) ?? 'member') ?? 'Member')}`, ...scoped(entry)],
304
+ then: ({ entry }) => {
305
+ const before = roleName(text(entry.metadata.previousRole) ?? '');
306
+ return [...(before === null ? [] : [`, was ${before}`]), ...(entry.metadata.reason === 'every-project' ? [', for their grants on every project'] : [])];
307
+ },
308
+ },
291
309
  'member.owner': {
292
310
  did: ({ entry }) => (entry.metadata.owner === false ? 'took owner from' : 'made'),
293
311
  tried: ({ entry }) => (entry.metadata.owner === false ? 'take owner from' : 'make'),
294
312
  what: ({ entry }) => [...subject(entry), ...(entry.metadata.owner === false ? [] : [' an owner'])],
295
313
  },
314
+ // Where people set up service accounts themselves, which those who run the instance set.
315
+ 'settings.change': {
316
+ did: 'set',
317
+ tried: 'set',
318
+ what: ({ entry }) => ['where people set up service accounts', ...setting(entry.metadata.serviceAccounts).map((words) => `: ${words}`)],
319
+ then: ({ entry }) => {
320
+ const previous = (entry.metadata.previous as { serviceAccounts?: unknown } | undefined)?.serviceAccounts;
321
+ return setting(previous).map((words) => `, was ${words}`);
322
+ },
323
+ },
296
324
  // A binding trusts CI runs to sign in as a service; removing one is its tombstone.
297
325
  'token.bind': {
298
326
  did: 'trusted CI runs to sign in as',
299
327
  tried: 'trust CI runs to sign in as',
300
328
  what: (facts) => subject(facts.entry),
329
+ then: ({ entry }) => reaching(entry),
330
+ },
331
+ // The tokens and bindings 0.4 made, given their own scope when 0.5 first checked a credential: what their account held then.
332
+ 'token.seal': {
333
+ did: 'sealed',
334
+ tried: 'seal',
335
+ what: ({ entry }) => [`${sealedOf(entry)} of `, ...subject(entry)],
336
+ then: ({ entry }) => reaching(entry, ', reaching nothing'),
301
337
  },
302
338
  'token.unbind': {
303
339
  did: 'stopped trusting CI runs to sign in as',
@@ -321,6 +357,8 @@ const TEMPLATES: Record<string, Template> = {
321
357
  'mcp.approve': { did: 'approved', tried: 'approve', what: ({ entry }) => tool(entry), then: ({ entry }) => [` for ${text(entry.metadata.clientName) ?? 'an app'}`] },
322
358
  'mcp.deny': { did: 'turned down', tried: 'turn down', what: ({ entry }) => tool(entry), then: ({ entry }) => [` for ${text(entry.metadata.clientName) ?? 'an app'}`] },
323
359
  'mcp.view': { did: 'opened an approval', tried: 'open an approval', what: () => [] },
360
+ // The app's prompt declined, which cancels the approval it would have opened.
361
+ 'mcp.cancel': { did: 'cancelled', tried: 'cancel', what: ({ entry }) => tool(entry), then: ({ entry }) => via(entry) },
324
362
  'mcp.disconnect': {
325
363
  did: 'disconnected',
326
364
  tried: 'disconnect',
@@ -335,7 +373,7 @@ const TEMPLATES: Record<string, Template> = {
335
373
  what: ({ entry }) => (text(entry.metadata.provider) === null ? [] : [`with ${text(entry.metadata.provider)}`]),
336
374
  },
337
375
  sign_out: { did: 'signed out', tried: 'sign out', what: () => [] },
338
- 'token.create': { did: 'issued a bearer token to', tried: 'issue a bearer token to', what: (facts) => subject(facts.entry) },
376
+ 'token.create': { did: 'issued a bearer token to', tried: 'issue a bearer token to', what: (facts) => subject(facts.entry), then: ({ entry }) => reaching(entry) },
339
377
  'token.revoke': { did: 'revoked a bearer token of', tried: 'revoke a bearer token of', what: (facts) => subject(facts.entry) },
340
378
  'device.approve': { did: 'approved a CLI sign-in', tried: 'approve a CLI sign-in', what: () => [] },
341
379
  'device.deny': { did: 'turned down a CLI sign-in', tried: 'turn down a CLI sign-in', what: () => [] },
@@ -382,6 +420,65 @@ function intoFolder(entry: AuditEntry): Part[] {
382
420
  return [folder === null ? ' out of its folder' : ` to the folder ${folder}`];
383
421
  }
384
422
 
423
+ /** An instance role's name, from its slug; null for none it knows. */
424
+ function roleName(role: string): string | null {
425
+ return isInstanceRole(role) ? INSTANCE_ROLES[role].name : null;
426
+ }
427
+
428
+ function article(name: string): string {
429
+ return `${/^[AEIOU]/.test(name) ? 'an' : 'a'} ${name}`;
430
+ }
431
+
432
+ /** Whether an entry's scope narrows the role; its projects are ids, which a sentence cannot name. */
433
+ function scoped(entry: AuditEntry): Part[] {
434
+ const scope = entry.metadata.scope as { projects?: unknown; environments?: unknown } | undefined;
435
+ return scope === undefined || (scope.projects === 'all' && scope.environments === 'all') ? [] : [', with a scope'];
436
+ }
437
+
438
+ /**
439
+ * What a token or binding reaches, its scope, each role with the places it
440
+ * holds, counted, as the log names them by id: `, reaching developer on 2 places`.
441
+ */
442
+ function reaching(entry: AuditEntry, none = ''): Part[] {
443
+ const scope = entry.metadata.scope;
444
+ if (!Array.isArray(scope)) return [];
445
+ if (scope.length === 0) return none === '' ? [] : [none];
446
+ const byRole = new Map<string, number>();
447
+ for (const grant of scope as { role?: unknown }[]) {
448
+ const role = text(grant.role);
449
+ if (role !== null) byRole.set(role, (byRole.get(role) ?? 0) + 1);
450
+ }
451
+ return [`, reaching ${[...byRole].map(([role, count]) => `${role} on ${plural(count, 'place')}`).join(', ')}`];
452
+ }
453
+
454
+ /** The tokens and trust bindings a `token.seal` names, counted: `2 tokens and a trust binding`. */
455
+ function sealedOf(entry: AuditEntry): string {
456
+ const count = (key: string) => (Array.isArray(entry.metadata[key]) ? (entry.metadata[key] as unknown[]).length : 0);
457
+ const parts = [[count('tokens'), 'token'], [count('bindings'), 'trust binding']] as const;
458
+ return parts.filter(([n]) => n > 0).map(([n, one]) => plural(n, one)).join(' and ') || 'nothing';
459
+ }
460
+
461
+ /**
462
+ * The setting of where people set up service accounts, in words: its
463
+ * projects counted, as the log names them by id, its environments by name.
464
+ *
465
+ * All projects · all but prod 2 projects · dev only
466
+ */
467
+ function setting(scope: unknown): string[] {
468
+ if (!isScope(scope)) return [];
469
+ const { projects } = scope;
470
+ const counted: Scope = projects === 'all'
471
+ ? scope
472
+ : { ...scope, projects: 'only' in projects ? { only: [plural(projects.only.length, 'project')] } : { except: [plural(projects.except.length, 'project')] } };
473
+ return [scopeInWords(counted)];
474
+ }
475
+
476
+ /** The role a member is added or brought back with, said when it is more than Member. */
477
+ function asRole(entry: AuditEntry): Part[] {
478
+ const role = roleName(text(entry.metadata.role) ?? '');
479
+ return role === null || role === 'Member' ? [] : [` as ${article(role)}`, ...scoped(entry)];
480
+ }
481
+
385
482
  function placeOnly(entry: AuditEntry): Part[] {
386
483
  // A grant on every project names no project: its place is a path in its payload, `*` or `*/dev`.
387
484
  const everywhere = entry.project === null ? text(entry.metadata.place) : null;
@@ -412,6 +509,12 @@ const REASONS: Record<string, string> = {
412
509
  deleted: 'the place was deleted',
413
510
  not_allowed: 'not allowed',
414
511
  root_admin: 'root admins are set by the deployment',
512
+ requires_instance_admin: 'requires an admin or owner of the whole instance',
513
+ requires_instance_owner: 'requires an instance owner',
514
+ own_role: 'nobody changes their own role',
515
+ own_grant: 'nobody grants themselves a role',
516
+ service_cannot_hold_role: 'service accounts hold project grants only',
517
+ not_service_manager: 'they hold less than the service account, or than they gave it',
415
518
  unknown_environment: 'no such environment',
416
519
  unknown_secret: 'no such secret',
417
520
  no_value: 'it holds no value',
@@ -427,6 +530,9 @@ const REASONS: Record<string, string> = {
427
530
  too_many_approvals: 'too many changes waiting for approval',
428
531
  request_state: 'its retry did not match its call',
429
532
  changed: 'the change was not the one shown',
533
+ replaced: 'what it replaces changed since it was shown',
534
+ unconfirmed: 'what it replaces could not be read',
535
+ disconnected: 'the app was disconnected',
430
536
  };
431
537
 
432
538
  /** What a missing permission meant, from the app's `missing_<permission>`. */
@@ -1,5 +1,5 @@
1
1
  import type { BindingPlan, BindingView } from '@coffre/client';
2
- import { fullRef, githubReusable, githubWorkflow, gitlabProject, type BindingClaims } from '@coffre/core/workloads';
2
+ import { claimValues, fullRef, githubReusable, githubWorkflow, gitlabProject, type BindingClaims } from '@coffre/core/workloads';
3
3
 
4
4
  /**
5
5
  * Trust bindings as the pages show and make them. A binding is its claims;
@@ -9,24 +9,43 @@ import { fullRef, githubReusable, githubWorkflow, gitlabProject, type BindingCla
9
9
 
10
10
  /** A binding in a line: what it trusts, and at which ref. */
11
11
  export function summary(binding: Pick<BindingView, 'profile' | 'issuer' | 'claims'>): { title: string; detail: string } {
12
- const { claims } = binding;
12
+ // Every claim is one value, but what started the run, which may be several.
13
+ const claims = binding.claims as Record<string, string | undefined>;
14
+ const events = claimValues(binding.claims.event_name).join(', ');
13
15
  const short = (ref: string | undefined) => ref?.replace(/^refs\/(heads|tags)\//, '') ?? '';
14
16
  const [workflow, at] = (claims.workflow_ref ?? '').split('@');
15
17
  if (binding.profile === 'github') {
16
18
  const [owner, repository, , , file] = (workflow ?? '').split('/');
17
- return { title: `${owner}/${repository} · ${file}`, detail: `${claims.event_name} at ${short(at)}` };
19
+ return { title: `${owner}/${repository} · ${file}`, detail: `${events} at ${short(at)}` };
18
20
  }
19
21
  if (binding.profile.startsWith('github-reusable')) {
20
22
  const called = (claims.job_workflow_ref ?? '').split('@')[0]!.replace('/.github/workflows/', ' · ');
21
23
  const from = binding.profile === 'github-reusable-organization' ? 'any repository of the organization' : `repository ${claims.repository_id}`;
22
- return { title: `${called} @ ${claims.job_workflow_sha?.slice(0, 7)}`, detail: `${claims.event_name} at ${short(claims.ref)}, from ${from}` };
24
+ return { title: `${called} @ ${claims.job_workflow_sha?.slice(0, 7)}`, detail: `${events} at ${short(claims.ref)}, from ${from}` };
23
25
  }
24
26
  if (binding.profile === 'gitlab') {
25
- return { title: `GitLab project ${claims.project_id}`, detail: `${claims.pipeline_source} at ${claims.ref_type} ${claims.ref}` };
27
+ return { title: `GitLab project ${claims.project_id}`, detail: `${claimValues(binding.claims.pipeline_source).join(', ')} at ${claims.ref_type} ${claims.ref}` };
26
28
  }
27
29
  return { title: new URL(binding.issuer).host, detail: `sub ${claims.sub}` };
28
30
  }
29
31
 
32
+ /**
33
+ * What a new binding reaches, in the Trust dialog: what the person chose in
34
+ * the picker, or until they do, what the server makes it, which the request
35
+ * leaves out: what the bindings it replaces reached, or all the account
36
+ * holds. Once a plan says it, the picker shows it.
37
+ */
38
+ export type ScopeChoice = { scope: BindingPlan['scope']; chosen: boolean };
39
+
40
+ /** The scope the dialog asks for: the person's, or none, for the server's own. */
41
+ export function askedScope(choice: ScopeChoice): { scope?: BindingPlan['scope'] } {
42
+ return choice.chosen ? { scope: choice.scope } : {};
43
+ }
44
+
45
+ /** The picker once a plan came back: what the server would make, unless the person chose. */
46
+ export function planned(choice: ScopeChoice, plan: Pick<BindingPlan, 'scope'>): ScopeChoice {
47
+ return choice.chosen ? choice : { scope: plan.scope, chosen: false };
48
+ }
30
49
 
31
50
  export type Platform = 'github' | 'gitlab' | 'other';
32
51
  export type RefKind = 'branch' | 'tag';
@@ -40,7 +59,7 @@ export const EMPTY_FORM = {
40
59
  repositoryId: '',
41
60
  ownerId: '',
42
61
  workflow: '',
43
- event: 'push' as string,
62
+ events: ['push'] as string[],
44
63
  refKind: 'branch' as RefKind,
45
64
  refName: 'main',
46
65
  called: '',
@@ -51,7 +70,7 @@ export const EMPTY_FORM = {
51
70
  project: '',
52
71
  projectId: '',
53
72
  namespaceId: '',
54
- source: 'push' as string,
73
+ sources: ['push'] as string[],
55
74
  // Any other issuer
56
75
  issuer: '',
57
76
  sub: '',
@@ -66,6 +85,10 @@ export function bindingOf(form: Form): { profile: BindingPlan['profile']; issuer
66
85
  if (value.trim() === '') throw new Error(`${what} is needed`);
67
86
  return value.trim();
68
87
  };
88
+ const some = (values: string[], what: string) => {
89
+ if (values.length === 0) throw new Error(`Choose at least one ${what}`);
90
+ return values;
91
+ };
69
92
  if (form.platform === 'github') {
70
93
  const ref = fullRef(form.refKind, needed(form.refName, form.refKind === 'branch' ? 'The branch' : 'The tag'));
71
94
  if (form.reusable) {
@@ -74,7 +97,7 @@ export function bindingOf(form: Form): { profile: BindingPlan['profile']; issuer
74
97
  const ownerId = needed(form.ownerId, "The owner's ID");
75
98
  const caller = form.workflow.trim() === '' ? undefined : { repository: needed(form.repository, 'The repository'), workflow: form.workflow.trim() };
76
99
  const repositoryId = form.anyRepository ? null : needed(form.repositoryId, "The repository's ID");
77
- return { ...githubReusable({ repositoryId, ownerId, ref, event: form.event, called, sha, caller }), issuer: null };
100
+ return { ...githubReusable({ repositoryId, ownerId, ref, events: some(form.events, 'event'), called, sha, caller }), issuer: null };
78
101
  }
79
102
  return {
80
103
  ...githubWorkflow({
@@ -83,7 +106,7 @@ export function bindingOf(form: Form): { profile: BindingPlan['profile']; issuer
83
106
  ownerId: needed(form.ownerId, "The owner's ID"),
84
107
  workflow: needed(form.workflow, 'The workflow file'),
85
108
  ref,
86
- event: form.event,
109
+ events: some(form.events, 'event'),
87
110
  }),
88
111
  issuer: null,
89
112
  };
@@ -95,7 +118,7 @@ export function bindingOf(form: Form): { profile: BindingPlan['profile']; issuer
95
118
  projectId: needed(form.projectId, "The project's ID"),
96
119
  refType: form.refKind,
97
120
  ref: needed(form.refName, form.refKind === 'branch' ? 'The branch' : 'The tag'),
98
- source: form.source,
121
+ sources: some(form.sources, 'pipeline source'),
99
122
  }),
100
123
  issuer: form.gitlabUrl.trim() === '' ? null : form.gitlabUrl.trim(),
101
124
  };
@@ -1,3 +1,5 @@
1
+ import { administers, makesProjects, type InstanceRole } from '@coffre/core/access';
2
+
1
3
  import type { Me, Permission, ProjectSummary } from '../shared/models';
2
4
 
3
5
  /**
@@ -8,13 +10,28 @@ import type { Me, Permission, ProjectSummary } from '../shared/models';
8
10
  * live outside one resource page.
9
11
  */
10
12
  export type UiCapabilities = {
13
+ /** Admins and owners, scoped or not, and root admins: the Users and Service accounts pages. */
11
14
  canManageGrants: boolean;
15
+ /**
16
+ * Admins and owners whose scope narrows nothing, and root admins: adding
17
+ * and removing people and service accounts, instance roles, tokens and
18
+ * trust bindings, offboarding reports, the instance's keys.
19
+ */
20
+ runsInstance: boolean;
21
+ /**
22
+ * Who sets up service accounts: those who run the instance, and a person
23
+ * who holds access somewhere the instance's setting takes in. The Service
24
+ * accounts page lists them the ones they manage.
25
+ */
26
+ setsUpServices: boolean;
12
27
  canReadAudit: boolean;
13
28
  canCreateProject: boolean;
14
29
  };
15
30
 
16
31
  const NONE: UiCapabilities = {
17
32
  canManageGrants: false,
33
+ runsInstance: false,
34
+ setsUpServices: false,
18
35
  canReadAudit: false,
19
36
  canCreateProject: false,
20
37
  };
@@ -25,17 +42,20 @@ export function deriveUiCapabilities(
25
42
  ): UiCapabilities {
26
43
  if (me === null) return NONE;
27
44
 
28
- const canManageInstance =
29
- me.instanceRole === 'owner' || me.instanceRole === 'root-admin';
45
+ const isRootAdmin = me.instanceRole === 'root-admin';
46
+ const role: InstanceRole = me.instanceRole === 'root-admin' ? 'owner' : me.instanceRole;
30
47
 
31
48
  return {
32
- // Users and Tokens manage the instance directory, not project grants.
33
- // Project access managers keep their grant controls on each project page.
34
- canManageGrants: canManageInstance,
49
+ // Users and Service accounts list the instance's directory, for those
50
+ // who manage access across projects. Project access managers keep their
51
+ // grant controls on each project page.
52
+ canManageGrants: isRootAdmin || administers(role),
53
+ runsInstance: me.runsInstance,
54
+ setsUpServices: me.setsUpServices,
35
55
  canReadAudit: me.canReadAudit,
36
- // Project creation has no resource on which to hold a grant, so it follows
37
- // the instance-wide owner role (including configured root admins).
38
- canCreateProject: canManageInstance,
56
+ // A new project is one no `only` list names: as the server decides,
57
+ // from the role and the scope, projects by slug.
58
+ canCreateProject: makesProjects({ isRootAdmin, role, scope: me.scope, grants: [] }),
39
59
  };
40
60
  }
41
61
 
@@ -1,5 +1,5 @@
1
1
  import type { CoffreClient, ConnectedApp, IdentityRow, OffboardingReport, SecretKey, SessionRow } from '@coffre/client';
2
- import type { Role } from '@coffre/core/access';
2
+ import { EVERYWHERE, unscoped, type InstanceRole, type Role, type Scope } from '@coffre/core/access';
3
3
  import type { QueryKey } from '@tanstack/react-query';
4
4
 
5
5
  import { failureMessage, memberRef, Refusal } from './coffre.ts';
@@ -42,10 +42,10 @@ const removing = (id: string): Target => ({ id, kind: 'removing' });
42
42
 
43
43
  type Grants = ({ ok: true; grants: GrantRow[] } | Failure);
44
44
 
45
- /** A grant's identity, before the server has given it an id: who, and where. */
45
+ /** A grant's identity, before the server has given it an id: who, and where; or who, by their instance role. */
46
46
  export function grantId(grant: Pick<GrantRow, 'principalType' | 'principalId' | 'environmentSlug'> & { scope?: GrantRow['scope'] }): string {
47
- const everywhere = grant.scope === 'every-project' ? '*/' : '';
48
- return `${memberRef(grant.principalType, grant.principalId)}@${everywhere}${grant.environmentSlug ?? ''}`;
47
+ const member = memberRef(grant.principalType, grant.principalId);
48
+ return grant.scope === 'instance-role' ? `${member}#role` : `${member}@${grant.environmentSlug ?? ''}`;
49
49
  }
50
50
 
51
51
  export function grantsList(project: string) {
@@ -127,10 +127,18 @@ type Directory = ({ ok: true; principals: DirectoryPrincipal[] } | Failure);
127
127
  const principalId = (principal: Pick<DirectoryPrincipal, 'principalType' | 'principalId'>) =>
128
128
  memberRef(principal.principalType, principal.principalId);
129
129
 
130
- /** The directory as owners read it: the only ones who change it. */
130
+ /** The directory as admins and owners read it: the only ones who change it. */
131
131
  export const directoryList = listOf<DirectoryPrincipal, Directory>([...keys.directory, { owner: true }], 'principals', principalId);
132
132
 
133
- export type InviteVars = { principalType: DirectoryPrincipal['principalType']; principalId: string; owner: boolean };
133
+ /** A person's instance role and where it applies, projects by slug: what Add and Change role set. */
134
+ export type RoleVars = { role: InstanceRole; scope: Scope };
135
+
136
+ export type InviteVars = { principalType: DirectoryPrincipal['principalType']; principalId: string } & RoleVars;
137
+
138
+ /** What the API takes for a role: the scope only when it narrows anything. */
139
+ function roleInput({ role, scope }: RoleVars): RoleVars | { role: InstanceRole } {
140
+ return role === 'member' || unscoped(scope) ? { role } : { role, scope };
141
+ }
134
142
 
135
143
  export function invite(client: CoffreClient): Change<Directory, DirectoryPrincipal, InviteVars, void> {
136
144
  return {
@@ -144,7 +152,8 @@ export function invite(client: CoffreClient): Change<Directory, DirectoryPrincip
144
152
  {
145
153
  principalType: vars.principalType,
146
154
  principalId: vars.principalId,
147
- instanceRole: vars.owner ? 'owner' : 'user',
155
+ instanceRole: vars.principalType === 'user' ? vars.role : 'member',
156
+ scope: vars.principalType === 'user' ? vars.scope : EVERYWHERE,
148
157
  isRootAdmin: false,
149
158
  },
150
159
  ],
@@ -155,12 +164,12 @@ export function invite(client: CoffreClient): Change<Directory, DirectoryPrincip
155
164
  const member = memberRef(vars.principalType, vars.principalId);
156
165
  const { members } = await client.members.list();
157
166
  if (members.some((entry) => entry.member === member)) throw new Refusal('That principal already exists.');
158
- await client.members.add(member, { owner: vars.principalType === 'user' && vars.owner });
167
+ await client.members.add(member, vars.principalType === 'user' ? roleInput(vars) : {});
159
168
  },
160
169
  };
161
170
  }
162
171
 
163
- export function changeRole(client: CoffreClient): Change<Directory, DirectoryPrincipal, { principalId: string; owner: boolean }, unknown> {
172
+ export function changeRole(client: CoffreClient): Change<Directory, DirectoryPrincipal, { principalId: string } & RoleVars, unknown> {
164
173
  const id = (vars: { principalId: string }) => memberRef('user', vars.principalId);
165
174
  return {
166
175
  list: directoryList,
@@ -168,10 +177,10 @@ export function changeRole(client: CoffreClient): Change<Directory, DirectoryPri
168
177
  targets: (vars) => [saving(id(vars))],
169
178
  apply: (principals, vars) =>
170
179
  principals.map((principal) =>
171
- principalId(principal) === id(vars) ? { ...principal, instanceRole: vars.owner ? 'owner' : 'user' } : principal,
180
+ principalId(principal) === id(vars) ? { ...principal, instanceRole: vars.role, scope: vars.scope } : principal,
172
181
  ),
173
182
  affects: (vars) => affects.role(id(vars)),
174
- run: (vars) => client.members.add(id(vars), { owner: vars.owner }),
183
+ run: (vars) => client.members.add(id(vars), roleInput(vars)),
175
184
  };
176
185
  }
177
186
 
@@ -295,8 +304,8 @@ export function createProject(client: CoffreClient): Change<Projects, ProjectSum
295
304
  name: vars.name,
296
305
  archivedAt: null,
297
306
  folder: null,
298
- // You become its owner.
299
- permissions: ['project.manage', 'environment.manage', 'grant.manage'],
307
+ // Its maker manages it by their instance role, an Admin's or an Owner's.
308
+ permissions: ['audit.read', 'environment.manage', 'grant.manage', 'project.manage'],
300
309
  environments: [],
301
310
  secretCount: null,
302
311
  },
@@ -81,3 +81,24 @@ export function projectAccessRoles(
81
81
  if (currentRole) allowed.add(currentRole);
82
82
  return roles.filter((role) => allowed.has(role.slug));
83
83
  }
84
+
85
+ /** A project on someone's Access tab: their grants there, or why those could not be read. */
86
+ export type ProjectAccess = { project: ProjectSummary; grants: GrantRow[]; grantsError: string | null };
87
+
88
+ /** A row of the Access tab: one of their grants, or a project whose grants could not be read. */
89
+ export type AccessRow =
90
+ | { project: ProjectSummary; grant: GrantRow; error: null }
91
+ | { project: ProjectSummary; grant: null; error: string };
92
+
93
+ /**
94
+ * The Access tab's rows, project by project: a row per grant, and a project
95
+ * whose read failed keeps a row of its own that says why, so a failure
96
+ * never reads as no access.
97
+ */
98
+ export function accessRows(access: ProjectAccess[]): AccessRow[] {
99
+ return access.flatMap(({ project, grants, grantsError }): AccessRow[] =>
100
+ grantsError === null
101
+ ? grants.map((grant) => ({ project, grant, error: null }))
102
+ : [{ project, grant: null, error: grantsError }],
103
+ );
104
+ }