flask 自定义鉴权装饰器(flask 94)

from functools import wraps

from flask import Markup, flash, url_for, redirect, abort
from flask_login import current_user

def confirm_required(func):
@wraps(func)
def decorated_function(*args, *kwargs):
if not current_user.confirmed:
message = Markup(
'Please confirm your account first.'
'Not receive the email?'
'Resend Confirm Email' %
url_for('auth.resend_confirm_email'))
flash(message, 'warning')
return redirect(url_for('main.index'))
return func(
args, **kwargs)
return decorated_function

如果用户没有邮箱确认,则需要重新确认

def permission_required(permission_name):
def decorator(func):
@wraps(func)
def decorated_function(*args, *kwargs):
if not current_user.can(permission_name):
abort(403)
return func(
args, **kwargs)
return decorated_function
return decorator

权限鉴定

def admin_required(func):
return permission_required('ADMINISTER')(func)

管理员

你可能感兴趣的:(flask 自定义鉴权装饰器(flask 94))