04/30 Web Checker web app ver. 1.0
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
"""
|
||||
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
|
||||
Reference in New Issue
Block a user