24 lines
976 B
Python
24 lines
976 B
Python
from functools import wraps
|
|
from flask import redirect, url_for, flash, request
|
|
from flask_login import current_user
|
|
def admin_required(f):
|
|
"""要求管理员权限的装饰器"""
|
|
@wraps(f)
|
|
def decorated_function(*args, **kwargs):
|
|
if not current_user.is_authenticated:
|
|
flash('请先登录', 'error')
|
|
return redirect(url_for('auth.login', next=request.url))
|
|
if not current_user.is_admin():
|
|
flash('权限不足,需要管理员权限', 'error')
|
|
return redirect(url_for('student.dashboard'))
|
|
return f(*args, **kwargs)
|
|
return decorated_function
|
|
def student_required(f):
|
|
"""要求学生权限的装饰器"""
|
|
@wraps(f)
|
|
def decorated_function(*args, **kwargs):
|
|
if not current_user.is_authenticated:
|
|
flash('请先登录', 'error')
|
|
return redirect(url_for('auth.login', next=request.url))
|
|
return f(*args, **kwargs)
|
|
return decorated_function |