- authz/catalog.py: 42 named permissions in groups and tiers (person, admin, host); authz/roles.py: built-in Operator (the owner), Admin (assignable with team mode), Member, Guest, Viewer + custom clones, in roles.json - a person = role + allow/deny overrides (authz/policy.py); users.json's old switches (cli, mcp, createProjects, scratchBash, codeMode, computers.enabled) migrate once to exactly what they allowed, backup kept - authz/scopes.py: what a credential acting for someone keeps (cli, worker.read|run, bot, phone) — a scope only narrows - authz/routes.py: (method, path) -> permission for all 310 /api routes; the gate asks Caller.allows() for every caller kind, replacing GUEST_ROUTES, CLI_ROUTES and the worker READ/RUN_ROUTES regex lists - golden route x caller matrix (authz/matrix.golden.txt, test_authz_matrix) - the claude bot reads the admin app minus people/security/the machine; the phone key reads its unread notifications only - loopback without the host token is logged (loopback_tokenless), refusals are perm_denied lines; the hub's own MCP calls send the token - /api/roles CRUD, /api/me and /api/users carry role/allow/deny/perms - worker-bound scheduled items and conversation-meta respect the binding Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
102 lines
2.9 KiB
Python
102 lines
2.9 KiB
Python
"""Settings → Users → Roles: the catalog, the roles, and editing them."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from fastapi import APIRouter, HTTPException
|
|
from pydantic import BaseModel
|
|
|
|
import schemas
|
|
from authz import catalog as cat
|
|
from authz import policy, roles
|
|
from core.http import _r
|
|
from core.state import State
|
|
from users.caller import OwnerDep
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
def _members(deps) -> dict[str, int]:
|
|
out: dict[str, int] = {}
|
|
for u in deps.users.all():
|
|
rid = policy.role_of(u)
|
|
out[rid] = out.get(rid, 0) + 1
|
|
return out
|
|
|
|
|
|
def _view(deps, r: dict) -> dict:
|
|
return roles.public(r, _members(deps).get(r["id"], 0))
|
|
|
|
|
|
@router.get("/api/roles", responses=_r(schemas.RolesResponse))
|
|
def roles_list(deps: State, _: OwnerDep):
|
|
members = _members(deps)
|
|
return {**cat.public(), "tiers": list(cat.TIERS),
|
|
"roles": [roles.public(r, members.get(r["id"], 0)) for r in roles.store().all()]}
|
|
|
|
|
|
class RoleClone(BaseModel):
|
|
clonedFrom: str
|
|
name: str
|
|
|
|
|
|
class RolePatch(BaseModel):
|
|
perms: list[str] | None = None
|
|
name: str | None = None
|
|
description: str | None = None
|
|
|
|
|
|
def _changed(deps) -> None:
|
|
# every guest's effective permissions may have moved: their apps refetch /api/me
|
|
deps.hub.publish({"type": "users"})
|
|
|
|
|
|
@router.post("/api/roles", responses=_r(schemas.RoleView))
|
|
def roles_clone(deps: State, _: OwnerDep, body: RoleClone):
|
|
try:
|
|
r = roles.store().clone(body.clonedFrom, body.name)
|
|
except KeyError:
|
|
raise HTTPException(404, "unknown role")
|
|
except ValueError as e:
|
|
raise HTTPException(400, str(e))
|
|
_changed(deps)
|
|
return _view(deps, r)
|
|
|
|
|
|
@router.put("/api/roles/{rid}", responses=_r(schemas.RoleView))
|
|
def roles_update(deps: State, _: OwnerDep, rid: str, body: RolePatch):
|
|
try:
|
|
r = roles.store().update(rid, perms=body.perms, name=body.name,
|
|
description=body.description)
|
|
except KeyError:
|
|
raise HTTPException(404, "unknown role")
|
|
except ValueError as e:
|
|
raise HTTPException(400, str(e))
|
|
_changed(deps)
|
|
return _view(deps, r)
|
|
|
|
|
|
@router.post("/api/roles/{rid}/reset", responses=_r(schemas.RoleView))
|
|
def roles_reset(deps: State, _: OwnerDep, rid: str):
|
|
try:
|
|
r = roles.store().reset(rid)
|
|
except ValueError as e:
|
|
raise HTTPException(400, str(e))
|
|
_changed(deps)
|
|
return _view(deps, r)
|
|
|
|
|
|
@router.delete("/api/roles/{rid}", responses=_r(schemas.OkResponse))
|
|
def roles_delete(deps: State, _: OwnerDep, rid: str):
|
|
n = _members(deps).get(rid, 0)
|
|
if n:
|
|
raise HTTPException(409, f"{n} {'person holds' if n == 1 else 'people hold'} this "
|
|
"role — give them another one first")
|
|
try:
|
|
roles.store().delete(rid)
|
|
except KeyError:
|
|
raise HTTPException(404, "unknown role")
|
|
except ValueError as e:
|
|
raise HTTPException(400, str(e))
|
|
_changed(deps)
|
|
return {"ok": True}
|