2019-12-19 07:12:29 +00:00
|
|
|
import ldap
|
2019-12-28 20:52:49 +00:00
|
|
|
from geruecht.model import MONEY, USER, GASTRO, BAR
|
|
|
|
from geruecht.exceptions import PermissionDenied
|
|
|
|
from . import Singleton
|
2019-12-19 07:12:29 +00:00
|
|
|
|
|
|
|
class LDAPController(metaclass=Singleton):
|
|
|
|
'''
|
|
|
|
Authentification over LDAP. Create Account on-the-fly
|
|
|
|
'''
|
|
|
|
|
|
|
|
def __init__(self, url="ldap://192.168.5.108", dn='dc=ldap,dc=example,dc=local'):
|
|
|
|
self.url = url
|
|
|
|
self.dn = dn
|
|
|
|
self.connect()
|
|
|
|
|
|
|
|
def connect(self):
|
|
|
|
try:
|
|
|
|
self.client = ldap.initialize(self.url, bytes_mode=False)
|
|
|
|
except Exception as err:
|
|
|
|
raise err
|
|
|
|
|
|
|
|
def login(self, username, password):
|
|
|
|
self.connect()
|
|
|
|
try:
|
2019-12-28 20:52:49 +00:00
|
|
|
cn = self.client.search_s("ou=user,{}".format(self.dn), ldap.SCOPE_SUBTREE, 'uid={}'.format(username),['cn'])[0][1]['cn'][0].decode('utf-8')
|
|
|
|
self.client.bind_s("cn={},ou=user,{}".format(cn, self.dn), password)
|
2019-12-19 07:12:29 +00:00
|
|
|
self.client.unbind_s()
|
|
|
|
except:
|
|
|
|
self.client.unbind_s()
|
2019-12-28 20:52:49 +00:00
|
|
|
raise PermissionDenied("Invalid Password or Username")
|
2019-12-19 07:12:29 +00:00
|
|
|
|
|
|
|
def getUserData(self, username):
|
|
|
|
self.connect()
|
2019-12-28 20:52:49 +00:00
|
|
|
search_data = self.client.search_s('ou=user,{}'.format(self.dn), ldap.SCOPE_SUBTREE, 'uid={}'.format(username), ['uid', 'givenName', 'sn'])
|
2019-12-19 07:12:29 +00:00
|
|
|
retVal = search_data[0][1]
|
|
|
|
for k,v in retVal.items():
|
|
|
|
retVal[k] = v[0].decode('utf-8')
|
|
|
|
retVal['dn'] = self.dn
|
2019-12-28 22:34:09 +00:00
|
|
|
retVal['firstname'] = retVal['givenName']
|
|
|
|
retVal['lastname'] = retVal['sn']
|
2019-12-19 07:12:29 +00:00
|
|
|
return retVal
|
|
|
|
|
|
|
|
|
|
|
|
def getGroup(self, username):
|
2019-12-22 21:27:39 +00:00
|
|
|
retVal = []
|
2019-12-19 07:12:29 +00:00
|
|
|
self.connect()
|
2019-12-28 20:52:49 +00:00
|
|
|
main_group_data = self.client.search_s('ou=user,{}'.format(self.dn), ldap.SCOPE_SUBTREE, 'uid={}'.format(username), ['gidNumber'])
|
2019-12-22 21:27:39 +00:00
|
|
|
if main_group_data:
|
|
|
|
main_group_number = main_group_data[0][1]['gidNumber'][0].decode('utf-8')
|
|
|
|
group_data = self.client.search_s('ou=group,{}'.format(self.dn), ldap.SCOPE_SUBTREE, 'gidNumber={}'.format(main_group_number), ['cn'])
|
|
|
|
if group_data:
|
|
|
|
group_name = group_data[0][1]['cn'][0].decode('utf-8')
|
|
|
|
if group_name == 'ldap-user':
|
|
|
|
retVal.append(USER)
|
|
|
|
|
2019-12-19 07:12:29 +00:00
|
|
|
groups_data = self.client.search_s('ou=group,{}'.format(self.dn), ldap.SCOPE_SUBTREE, 'memberUID={}'.format(username), ['cn'])
|
2019-12-22 21:27:39 +00:00
|
|
|
for data in groups_data:
|
|
|
|
print(data[1]['cn'][0].decode('utf-8'))
|
|
|
|
group_name = data[1]['cn'][0].decode('utf-8')
|
|
|
|
if group_name == 'finanzer':
|
|
|
|
retVal.append(MONEY)
|
|
|
|
elif group_name == 'gastro':
|
|
|
|
retVal.append(GASTRO)
|
|
|
|
elif group_name == 'bar':
|
|
|
|
retVal.append(BAR)
|
|
|
|
return retVal
|
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):
|
|
|
|
self.connect()
|
|
|
|
retVal = []
|
2019-12-28 20:52:49 +00:00
|
|
|
data = self.client.search_s('ou=user,{}'.format(self.dn), ldap.SCOPE_SUBTREE, attrlist=['uid', 'givenName', 'sn'])
|
2019-12-26 09:28:30 +00:00
|
|
|
for user in data:
|
2019-12-28 20:52:49 +00:00
|
|
|
if 'uid' in user[1]:
|
|
|
|
username = user[1]['uid'][0].decode('utf-8')
|
2019-12-26 09:28:30 +00:00
|
|
|
firstname = user[1]['givenName'][0].decode('utf-8')
|
|
|
|
lastname = user[1]['sn'][0].decode('utf-8')
|
|
|
|
retVal.append({'username': username, 'firstname': firstname, 'lastname': lastname})
|
|
|
|
return retVal
|
|
|
|
|
2019-12-22 22:09:18 +00:00
|
|
|
def searchUser(self, searchString):
|
|
|
|
self.connect()
|
|
|
|
|
|
|
|
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] == "**":
|
|
|
|
name_result.append(self.client.search_s('ou=user,{}'.format(self.dn), ldap.SCOPE_SUBTREE,
|
2019-12-28 20:52:49 +00:00
|
|
|
attrlist=['uid', 'givenName', 'sn']))
|
2019-12-26 09:28:30 +00:00
|
|
|
else:
|
2019-12-28 20:52:49 +00:00
|
|
|
name_result.append(self.client.search_s('ou=user,{}'.format(self.dn), ldap.SCOPE_SUBTREE, 'givenName={}'.format(name[0]), ['uid', 'givenName', 'sn']))
|
|
|
|
name_result.append(self.client.search_s('ou=user,{}'.format(self.dn), ldap.SCOPE_SUBTREE, 'sn={}'.format(name[0]),['uid', 'givenName', 'sn']))
|
2019-12-22 22:09:18 +00:00
|
|
|
else:
|
2019-12-26 09:28:30 +00:00
|
|
|
name_result.append(self.client.search_s('ou=user,{}'.format(self.dn), ldap.SCOPE_SUBTREE,
|
2019-12-28 20:52:49 +00:00
|
|
|
'givenName={}'.format(name[1]), ['uid', 'givenName', 'sn']))
|
2019-12-26 09:28:30 +00:00
|
|
|
name_result.append(self.client.search_s('ou=user,{}'.format(self.dn), ldap.SCOPE_SUBTREE, 'sn={}'.format(name[1]),
|
2019-12-28 20:52:49 +00:00
|
|
|
['uid', 'givenName', 'sn']))
|
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:
|
2019-12-28 20:52:49 +00:00
|
|
|
if 'uid' in user[1]:
|
|
|
|
username = user[1]['uid'][0].decode('utf-8')
|
2019-12-26 09:28:30 +00:00
|
|
|
if not self.__isUserInList(retVal, username):
|
|
|
|
firstname = user[1]['givenName'][0].decode('utf-8')
|
|
|
|
lastname = user[1]['sn'][0].decode('utf-8')
|
|
|
|
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')
|