Merge branch 'feature/ssl_email' into develop

This commit is contained in:
Tim Gröger 2020-02-26 23:08:46 +01:00
commit 11dcffd3f3
5 changed files with 47 additions and 12 deletions

View File

@ -5,7 +5,7 @@
""" """
from .logger import getLogger from .logger import getLogger
from geruecht.controller import dbConfig from geruecht.controller import dbConfig, ldapConfig
from flask_mysqldb import MySQL from flask_mysqldb import MySQL
from flask_ldapconn import LDAPConn from flask_ldapconn import LDAPConn
@ -24,9 +24,9 @@ app.config['MYSQL_USER'] = dbConfig['user']
app.config['MYSQL_PASSWORD'] = dbConfig['passwd'] app.config['MYSQL_PASSWORD'] = dbConfig['passwd']
app.config['MYSQL_DB'] = dbConfig['database'] app.config['MYSQL_DB'] = dbConfig['database']
app.config['MYSQL_CURSORCLASS'] = 'DictCursor' app.config['MYSQL_CURSORCLASS'] = 'DictCursor'
app.config['LDAP_SERVER'] = '192.168.5.128' app.config['LDAP_SERVER'] = ldapConfig['URL']
app.config['LDAP_PORT'] = 389 app.config['LDAP_PORT'] = ldapConfig['port']
app.config['LDAP_BINDDN'] = 'dc=ldap,dc=example,dc=local' app.config['LDAP_BINDDN'] = ldapConfig['dn']
app.config['LDAP_USE_TLS'] = False app.config['LDAP_USE_TLS'] = False
app.config['FORCE_ATTRIBUTE_VALUE_AS_LIST'] = True app.config['FORCE_ATTRIBUTE_VALUE_AS_LIST'] = True

View File

@ -10,7 +10,8 @@ default = {
'port': 0, 'port': 0,
'user': '', 'user': '',
'passwd': '', 'passwd': '',
'email': '' 'email': '',
'crypt': 'STARTTLS'
} }
} }
@ -32,6 +33,9 @@ class ConifgParser():
self.__error__('Wrong Configuration for LDAP. You should configure ldapconfig with "URL" and "dn"') 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']: 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"') 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'] self.ldap = self.config['LDAP']
LOGGER.info("Set LDAPconfig: {}".format(self.ldap)) LOGGER.info("Set LDAPconfig: {}".format(self.ldap))
if 'AccessTokenLifeTime' in self.config: if 'AccessTokenLifeTime' in self.config:
@ -61,6 +65,9 @@ class ConifgParser():
if 'email' not in self.config['Mail']: if 'email' not in self.config['Mail']:
self.config['Mail']['email'] = default['Mail']['email'] self.config['Mail']['email'] = default['Mail']['email']
LOGGER.info("No Config for email in Mail found. Set it to default") 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'] self.mail = self.config['Mail']
LOGGER.info('Set Mailconfig: {}'.format(self.mail)) LOGGER.info('Set Mailconfig: {}'.format(self.mail))

View File

@ -3,24 +3,38 @@ from datetime import datetime
from email.mime.multipart import MIMEMultipart from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText from email.mime.text import MIMEText
from email.header import Header from email.header import Header
from . import LOGGER from geruecht import getLogger
LOGGER = getLogger('E-MailController')
class EmailController(): 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.smtpServer = smtpServer
self.port = port self.port = port
self.user = user self.user = user
self.passwd = passwd self.passwd = passwd
self.crypt = crypt
if email: if email:
self.email = email self.email = email
else: else:
self.email = user self.email = user
LOGGER.debug('Init EmailController with smtpServer={}, port={}, user={}, crypt={}, email={}'.format(smtpServer, user, port, crypt, self.email))
def __connect__(self): def __connect__(self):
self.smtp = smtplib.SMTP(self.smtpServer, self.port) LOGGER.info('Connect to E-Mail-Server')
self.smtp.starttls() if self.crypt == 'SSL':
self.smtp.login(self.user, self.passwd) 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): def sendMail(self, user):
try: try:

View File

@ -38,8 +38,10 @@ class LDAPController(metaclass=Singleton):
'dn': self.ldap.connection.response[0]['dn'], 'dn': self.ldap.connection.response[0]['dn'],
'firstname': user['givenName'][0], 'firstname': user['givenName'][0],
'lastname': user['sn'][0], 'lastname': user['sn'][0],
'uid': username 'uid': username,
} }
if user['mail']:
retVal['mail'] = user['mail'][0]
return retVal return retVal
except: except:
raise PermissionDenied("No User exists with this uid.") raise PermissionDenied("No User exists with this uid.")

View File

@ -10,7 +10,7 @@ from geruecht.exceptions import UsernameExistLDAP, UsernameExistDB, DatabaseExec
db = dc.DatabaseController() db = dc.DatabaseController()
ldap = lc.LDAPController(ldapConfig['dn']) 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): class UserController(metaclass=Singleton):
@ -110,6 +110,14 @@ class UserController(metaclass=Singleton):
db.updateUser(user) db.updateUser(user)
return self.getUser(username) 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): def autoLock(self, user):
if user.autoLock: if user.autoLock:
if user.getGeruecht(year=datetime.now().year).getSchulden() <= (-1*user.limit): if user.getGeruecht(year=datetime.now().year).getSchulden() <= (-1*user.limit):
@ -141,6 +149,10 @@ class UserController(metaclass=Singleton):
def getAllUsersfromDB(self): def getAllUsersfromDB(self):
users = db.getAllUser() users = db.getAllUser()
for user in users: for user in users:
try:
self.__updateDataFromLDAP(user)
except:
pass
self.__updateGeruechte(user) self.__updateGeruechte(user)
return db.getAllUser() return db.getAllUser()