diff --git a/geruecht/baruser/routes.py b/geruecht/baruser/routes.py index 88290b8..7f6248d 100644 --- a/geruecht/baruser/routes.py +++ b/geruecht/baruser/routes.py @@ -1,10 +1,17 @@ from flask import Blueprint, request, jsonify -from geruecht.controller import ldapController as ldap, accesTokenController, userController +import geruecht.controller as gc +import geruecht.controller.ldapController as lc +import geruecht.controller.accesTokenController as ac +import geruecht.controller.userController as uc from datetime import datetime from geruecht.model import BAR, MONEY baruser = Blueprint("baruser", __name__) +ldap= lc.LDAPController(gc.ldapConfig['URL'], gc.ldapConfig['dn']) +accesTokenController = ac.AccesTokenController() +userController = uc.UserController() + @baruser.route("/bar") def _bar(): """ Main function for Baruser diff --git a/geruecht/configparser.py b/geruecht/configparser.py index 247f23c..fc92b61 100644 --- a/geruecht/configparser.py +++ b/geruecht/configparser.py @@ -34,7 +34,7 @@ class ConifgParser(): self.ldap = self.config['LDAP'] LOGGER.info("Set LDAPconfig: {}".format(self.ldap)) if 'AccessTokenLifeTime' in self.config: - self.accessTokenLifeTime = self.config['AccessTokenLifeTime'] + self.accessTokenLifeTime = int(self.config['AccessTokenLifeTime']) LOGGER.info("Set AccessTokenLifeTime: {}".format(self.accessTokenLifeTime)) else: self.accessTokenLifeTime = default['AccessTokenLifeTime'] diff --git a/geruecht/controller/__init__.py b/geruecht/controller/__init__.py index 7b0d1c8..b659474 100644 --- a/geruecht/controller/__init__.py +++ b/geruecht/controller/__init__.py @@ -15,35 +15,7 @@ class Singleton(type): cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs) return cls._instances[cls] -from .databaseController import DatabaseController -def getDatabesController(): - if db is not None: - return db - else: - return DatabaseController(dbConfig['URL'], dbConfig['user'], dbConfig['passwd'], dbConfig['database']) -from .ldapController import LDAPController -def getLDAPController(): - if ldapController is not None: - return ldapController - else: - return LDAPController(ldapConfig['URL'], ldapConfig['dn']) -from .accesTokenController import AccesTokenController - dbConfig = config.getDatabase() ldapConfig = config.getLDAP() accConfig = config.getAccessToken() mailConfig = config.getMail() - -db = DatabaseController(dbConfig['URL'], dbConfig['user'], dbConfig['passwd'], dbConfig['database']) -ldapController = LDAPController(ldapConfig['URL'], ldapConfig['dn']) - -from . emailController import EmailController -emailController = EmailController(mailConfig['URL'], mailConfig['user'], mailConfig['passwd'], mailConfig['port'], mailConfig['email']) -from . userController import UserController -def getUserController(): - if userController is not None: - return userController - else: - return UserController() -userController = UserController() -accesTokenController = AccesTokenController(accConfig) \ No newline at end of file diff --git a/geruecht/controller/accesTokenController.py b/geruecht/controller/accesTokenController.py index 7ec0c08..160459e 100644 --- a/geruecht/controller/accesTokenController.py +++ b/geruecht/controller/accesTokenController.py @@ -1,11 +1,13 @@ from geruecht.model.accessToken import AccessToken -#import geruecht.controller.userController as userController +import geruecht.controller as gc +import geruecht.controller.userController as uc from geruecht.model import BAR from geruecht.controller import LOGGER from datetime import datetime, timedelta import hashlib from . import Singleton +userController = uc.UserController() class AccesTokenController(metaclass=Singleton): """ Control all createt AccesToken @@ -25,15 +27,15 @@ class AccesTokenController(metaclass=Singleton): Initialize Thread and set tokenList empty. """ LOGGER.info("Initialize AccessTokenController") - self.lifetime = lifetime + self.lifetime = gc.accConfig self.tokenList = [] - #def checkBar(self, user): - # if (userController.checkBarUser(user)): - # user.group.append(BAR) - # elif BAR in user.group: - # user.group.remove(BAR) + def checkBar(self, user): + if (userController.checkBarUser(user)): + user.group.append(BAR) + elif BAR in user.group: + user.group.remove(BAR) def validateAccessToken(self, token, group): """ Verify Accestoken diff --git a/geruecht/controller/databaseController.py b/geruecht/controller/databaseController.py index fa4f6a9..50153c7 100644 --- a/geruecht/controller/databaseController.py +++ b/geruecht/controller/databaseController.py @@ -189,7 +189,7 @@ class DatabaseController(metaclass=Singleton): self.db.close() except Exception as err: raise err - return {"user": user, "startdatetime": data['startdatetime'], "enddatetime": data['enddatetime']} + return {"user": user, "startdatetime": data['startdatetime'], "enddatetime": data['enddatetime']} if data else None def getWorkers(self, date): self.connect() diff --git a/geruecht/controller/userController.py b/geruecht/controller/userController.py index 541ff75..151d248 100644 --- a/geruecht/controller/userController.py +++ b/geruecht/controller/userController.py @@ -1,8 +1,15 @@ -from . import LOGGER, Singleton, db, ldapController as ldap, emailController +from . import LOGGER, Singleton, ldapConfig, dbConfig, mailConfig +import geruecht.controller.databaseController as dc +import geruecht.controller.ldapController as lc +import geruecht.controller.emailController as ec from geruecht.model.user import User from geruecht.exceptions import PermissionDenied from datetime import datetime, timedelta +db = dc.DatabaseController(dbConfig['URL'], dbConfig['user'], dbConfig['passwd'], dbConfig['database']) +ldap = lc.LDAPController(ldapConfig['URL'], ldapConfig['dn']) +emailController = ec.EmailController(mailConfig['URL'], mailConfig['user'], mailConfig['passwd'], mailConfig['port'], mailConfig['email']) + class UserController(metaclass=Singleton): def __init__(self): @@ -71,7 +78,11 @@ class UserController(metaclass=Singleton): def checkBarUser(self, user): date = datetime.now() - startdatetime = date.replace(hour=11, minute=0, microsecond=0) + zero = date.replace(hour=0, minute=0, second=0, microsecond=0) + end = zero + timedelta(hours=11) + startdatetime = date.replace(hour=11, minute=0, second=0, microsecond=0) + if date > zero and end > date: + startdatetime = startdatetime - timedelta(days=1) enddatetime = startdatetime + timedelta(days=1) result = False if date >= startdatetime and date < enddatetime: diff --git a/geruecht/finanzer/routes.py b/geruecht/finanzer/routes.py index 209f14e..15bfc3b 100644 --- a/geruecht/finanzer/routes.py +++ b/geruecht/finanzer/routes.py @@ -1,11 +1,14 @@ from flask import Blueprint, request, jsonify from geruecht.finanzer import LOGGER from datetime import datetime -from geruecht.controller import accesTokenController, userController +import geruecht.controller.userController as uc +import geruecht.controller.accesTokenController as ac from geruecht.model import MONEY finanzer = Blueprint("finanzer", __name__) +accesTokenController = ac.AccesTokenController() +userController = uc.UserController() @finanzer.route("/getFinanzerMain") def _getFinanzer(): diff --git a/geruecht/routes.py b/geruecht/routes.py index 346691e..daf8d78 100644 --- a/geruecht/routes.py +++ b/geruecht/routes.py @@ -1,9 +1,12 @@ from geruecht import app, LOGGER from geruecht.exceptions import PermissionDenied -from geruecht.controller import accesTokenController, userController +import geruecht.controller.accesTokenController as ac +import geruecht.controller.userController as uc from geruecht.model import MONEY, BAR, USER, GASTRO from flask import request, jsonify +accesTokenController = ac.AccesTokenController() +userController = uc.UserController() def login(user, password): return user.login(password) @@ -48,7 +51,7 @@ def _login(): user = userController.loginUser(username, password) user.password = password token = accesTokenController.createAccesToken(user) - dic = user.toJSON() + dic = accesTokenController.validateAccessToken(token, [USER]).user.toJSON() dic["token"] = token dic["accessToken"] = token LOGGER.info("User {} success login.".format(username)) diff --git a/geruecht/user/routes.py b/geruecht/user/routes.py index 03f3a0f..089f3c7 100644 --- a/geruecht/user/routes.py +++ b/geruecht/user/routes.py @@ -1,10 +1,15 @@ from flask import Blueprint, request, jsonify -from geruecht.controller import userController, accesTokenController +import geruecht.controller as gc +import geruecht.controller.userController as uc +import geruecht.controller.accesTokenController as ac from geruecht.model import USER from datetime import datetime user = Blueprint("user", __name__) +accesTokenController = ac.AccesTokenController() +userController = uc.UserController() + @user.route("/user/main") def _main():