33 lines
1.2 KiB
Python
33 lines
1.2 KiB
Python
from functools import wraps
|
|
from flask import redirect, url_for, flash, request
|
|
from flask_login import current_user
|
|
|
|
|
|
def login_required(f):
|
|
@wraps(f)
|
|
def decorated_function(*args, **kwargs):
|
|
print(f"DEBUG: login_required 检查 - current_user.is_authenticated = {current_user.is_authenticated}")
|
|
if not current_user.is_authenticated:
|
|
flash('请先登录', 'warning')
|
|
return redirect(url_for('user.login', next=request.url))
|
|
return f(*args, **kwargs)
|
|
|
|
return decorated_function
|
|
|
|
|
|
def admin_required(f):
|
|
@wraps(f)
|
|
def decorated_function(*args, **kwargs):
|
|
print(f"DEBUG: admin_required 检查 - current_user.is_authenticated = {current_user.is_authenticated}")
|
|
if not current_user.is_authenticated:
|
|
flash('请先登录', 'warning')
|
|
return redirect(url_for('user.login', next=request.url))
|
|
|
|
print(f"DEBUG: admin_required 检查 - current_user.role_id = {getattr(current_user, 'role_id', None)}")
|
|
if getattr(current_user, 'role_id', None) != 1: # 安全地获取role_id属性
|
|
flash('权限不足', 'danger')
|
|
return redirect(url_for('index'))
|
|
return f(*args, **kwargs)
|
|
|
|
return decorated_function
|