你如何使用 Flask-Login 和自定义 Python 装饰器来分配用户权限?
Posted
技术标签:
【中文标题】你如何使用 Flask-Login 和自定义 Python 装饰器来分配用户权限?【英文标题】:How do you use Flask-Login and custom Python decorators to assign user permissions? 【发布时间】:2020-06-17 14:10:21 【问题描述】:我不知道如何让 Python 装饰器在 Flask 视图上工作。
这是我的装饰器:
from functools import wraps
def admin_only(func):
@wraps(func)
def wrapper(*args, **kwargs):
print("It did something")
if current_user.is_anonymous:
flash("Please log in to use this site.", "info")
return redirect(url_for('login'))
if not current_user.admin:
abort(403)
return func(*args, **kwargs)
return wrapper
@bp.errorhandler(403)
def forbidden_403(exception):
return 'Admin access only', 403
这是我的观点:
@bp.route('/add_info', methods=['GET', 'POST'])
@admin_only
def add_info():
add_info_form = AddInfoForm()
if add_info_form.validate_on_submit():
print("Code here")
else:
for error in add_info_form.errors:
flash(error, "info")
context =
'add_info_form': add_info_form,
return render_template('admin/add_info.html', **context)
装饰器根本不起作用——视图可以工作,但允许任何用户通过。在视图函数运行之前应该实现的代码都不会运行。我尝试了各种不同的格式化技巧,但没有任何效果。
我使用了这个答案中的代码 - Python decorator with Flask:
def decorator(take_a_function):
def wrapper1(take_a_function):
def wrapper2(*takes_multiple_arguments):
# do stuff
return take_a_function(*takes_multiple_arguments)
return wrapper2
return wrapper1
似乎没有任何效果。我错过了什么?
提前致谢。
【问题讨论】:
【参考方案1】:好的,我不确定为什么会这样,但我会这样说:
我正在使用类似这样的蓝图文件结构:
/app
/user
/__init__.py
/forms.py
/templates/user
/user.html
/lists
/__init__.py
/forms.py
/templates/lists
/lists.html
/admin
/__init__.py
/forms.py
/templates/admin
/add_user.html
/add_info.html
/all_lists.html
__init__.py
forms.py
models.py
/templates
index.html
我之前一直在/admin/__init__.py
中定义装饰器函数。当我简单地将其移至app/__init__.py
文件时,该函数运行良好!
我很高兴,但我也希望有人能提供一些细节,说明为什么会这样。如果没有,我会接受我的回答。也许这与 Flask 在__init__.py
文件中运行函数的顺序有关...
【讨论】:
我怀疑当您在admin/__init__.py
中拥有装饰器功能时它不起作用,因为您没有将它正确导入您的routes.py
模块。您的routes.py
中是否有from app import admin_only
而不是from app.admin import admin_only
?以上是关于你如何使用 Flask-Login 和自定义 Python 装饰器来分配用户权限?的主要内容,如果未能解决你的问题,请参考以下文章