From 80fbe2b7592b6b24d5db632c1cae8b316e4fe53a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Gr=C3=B6ger?= Date: Wed, 26 Feb 2020 22:13:44 +0100 Subject: [PATCH 1/3] finished ##177 --- geruecht/configparser.py | 6 +++++- geruecht/controller/emailController.py | 24 +++++++++++++++++++----- geruecht/controller/userController.py | 2 +- 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/geruecht/configparser.py b/geruecht/configparser.py index e1ab855..dc519a0 100644 --- a/geruecht/configparser.py +++ b/geruecht/configparser.py @@ -10,7 +10,8 @@ default = { 'port': 0, 'user': '', 'passwd': '', - 'email': '' + 'email': '', + 'crypt': 'STARTTLS' } } @@ -61,6 +62,9 @@ class ConifgParser(): if 'email' not in self.config['Mail']: self.config['Mail']['email'] = default['Mail']['email'] LOGGER.info("No Config for email in Mail found. Set it to default") + if 'crypt' not in self.config['Mail']: + self.config['Mail']['crypt'] = default['Mail']['crypt'] + LOGGER.info("No Config for crypt in Mail found. Set it to default") self.mail = self.config['Mail'] LOGGER.info('Set Mailconfig: {}'.format(self.mail)) diff --git a/geruecht/controller/emailController.py b/geruecht/controller/emailController.py index b11eb40..ebad0da 100644 --- a/geruecht/controller/emailController.py +++ b/geruecht/controller/emailController.py @@ -3,24 +3,38 @@ from datetime import datetime from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.header import Header -from . import LOGGER +from geruecht import getLogger + +LOGGER = getLogger('E-MailController') class EmailController(): - def __init__(self, smtpServer, user, passwd, port = 587, email = ""): + def __init__(self, smtpServer, user, passwd, crypt, port=587, email=""): self.smtpServer = smtpServer self.port = port self.user = user self.passwd = passwd + self.crypt = crypt if email: self.email = email else: self.email = user + LOGGER.debug('Init EmailController with smtpServer={}, port={}, user={}, crypt={}, email={}'.format(smtpServer, user, port, crypt, self.email)) def __connect__(self): - self.smtp = smtplib.SMTP(self.smtpServer, self.port) - self.smtp.starttls() - self.smtp.login(self.user, self.passwd) + LOGGER.info('Connect to E-Mail-Server') + if self.crypt == 'SSL': + self.smtp = smtplib.SMTP_SSL(self.smtpServer, self.port) + log = self.smtp.ehlo() + LOGGER.debug(log) + if self.crypt == 'STARTTLS': + self.smtp = smtplib.SMTP(self.smtpServer, self.port) + log = self.smtp.ehlo() + LOGGER.debug(log) + log = self.smtp.starttls() + LOGGER.debug(log) + log = self.smtp.login(self.user, self.passwd) + LOGGER.debug(log) def sendMail(self, user): try: diff --git a/geruecht/controller/userController.py b/geruecht/controller/userController.py index b7210ae..ea562ca 100644 --- a/geruecht/controller/userController.py +++ b/geruecht/controller/userController.py @@ -10,7 +10,7 @@ from geruecht.exceptions import UsernameExistLDAP, UsernameExistDB, DatabaseExec db = dc.DatabaseController() ldap = lc.LDAPController(ldapConfig['dn']) -emailController = ec.EmailController(mailConfig['URL'], mailConfig['user'], mailConfig['passwd'], mailConfig['port'], mailConfig['email']) +emailController = ec.EmailController(mailConfig['URL'], mailConfig['user'], mailConfig['passwd'], mailConfig['crypt'], mailConfig['port'], mailConfig['email']) class UserController(metaclass=Singleton): From 358826e8c406639411cbe4da2ce11115433e9d79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Gr=C3=B6ger?= Date: Wed, 26 Feb 2020 22:49:23 +0100 Subject: [PATCH 2/3] try to update userdata in database from ldap when getAllUsersFromDB is executed --- geruecht/controller/ldapController.py | 4 +++- geruecht/controller/userController.py | 12 ++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/geruecht/controller/ldapController.py b/geruecht/controller/ldapController.py index 1356c45..9fa3d5b 100644 --- a/geruecht/controller/ldapController.py +++ b/geruecht/controller/ldapController.py @@ -38,8 +38,10 @@ class LDAPController(metaclass=Singleton): 'dn': self.ldap.connection.response[0]['dn'], 'firstname': user['givenName'][0], 'lastname': user['sn'][0], - 'uid': username + 'uid': username, } + if user['mail']: + retVal['mail'] = user['mail'][0] return retVal except: raise PermissionDenied("No User exists with this uid.") diff --git a/geruecht/controller/userController.py b/geruecht/controller/userController.py index ea562ca..a7a542c 100644 --- a/geruecht/controller/userController.py +++ b/geruecht/controller/userController.py @@ -110,6 +110,14 @@ class UserController(metaclass=Singleton): db.updateUser(user) return self.getUser(username) + 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) + def autoLock(self, user): if user.autoLock: if user.getGeruecht(year=datetime.now().year).getSchulden() <= (-1*user.limit): @@ -141,6 +149,10 @@ class UserController(metaclass=Singleton): def getAllUsersfromDB(self): users = db.getAllUser() for user in users: + try: + self.__updateDataFromLDAP(user) + except: + pass self.__updateGeruechte(user) return db.getAllUser() From ff2df817a3bbe5274c4998955aa27310a5816da2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Gr=C3=B6ger?= Date: Wed, 26 Feb 2020 23:03:02 +0100 Subject: [PATCH 3/3] finished ##179 --- geruecht/__init__.py | 8 ++++---- geruecht/configparser.py | 3 +++ 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/geruecht/__init__.py b/geruecht/__init__.py index 0963dd8..ed985cc 100644 --- a/geruecht/__init__.py +++ b/geruecht/__init__.py @@ -5,7 +5,7 @@ """ from .logger import getLogger -from geruecht.controller import dbConfig +from geruecht.controller import dbConfig, ldapConfig from flask_mysqldb import MySQL from flask_ldapconn import LDAPConn @@ -24,9 +24,9 @@ app.config['MYSQL_USER'] = dbConfig['user'] app.config['MYSQL_PASSWORD'] = dbConfig['passwd'] app.config['MYSQL_DB'] = dbConfig['database'] app.config['MYSQL_CURSORCLASS'] = 'DictCursor' -app.config['LDAP_SERVER'] = '192.168.5.128' -app.config['LDAP_PORT'] = 389 -app.config['LDAP_BINDDN'] = 'dc=ldap,dc=example,dc=local' +app.config['LDAP_SERVER'] = ldapConfig['URL'] +app.config['LDAP_PORT'] = ldapConfig['port'] +app.config['LDAP_BINDDN'] = ldapConfig['dn'] app.config['LDAP_USE_TLS'] = False app.config['FORCE_ATTRIBUTE_VALUE_AS_LIST'] = True diff --git a/geruecht/configparser.py b/geruecht/configparser.py index dc519a0..5e98271 100644 --- a/geruecht/configparser.py +++ b/geruecht/configparser.py @@ -33,6 +33,9 @@ class ConifgParser(): self.__error__('Wrong Configuration for LDAP. You should configure ldapconfig with "URL" and "dn"') if 'URL' not in self.config['LDAP'] or 'dn' not in self.config['LDAP']: self.__error__('Wrong Configuration for LDAP. You should configure ldapconfig with "URL" and "dn"') + if 'port' not in self.config['LDAP']: + LOGGER.info('No Config for port in LDAP found. Set it to default: {}'.format(389)) + self.config['LDAP']['port'] = 389 self.ldap = self.config['LDAP'] LOGGER.info("Set LDAPconfig: {}".format(self.ldap)) if 'AccessTokenLifeTime' in self.config: