Module dhm.ldapObject
LDAP object helper
This python module implements the ldapObject class, which represents
an entry in a LDAP directory. You can use an ldapObject as a normal map,
when you are ready to commit your changes to the LDAP directory the
object will perform the necessary actions itself.
Creating a new entry in a directory
Adding a new entry to a directory is simple: first you create a
ldapObject with the right dn, then you fill it with the necessary data
and finally you commit it to the directory using the modify()
function.
A simple example:
import ldap, ldapObject
# Open a connection to the LDAP directory
db=ldap.open("ldap", 389)
db.simple_bind("user", "password")
# Create a new object
obj=ldapObject.ldapObject("sn=John Doe,ou=People,dc=domain")
obj["objectClass"].append("inetOrgPerson")
obj["sn"]="John Doe"
obj["givenName"]="John"
# Commit changes to the directory
try:
obj.modify(db)
except ldap.LDAPError, e:
print "Error adding entry %s: %s" % (obj.dn, str(e))
Turning LDAP search results into ldapObjects
Since ldapObject aims to be a minimal class it does not have
specific functionality to search in a LDAP directory. However you can
easily convert the results from ldap.search_s into a sequence of
ldapObjects using map().
An example:
import ldap, ldapObject
# Open a connection to the LDAP directory
db=ldap.open("ldap", 389)
db.simple_bind("user", "password")
# Perform a search for all objects with a uid attribute
results=db.search_s("ou=People,dc=domain", ldap.SCOPE_SUBTREE, "uid=*")
results=map(ldapObject.ldapObject, results)
Deriving from ldapObject
You might want to dervice a new class from ldapObject if you want to
add new functions or change the sorting order.
Simple derived class
This example shows a new class that represents a companies
office:
class Office(ldapObject.ldapObject):
"A simple class to represent an office"
ObjectClasses = [ "locality", "organizationalUnit" ]
def __init__(self, init=""):
"Initialize the Office object"
ldapObject.ldapObject.__init__(self, init)
# Add the essential objectClasses
for oc in self.ObjectClases:
if not oc in self.attr["ObjectClass"]:
self.attr["ObjectClass"].append(oc)
# Snapshot the current state of the object
self._snapshot()
def name(self):
'''Return a string with a name for this office. The name is
assembled from the location, state and country.'''
name=[]
if self.attr.has_key("l"):
name.append(self.attr["l"][0])
if self.attr.has_key("st"):
name.append(self.attr["st"][0])
if self.attr.has_key("c"):
name.append(data.Countries[self.attr["c"][0]])
if len(name):
name=", ".join(name)
else:
name=self.attr["ou"]
return name
Changing the sort order
The sorting order for classes can be changed by modifying the
SortOrder variable. SortOder is a list for tuples containing the
attribute to sort on and the direction (one of
ldapObject.SORT_ASCENDING or ldapObject.SORT_DESCENDING).
An example:
class Office(ldapObject.ldapObject):
"A simple class to represent an office"
SortOrder = [
("c", ldapObject.SORT_ASCENDING), # Sort by country first
("st", ldapObject.SORT_ASCENDING), # Then by state
("l", ldapObject.SORT_ASCENDING), # And finally sort by city
]
| Variable Summary |
int |
SORT_ASCENDING: use ascending sort order, used for ldapObject.SortOrder. |
int |
SORT_DESCENDING: use ascending sort order, used for ldapObject.SortOrder. |
SORT_ASCENDING
use ascending sort order, used for ldapObject.SortOrder.
-
- Type:
-
int
- Value:
|
SORT_DESCENDING
use ascending sort order, used for ldapObject.SortOrder.
-
- Type:
-
int
- Value:
|