from functools import wraps
from flask import abort
from flask_login import current_user
from .models import Permission


def permission_required(permission):
    def decorate(func):
        @wraps(func)
        def decorated_function(*args, **kw):
            if not current_user.has_permission(permission):
                flash('你的权限不足啊', 'warning')
                abort(403)
            return func(*args, **kw)
        return decorated_function
    return decorate
