Pages

Ads 468x60px

Wednesday, July 25, 2012

Java: Modify User Attributes in IBM Tivoli Directory Server (TDS) LDAP using JNDI

It will help you to modify user attribute (add, replace, delete) in IBM Tivoli Directory Server (TDS) using JNDI. I used default values, so you have to change it according your requirements. Also the user attributes will depend upon your directory server schema.For connection with IBM Tivoli Directory Server (TDS)/ LDAP visit: http://usmanali112.blogspot.com/2012/07/java-authentication-and-connection-with.html



import javax.naming.ldap.InitialLdapContext;
import javax.naming.directory.*;

//this class perform all LDAP actions for the user
public class LDAPService {

private String base = "c=us"; // base for LDAP
private String realm = "cn=realm1"; // LDAP realm for user
     
// to set user password
public void setPassword(String surname, String password) {

try {
// Set up default values for LDAP information
String distinguish_name = "sn=" + surname + "," + realm + ","
+ base;

// Initialize LDAP Context
InitialLdapContext context = LDAPConnection.InitializeContext();

ModificationItem[] mods = new ModificationItem[1];
Attribute mod0 = new BasicAttribute("userpassword", password);
mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, mod0);
                       //use DirContext.ADD_ATTRIBUTE to add an attribute
//use DirContext.DELETE_ATTRIBUTE to delete an attribute

// Perform modification of user
context.modifyAttributes(distinguish_name, mods);

// Close LDAP Context
LDAPConnection.CloseContext();

} catch (Exception ex) {
ex.getMessage();
}
}
}

No comments:

Post a Comment