From 5d1608896d02038acc8fde023cbaf7ff4d5ec056 Mon Sep 17 00:00:00 2001
From: Andreas <andreasv@fsmpi.rwth-aachen.de>
Date: Sun, 15 Oct 2017 19:20:37 +0200
Subject: [PATCH] moved ldap stuff from db.py to ldap.py closes #343

---
 db.py     | 30 ------------------------------
 ldap.py   | 30 ++++++++++++++++++++++++++++++
 server.py |  3 ++-
 3 files changed, 32 insertions(+), 31 deletions(-)
 create mode 100644 ldap.py

diff --git a/db.py b/db.py
index b4b04fc..22e09a5 100644
--- a/db.py
+++ b/db.py
@@ -1,5 +1,4 @@
 from server import *
-import re
 
 if config['DB_ENGINE'] == 'sqlite':
 	import sqlite3
@@ -25,7 +24,6 @@ if config['DB_ENGINE'] == 'sqlite':
 		cur = db.cursor()
 		if config['SQLITE_INIT_SCHEMA']:
 			cur.executescript(open(config['DB_SCHEMA']).read())
-		if config['SQLITE_INIT_DATA'] and created:
 			cur.executescript(open(config['DB_DATA']).read())
 		db.commit()
 		db.close()
@@ -144,31 +142,3 @@ def searchquery(text, columns, match, tables, suffix, *suffixparams):
 		return []
 	expr = 'SELECT *,SUM(_prio) AS _score FROM (%s) AS _tmp %s'%(' UNION '.join(subexprs), suffix)
 	return query(expr, *(list(params)+list(suffixparams)))
-
-LDAP_USERRE = re.compile(r'[^a-z0-9]')
-if 'LDAP_HOST' in config:
-	import ldap3
-
-	def ldapauth(user, password):
-		user = LDAP_USERRE.sub(r'', user.lower())
-		try:
-			conn = ldap3.Connection(ldap3.Server(config['LDAP_HOST'], port=config['LDAP_PORT'], use_ssl=True), 'fsmpi\\%s'%user, password, auto_bind=True, check_names=False)
-		except ldap3.core.exceptions.LDAPBindError:
-			return {}, []
-		conn.search("cn=users,dc=fsmpi,dc=rwth-aachen,dc=de", "(cn=%s)"%user, attributes=['memberOf', 'givenName', 'sn'])
-		info = {'uid': user, 'givenName': conn.response[0]['attributes']['givenName'][0], 'sn': conn.response[0]['attributes']['sn'][0]}
-		groups = [g.split(',')[0].split('=')[-1] for g in conn.response[0]['attributes']['memberOf']]
-		conn.unbind()
-		return info, groups
-
-else:
-	notldap = {
-		'videoag':('videoag', ['fachschaft','videoag'], {'uid': 'videoag', 'givenName': 'Video', 'sn': 'Geier'}),
-		'gustav':('passwort', ['fachschaft'], {'uid': 'gustav', 'givenName': 'Gustav', 'sn': 'Geier'})
-	}
-
-	def ldapauth(user, password):
-		user = LDAP_USERRE.sub(r'', user.lower())
-		if config.get('DEBUG') and user in notldap and password == notldap[user][0]:
-			return notldap[user][2], notldap[user][1]
-		return {}, []
diff --git a/ldap.py b/ldap.py
new file mode 100644
index 0000000..0391176
--- /dev/null
+++ b/ldap.py
@@ -0,0 +1,30 @@
+from server import *
+import re
+
+LDAP_USERRE = re.compile(r'[^a-z0-9]')
+if 'LDAP_HOST' in config:
+	import ldap3
+
+	def ldapauth(user, password):
+		user = LDAP_USERRE.sub(r'', user.lower())
+		try:
+			conn = ldap3.Connection(ldap3.Server(config['LDAP_HOST'], port=config['LDAP_PORT'], use_ssl=True), 'fsmpi\\%s'%user, password, auto_bind=True, check_names=False)
+		except ldap3.core.exceptions.LDAPBindError:
+			return {}, []
+		conn.search("cn=users,dc=fsmpi,dc=rwth-aachen,dc=de", "(cn=%s)"%user, attributes=['memberOf', 'givenName', 'sn'])
+		info = {'uid': user, 'givenName': conn.response[0]['attributes']['givenName'][0], 'sn': conn.response[0]['attributes']['sn'][0]}
+		groups = [g.split(',')[0].split('=')[-1] for g in conn.response[0]['attributes']['memberOf']]
+		conn.unbind()
+		return info, groups
+
+else:
+	notldap = {
+		'videoag':('videoag', ['fachschaft','videoag'], {'uid': 'videoag', 'givenName': 'Video', 'sn': 'Geier'}),
+		'gustav':('passwort', ['fachschaft'], {'uid': 'gustav', 'givenName': 'Gustav', 'sn': 'Geier'})
+	}
+
+	def ldapauth(user, password):
+		user = LDAP_USERRE.sub(r'', user.lower())
+		if config.get('DEBUG') and user in notldap and password == notldap[user][0]:
+			return notldap[user][2], notldap[user][1]
+		return {}, []
diff --git a/server.py b/server.py
index 0f60ae4..8d2c466 100644
--- a/server.py
+++ b/server.py
@@ -72,7 +72,8 @@ app.jinja_env.globals['gitversion'] = { 'hash': output[1], 'longhash': output[0]
 if not config.get('SECRET_KEY', None):
 	config['SECRET_KEY'] = os.urandom(24)
 
-from db import query, modify, show, searchquery, ldapauth
+from db import query, modify, show, searchquery
+from ldap import ldapauth
 
 mod_endpoints = []
 
-- 
GitLab