The members area had grown one navigation entry per phase, and three of the five were inside an admin-only block in base.html — so an ordinary member signed in to a wall and a member list, with their own profile, their own private messages and the content editor all invisible. Everything worked and nothing was reachable. That is the bug this starts from. The navigation is now Inicio · Muro · Privados · Publicaciones, plus Gestión for admins, and the account items (Mi perfil, Descargar mis datos, Salir) moved to a menu under the member's own name — a <details> element, because the CSP has no 'unsafe-inline' and a menu needing onclick="" silently never opens. Inicio is a new dashboard; the wall moved from /comunidad/ to /comunidad/muro and kept its thread URLs. test_navigation.py asserts the navigation per role from the rendered page, so this cannot come back quietly. Public posts are now curated. A member proposes one and it waits in a new `submissions` table; a moderator or admin approves it — which commits it through content.publish, the same function the editor uses, with the author named per commit so git history says who wrote it, not who approved it — or returns it with a reason the author reads. Moderators and admins do not queue. Nothing a member writes reaches the repository before approval, because a commit to it *is* publication: the webhook starts the pipeline. A proposal's picture stays private in /data/uploads until then, and the private copy goes when the public one is committed. There is deliberately no way to promote a wall thread into a proposal. The moderator role needed the role CHECK widened, and SQLite has no DROP CONSTRAINT, so migration 4 rebuilds `members` — eight tables reference it, the one-owner index has to be recreated by hand, and PRAGMA foreign_key_check runs before the commit. One trap worth recording: the allowed roles can only be read back out of sqlite_master's verbatim CREATE TABLE text, comments included, so schema.sql's own comment explaining the rebuild made the step decide it had already run. The start-up test caught it; comments are stripped now. A moderator curates content and has no power over people: no creating, suspending, promoting or erasing anybody. set_role moved from owner-only to admin-level so moderators can be appointed routinely, and checks may_create on the new role as well, so admins still cannot mint admins. content.py splits per collection — posts need a moderator, the site's static pages need an admin. Also: erasure deletes a member's submissions and their pictures and clears reviewed_by, which the existing foreign-key guard forced; deploy-board.sh takes a backup before restarting, since two migrations now rebuild a table in place and one of them holds your own account; README and docs/server-setup.md updated for the roles, the sections and migration 4. 370 tests pass. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01NizVpJ2dwzCbjCrTLCjeHn
82 lines
2.4 KiB
Python
82 lines
2.4 KiB
Python
"""CSRF tokens and the decorators that gate routes by role.
|
|
|
|
The CSRF check is installed once as a before-request hook in app.py rather than
|
|
as a decorator on each handler. A decorator is something a future route can
|
|
forget; a hook covering every unsafe method is something it has to actively opt
|
|
out of.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import hmac
|
|
import secrets
|
|
from functools import wraps
|
|
|
|
from flask import abort, g, redirect, request, session, url_for
|
|
|
|
SAFE_METHODS = {"GET", "HEAD", "OPTIONS"}
|
|
|
|
|
|
def csrf_token() -> str:
|
|
if "csrf" not in session:
|
|
session["csrf"] = secrets.token_urlsafe(32)
|
|
return session["csrf"]
|
|
|
|
|
|
def check_csrf() -> None:
|
|
if request.method in SAFE_METHODS:
|
|
return
|
|
sent = request.form.get("csrf_token", "")
|
|
expected = session.get("csrf", "")
|
|
# compare_digest rather than == so a wrong token cannot be guessed a
|
|
# character at a time by measuring how long the comparison takes.
|
|
if not expected or not hmac.compare_digest(sent, expected):
|
|
abort(400, "Formulario caducado. Vuelve a cargar la página e inténtalo de nuevo.")
|
|
|
|
|
|
def login_required(view):
|
|
@wraps(view)
|
|
def wrapped(*args, **kwargs):
|
|
if g.member is None:
|
|
return redirect(url_for("auth.login", next=request.path))
|
|
return view(*args, **kwargs)
|
|
return wrapped
|
|
|
|
|
|
def admin_required(view):
|
|
"""Owner counts as an admin. Admin does not count as owner."""
|
|
@wraps(view)
|
|
@login_required
|
|
def wrapped(*args, **kwargs):
|
|
if g.member["role"] not in ("owner", "admin"):
|
|
abort(403)
|
|
return view(*args, **kwargs)
|
|
return wrapped
|
|
|
|
|
|
def moderator_required(view):
|
|
"""Curating the public site. Admins and the owner count as moderators.
|
|
|
|
Deliberately separate from admin_required: a moderator decides what the
|
|
public sees and nothing else. They cannot reach Gestión, cannot create or
|
|
deactivate anybody, and cannot change a role — moderating content is not
|
|
power over people.
|
|
"""
|
|
@wraps(view)
|
|
@login_required
|
|
def wrapped(*args, **kwargs):
|
|
if g.member["role"] not in ("owner", "admin", "moderator"):
|
|
abort(403)
|
|
return view(*args, **kwargs)
|
|
return wrapped
|
|
|
|
|
|
def owner_required(view):
|
|
@wraps(view)
|
|
@login_required
|
|
def wrapped(*args, **kwargs):
|
|
if g.member["role"] != "owner":
|
|
abort(403)
|
|
return view(*args, **kwargs)
|
|
return wrapped
|