Files
ai-agent/backend/authz/catalog.py

193 lines
9.1 KiB
Python

"""The permission catalog: every action the hub can allow, named once.
A **permission** is either
* a **route** permission — the gate (``core/auth.py``) checks it before any
handler runs; ``authz/routes.py`` says which permission each API route needs;
* a **feature** permission — no route of its own: a handler or the run's scope
reads it (``caller.has("cli.use")``), e.g. whether a guest's runs get the
``ai-agent`` CLI.
and sits in a **tier**:
* ``person`` — may be held by anyone in ``users.json`` (through their role or
an override);
* ``admin`` — the administration of the hub: held by the Admin role, which
nobody but the owner holds yet (team mode turns it on for people);
* ``host`` — amounts to running code on the machine (hooks, the ``.claude``
tree, unscoped runs, workers, Claude logins): the **operator** only — the
machine's owner. No role but Operator ever holds one.
Roles (``authz/roles.py``) are named bundles of these ids; a person's
effective set is their role's, plus their ``allow`` overrides, minus their
``deny`` ones (``authz/policy.py``). See docs/permissions.md.
"""
from __future__ import annotations
import dataclasses
PERSON, ADMIN, HOST = "person", "admin", "host"
TIERS = (PERSON, ADMIN, HOST)
ROUTE, FEATURE = "route", "feature"
@dataclasses.dataclass(frozen=True)
class Permission:
id: str
group: str
label: str
help: str
tier: str = PERSON
kind: str = ROUTE
# Every signed-in role holds it: the app doesn't load without it.
core: bool = False
GROUPS = (
("basics", "Basics"),
("conversations", "Conversations"),
("runs", "Runs"),
("inbox", "Forms, questions and pushes"),
("projects", "Projects and files"),
("own", "Their own things"),
("admin", "Administration"),
("host", "This machine (operator)"),
)
_P = Permission
PERMISSIONS: tuple[Permission, ...] = (
# ── basics ──
_P("whoami", "basics", "Who they are",
"Their name, role, permissions and project grants (`/api/me`).", core=True),
_P("self", "basics", "Their account",
"Profile, push devices, drafts, display settings, their stats, live updates.",
core=True),
_P("app.read", "basics", "App basics",
"The model list, whether the Claude logins are healthy, Code Mode's availability.",
core=True),
_P("self.claude", "basics", "Their own Claude login",
"Connect their own Claude subscription; their runs then bill it."),
# ── conversations ──
_P("conversations.read", "conversations", "Read conversations",
"Their own conversations and the ones shared with them — thread, diffs, subagents."),
_P("conversations.edit", "conversations", "Rename and archive",
"Change the title of their conversations, archive them."),
_P("conversations.share", "conversations", "Share and follow",
"Share a conversation with someone, mute or follow one."),
_P("conversations.all", "conversations", "Everyone's conversations",
"See every conversation on the hub, not only theirs and the shared ones.",
tier=ADMIN, kind=FEATURE),
_P("conversations.past", "conversations", "Past-conversation search",
"Find conversations by issue, PR or id across the hub (`ai-agent past`).",
tier=ADMIN),
_P("conversations.manage", "conversations", "Conversation upkeep",
"Archive old ones, colours and icons, summaries, scaffolds.", tier=ADMIN),
# ── runs ──
_P("runs.start", "runs", "Start and continue runs",
"Start, reply to, fork and stop conversations — in projects they hold chat on."),
_P("runs.scratch.bash", "runs", "Shell in No-project chats",
"Their No-project chats may run commands (Bash).", kind=FEATURE),
_P("runs.codemode", "runs", "Code Mode (beta)",
"Start conversations in Claude Code's Code Mode.", kind=FEATURE),
_P("cli.use", "runs", "ai-agent CLI in their runs",
"Their runs may drive the hub as them: start, read and schedule conversations.",
kind=FEATURE),
_P("mcp.connect", "runs", "Connect AI apps",
"Connect Claude.ai, ChatGPT and other AI apps to the hub, as them.", kind=FEATURE),
# ── inbox ──
_P("inbox.read", "inbox", "See forms, questions and pushes",
"The forms, questions and pushes of the conversations they hear about."),
_P("inbox.answer", "inbox", "Answer them",
"Submit forms, answer questions, mark pushes seen. A run's own CLI never "
"gets this: the human answers."),
_P("notify.send", "inbox", "Raise pushes and forms",
"What the notify / ask-form CLIs and hooks call.", tier=ADMIN),
_P("notifications.ledger", "inbox", "The owner's read state",
"Which notifications the owner has opened; the desk phone's unread list.",
tier=ADMIN),
# ── projects ──
_P("projects.read", "projects", "Their projects",
"List the projects granted to them and open their files."),
_P("files.upload", "projects", "Attach files", "Upload attachments to a message."),
_P("projects.manage", "projects", "Project settings",
".env editor, merges, deploy hosts, new-project requests.", tier=ADMIN),
# ── their own things ──
_P("cron.own", "own", "Their scheduled jobs",
"Discuss a result of one of their scheduled jobs (the jobs themselves are "
"the Cron jobs plugin's, through its grant)."),
_P("computers.pair", "own", "Their own computers",
"Pair computers of their own as workers.", kind=FEATURE),
_P("plugins.use", "own", "Use plugins",
"Install hub-catalog plugins and configure their settings and secrets."),
_P("plugins.install", "own", "Install custom plugins",
"Install live plugins from paths or git URLs.", kind=FEATURE),
_P("plugins.create", "own", "Create plugins",
"Create and iterate on live plugins from inside the app.", kind=FEATURE),
# ── administration ──
_P("plugins.manage", "admin", "Plugin catalog",
"Promote plugins to the hub catalog and approve internal-host plugins.", tier=ADMIN),
_P("users.manage", "admin", "People and roles",
"Invite people, grant projects, edit roles, impersonate, What's new.", tier=ADMIN),
_P("security.manage", "admin", "Security",
"The security log, lock guests, stop every run.", tier=ADMIN),
_P("usage.read", "admin", "Plan usage", "Plan usage, budgets and burn.", tier=ADMIN),
_P("usage.manage", "admin", "Plan usage settings",
"Plans, calibration, imports.", tier=ADMIN),
_P("perf.read", "admin", "Performance and storage",
"What runs cost in CPU, memory and disk.", tier=ADMIN),
_P("metrics.read", "admin", "Hub metrics",
"The hub's Prometheus metrics (/api/metrics): latency, lag, streams.", tier=ADMIN),
_P("perf.manage", "admin", "Kill and clean up",
"Kill a process, remove what a session left on disk.", tier=ADMIN),
_P("workers.read", "admin", "See workers", "The runners and how the profiles use them.",
tier=ADMIN),
_P("lab.read", "admin", "The lab's catalogue",
"Dashboards, skills, memories, plans, services, templates, the .claude tree.",
tier=ADMIN),
_P("lab.settings", "admin", "App settings",
"Tags, model presets, fresh-context thresholds, the clarifier.", tier=ADMIN),
# ── this machine ──
_P("accounts.manage", "host", "Claude accounts and API keys",
"Sign the machine's Claude logins in and out, the API keys.", tier=HOST),
_P("workers.manage", "host", "Workers and profiles",
"Pair, update and route workers; MCP servers; plugins.", tier=HOST),
_P("mcp.manage", "host", "Everyone's AI-app connections",
"Approve and revoke MCP connections, owner ones included.", tier=HOST),
_P("automation.manage", "host", "Cron jobs and agents",
"Scheduled agents run unscoped on the machine.", tier=HOST),
_P("lab.context.edit", "host", "Edit the .claude tree",
"Hooks, skills, CLAUDE.md: what every run loads.", tier=HOST,
kind=FEATURE),
_P("runs.unscoped", "host", "Unscoped runs",
"Runs with every permission on the machine, in any folder.", tier=HOST,
kind=FEATURE),
)
BY_ID: dict[str, Permission] = {p.id: p for p in PERMISSIONS}
ALL: frozenset[str] = frozenset(BY_ID)
CORE: frozenset[str] = frozenset(p.id for p in PERMISSIONS if p.core)
ROUTE_PERMS: frozenset[str] = frozenset(p.id for p in PERMISSIONS if p.kind == ROUTE)
FEATURE_PERMS: frozenset[str] = ALL - ROUTE_PERMS
def of_tier(*tiers: str) -> frozenset[str]:
return frozenset(p.id for p in PERMISSIONS if p.tier in tiers)
PERSON_PERMS = of_tier(PERSON)
ADMIN_PERMS = of_tier(PERSON, ADMIN) # what the Admin role may hold
HOST_PERMS = of_tier(HOST)
def clean(ids, allowed: frozenset[str] = ALL) -> list[str]:
"""Known ids of ``allowed``, in catalog order, without duplicates."""
want = {i for i in (ids or ()) if isinstance(i, str)}
return [p.id for p in PERMISSIONS if p.id in want and p.id in allowed]
def public() -> dict:
"""The catalog as the Roles page draws it."""
return {"groups": [{"id": g, "label": label} for g, label in GROUPS],
"permissions": [dataclasses.asdict(p) for p in PERMISSIONS]}