31 lines
943 B
Python
31 lines
943 B
Python
"""
|
|
utils/decorators.py — Route protection decorators for the Flask web app.
|
|
"""
|
|
|
|
import functools
|
|
from flask import session, redirect, url_for, flash, abort
|
|
|
|
|
|
def login_required(f):
|
|
"""Redirect to login if the user is not authenticated."""
|
|
@functools.wraps(f)
|
|
def decorated(*args, **kwargs):
|
|
if "user" not in session:
|
|
flash("Please log in to access this page.", "warning")
|
|
return redirect(url_for("auth.login"))
|
|
return f(*args, **kwargs)
|
|
return decorated
|
|
|
|
|
|
def admin_required(f):
|
|
"""Abort 403 if the authenticated user is not an admin."""
|
|
@functools.wraps(f)
|
|
def decorated(*args, **kwargs):
|
|
if "user" not in session:
|
|
flash("Please log in to access this page.", "warning")
|
|
return redirect(url_for("auth.login"))
|
|
if session["user"].get("role") != "admin":
|
|
abort(403)
|
|
return f(*args, **kwargs)
|
|
return decorated
|