192 lines
6.9 KiB
Python
192 lines
6.9 KiB
Python
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
|
|
import calendar
|
|
from geruecht.model.user import User
|
|
from geruecht.exceptions import PermissionDenied
|
|
from datetime import datetime, timedelta
|
|
from geruecht.exceptions import UsernameExistLDAP, UsernameExistDB, DatabaseExecption, LDAPExcetpion, DayLocked
|
|
|
|
db = dc.DatabaseController()
|
|
ldap = lc.LDAPController(ldapConfig['dn'])
|
|
emailController = ec.EmailController(mailConfig['URL'], mailConfig['user'], mailConfig['passwd'], mailConfig['port'], mailConfig['email'])
|
|
|
|
class UserController(metaclass=Singleton):
|
|
|
|
def __init__(self):
|
|
pass
|
|
|
|
def setLockedDay(self, date, locked,hard=False):
|
|
return db.setLockedDay(date.date(), locked, hard)
|
|
|
|
def getLockedDay(self, date):
|
|
now = datetime.now()
|
|
daysInMonth = calendar.monthrange(date.year, date.month)[1]
|
|
startMonth = 1
|
|
for i in range(1, 8):
|
|
if datetime(date.year, date.month, i).weekday() == 2:
|
|
startMonth = i
|
|
break
|
|
if date.year <= now.year and date.month <= now.month:
|
|
for i in range(startMonth, daysInMonth + 1):
|
|
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)
|
|
return db.getLockedDay(date.date())
|
|
|
|
def getWorker(self, date, username=None):
|
|
if (username):
|
|
user = self.getUser(username)
|
|
return [db.getWorker(user, date)]
|
|
return db.getWorkers(date)
|
|
|
|
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")
|
|
user = self.getUser(username)
|
|
if (not db.getWorker(user, date)):
|
|
db.setWorker(user, date)
|
|
return self.getWorker(date, username=username)
|
|
|
|
def deleteWorker(self, username, date, userExc=False):
|
|
if userExc:
|
|
lockedDay = self.getLockedDay(date)
|
|
if lockedDay:
|
|
if lockedDay['locked']:
|
|
raise DayLocked("Day is locked. You can't delete the Job")
|
|
user = self.getUser(username)
|
|
db.deleteWorker(user, date)
|
|
|
|
def lockUser(self, username, locked):
|
|
user = self.getUser(username)
|
|
user.updateData({'locked': locked})
|
|
db.updateUser(user)
|
|
return self.getUser(username)
|
|
|
|
def updateConfig(self, username, data):
|
|
user = self.getUser(username)
|
|
user.updateData(data)
|
|
db.updateUser(user)
|
|
return self.getUser(username)
|
|
|
|
def autoLock(self, user):
|
|
if user.autoLock:
|
|
if user.getGeruecht(year=datetime.now().year).getSchulden() <= (-1*user.limit):
|
|
user.updateData({'locked': True})
|
|
emailController.sendMail(user)
|
|
else:
|
|
user.updateData({'locked': False})
|
|
db.updateUser(user)
|
|
|
|
def addAmount(self, username, amount, year, month, finanzer=False):
|
|
user = self.getUser(username)
|
|
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)
|
|
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)
|
|
self.autoLock(user)
|
|
return user.getGeruecht(year)
|
|
|
|
def getAllUsersfromDB(self):
|
|
users = db.getAllUser()
|
|
for user in users:
|
|
self.__updateGeruechte(user)
|
|
return db.getAllUser()
|
|
|
|
def checkBarUser(self, user):
|
|
date = datetime.now()
|
|
zero = date.replace(hour=0, minute=0, second=0, microsecond=0)
|
|
end = zero + timedelta(hours=12)
|
|
startdatetime = date.replace(hour=12, 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:
|
|
result = db.getWorker(user, startdatetime)
|
|
return True if result else False
|
|
|
|
|
|
def getUser(self, username):
|
|
user = db.getUser(username)
|
|
groups = ldap.getGroup(username)
|
|
user_data = ldap.getUserData(username)
|
|
user_data['gruppe'] = groups
|
|
user_data['group'] = groups
|
|
if user is None:
|
|
user = User(user_data)
|
|
db.insertUser(user)
|
|
else:
|
|
user.updateData(user_data)
|
|
db.updateUser(user)
|
|
user = db.getUser(username)
|
|
self.__updateGeruechte(user)
|
|
return user
|
|
|
|
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
|
|
|
|
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)
|
|
|
|
def loginUser(self, username, password):
|
|
try:
|
|
user = self.getUser(username)
|
|
user.password = password
|
|
ldap.login(username, password)
|
|
ldap_conn = ldap.bind(user, password)
|
|
return user, ldap_conn
|
|
except PermissionDenied as err:
|
|
raise err
|