flaschengeist/geruecht/routes.py

188 lines
7.4 KiB
Python
Raw Normal View History

2020-03-09 18:54:51 +00:00
from geruecht import app
from geruecht.logger import getDebugLogger, getInfoLogger
2020-03-03 21:33:47 +00:00
from geruecht.decorator import login_required
from geruecht.exceptions import PermissionDenied
import geruecht.controller.accesTokenController as ac
import geruecht.controller.userController as uc
2020-03-04 20:11:41 +00:00
from geruecht.model import MONEY, BAR, USER, GASTRO, VORSTAND, EXTERN
2019-04-11 21:56:55 +00:00
from flask import request, jsonify
accesTokenController = ac.AccesTokenController()
userController = uc.UserController()
2020-03-09 18:54:51 +00:00
debug = getDebugLogger("MAIN-ROUTE", True)
info = getInfoLogger("MAIN-ROUTE")
def login(user, password):
return user.login(password)
2020-01-17 00:05:58 +00:00
2019-04-23 22:08:25 +00:00
@app.route("/valid")
def _valid():
2019-04-23 22:08:25 +00:00
token = request.headers.get("Token")
2020-01-18 22:31:49 +00:00
accToken = accesTokenController.validateAccessToken(token, [MONEY])
if accToken:
2019-04-23 22:08:25 +00:00
return jsonify(accToken.user.toJSON())
2020-01-18 22:31:49 +00:00
accToken = accesTokenController.validateAccessToken(token, [BAR])
if accToken:
2019-04-23 22:08:25 +00:00
return jsonify(accToken.user.toJSON())
2020-01-18 22:31:49 +00:00
accToken = accesTokenController.validateAccessToken(token, [GASTRO])
if accToken:
2019-04-23 22:08:25 +00:00
return jsonify(accToken.user.toJSON())
2020-01-18 22:31:49 +00:00
accToken = accesTokenController.validateAccessToken(token, [USER])
if accToken:
return jsonify(accToken.user.toJSON())
return jsonify({"error": "permission denied"}), 401
2020-02-27 20:55:00 +00:00
@app.route("/pricelist", methods=['GET'])
def _getPricelist():
try:
2020-03-09 18:54:51 +00:00
debug.info("get pricelist")
2020-02-27 20:55:00 +00:00
retVal = userController.getPricelist()
2020-03-09 18:54:51 +00:00
debug.info("return pricelist {{}}".format(retVal))
2020-02-27 20:55:00 +00:00
return jsonify(retVal)
except Exception as err:
2020-03-09 18:54:51 +00:00
debug.warning("exception in get pricelist.", exc_info=True)
return jsonify({"error": str(err)}), 500
2020-02-27 20:55:00 +00:00
@app.route('/drinkTypes', methods=['GET'])
def getTypes():
try:
2020-03-09 18:54:51 +00:00
debug.info("get drinktypes")
retVal = userController.getAllDrinkTypes()
2020-03-09 18:54:51 +00:00
debug.info("return drinktypes {{}}".format(retVal))
return jsonify(retVal)
except Exception as err:
2020-03-09 18:54:51 +00:00
debug.warning("exception in get drinktypes.", exc_info=True)
return jsonify({"error": str(err)}), 500
2020-03-03 21:33:47 +00:00
@app.route('/getAllStatus', methods=['GET'])
2020-03-04 20:11:41 +00:00
@login_required(groups=[USER, MONEY, GASTRO, BAR, VORSTAND])
2020-03-03 21:33:47 +00:00
def _getAllStatus(**kwargs):
try:
2020-03-09 18:54:51 +00:00
debug.info("get all status for users")
2020-03-03 21:33:47 +00:00
retVal = userController.getAllStatus()
2020-03-09 18:54:51 +00:00
debug.info("return all status for users {{}}".format(retVal))
2020-03-03 21:33:47 +00:00
return jsonify(retVal)
except Exception as err:
2020-03-09 18:54:51 +00:00
debug.warning("exception in get all status for users.", exc_info=True)
2020-03-03 21:33:47 +00:00
return jsonify({"error": str(err)}), 500
2020-02-27 20:55:00 +00:00
2020-03-03 21:33:47 +00:00
@app.route('/getStatus', methods=['POST'])
2020-03-04 20:11:41 +00:00
@login_required(groups=[USER, MONEY, GASTRO, BAR, VORSTAND])
2020-03-03 21:33:47 +00:00
def _getStatus(**kwargs):
try:
2020-03-09 18:54:51 +00:00
debug.info("get status from user")
2020-03-03 21:33:47 +00:00
data = request.get_json()
name = data['name']
2020-03-09 18:54:51 +00:00
debug.info("get status from user {{}}".format(name))
2020-03-03 21:33:47 +00:00
retVal = userController.getStatus(name)
2020-03-09 18:54:51 +00:00
debug.info("return status from user {{}} : {{}}".format(name, retVal))
2020-03-03 21:33:47 +00:00
return jsonify(retVal)
except Exception as err:
2020-03-09 18:54:51 +00:00
debug.warning("exception in get status from user.", exc_info=True)
2020-03-03 21:33:47 +00:00
return jsonify({"error": str(err)}), 500
@app.route('/getUsers', methods=['GET'])
2020-03-04 20:11:41 +00:00
@login_required(groups=[MONEY, GASTRO, VORSTAND])
2020-03-03 21:33:47 +00:00
def _getUsers(**kwargs):
try:
2020-03-09 18:54:51 +00:00
debug.info("get all users from database")
2020-03-03 21:33:47 +00:00
users = userController.getAllUsersfromDB()
2020-03-09 18:54:51 +00:00
debug.debug("users are {{}}".format(users))
2020-03-03 21:33:47 +00:00
retVal = [user.toJSON() for user in users]
2020-03-09 18:54:51 +00:00
debug.info("return all users from database {{}}".format(retVal))
2020-03-03 21:33:47 +00:00
return jsonify(retVal)
except Exception as err:
2020-03-09 18:54:51 +00:00
debug.warning("exception in get all users from database.", exc_info=True)
2020-03-03 21:33:47 +00:00
return jsonify({"error": str(err)}), 500
2020-01-17 00:05:58 +00:00
2020-03-07 13:56:44 +00:00
@app.route("/getLifeTime", methods=['GET'])
@login_required(groups=[MONEY, GASTRO, VORSTAND, EXTERN, USER])
def _getLifeTime(**kwargs):
try:
2020-03-09 18:54:51 +00:00
debug.info("get lifetime of accesstoken")
2020-03-07 13:56:44 +00:00
if 'accToken' in kwargs:
accToken = kwargs['accToken']
2020-03-09 18:54:51 +00:00
debug.debug("accessToken is {{}}".format(accToken))
retVal = {"value": accToken.lifetime}
debug.info("return get lifetime from accesstoken {{}}".format(retVal))
return jsonify(retVal)
2020-03-07 13:56:44 +00:00
except Exception as err:
2020-03-09 18:54:51 +00:00
debug.info("exception in get lifetime of accesstoken.", exc_info=True)
2020-03-07 13:56:44 +00:00
return jsonify({"error": str(err)}), 500
@app.route("/saveLifeTime", methods=['POST'])
@login_required(groups=[MONEY, GASTRO, VORSTAND, EXTERN, USER])
def _saveLifeTime(**kwargs):
try:
2020-03-09 18:54:51 +00:00
debug.info("save lifetime for accessToken")
2020-03-07 13:56:44 +00:00
if 'accToken' in kwargs:
accToken = kwargs['accToken']
2020-03-09 18:54:51 +00:00
debug.debug("accessToken is {{}}".format(accToken))
2020-03-07 13:56:44 +00:00
data = request.get_json()
lifetime = data['value']
2020-03-09 18:54:51 +00:00
debug.debug("lifetime is {{}}".format(lifetime))
debug.info("set lifetime {{}} to accesstoken {{}}".format(lifetime, accToken))
2020-03-07 13:56:44 +00:00
accToken.lifetime = lifetime
2020-03-09 18:54:51 +00:00
debug.info("update accesstoken timestamp")
2020-03-07 13:56:44 +00:00
accToken.updateTimestamp()
2020-03-09 18:54:51 +00:00
retVal = {"value": accToken.lifetime}
debug.info("return save lifetime for accessToken {{}}".format(retVal))
return jsonify(retVal)
2020-03-07 13:56:44 +00:00
except Exception as err:
2020-03-09 18:54:51 +00:00
debug.warning("exception in save lifetime for accesstoken.", exc_info=True)
2020-03-07 13:56:44 +00:00
return jsonify({"error": str(err)}), 500
@app.route("/logout", methods=['GET'])
@login_required(groups=[MONEY, GASTRO, VORSTAND, EXTERN, USER])
def _logout(**kwargs):
try:
2020-03-09 18:54:51 +00:00
debug.info("logout user")
2020-03-07 13:56:44 +00:00
if 'accToken' in kwargs:
accToken = kwargs['accToken']
2020-03-09 18:54:51 +00:00
debug.debug("accesstoken is {{}}".format(accToken))
debug.info("delete accesstoken")
2020-03-07 13:56:44 +00:00
accesTokenController.deleteAccessToken(accToken)
2020-03-09 18:54:51 +00:00
debug.info("return ok logout user")
2020-03-07 13:56:44 +00:00
return jsonify({"ok": "ok"})
except Exception as err:
2020-03-09 18:54:51 +00:00
debug.warning("exception in logout user.", exc_info=True)
2020-03-07 13:56:44 +00:00
return jsonify({"error": str(err)}), 500
2019-04-11 21:56:55 +00:00
@app.route("/login", methods=['POST'])
def _login():
2019-04-17 12:46:46 +00:00
""" Login User
2019-04-17 12:46:46 +00:00
Nothing to say.
Login in User and create an AccessToken for the User.
Returns:
A JSON-File with createt Token or Errors
"""
2020-03-09 18:54:51 +00:00
debug.info("Start log in.")
2019-04-11 21:56:55 +00:00
data = request.get_json()
2020-03-09 18:54:51 +00:00
debug.debug("JSON from request: {}".format(data))
2019-04-11 21:56:55 +00:00
username = data['username']
password = data['password']
try:
2020-03-09 18:54:51 +00:00
debug.info("search {{}} in database".format(username))
2020-01-26 22:31:22 +00:00
user, ldap_conn = userController.loginUser(username, password)
2020-03-09 18:54:51 +00:00
debug.debug("user is {{}}".format(user))
2020-01-17 00:05:58 +00:00
user.password = password
2020-01-26 22:31:22 +00:00
token = accesTokenController.createAccesToken(user, ldap_conn)
2020-03-09 18:54:51 +00:00
debug.debug("accesstoken is {{}}".format(token))
debug.info("validate accesstoken")
2020-03-04 20:11:41 +00:00
dic = accesTokenController.validateAccessToken(token, [USER, EXTERN]).user.toJSON()
dic["token"] = token
dic["accessToken"] = token
2020-03-09 18:54:51 +00:00
debug.info("User {{}} success login.".format(username))
debug.info("return login {{}}".format(dic))
return jsonify(dic)
except PermissionDenied as err:
2020-03-09 18:54:51 +00:00
debug.warning("permission denied exception in logout", exc_info=True)
return jsonify({"error": str(err)}), 401
2020-03-04 20:11:41 +00:00
except Exception as err:
2020-03-09 18:54:51 +00:00
debug.warning("exception in logout.", exc_info=True)
return jsonify({"error": "permission denied"}), 401