2020-01-18 23:37:40 +00:00
|
|
|
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
|
2020-02-23 20:27:03 +00:00
|
|
|
import calendar
|
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
|
2020-01-18 22:31:49 +00:00
|
|
|
from datetime import datetime, timedelta
|
2020-02-25 21:50:32 +00:00
|
|
|
from geruecht.exceptions import UsernameExistLDAP, UsernameExistDB, DatabaseExecption, LDAPExcetpion, DayLocked, TansactJobIsAnswerdException
|
2019-12-28 20:52:49 +00:00
|
|
|
|
2020-01-19 20:32:58 +00:00
|
|
|
db = dc.DatabaseController()
|
2020-01-21 05:54:35 +00:00
|
|
|
ldap = lc.LDAPController(ldapConfig['dn'])
|
2020-02-26 21:13:44 +00:00
|
|
|
emailController = ec.EmailController(mailConfig['URL'], mailConfig['user'], mailConfig['passwd'], mailConfig['crypt'], mailConfig['port'], mailConfig['email'])
|
2020-01-18 23:37:40 +00:00
|
|
|
|
2019-12-28 20:52:49 +00:00
|
|
|
class UserController(metaclass=Singleton):
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
pass
|
|
|
|
|
2020-02-27 20:55:00 +00:00
|
|
|
def getPricelist(self):
|
|
|
|
list = db.getPriceList()
|
|
|
|
for element in list:
|
|
|
|
type = db.getDrinkType(element['type'])
|
|
|
|
element['type'] = type['name']
|
|
|
|
return list
|
|
|
|
|
2020-02-25 21:50:32 +00:00
|
|
|
def setTransactJob(self, from_user, to_user, date):
|
2020-02-27 14:01:41 +00:00
|
|
|
jobtransact = db.setTransactJob(from_user, to_user, date.date())
|
|
|
|
emailController.sendMail(jobtransact['to_user'], 'jobtransact', jobtransact)
|
|
|
|
return jobtransact
|
2020-02-25 21:50:32 +00:00
|
|
|
|
|
|
|
def getTransactJobFromUser(self, user, date):
|
|
|
|
return db.getTransactJobFromUser(user, date.date())
|
|
|
|
|
|
|
|
def getAllTransactJobFromUser(self, user, date):
|
|
|
|
return db.getAllTransactJobFromUser(user, date.date())
|
|
|
|
|
|
|
|
def getAllTransactJobToUser(self, user, date):
|
|
|
|
return db.getAllTransactJobToUser(user, date.date())
|
|
|
|
|
|
|
|
def getTransactJob(self, from_user, to_user, date):
|
|
|
|
return db.getTransactJob(from_user, to_user, date.date())
|
|
|
|
|
|
|
|
def deleteTransactJob(self, from_user, to_user, date):
|
|
|
|
transactJob = self.getTransactJob(from_user, to_user, date)
|
|
|
|
if transactJob['answerd']:
|
|
|
|
raise TansactJobIsAnswerdException("TransactJob is already answerd")
|
|
|
|
db.deleteTransactJob(from_user, to_user, date.date())
|
|
|
|
|
|
|
|
def answerdTransactJob(self, from_user, to_user, date, answer):
|
|
|
|
transactJob = db.updateTransactJob(from_user, to_user, date.date(), answer)
|
|
|
|
if answer:
|
|
|
|
self.addWorker(to_user.uid, date)
|
|
|
|
return transactJob
|
|
|
|
|
|
|
|
def setLockedDay(self, date, locked, hard=False):
|
2020-02-24 11:19:52 +00:00
|
|
|
return db.setLockedDay(date.date(), locked, hard)
|
|
|
|
|
2020-02-23 20:27:03 +00:00
|
|
|
def getLockedDay(self, date):
|
|
|
|
now = datetime.now()
|
|
|
|
daysInMonth = calendar.monthrange(date.year, date.month)[1]
|
2020-02-24 17:55:57 +00:00
|
|
|
startMonth = 1
|
|
|
|
for i in range(1, 8):
|
|
|
|
if datetime(date.year, date.month, i).weekday() == 2:
|
|
|
|
startMonth = i
|
|
|
|
break
|
2020-02-23 20:27:03 +00:00
|
|
|
if date.year <= now.year and date.month <= now.month:
|
2020-02-24 17:55:57 +00:00
|
|
|
for i in range(startMonth, daysInMonth + 1):
|
2020-02-24 11:19:52 +00:00
|
|
|
self.setLockedDay(datetime(date.year, date.month, i), True)
|
|
|
|
for i in range(1, 8):
|
|
|
|
nextMonth = datetime(date.year, date.month + 1, i)
|
|
|
|
if nextMonth.weekday() == 2:
|
|
|
|
break
|
|
|
|
self.setLockedDay(nextMonth, True)
|
2020-02-23 20:27:03 +00:00
|
|
|
return db.getLockedDay(date.date())
|
|
|
|
|
2020-01-18 22:31:49 +00:00
|
|
|
def getWorker(self, date, username=None):
|
|
|
|
if (username):
|
|
|
|
user = self.getUser(username)
|
|
|
|
return [db.getWorker(user, date)]
|
|
|
|
return db.getWorkers(date)
|
|
|
|
|
2020-02-23 21:31:22 +00:00
|
|
|
def addWorker(self, username, date, userExc=False):
|
|
|
|
if (userExc):
|
|
|
|
lockedDay = self.getLockedDay(date)
|
|
|
|
if lockedDay:
|
|
|
|
if lockedDay['locked']:
|
|
|
|
raise DayLocked("Day is locked. You can't get the Job")
|
2020-01-18 22:31:49 +00:00
|
|
|
user = self.getUser(username)
|
|
|
|
if (not db.getWorker(user, date)):
|
|
|
|
db.setWorker(user, date)
|
2020-01-19 20:32:58 +00:00
|
|
|
return self.getWorker(date, username=username)
|
2020-01-18 22:31:49 +00:00
|
|
|
|
2020-02-24 17:55:57 +00:00
|
|
|
def deleteWorker(self, username, date, userExc=False):
|
2020-02-25 21:50:32 +00:00
|
|
|
user = self.getUser(username)
|
2020-02-24 17:55:57 +00:00
|
|
|
if userExc:
|
|
|
|
lockedDay = self.getLockedDay(date)
|
|
|
|
if lockedDay:
|
|
|
|
if lockedDay['locked']:
|
2020-02-25 21:50:32 +00:00
|
|
|
transactJobs = self.getTransactJobFromUser(user, date)
|
|
|
|
found = False
|
|
|
|
for job in transactJobs:
|
|
|
|
if job['accepted'] and job['answerd']:
|
|
|
|
found = True
|
|
|
|
break
|
|
|
|
if not found:
|
|
|
|
raise DayLocked("Day is locked. You can't delete the Job")
|
2020-01-19 20:32:58 +00:00
|
|
|
db.deleteWorker(user, date)
|
2020-01-18 22:31:49 +00:00
|
|
|
|
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)
|
|
|
|
|
2020-02-26 21:49:23 +00:00
|
|
|
def __updateDataFromLDAP(self, user):
|
|
|
|
groups = ldap.getGroup(user.uid)
|
|
|
|
user_data = ldap.getUserData(user.uid)
|
|
|
|
user_data['gruppe'] = groups
|
|
|
|
user_data['group'] = groups
|
|
|
|
user.updateData(user_data)
|
|
|
|
db.updateUser(user)
|
|
|
|
|
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:
|
2020-02-26 21:49:23 +00:00
|
|
|
try:
|
|
|
|
self.__updateDataFromLDAP(user)
|
|
|
|
except:
|
|
|
|
pass
|
2020-01-05 13:15:02 +00:00
|
|
|
self.__updateGeruechte(user)
|
2019-12-28 20:52:49 +00:00
|
|
|
return db.getAllUser()
|
|
|
|
|
2020-01-18 22:31:49 +00:00
|
|
|
def checkBarUser(self, user):
|
|
|
|
date = datetime.now()
|
2020-01-18 23:37:40 +00:00
|
|
|
zero = date.replace(hour=0, minute=0, second=0, microsecond=0)
|
2020-02-23 10:46:38 +00:00
|
|
|
end = zero + timedelta(hours=12)
|
|
|
|
startdatetime = date.replace(hour=12, minute=0, second=0, microsecond=0)
|
2020-01-18 23:37:40 +00:00
|
|
|
if date > zero and end > date:
|
|
|
|
startdatetime = startdatetime - timedelta(days=1)
|
2020-01-18 22:31:49 +00:00
|
|
|
enddatetime = startdatetime + timedelta(days=1)
|
|
|
|
result = False
|
|
|
|
if date >= startdatetime and date < enddatetime:
|
|
|
|
result = db.getWorker(user, startdatetime)
|
|
|
|
return True if result else False
|
|
|
|
|
|
|
|
|
2019-12-28 20:52:49 +00:00
|
|
|
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
|
|
|
|
|
2020-01-26 22:31:22 +00:00
|
|
|
def modifyUser(self, user, ldap_conn, attributes):
|
|
|
|
try:
|
|
|
|
if 'username' in attributes:
|
|
|
|
db.changeUsername(user, attributes['username'])
|
|
|
|
ldap.modifyUser(user, ldap_conn, attributes)
|
|
|
|
if 'username' in attributes:
|
|
|
|
return self.getUser(attributes['username'])
|
|
|
|
else:
|
|
|
|
return self.getUser(user.uid)
|
|
|
|
except UsernameExistLDAP as err:
|
|
|
|
db.changeUsername(user, user.uid)
|
|
|
|
raise Exception(err)
|
|
|
|
except LDAPExcetpion as err:
|
|
|
|
if 'username' in attributes:
|
|
|
|
db.changeUsername(user, user.uid)
|
|
|
|
raise Exception(err)
|
|
|
|
except Exception as err:
|
|
|
|
raise Exception(err)
|
|
|
|
|
2019-12-28 20:52:49 +00:00
|
|
|
def loginUser(self, username, password):
|
|
|
|
try:
|
|
|
|
user = self.getUser(username)
|
2020-01-26 22:31:22 +00:00
|
|
|
user.password = password
|
2019-12-28 20:52:49 +00:00
|
|
|
ldap.login(username, password)
|
2020-01-26 22:31:22 +00:00
|
|
|
ldap_conn = ldap.bind(user, password)
|
|
|
|
return user, ldap_conn
|
2019-12-28 20:52:49 +00:00
|
|
|
except PermissionDenied as err:
|
|
|
|
raise err
|