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:
+33
-17
@@ -13,6 +13,7 @@ from lembas.config import settings
|
||||
from lembas.db.models import ROLE_ADMIN, ROLE_USER, User
|
||||
from lembas.security.passwords import hash_password, validate_password, verify_password
|
||||
from lembas.security.sessions import COOKIE_NAME, create_session, revoke_session
|
||||
from lembas.services import settings_store
|
||||
from lembas.web.templating import render
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
@@ -48,6 +49,19 @@ def _safe_next(raw: str | None) -> str:
|
||||
return raw
|
||||
|
||||
|
||||
def _login_page(request: Request, db: Db, *, status_code: int = 200, **context):
|
||||
"""Render the sign-in page.
|
||||
|
||||
Always goes through here so `allow_signup` reflects the *stored* setting
|
||||
rather than the environment default baked in by render(). Otherwise the
|
||||
"Create one" link would keep appearing after an administrator closed
|
||||
registration, offering a link that only leads to a refusal.
|
||||
"""
|
||||
context.setdefault("next", "/")
|
||||
context["allow_signup"] = settings_store.signup_allowed(db)
|
||||
return render(request, "auth/login.html", context, status_code=status_code)
|
||||
|
||||
|
||||
@router.get("/login")
|
||||
async def login_form(request: Request, db: Db, user: CurrentUser, next: str = "/"):
|
||||
if user is not None:
|
||||
@@ -57,7 +71,7 @@ async def login_form(request: Request, db: Db, user: CurrentUser, next: str = "/
|
||||
# cannot possibly satisfy.
|
||||
if _no_users_yet(db):
|
||||
return RedirectResponse("/auth/register", status_code=status.HTTP_303_SEE_OTHER)
|
||||
return render(request, "auth/login.html", {"next": _safe_next(next)})
|
||||
return _login_page(request, db, next=_safe_next(next))
|
||||
|
||||
|
||||
@router.post("/login")
|
||||
@@ -75,21 +89,23 @@ async def login(
|
||||
# cannot be used to discover which addresses are registered.
|
||||
if user is None or not verify_password(password, user.password_hash):
|
||||
log.info("failed sign-in for %s", email)
|
||||
return render(
|
||||
return _login_page(
|
||||
request,
|
||||
"auth/login.html",
|
||||
{"error": "That email and password do not match.", "email": email,
|
||||
"next": _safe_next(next)},
|
||||
db,
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
error="That email and password do not match.",
|
||||
email=email,
|
||||
next=_safe_next(next),
|
||||
)
|
||||
|
||||
if not user.active:
|
||||
return render(
|
||||
return _login_page(
|
||||
request,
|
||||
"auth/login.html",
|
||||
{"error": "This account has been deactivated. Ask an administrator.",
|
||||
"email": email, "next": _safe_next(next)},
|
||||
db,
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
error="This account has been deactivated. Ask an administrator.",
|
||||
email=email,
|
||||
next=_safe_next(next),
|
||||
)
|
||||
|
||||
token = create_session(
|
||||
@@ -108,12 +124,12 @@ async def register_form(request: Request, db: Db, user: CurrentUser):
|
||||
if user is not None:
|
||||
return RedirectResponse("/", status_code=status.HTTP_303_SEE_OTHER)
|
||||
first_run = _no_users_yet(db)
|
||||
if not first_run and not settings.allow_signup:
|
||||
return render(
|
||||
if not first_run and not settings_store.signup_allowed(db):
|
||||
return _login_page(
|
||||
request,
|
||||
"auth/login.html",
|
||||
{"error": "Registration is closed. Ask an administrator for an account."},
|
||||
db,
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
error="Registration is closed. Ask an administrator for an account.",
|
||||
)
|
||||
return render(request, "auth/register.html", {"first_run": first_run})
|
||||
|
||||
@@ -127,12 +143,12 @@ async def register(
|
||||
password: str = Form(...),
|
||||
):
|
||||
first_run = _no_users_yet(db)
|
||||
if not first_run and not settings.allow_signup:
|
||||
return render(
|
||||
if not first_run and not settings_store.signup_allowed(db):
|
||||
return _login_page(
|
||||
request,
|
||||
"auth/login.html",
|
||||
{"error": "Registration is closed. Ask an administrator for an account."},
|
||||
db,
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
error="Registration is closed. Ask an administrator for an account.",
|
||||
)
|
||||
|
||||
name = name.strip()
|
||||
|
||||
Reference in New Issue
Block a user