update userControler, databaseController, ldapControlle and user for locking user

This commit is contained in:
Tim Gröger 2019-12-28 23:34:09 +01:00
parent 5fd734dc4e
commit 5607ec72f7
4 changed files with 53 additions and 11 deletions

View File

@ -69,13 +69,13 @@ class DatabaseController(metaclass=Singleton):
retVal += group
return retVal
def insertUser(self, data):
def insertUser(self, user):
self.connect()
cursor = self.db.cursor()
groups = self._convertGroupToString(data['group'])
groups = self._convertGroupToString(user.group)
try:
cursor.execute("insert into user (uid, dn, firstname, lastname, gruppe) VALUES ('{}','{}','{}','{}','{}')".format(
data['uid'], data['dn'], data['givenName'], data['sn'], groups))
cursor.execute("insert into user (uid, dn, firstname, lastname, gruppe, limit, locked, autoLock) VALUES ('{}','{}','{}','{}','{}',{},{},{})".format(
user.uid, user.dn, user.firstname, user.lastname, groups))
self.db.commit()
except Exception as err:
self.db.rollback()
@ -83,13 +83,13 @@ class DatabaseController(metaclass=Singleton):
raise err
self.db.close()
def updateUser(self, data):
def updateUser(self, user):
self.connect()
cursor = self.db.cursor()
groups = self._convertGroupToString(data['group'])
groups = self._convertGroupToString(user.group)
try:
cursor.execute("update user set dn='{}', firstname='{}', lastname='{}', gruppe='{}' where uid='{}'".format(
data['dn'], data['givenName'], data['sn'], groups, data['uid']))
cursor.execute("update user set dn='{}', firstname='{}', lastname='{}', gruppe='{}, limit={}, locked={}, autoLock={}' where uid='{}'".format(
user.dn, user.firstname, user.lastname, groups, user.limit, user.locked, user.autoLock, user.uid))
self.db.commit()
except Exception as err:
self.db.rollback()

View File

@ -36,6 +36,8 @@ class LDAPController(metaclass=Singleton):
for k,v in retVal.items():
retVal[k] = v[0].decode('utf-8')
retVal['dn'] = self.dn
retVal['firstname'] = retVal['givenName']
retVal['lastname'] = retVal['sn']
return retVal

View File

@ -1,4 +1,5 @@
from . import LOGGER, Singleton, db, ldapController as ldap
from geruecht.model.user import User
from geruecht.exceptions import PermissionDenied
class UserController(metaclass=Singleton):
@ -6,6 +7,12 @@ class UserController(metaclass=Singleton):
def __init__(self):
pass
def updateConfig(self, username, data):
user = self.getUser(username)
user.updateData(data)
db.updateUser(user)
return self.getUser(username)
def addAmount(self, username, amount, year, month):
user = self.getUser(username)
user.addAmount(amount, year=year, month=month)
@ -31,9 +38,11 @@ class UserController(metaclass=Singleton):
user_data = ldap.getUserData(username)
user_data['group'] = groups
if user is None:
db.insertUser(user_data)
user = User(user_data)
db.insertUser(user)
else:
db.updateUser(user_data)
user.updateData(user_data)
db.updateUser(user)
user = db.getUser(username)
return user

View File

@ -26,6 +26,18 @@ class User():
self.firstname = data['firstname']
self.lastname = data['lastname']
self.group = data['gruppe']
if 'limit' in data:
self.limit = data['limit']
else:
self.limit = 4200
if 'locked' in data:
self.locked = bool(data['locked'])
else:
self.locked = False
if 'autoLock' in data:
self.autoLock = bool(data['autoLock'])
else:
self.autoLock = True
if type(data['gruppe']) == list:
self.group = data['gruppe']
elif type(data['gruppe']) == str:
@ -33,6 +45,22 @@ class User():
if 'creditLists' in data:
self.geruechte = data['creditLists']
def updateData(self, data):
if 'dn' in data:
self.dn = data['dn']
if 'firstname' in data:
self.firstname = data['firstname']
if 'lastname' in data:
self.lastname = data['lastname']
if 'gruppe' in data:
self.group = data['gruppe']
if 'limit' in data:
self.limit = data['limit']
if 'locked' in data:
self.locked = bool(data['locked'])
if 'autoLock' in data:
self.autoLock = bool(data['autoLock'])
def initGeruechte(self, creditLists):
if type(creditLists) == list:
self.geruechte = creditLists
@ -166,7 +194,10 @@ class User():
"firstname": self.firstname,
"lastname": self.lastname,
"group": self.group,
"username": self.uid
"username": self.uid,
"locked": self.locked,
"autoLock": self.autoLock,
"limit": self.limit
}
return dic