Improved error handling in LDAP module.

This commit is contained in:
Michael Spang 2007-02-15 01:44:56 -05:00
parent 7a37d80e3e
commit 4a4342c8d7
1 changed files with 32 additions and 12 deletions

View File

@ -66,7 +66,7 @@ class LDAPConnection(object):
# open the connection
self.ldap = ldap.initialize(server)
# authenticate as ceo
# authenticate
self.ldap.simple_bind_s(bind_dn, bind_pw)
except ldap.LDAPError, e:
@ -109,6 +109,8 @@ class LDAPConnection(object):
None of the dn does not exist in the directory
"""
if not self.connected(): raise LDAPException("Not connected!")
# search for the specified dn
try:
matches = self.ldap.search_s(dn, ldap.SCOPE_BASE)
@ -144,8 +146,6 @@ class LDAPConnection(object):
{ 'uid': 'mspang', 'uidNumber': 21292 ...}
"""
if not self.connected(): raise LDAPException("Not connected!")
dn = 'uid=' + uid + ',' + self.user_base
return self.lookup(dn)
@ -160,6 +160,8 @@ class LDAPConnection(object):
Returns: the list of uids matched (usernames)
"""
if not self.connected(): raise LDAPException("Not connected!")
# search for entries that match the filter
try:
matches = self.ldap.search_s(self.user_base, ldap.SCOPE_SUBTREE, search_filter)
@ -239,6 +241,8 @@ class LDAPConnection(object):
'Michael Spang,,,')
"""
if not self.connected(): raise LDAPException("Not connected!")
dn = 'uid=' + uid + ',' + self.user_base
attrs = {
'objectClass': [ 'top', 'account', 'posixAccount', 'shadowAccount' ],
@ -277,6 +281,8 @@ class LDAPConnection(object):
connection.user_modify('mspang', user)
"""
if not self.connected(): raise LDAPException("Not connected!")
# distinguished name of the entry to modify
dn = 'uid=' + uid + ',' + self.user_base
@ -302,6 +308,8 @@ class LDAPConnection(object):
Example: connection.user_delete('mspang')
"""
if not self.connected(): raise LDAPException("Not connected!")
try:
dn = 'uid=' + uid + ',' + self.user_base
self.ldap.delete_s(dn)
@ -329,7 +337,7 @@ class LDAPConnection(object):
"""
dn = 'cn=' + cn + ',' + self.group_base
return self.lookup(dn)
return self.lookup(dn, 'posixGroup')
def group_search_id(self, gidNumber):
@ -341,6 +349,8 @@ class LDAPConnection(object):
Example: connection.group_search_id(1001) -> ['office']
"""
if not self.connected(): raise LDAPException("Not connected!")
# search for posixAccount entries with the specified uidNumber
try:
search_filter = '(&(objectClass=posixGroup)(gidNumber=%d))' % gidNumber
@ -375,6 +385,8 @@ class LDAPConnection(object):
Example: connection.group_add('office', 1001, 'Office Staff')
"""
if not self.connected(): raise LDAPException("Not connected!")
dn = 'cn=' + cn + ',' + self.group_base
attrs = {
'objectClass': [ 'top', 'posixGroup' ],
@ -409,6 +421,8 @@ class LDAPConnection(object):
connection.group_modify('office', group)
"""
if not self.connected(): raise LDAPException("Not connected!")
# distinguished name of the entry to modify
dn = 'cn=' + cn + ',' + self.group_base
@ -434,6 +448,8 @@ class LDAPConnection(object):
Example: connection.group_delete('office')
"""
if not self.connected(): raise LDAPException("Not connected!")
try:
dn = 'cn=' + cn + ',' + self.group_base
self.ldap.delete_s(dn)
@ -456,6 +472,8 @@ class LDAPConnection(object):
Example: connection.used_uids(20000, 40000) -> [20000, 20001, ...]
"""
if not self.connected(): raise LDAPException("Not connected!")
try:
users = self.ldap.search_s(self.user_base, ldap.SCOPE_SUBTREE, '(objectClass=posixAccount)', ['uidNumber'])
except ldap.LDAPError, e:
@ -484,6 +502,8 @@ class LDAPConnection(object):
Example: connection.used_gids(20000, 40000) -> [20000, 20001, ...]
"""
if not self.connected(): raise LDAPException("Not connected!")
try:
users = self.ldap.search_s(self.user_base, ldap.SCOPE_SUBTREE, '(objectClass=posixAccount)', ['gidNumber'])
except ldap.LDAPError, e: