Coverage for app / services / platform_roles.py: 100%
37 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-10-31 11:21 +0000
« prev ^ index » next coverage.py v7.13.5, created at 2026-10-31 11:21 +0000
1"""Platform roles: what a person may do on CHAIN-R itself, set in Auth0.
3Membership roles (``app/services/membership.py``) say what a person may do in
4an organisation or project. A platform role says what they may do on the
5platform: run its developer tools, administer it. They are assigned in Auth0,
6where an Action puts them in the ID token
7(``docs/system/security/auth-flow.md#platform-roles``); svc-auth keeps them
8on the session, svc-bff carries them in the internal token, and this module
9keeps a user's row in step with them (:func:`sync`).
11Auth0 is the source of truth only where it says something. A request whose
12identity carries no roles at all (no Action configured, an older session)
13changes nothing, so a superuser seeded locally or the platform's own system
14user keeps theirs. A request that carries a list, even an empty one, sets
15the user's roles to exactly the ones this registry knows.
17**A superuser holds every role.** ``is_superuser`` stays the column every
18access check reads: it passes every one of them, which is why scenario runs
19ask for ``developer`` instead — a developer's own pages show only their own
20data.
21"""
23from __future__ import annotations
25import logging
26from collections.abc import Iterable
27from dataclasses import dataclass
28from enum import Enum
29from typing import Any
31logger = logging.getLogger(__name__)
34class PlatformRole(str, Enum):
35 superuser = "superuser"
36 developer = "developer"
39@dataclass(frozen=True)
40class RoleSpec:
41 role: PlatformRole
42 label: str
43 #: What holding it unlocks, as the docs and the API say it.
44 unlocks: str
47ROLES: dict[PlatformRole, RoleSpec] = {
48 spec.role: spec
49 for spec in (
50 RoleSpec(
51 PlatformRole.superuser,
52 "Superuser",
53 "Platform administration: passes every access check, so sees and "
54 "changes every user's resources. Holds every other role.",
55 ),
56 RoleSpec(
57 PlatformRole.developer,
58 "Developer",
59 "Developer tools such as scenario runs, on a deployment that "
60 "switches them on. Changes no access check.",
61 ),
62 )
63}
65assert set(ROLES) == set(PlatformRole), "every role needs its spec"
68def known(roles: Iterable[str]) -> list[str]:
69 """The roles of ``roles`` this registry knows, sorted and distinct; an
70 Auth0 role CHAIN-R has no use for is not kept."""
71 values = {role.value for role in PlatformRole}
72 return sorted({role for role in roles if role in values})
75def roles_of(user: Any) -> list[str]:
76 """The platform roles a user holds: their own, or every one for a
77 superuser."""
78 if getattr(user, "is_superuser", False):
79 return sorted(role.value for role in PlatformRole)
80 return known(getattr(user, "platform_roles", None) or [])
83def holds(user: Any, role: PlatformRole) -> bool:
84 return role.value in roles_of(user)
87def sync(user: Any, roles: Iterable[str] | None) -> bool:
88 """Set ``user``'s platform roles to what Auth0 said; ``None`` (nothing
89 said) leaves them. True when the row changed, so the caller commits."""
90 if roles is None:
91 return False
92 kept = known(roles)
93 superuser = PlatformRole.superuser.value in kept
94 if list(user.platform_roles or []) == kept and bool(user.is_superuser) == superuser:
95 return False
96 user.platform_roles = kept
97 user.is_superuser = superuser
98 logger.info("Platform roles of user %s are now %s", user.id, kept or "none")
99 return True