2020-01-19 08:07:45 +00:00
|
|
|
from functools import wraps
|
2020-03-10 08:19:11 +00:00
|
|
|
from .logger import getDebugLogger
|
|
|
|
DEBUG = getDebugLogger()
|
2020-03-10 10:08:24 +00:00
|
|
|
|
|
|
|
|
2020-01-19 08:07:45 +00:00
|
|
|
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
|
2020-01-19 08:07:45 +00:00
|
|
|
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
|
2020-01-19 08:07:45 +00:00
|
|
|
if "groups" in kwargs:
|
|
|
|
groups = kwargs["groups"]
|
2020-03-17 19:37:01 +00:00
|
|
|
if "bar" in kwargs:
|
|
|
|
bar = kwargs["bar"]
|
2020-03-10 10:08:24 +00:00
|
|
|
DEBUG.debug("groups are {{ {} }}".format(groups))
|
|
|
|
|
2020-01-19 08:07:45 +00:00
|
|
|
def real_decorator(func):
|
|
|
|
@wraps(func)
|
|
|
|
def wrapper(*args, **kwargs):
|
|
|
|
token = request.headers.get('Token')
|
2020-03-10 10:08:24 +00:00
|
|
|
DEBUG.debug("token is {{ {} }}".format(token))
|
2020-01-19 08:07:45 +00:00
|
|
|
accToken = accessController.validateAccessToken(token, groups)
|
2020-03-10 10:08:24 +00:00
|
|
|
DEBUG.debug("accToken is {{ {} }}".format(accToken))
|
2020-01-19 08:07:45 +00:00
|
|
|
kwargs['accToken'] = accToken
|
|
|
|
if accToken:
|
2020-03-10 10:08:24 +00:00
|
|
|
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
|
2020-01-19 08:07:45 +00:00
|
|
|
return func(*args, **kwargs)
|
|
|
|
else:
|
2020-03-10 10:08:24 +00:00
|
|
|
DEBUG.warning("token {{ {} }} is not valid".format(token))
|
|
|
|
return jsonify({"error": "error",
|
|
|
|
"message": "permission denied"}), 401
|
2020-01-19 08:07:45 +00:00
|
|
|
return wrapper
|
2020-03-10 10:08:24 +00:00
|
|
|
return real_decorator
|