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:
@@ -13,7 +13,8 @@ module_required(key) is the FastAPI dependency main.py adds to each non-core rou
|
||||
Agent notes:
|
||||
- Always change enablement through set_enabled(), never by writing SiteModule rows,
|
||||
or the cache and the workers won't follow.
|
||||
- Step 1.4 replaces `_entitled()` with the license evaluation (entitled ∪ free tier).
|
||||
- Entitlement comes from core/modules/entitlements.py (license ∪ free tier, step 1.4).
|
||||
A blocked request says why: module.not_licensed (402) before module.disabled (403).
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
@@ -73,7 +74,19 @@ def enabled_map(db: Session | None = None) -> dict[str, bool]:
|
||||
|
||||
|
||||
def _entitled(key: str) -> bool:
|
||||
return True # >>> step 1.4: license entitlements ∪ free tier
|
||||
from core.modules import entitlements
|
||||
return entitlements.current().entitled(key)
|
||||
|
||||
|
||||
def notify() -> None:
|
||||
"""Tell listeners (the worker supervisor) the effective set may have changed,
|
||||
e.g. after the license was re-evaluated."""
|
||||
eff = effective_set()
|
||||
for fn in list(_listeners):
|
||||
try:
|
||||
fn(eff)
|
||||
except Exception:
|
||||
logger.exception("module listener failed")
|
||||
|
||||
|
||||
def effective_set(db: Session | None = None) -> frozenset[str]:
|
||||
@@ -122,6 +135,23 @@ def set_enabled(db: Session, key: str, enabled: bool, user_id: int | None = None
|
||||
return eff
|
||||
|
||||
|
||||
def blocked_error(key: str, db: Session | None = None) -> XeniaError:
|
||||
"""Why `key` isn't effective: not licensed (itself or a dependency) beats turned off."""
|
||||
reg = registry()
|
||||
seen, stack = set(), [key]
|
||||
while stack:
|
||||
k = stack.pop()
|
||||
if k in seen:
|
||||
continue
|
||||
seen.add(k)
|
||||
if not _entitled(k):
|
||||
return XeniaError("module.not_licensed", module=k)
|
||||
stack.extend(reg[k].depends)
|
||||
enabled = enabled_map(db)
|
||||
off = next((k for k in [key, *sorted(seen - {key})] if not enabled.get(k, True)), key)
|
||||
return XeniaError("module.disabled", module=off)
|
||||
|
||||
|
||||
def module_required(key: str):
|
||||
"""FastAPI dependency: the endpoint answers `module.disabled` unless the module is effective."""
|
||||
if key not in registry():
|
||||
@@ -129,7 +159,7 @@ def module_required(key: str):
|
||||
|
||||
def _check(db: Session = Depends(get_db)) -> None:
|
||||
if not is_effective(key, db):
|
||||
raise XeniaError("module.disabled", module=key)
|
||||
raise blocked_error(key, db)
|
||||
|
||||
_check.__name__ = f"module_required_{key}"
|
||||
return _check
|
||||
|
||||
Reference in New Issue
Block a user