Add registration toggle and password change; genericise deploy
Two things the running instance needed. **Registration toggle.** Admin -> General, backed by a new settings table group rather than the environment. LEMBAS_ALLOW_SIGNUP now seeds only the initial value: once an administrator saves the setting, the stored value wins. The alternative -- environment always winning -- means a toggle in the UI silently reverts on the next restart, which is worse than not offering one. Closing registration also removes the "Create one" link from the sign-in page, so the link never leads somewhere that refuses. **Password change**, on the user settings page. Changing a password revokes every other session and immediately re-issues a cookie for the current one: if the reason for the change is that somebody else knows the password, leaving their session alive defeats the point, but signing the user out of the tab they are standing in is merely rude. **deploy/ is now host-agnostic.** This repository is public, so the unit and vhost became templates with __PREFIX__ / __SITE_HOST__ / __APP_PORT__ substituted at install time, and every path, hostname and port moved to environment variables. REPO_URL defaults to the checkout's own origin so a fork deploys itself. Machine-specific values belong in private notes, not here -- CLAUDE.md now says so. 83 tests, ruff clean. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
"""Instance-wide settings that administrators can change at runtime.
|
||||
|
||||
Distinct from ``lembas.config``, which holds deployment configuration read from
|
||||
the environment at startup. Anything here is editable from the admin UI and
|
||||
lives in the ``settings`` table.
|
||||
|
||||
Environment variables act as the *initial* value only. Once an administrator
|
||||
sets something in the UI, the stored value wins -- otherwise a toggle in the
|
||||
interface would silently revert on the next restart, which is worse than not
|
||||
offering the toggle at all.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
from sqlalchemy.orm import Session as DBSession
|
||||
|
||||
from lembas.config import settings as env_settings
|
||||
from lembas.db.models import Setting
|
||||
|
||||
GENERAL = "general"
|
||||
|
||||
|
||||
def _defaults() -> dict[str, Any]:
|
||||
return {
|
||||
"allow_signup": env_settings.allow_signup,
|
||||
# When on, new accounts land in the `pending` role and cannot sign in
|
||||
# until an administrator approves them. Reserved for the users pass.
|
||||
"require_approval": False,
|
||||
"instance_name": "LLeMbas",
|
||||
}
|
||||
|
||||
|
||||
def get_group(db: DBSession, key: str = GENERAL) -> dict[str, Any]:
|
||||
"""Stored settings for a group, with defaults filled in for absent keys."""
|
||||
values = _defaults() if key == GENERAL else {}
|
||||
row = db.get(Setting, key)
|
||||
if row is not None and isinstance(row.value, dict):
|
||||
values.update(row.value)
|
||||
return values
|
||||
|
||||
|
||||
def get(db: DBSession, name: str, *, key: str = GENERAL) -> Any:
|
||||
return get_group(db, key).get(name)
|
||||
|
||||
|
||||
def update(db: DBSession, changes: dict[str, Any], *, key: str = GENERAL) -> dict[str, Any]:
|
||||
"""Merge changes into a settings group and persist them."""
|
||||
row = db.get(Setting, key)
|
||||
if row is None:
|
||||
row = Setting(key=key, value={})
|
||||
db.add(row)
|
||||
|
||||
# Reassigned rather than mutated: SQLAlchemy only reliably detects a change
|
||||
# to a JSON column when the whole value is replaced.
|
||||
row.value = {**(row.value or {}), **changes}
|
||||
db.commit()
|
||||
return get_group(db, key)
|
||||
|
||||
|
||||
def signup_allowed(db: DBSession) -> bool:
|
||||
return bool(get(db, "allow_signup"))
|
||||
Reference in New Issue
Block a user