§22 S4: scope-role invitation backend + capability flags (@S4 C.2)

The Owner-only scope-role grant surface (Part E S4 / Part C.2). An Owner
grants {owner, contributor} at a scope their reach covers — the project or
one collection within it — to an existing account looked up by email; the
grant writes a `memberships` row immediately and §15-notifies the grantee
(direct grant, no accept round-trip — the C.2 scenarios name existing
accounts and write the row directly).

- auth.can_invite_at_project / can_invite_at_collection — the Owner-reach
  invite gates (is_project_superuser / is_collection_superuser).
- memberships.py — grant (with the C.2.6 broader-supersedes-narrower prune,
  preserving a stronger child grant — no negative override), revoke, list,
  user_by_email.
- api_memberships.py — GET/POST/DELETE /api/projects/:id/members, the single
  POST keying on optional collection_id so the invite UI's one control maps
  to one endpoint; reach bounded by the inviter's Owner reach (C.2.3);
  contributors refused (C.2.4); a pending grantee's row is recorded but
  confers no write (C.2.7, the §6 floor).
- notify.notify_scope_role_granted + render_summary — the §15 personal-direct
  notification naming the project and role.
- api_collections — surface viewer capabilities (can_create_collection,
  can_invite, can_contribute, role) on the project/collection GETs to drive
  the C.3 role-aware empty states.

Acceptance: test_s4_invitations_vertical.py covers C.2.1–C.2.7 over the HTTP
surface + resolver, plus the C.3 (@S4) capability flags. Full suite green
(504 passed).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Ben Stull
2026-06-05 23:47:28 -07:00
parent e6bd69f132
commit c9fd1c535e
7 changed files with 740 additions and 1 deletions
+55
View File
@@ -324,6 +324,48 @@ def fan_out_contribution_request(
return notif_ids
def notify_scope_role_granted(
*,
recipient_user_id: int,
granter_user_id: int | None,
scope_type: str,
scope_id: str,
role: str,
project_id: str | None,
project_name: str | None,
collection_name: str | None,
) -> int | None:
"""§22 S4 (C.2): a scope Owner granted `recipient` a role at a scope.
Personal-direct — the recipient is the named subject — so it rides the
`email_personal_direct` gate like the other owner-facing personal events.
The scope facts ride in the payload so the inbox row (and email body) names
the project and role without a second fetch. Actor is the granter (§15.9);
a system/administrative grant with no granter renders as "the app".
Returns the notification id, or None when the grantee would be notifying
themselves (a self-grant — no notification)."""
if granter_user_id is not None and recipient_user_id == granter_user_id:
return None
details = {
"scope_type": scope_type,
"scope_id": scope_id,
"role": role,
"project_id": project_id or "",
"project_name": project_name or "",
"collection_name": collection_name or "",
}
return _emit_one(
recipient_user_id=recipient_user_id,
event_kind="scope_role_granted",
category=CATEGORY_PERSONAL,
actor_user_id=granter_user_id,
rfc_slug=None,
branch_name=None,
pr_number=None,
details=details,
)
def notify_contribution_decided(
*,
rfc_slug: str,
@@ -859,6 +901,19 @@ def render_summary(event_kind: str, actor_display: str | None, rfc_title: str |
return f"{actor} accepted your request to contribute to {title} — check your email to accept the invitation."
if event_kind == "contribution_request_declined":
return f"{actor} declined your request to contribute to {title}."
if event_kind == "scope_role_granted":
# §22 S4 (C.2): names the role and the scope (the project, and the
# collection when collection-scoped) per "a §15 notification naming the
# project and role".
role_label = "Owner" if extras.get("role") == "owner" else "RFC Contributor"
project_label = extras.get("project_name") or extras.get("project_id") or "a project"
scope_type = extras.get("scope_type")
if scope_type == "collection":
col_label = extras.get("collection_name") or extras.get("scope_id") or "a collection"
return f"{actor} granted you {role_label} on collection {project_label}/{col_label}."
if scope_type == "global":
return f"{actor} granted you {role_label} across the whole deployment."
return f"{actor} granted you {role_label} on project {project_label}."
if event_kind == "new_beta_request":
# v0.9.0: framework-scoped, not RFC-scoped. The actor (the
# requester) and the captured full name + email read as