2020-01-21 05:54:35 +00:00
|
|
|
from geruecht import ldap
|
|
|
|
from ldap3 import SUBTREE, Connection
|
2019-12-28 20:52:49 +00:00
|
|
|
from geruecht.model import MONEY, USER, GASTRO, BAR
|
|
|
|
from geruecht.exceptions import PermissionDenied
|
|
|
|
from . import Singleton
|
2020-01-21 05:54:35 +00:00
|
|
|
import traceback
|
2019-12-19 07:12:29 +00:00
|
|
|
|
|
|
|
class LDAPController(metaclass=Singleton):
|
|
|
|
'''
|
|
|
|
Authentification over LDAP. Create Account on-the-fly
|
|
|
|
'''
|
|
|
|
|
2020-01-21 05:54:35 +00:00
|
|
|
def __init__(self, dn='dc=ldap,dc=example,dc=local'):
|
2019-12-19 07:12:29 +00:00
|
|
|
self.dn = dn
|
2020-01-21 05:54:35 +00:00
|
|
|
self.ldap = ldap
|
2019-12-19 07:12:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
def login(self, username, password):
|
|
|
|
try:
|
2020-01-21 05:54:35 +00:00
|
|
|
retVal = self.ldap.authenticate(username, password, 'uid', self.dn)
|
|
|
|
if not retVal:
|
|
|
|
raise PermissionDenied("Invalid Password or Username")
|
|
|
|
except Exception as err:
|
|
|
|
traceback.print_exception(err)
|
|
|
|
raise PermissionDenied("Wrong username or password.")
|
2019-12-19 07:12:29 +00:00
|
|
|
|
|
|
|
def getUserData(self, username):
|
2019-12-31 12:06:40 +00:00
|
|
|
try:
|
2020-01-21 05:54:35 +00:00
|
|
|
self.ldap.connection.search('ou=user,{}'.format(self.dn), '(uid={})'.format(username), SUBTREE, attributes=['uid', 'givenName', 'sn', 'mail'])
|
|
|
|
user = self.ldap.connection.response[0]['attributes']
|
|
|
|
retVal = {
|
|
|
|
'dn': self.ldap.connection.response[0]['dn'],
|
|
|
|
'firstname': user['givenName'][0],
|
|
|
|
'lastname': user['sn'][0],
|
|
|
|
'uid': username
|
|
|
|
}
|
2019-12-31 12:06:40 +00:00
|
|
|
return retVal
|
|
|
|
except:
|
|
|
|
raise PermissionDenied("No User exists with this uid.")
|
2019-12-19 07:12:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
def getGroup(self, username):
|
2020-01-21 05:54:35 +00:00
|
|
|
try:
|
|
|
|
retVal = []
|
|
|
|
self.ldap.connection.search('ou=user,{}'.format(self.dn), '(uid={})'.format(username), SUBTREE, attributes=['gidNumber'])
|
|
|
|
main_group_number = self.ldap.connection.response[0]['attributes']['gidNumber']
|
|
|
|
if main_group_number:
|
|
|
|
group_data = self.ldap.connection.search('ou=group,{}'.format(self.dn), '(gidNumber={})'.format(main_group_number), attributes=['cn'])
|
|
|
|
group_name = self.ldap.connection.response[0]['attributes']['cn'][0]
|
2019-12-22 21:27:39 +00:00
|
|
|
if group_name == 'ldap-user':
|
|
|
|
retVal.append(USER)
|
|
|
|
|
2020-01-21 05:54:35 +00:00
|
|
|
self.ldap.connection.search('ou=group,{}'.format(self.dn), '(memberUID={})'.format(username), SUBTREE, attributes=['cn'])
|
|
|
|
groups_data = self.ldap.connection.response
|
|
|
|
for data in groups_data:
|
|
|
|
group_name = data['attributes']['cn'][0]
|
|
|
|
if group_name == 'finanzer':
|
|
|
|
retVal.append(MONEY)
|
|
|
|
elif group_name == 'gastro':
|
|
|
|
retVal.append(GASTRO)
|
|
|
|
elif group_name == 'bar':
|
|
|
|
retVal.append(BAR)
|
|
|
|
return retVal
|
|
|
|
except Exception as err:
|
|
|
|
traceback.print_exception(err)
|
2019-12-19 07:12:29 +00:00
|
|
|
|
2019-12-22 22:09:18 +00:00
|
|
|
def __isUserInList(self, list, username):
|
|
|
|
help_list = []
|
|
|
|
for user in list:
|
2019-12-26 09:28:30 +00:00
|
|
|
help_list.append(user['username'])
|
2019-12-22 22:09:18 +00:00
|
|
|
if username in help_list:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2019-12-26 09:28:30 +00:00
|
|
|
def getAllUser(self):
|
|
|
|
retVal = []
|
2020-01-21 05:54:35 +00:00
|
|
|
self.ldap.connection.search()
|
|
|
|
self.ldap.connection.search('ou=user,{}'.format(self.dn), '(uid=*)', SUBTREE, attributes=['uid', 'givenName', 'sn', 'mail'])
|
|
|
|
data = self.ldap.connection.response
|
2019-12-26 09:28:30 +00:00
|
|
|
for user in data:
|
2020-01-21 05:54:35 +00:00
|
|
|
if 'uid' in user['attributes']:
|
|
|
|
username = user['attributes']['uid'][0]
|
|
|
|
firstname = user['attributes']['givenName'][0]
|
|
|
|
lastname = user['attributes']['sn'][0]
|
2019-12-26 09:28:30 +00:00
|
|
|
retVal.append({'username': username, 'firstname': firstname, 'lastname': lastname})
|
|
|
|
return retVal
|
|
|
|
|
2019-12-22 22:09:18 +00:00
|
|
|
def searchUser(self, searchString):
|
|
|
|
|
|
|
|
name = searchString.split(" ")
|
2019-12-26 09:28:30 +00:00
|
|
|
|
|
|
|
for i in range(len(name)):
|
|
|
|
name[i] = "*"+name[i]+"*"
|
|
|
|
|
|
|
|
|
|
|
|
print(name)
|
|
|
|
|
2019-12-22 22:09:18 +00:00
|
|
|
name_result = []
|
|
|
|
|
|
|
|
if len(name) == 1:
|
2019-12-26 09:28:30 +00:00
|
|
|
if name[0] == "**":
|
2020-01-21 05:54:35 +00:00
|
|
|
self.ldap.connection.search('ou=user,{}'.format(self.dn), '(uid=*)', SUBTREE,
|
|
|
|
attributes=['uid', 'givenName', 'sn'])
|
|
|
|
name_result.append(self.ldap.connection.response)
|
2019-12-26 09:28:30 +00:00
|
|
|
else:
|
2020-01-21 05:54:35 +00:00
|
|
|
self.ldap.connection.search('ou=user,{}'.format(self.dn), '(givenName={})'.format(name[0]), SUBTREE, attributes=['uid', 'givenName', 'sn', 'mail'])
|
|
|
|
name_result.append(self.ldap.connection.response)
|
|
|
|
self.ldap.connection.search('ou=user,{}'.format(self.dn), '(sn={})'.format(name[0]), SUBTREE, attributes=['uid', 'givenName', 'sn', 'mail'])
|
|
|
|
name_result.append(self.ldap.connection.response)
|
2019-12-22 22:09:18 +00:00
|
|
|
else:
|
2020-01-21 05:54:35 +00:00
|
|
|
self.ldap.connection.search('ou=user,{}'.format(self.dn), '(givenName={})'.format(name[1]), SUBTREE, attributes=['uid', 'givenName', 'sn'])
|
|
|
|
name_result.append(self.ldap.connection.response)
|
|
|
|
self.ldap.connection.search('ou=user,{}'.format(self.dn), '(sn={})'.format(name[1]), SUBTREE, attributes=['uid', 'givenName', 'sn', 'mail'])
|
|
|
|
name_result.append(self.ldap.connection.response)
|
2019-12-22 22:09:18 +00:00
|
|
|
retVal = []
|
|
|
|
|
2019-12-26 09:28:30 +00:00
|
|
|
for names in name_result:
|
|
|
|
for user in names:
|
2020-01-21 05:54:35 +00:00
|
|
|
if 'uid' in user['attributes']:
|
|
|
|
username = user['attributes']['uid'][0]
|
2019-12-26 09:28:30 +00:00
|
|
|
if not self.__isUserInList(retVal, username):
|
2020-01-21 05:54:35 +00:00
|
|
|
firstname = user['attributes']['givenName'][0]
|
|
|
|
lastname = user['attributes']['sn'][0]
|
2019-12-26 09:28:30 +00:00
|
|
|
retVal.append({'username': username, 'firstname': firstname, 'lastname': lastname})
|
2019-12-22 22:09:18 +00:00
|
|
|
|
|
|
|
return retVal
|
|
|
|
|
2019-12-19 07:12:29 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
a = LDAPController()
|
|
|
|
a.getUserData('jhille')
|