57 lines
1.7 KiB
Python
57 lines
1.7 KiB
Python
import ldap
|
|
from geruecht import MONEY, USER, GASTRO, BAR, Singleton
|
|
|
|
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:
|
|
self.client.bind_s("cn={},ou=user,{}".format(username, self.dn), password)
|
|
self.client.unbind_s()
|
|
except:
|
|
self.client.unbind_s()
|
|
raise Exception("Invalid Password or Username")
|
|
|
|
def getUserData(self, username):
|
|
self.connect()
|
|
search_data = self.client.search_s('ou=user,{}'.format(self.dn), ldap.SCOPE_SUBTREE, 'cn={}'.format(username), ['cn', 'givenName', 'sn'])
|
|
retVal = search_data[0][1]
|
|
for k,v in retVal.items():
|
|
retVal[k] = v[0].decode('utf-8')
|
|
retVal['dn'] = self.dn
|
|
return retVal
|
|
|
|
|
|
def getGroup(self, username):
|
|
self.connect()
|
|
groups_data = self.client.search_s('ou=group,{}'.format(self.dn), ldap.SCOPE_SUBTREE, 'memberUID={}'.format(username), ['cn'])
|
|
if len(groups_data) == 0:
|
|
return USER
|
|
else:
|
|
data = groups_data[0][1]['cn'][0].decode('utf-8')
|
|
if data == 'finanzer':
|
|
return MONEY
|
|
elif data == 'gastro':
|
|
return GASTRO
|
|
elif data == 'bar':
|
|
return BAR
|
|
|
|
|
|
if __name__ == '__main__':
|
|
a = LDAPController()
|
|
a.getUserData('jhille')
|