diff options
author | Marian Sigler <m@qjym.de> | 2012-09-21 02:44:02 +0200 |
---|---|---|
committer | Marian Sigler <m@qjym.de> | 2012-09-21 02:44:02 +0200 |
commit | d585148ab5e79621cd079f1002337cb2cc37ed44 (patch) | |
tree | 8e3645d2b314acd1d7f1bbdfb33b71cfe37a2210 /account.py | |
parent | f774c99fb887b7e2ad7c9f0e62efa631d319440d (diff) | |
download | web-d585148ab5e79621cd079f1002337cb2cc37ed44.tar.gz web-d585148ab5e79621cd079f1002337cb2cc37ed44.tar.bz2 web-d585148ab5e79621cd079f1002337cb2cc37ed44.zip |
AccountService: expect admin credentials at initialization.
This makes it possible to call the methods (find, register, etc) without
specifying the admin credentials each time again.
Diffstat (limited to 'account.py')
-rw-r--r-- | account.py | 64 |
1 files changed, 32 insertions, 32 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'] @@ -35,7 +35,7 @@ class AccountService: >> service.update(foo) # save changes in ldap backend # save changes in ldap backend as admin user >> service.update(foo, LDAP_ADMIN_USER, LDAP_ADMIN_PASS) - + * delete an account >> service = AccountService(LDAP_HOST, LDAP_BASE_DN,SERVICES) >> service.delete(Account) @@ -47,9 +47,11 @@ class AccountService: >> print([x.uid for x in all_accounts]) """ - def __init__(self, ldap_host, base_dn, services): + def __init__(self, ldap_host, base_dn, admin_user, admin_pass, services): self.ldap_host = ldap_host self.base_dn = base_dn + self.admin_user = admin_user + self.admin_pass = admin_pass self.services = services @@ -80,11 +82,11 @@ class AccountService: return acc - def find(self, admin_user, admin_pass, filterstr = '(objectClass=*)'): + def find(self, filterstr = '(objectClass=*)'): """ Find accounts with raw ldap filter syntax """ - self._bind('cn=%s,%s' % (admin_user, self.base_dn), admin_pass) + self._bind_as_admin() dn = 'ou=users,%s' % self.base_dn data = self.connection.search_s(dn,ldap.SCOPE_SUBTREE,filterstr) @@ -98,11 +100,11 @@ class AccountService: return accounts - def register(self, account, admin_user, admin_pass): + def register(self, account): """ Persists an account in the ldap backend """ - self._bind('cn=%s,%s' % (admin_user, self.base_dn), admin_pass) + self._bind_as_admin() dn = 'uid=%s,ou=users,%s' % (account.uid, self.base_dn) attr = [ @@ -118,19 +120,16 @@ class AccountService: self._unbind() - def update(self, account, admin_user = None, admin_pass = None): + def update(self, account, as_admin=False): """ Updates account informations like passwords or email. """ - user = 'uid=%s,ou=users' % account.uid - if admin_user: - user = 'cn=%s' % admin_user - - password = account.password - if admin_pass: - password = admin_pass - - self._bind('%s,%s' % (user, self.base_dn), password) + if as_admin: + self._bind_as_admin() + else: + user = 'uid=%s,ou=users' % account.uid + password = account.password + self._bind('%s,%s' % (user, self.base_dn), password) attr = [(ldap.MOD_REPLACE, 'mail', account.mail)] dn = 'uid=%s,ou=users,%s' % (account.uid, self.base_dn) @@ -144,18 +143,16 @@ class AccountService: """ Deletes an account permanently. """ - try: dn_user = account.dn - except: dn_user = 'uid=%s,ou=users,%s' % (account, self.base_dn) - user = dn_user - if admin_user: - user = 'cn=%s,%s' % (admin_user, self.base_dn) - - password = account.password - if admin_pass: - password = admin_pass + if as_admin: + self._bind_as_admin() + else: + try: dn_user = account.dn + except: dn_user = 'uid=%s,ou=users,%s' % (account, self.base_dn) + user = dn_user - self._bind(user, password) + password = account.password + self._bind('%s,%s' % (user, self.base_dn), password) dn = ['uid=%s,cn=%s,ou=services,%s' % (account.uid,s,self.base_dn) for s in account.services] dn.append(dn_user) @@ -171,6 +168,9 @@ class AccountService: self.connection.simple_bind_s(dn, password) + def _bind_as_admin(): + self._bind('cn=%s,%s' % (self.admin_user, self.base_dn), self.admin_pass) + def _unbind(self): self.connection.unbind_s() @@ -218,9 +218,9 @@ class Account: def change_password(self, new_password, service = None): """ - Changes a password for a given service. You have to use the Ldap class - to make the changes permanent. If no service is given, the root - password will be changed. + Changes a password for a given service. You have to use the + AccountService class to make the changes permanent. If no service is + given, the root password will be changed. """ if not service: self.new_password_root = new_password @@ -230,7 +230,7 @@ class Account: def change_email(self, new_mail): """ - Changes the mail address of an account. You have to use the Ldap class - to make changes permanent. + Changes the mail address of an account. You have to use the + AccountService class to make changes permanent. """ self.mail = new_mail |