flaschengeist/geruecht/decorator.py

39 lines
1.5 KiB
Python
Raw Permalink Normal View History

from functools import wraps
from .logger import getDebugLogger
DEBUG = getDebugLogger()
def login_required(**kwargs):
import geruecht.controller.accesTokenController as ac
2020-03-17 19:37:01 +00:00
from geruecht.model import BAR, USER, MONEY, GASTRO, VORSTAND, EXTERN
from flask import request, jsonify
accessController = ac.AccesTokenController()
2020-03-17 19:37:01 +00:00
groups = [USER, BAR, GASTRO, MONEY, VORSTAND, EXTERN]
bar = False
if "groups" in kwargs:
groups = kwargs["groups"]
2020-03-17 19:37:01 +00:00
if "bar" in kwargs:
bar = kwargs["bar"]
DEBUG.debug("groups are {{ {} }}".format(groups))
def real_decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
token = request.headers.get('Token')
DEBUG.debug("token is {{ {} }}".format(token))
accToken = accessController.validateAccessToken(token, groups)
DEBUG.debug("accToken is {{ {} }}".format(accToken))
kwargs['accToken'] = accToken
if accToken:
DEBUG.debug("token {{ {} }} is valid".format(token))
2020-03-17 19:37:01 +00:00
if accToken.lock_bar and not bar:
return jsonify({"error": "error",
"message": "permission forbidden"}), 403
return func(*args, **kwargs)
else:
DEBUG.warning("token {{ {} }} is not valid".format(token))
return jsonify({"error": "error",
"message": "permission denied"}), 401
return wrapper
return real_decorator