Novell Home

Sample Code: Using the LDAP Connection Class

Novell Cool Solutions: Feature

Digg This - Slashdot This

Posted: 22 Jun 2005
 

This snippet shows a sample application that uses the LDAP Connection class along with the DN and CN classes, in a module called ldapconn. This module is used to perform some simple LDAP operations within the test application. You can also find this sample code at:
http://forge.novell.com/modules/xfref_library/detail.php?reference_id=204

Sample Code

import ldap
import ldapconn
              
h = "127.0.0.1"
p = 389
user = "cn=admin,o=novell"
pwd = "novell"
scope = "sub"

johndoe_CN = ldapconn.CN( "cn=jdoe,ou=lynxlab,ou=testusers,o=users" )
janedoe_CN = ldapconn.CN( "cn=janedoe,ou=lynxlab,ou=testusers,o=users" )

def main():
    ldc = ldapconn.LDAPConnection( h, p, user, pwd, scope)
    if not ldc.TestConnection( "o=users", "objectclass=posixAccount" ):
        print "Unable to make connection to eDirectory.\n"
        return 1
    else:
        print "Connection to eDirectory successful.\n"
    for entry in ldc.GetAllUserObjects( "o=users", "objectclass=posixAccount" ):
        print entry[0]
        
    attrs = []
    attrs.append( ("CN",johndoe_CN.toString()) )
    attrs.append( ("Surname","Doe") )
    attrs.append( ("objectClass","inetOrgPerson") )
    attrs.append( ("objectClass","posixAccount") )
    attrs.append( ("homeDirectory","/home/jdoe") )
    attrs.append( ("gidNumber","1234") )
    attrs.append( ("uidNumber","1234") )
    attrs.append( ("uid","jdoe") )
    attrs.append( ("Language","ENGLISH") )
    attrs.append( ("passwordAllowChange","TRUE") )
    ldc.CreateObject( johndoe_CN, attrs )
    
    found = False
    for entry in ldc.GetAllUserObjects( "o=users", "objectclass=posixAccount" ):
        if entry[0] == johndoe_CN.toString():
            found = True
            break
    if not found:
        print "Failed to create new user object for John Doe.\n"
    else:
        print "User object for John Doe added successfully.\n"
        ldc.DeleteObject( johndoe_CN )
        
    found = False
    for entry in ldc.GetAllUserObjects( "o=users", "objectclass=posixAccount" ):
        if entry[0] == johndoe_CN.toString():
            found = True
            break
    if found:
        print "Failed to delete user object for John Doe.\n"
    else:
        print "User object for John Doe deleted successfully.\n"
        
    ldc.CreateObject( janedoe_CN, attrs )
    ldc.RenameObject( janedoe_CN, johndoe_CN )
    found = False
    for entry in ldc.GetAllUserObjects( "o=users", "objectclass=posixAccount" ):
        if entry[0] == johndoe_CN.toString():
            found = True
            break
    if not found:
        print "Failed to rename user object for Jane Doe as John Doe.\n"
        ldc.DeleteObject( johndoe_CN )
    else:
        print "User object for Jane Doe successfully renamed to John Doe.\n"
        ldc.DeleteObject( johndoe_CN )
        
    ldc.CreateObject( johndoe_CN, attrs )
    ldc.AddAttributes( johndoe_CN, [("instantMessagingID","johndoe")] )
    found = False
    for entry in ldc.GetAllUserObjects( "o=users", "objectclass=posixAccount" ):
        if entry[0] == johndoe_CN.toString():
            for type,value in entry[1].items():
                if type == "instantMessagingID" and value[0] == "johndoe":
                    found = True
                    break
            break
    if not found:
        print "Failed to add attribute \"instantMessagingID\" to user object.\n"
        ldc.DeleteObject( johndoe_CN )
    else:
        print "Attribute \"instantMessagingID\" added to user object successfully.\n"
        ldc.ModifyAttributes( johndoe_CN, [("instantMessagingID","littlejohn")] )
        found = False
        for entry in ldc.GetAllUserObjects( "o=users", "objectclass=posixAccount" ):
            if entry[0] == johndoe_CN.toString():
                for type,value in entry[1].items():
                    if type == "instantMessagingID" and value[0] == "littlejohn":
                        found = True
                break
        if not found:
            print "Failed to modify attribute \"instantMessagingID\" in user object.\n"
            ldc.DeleteObject( johndoe_CN )
        else:
            print "Attribute \"instantMessagingID\" modified successfully.\n"
            ldc.DeleteAttributes( johndoe_CN, [("instantMessagingID",None)] )
            found = False
            for entry in ldc.GetAllUserObjects( "o=users", "objectclass=posixAccount" ):
                if entry[0] == johndoe_CN.toString():
                    for type,value in entry[1].items():
                        if type == "instantMessagingID":
                            found = True
                            break
                    break
            if found:
                print "Failed to delete attribute \"instantMessagingID\" from user object.\n"
                ldc.DeleteObject( johndoe_CN )
            else:
                print "Attribute \"instantMessagingID\" deleted successfully.\n"
                ldc.ModifyObject( johndoe_CN, [(ldap.MOD_ADD,"instantMessagingID","johndoe"),(ldap.MOD_ADD,"instantMessagingID","littlejohn")] )
                found = False
                for entry in ldc.GetAllUserObjects( "o=users", "objectclass=posixAccount" ):
                    if entry[0] == johndoe_CN.toString():
                        for type,value in entry[1].items():
                            if type == "instantMessagingID":
                                if value[0] == "johndoe" and value[1] == "littlejohn":
                                    found = True
                                elif value[0] == "littlejohn" and value[1] == "johndoe":
                                    found = True
                                break
                        break
                if not found:
                    print "Failed to add new attributes for \"instantMessagingID\" via ModifyObject.\n"
                else:
                    print "New attributes for \"instantMessagingID\" added via ModifyObject successfully.\n"
    ldc.DeleteObject( johndoe_CN )

if __name__ == '__main__':
    main()


Novell Cool Solutions (corporate web communities) are produced by WebWise Solutions. www.webwiseone.com

© 2014 Novell