diff options
author | Nico von Geyso <Nico.Geyso@FU-Berlin.de> | 2012-09-23 18:15:23 +0200 |
---|---|---|
committer | Nico von Geyso <Nico.Geyso@FU-Berlin.de> | 2012-09-23 18:15:59 +0200 |
commit | 42595026cec2d779f540cb87bf28ab2aa0daf608 (patch) | |
tree | 68e50a803e8a11f9f09e67344021c83567e59260 /account.py | |
parent | 2bdceb1209a081e9ccb80a35dd605c120aaf32e9 (diff) | |
download | web-42595026cec2d779f540cb87bf28ab2aa0daf608.tar.gz web-42595026cec2d779f540cb87bf28ab2aa0daf608.tar.bz2 web-42595026cec2d779f540cb87bf28ab2aa0daf608.zip |
find() now uses dict instead of ldap search str
examples:
service.find() # find all users
service.find_by_uid('test') # find users by uid
service.find_by_mail('test@test.de') # find users by mail
Diffstat (limited to 'account.py')
-rw-r--r-- | account.py | 31 |
1 files changed, 24 insertions, 7 deletions
@@ -6,7 +6,7 @@ LDAP_HOST = 'ldap://localhost:5678' LDAP_BASE_DN = 'dc=account,dc=spline,dc=inf,dc=fu-berlin,dc=de' LDAP_ADMIN_USER = 'admin' LDAP_ADMIN_PASS = 'admin' -SERVICES = ['foren','jabber', 'gitlab'] +SERVICES = ['foren','jabber', 'gitlab', 'osqa'] @@ -17,7 +17,7 @@ class AccountService: its own authentication request (bind). To test you stuff against our test setup use Port-Forwarding - ssh spline -L 5678:vm-splinux:389 -N + ssh spline -L 5678:vm-acocunt:389 -N * register a new user >> service = AccountService(LDAP_HOST, LDAP_BASE_DN, ADMIN_USER, ADMIN_PW, SERVICES) @@ -43,8 +43,10 @@ class AccountService: * find accounts >> service = AccountService(LDAP_HOST, LDAP_BASE_DN, LDAP_ADMIN_USER, LDAP_ADMIN_PASS, SERVICES) - >> all_accounts = service.find() + >> all_accounts = service.find() # find all accounts >> print([x.uid for x in all_accounts]) + >> service.find_by_uid('test') # find users by uid + >> service.find_by_mail('test@test.de') # find users by mail """ def __init__(self, ldap_host, base_dn, admin_user, admin_pass, services): @@ -81,18 +83,30 @@ class AccountService: return acc + def find_by_uid(self, uid): + return self.find({'uid':uid}) - def find(self, filterstr = '(objectClass=*)'): + def find_by_mail(self, mail): + return self.find({'mail':mail}) + + def find(self, filters = {}): """ - Find accounts with raw ldap filter syntax + Find accounts by a given filter with key:value semantic) """ - self._bind_as_admin() + self._bind_anonymous() dn = 'ou=users,%s' % self.base_dn + + filters['objectClass'] = 'inetOrgPerson' + filter_as_list = ['(%s=%s)' % (k,v) for k,v in filters.items()] + filterstr = ''.join(filter_as_list) + if len(filter_as_list) > 1: + filterstr = '(&%s)' % filterstr + data = self.connection.search_s(dn,ldap.SCOPE_SUBTREE,filterstr) accounts = [] - for a in data[1:]: + for a in data: accounts.append(Account(a[1]['uid'],a[1]['mail'])) self._unbind() @@ -172,6 +186,9 @@ class AccountService: def _bind_as_admin(self): self._bind('cn=%s,%s' % (self.admin_user, self.base_dn), self.admin_pass) + def _bind_anonymous(self): + self._bind('','') + def _unbind(self): self.connection.unbind_s() |