27 lines
790 B
Python
27 lines
790 B
Python
"""权限装饰器"""
|
|
from functools import wraps
|
|
from flask import abort
|
|
from flask_login import current_user
|
|
|
|
def admin_required(f):
|
|
"""仅管理员可访问"""
|
|
@wraps(f)
|
|
def decorated_function(*args, **kwargs):
|
|
if not current_user.is_authenticated or current_user.role != 'admin':
|
|
abort(403)
|
|
return f(*args, **kwargs)
|
|
return decorated_function
|
|
|
|
def permission_required(*roles):
|
|
"""指定角色可访问"""
|
|
def decorator(f):
|
|
@wraps(f)
|
|
def decorated_function(*args, **kwargs):
|
|
if not current_user.is_authenticated:
|
|
abort(401)
|
|
if current_user.role not in roles:
|
|
abort(403)
|
|
return f(*args, **kwargs)
|
|
return decorated_function
|
|
return decorator
|