feat(backend): license entitlements, enforced limits, free tier

Platform v2 step 1.4 (APPS_PLATFORM D-3..D-6, D-21, §5).
- core/modules/entitlements.py: compute() turns the evaluated license into
  per-module grants: the v2 token's `modules` map (own `exp` with the 5-day
  grace, never cut mid-service) + the free tier; no/invalid token, expired
  after grace, inactive, locked or clock tampering -> free tier only; core
  always. limit(module, name) with token value -> manifest licensed_default ->
  free value; None = unlimited. LICENSE_MODE=off (dev/test only) = everything
- module gating says why: module.not_licensed (402, the first unlicensed
  module in the dependency chain) before module.disabled (403)
- services/license.py accepts only v2 tokens with a modules map; the v1
  "legacy unsigned 72h" path is gone
- apply_license() always installs entitlements (startup before workers,
  every heartbeat, workday close); workers follow license changes
- the global license middleware is deleted (no more whole-API 402/423);
  license_state moved to services/license_state.py
- history_days enforced in every report/history endpoint: lower date bounds
  clamped (also when no "from" is given), and business days older than the
  limit refused by id (license.history_limited); data is never deleted
- bootstrap/modules API show license mode, entitlement source and limit values
- LICENSE_MODE=off in docker-compose.dev.yml and the e2e launchers; sites
  default to enforced
- tests: entitlement rules, limits, the three done-when checks, signed-token
  apply_license path; license tests moved to v2 tokens

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-12 18:32:57 +02:00
co-authored by Claude Opus 5.5
parent 231a97c186
commit 0d43e6a111
21 changed files with 628 additions and 172 deletions
+10 -4
View File
@@ -12,7 +12,7 @@ Agent notes:
adds per-user grants; superadmin gets every key (D-7).
- Access: PUT uses require_settings_manager until step 1.5 switches it to
require("core.modules.manage").
- `entitled` is True for every module until step 1.4.
- `entitled`/`entitlement`/limit `value` come from core/modules/entitlements.py (step 1.4).
"""
from __future__ import annotations
@@ -24,7 +24,7 @@ from pydantic import BaseModel
from sqlalchemy.orm import Session
from core import i18n, timeutil
from core.modules import state
from core.modules import entitlements, state
from core.modules.registry import all_permissions, registry
from database import get_db
from routers.deps import get_current_user, require_settings_manager
@@ -48,16 +48,21 @@ def _setting(db: Session, key: str, default: str) -> str:
def _module_out(m, enabled: dict, effective: frozenset) -> dict:
ent = entitlements.current()
grant = ent.grants.get(m.key)
return {
"key": m.key, "kind": m.kind, "name": m.name, "description": m.description,
"depends": list(m.depends),
"entitled": True,
"entitled": grant is not None,
"entitlement": ({"source": grant.source, "exp": grant.exp, "exp_deferred": grant.exp_deferred}
if grant else None),
"enabled": enabled.get(m.key, True),
"effective": m.key in effective,
"can_disable": m.can_disable,
"permissions": [{"key": p.key, "description": p.description, "roles": list(p.roles)} for p in m.permissions],
"limits": [{"key": lim.key, "description": lim.description, "free": lim.free,
"licensed_default": lim.licensed_default} for lim in m.limits],
"licensed_default": lim.licensed_default,
"value": ent.limit(m.key, lim.key)} for lim in m.limits],
"settings": [{"key": sd.key, "type": sd.type, "default": sd.default, "description": sd.description}
for sd in m.settings],
"device_app": ({"login_modes": list(m.device_app.login_modes), "scope": list(m.device_app.scope)}
@@ -87,6 +92,7 @@ def bootstrap(
"business_day_rollover_hour": timeutil.rollover_hour(),
},
"modules": [{"key": m.key, "kind": m.kind} for m in registry().values() if m.key in effective],
"license": {"mode": entitlements.current().mode, "reason": entitlements.current().reason},
"languages": list(i18n.SUPPORTED_LANGUAGES),
"user": None,
"language": i18n.venue_language(),