2020-01-05 13:15:02 +00:00
|
|
|
from . import LOGGER, Singleton, db, ldapController as ldap, emailController
|
2019-12-28 22:34:09 +00:00
|
|
|
from geruecht.model.user import User
|
2019-12-28 20:52:49 +00:00
|
|
|
from geruecht.exceptions import PermissionDenied
|
2019-12-29 16:55:21 +00:00
|
|
|
from datetime import datetime
|
2019-12-28 20:52:49 +00:00
|
|
|
|
|
|
|
class UserController(metaclass=Singleton):
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
pass
|
|
|
|
|
2019-12-29 16:55:21 +00:00
|
|
|
def lockUser(self, username, locked):
|
|
|
|
user = self.getUser(username)
|
|
|
|
user.updateData({'locked': locked})
|
|
|
|
db.updateUser(user)
|
|
|
|
return self.getUser(username)
|
|
|
|
|
2019-12-28 22:34:09 +00:00
|
|
|
def updateConfig(self, username, data):
|
|
|
|
user = self.getUser(username)
|
|
|
|
user.updateData(data)
|
|
|
|
db.updateUser(user)
|
|
|
|
return self.getUser(username)
|
|
|
|
|
2019-12-29 16:55:21 +00:00
|
|
|
def autoLock(self, user):
|
|
|
|
if user.autoLock:
|
|
|
|
if user.getGeruecht(year=datetime.now().year).getSchulden() <= (-1*user.limit):
|
|
|
|
user.updateData({'locked': True})
|
2020-01-05 13:15:02 +00:00
|
|
|
emailController.sendMail(user)
|
2019-12-29 16:55:21 +00:00
|
|
|
else:
|
|
|
|
user.updateData({'locked': False})
|
|
|
|
db.updateUser(user)
|
|
|
|
|
|
|
|
def addAmount(self, username, amount, year, month, finanzer=False):
|
2019-12-28 20:52:49 +00:00
|
|
|
user = self.getUser(username)
|
2019-12-29 16:55:21 +00:00
|
|
|
if not user.locked or finanzer:
|
|
|
|
user.addAmount(amount, year=year, month=month)
|
|
|
|
creditLists = user.updateGeruecht()
|
|
|
|
for creditList in creditLists:
|
|
|
|
db.updateCreditList(creditList)
|
|
|
|
self.autoLock(user)
|
2019-12-28 20:52:49 +00:00
|
|
|
return user.getGeruecht(year)
|
|
|
|
|
|
|
|
def addCredit(self, username, credit, year, month):
|
|
|
|
user = self.getUser(username)
|
|
|
|
user.addCredit(credit, year=year, month=month)
|
|
|
|
creditLists = user.updateGeruecht()
|
|
|
|
for creditList in creditLists:
|
|
|
|
db.updateCreditList(creditList)
|
2019-12-29 16:55:21 +00:00
|
|
|
self.autoLock(user)
|
2019-12-28 20:52:49 +00:00
|
|
|
return user.getGeruecht(year)
|
|
|
|
|
|
|
|
def getAllUsersfromDB(self):
|
2020-01-05 13:15:02 +00:00
|
|
|
users = db.getAllUser()
|
|
|
|
for user in users:
|
|
|
|
self.__updateGeruechte(user)
|
2019-12-28 20:52:49 +00:00
|
|
|
return db.getAllUser()
|
|
|
|
|
|
|
|
def getUser(self, username):
|
|
|
|
user = db.getUser(username)
|
|
|
|
groups = ldap.getGroup(username)
|
|
|
|
user_data = ldap.getUserData(username)
|
2019-12-29 16:55:21 +00:00
|
|
|
user_data['gruppe'] = groups
|
2019-12-28 20:52:49 +00:00
|
|
|
user_data['group'] = groups
|
|
|
|
if user is None:
|
2019-12-28 22:34:09 +00:00
|
|
|
user = User(user_data)
|
|
|
|
db.insertUser(user)
|
2019-12-28 20:52:49 +00:00
|
|
|
else:
|
2019-12-28 22:34:09 +00:00
|
|
|
user.updateData(user_data)
|
|
|
|
db.updateUser(user)
|
2019-12-28 20:52:49 +00:00
|
|
|
user = db.getUser(username)
|
2020-01-05 13:15:02 +00:00
|
|
|
self.__updateGeruechte(user)
|
2019-12-28 20:52:49 +00:00
|
|
|
return user
|
|
|
|
|
2020-01-05 13:15:02 +00:00
|
|
|
def __updateGeruechte(self, user):
|
|
|
|
user.getGeruecht(datetime.now().year)
|
|
|
|
creditLists = user.updateGeruecht()
|
|
|
|
if user.getGeruecht(datetime.now().year).getSchulden() != 0:
|
|
|
|
for creditList in creditLists:
|
|
|
|
db.updateCreditList(creditList)
|
|
|
|
|
|
|
|
def sendMail(self, username):
|
|
|
|
if type(username) == User:
|
|
|
|
user = username
|
|
|
|
if type(username) == str:
|
|
|
|
user = db.getUser(username)
|
|
|
|
return emailController.sendMail(user)
|
|
|
|
|
|
|
|
def sendAllMail(self):
|
|
|
|
retVal = []
|
|
|
|
users = db.getAllUser()
|
|
|
|
for user in users:
|
|
|
|
retVal.append(self.sendMail(user))
|
|
|
|
return retVal
|
|
|
|
|
2019-12-28 20:52:49 +00:00
|
|
|
def loginUser(self, username, password):
|
|
|
|
try:
|
|
|
|
user = self.getUser(username)
|
|
|
|
ldap.login(username, password)
|
|
|
|
return user
|
|
|
|
except PermissionDenied as err:
|
|
|
|
raise err
|