|
Home > Archive > WebSphere Portal Server > July 2006 > Connecting to an LDAP from a portlet
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Connecting to an LDAP from a portlet
|
|
|
| Hi!
How do I connect to a LDAP directory from a portlet? I want to build a portlet to administrat a LDAP directory, I need to be able to search, update, create and delete in the directory.
I'm a novice when it comes to LDAP but I have built a couple of portlets that connects to DB2 using JNDI in WAS. But I can't find any information on connecting to LDAP anywhere... Could someone please point me to the right location?
Cheers!
| |
|
| Hi,
There are 2 options for you, to connect directly to LDAP server or to use the puma service API deployed within your portal serve.
I've used the direct connection to LDAP only when I worked outside the portal e.g. from WAS or plan Java code, but when I worked on portal I used the puma service.
The starting steps would be like this
import com.ibm.portal.puma.Group;
import com.ibm.portal.puma.User;
import com.ibm.wps.command.CommandException;
import com.ibm.wps.command.puma.CreateCNCommand;
import com.ibm.wps.command.puma.EnrollUserCommand;
import com.ibm.wps.command.puma.GetUserAttributesCommand;
import com.ibm.wps.command.puma.UpdateUserCommand;
import com.ibm.wps.command.um.UpdateGroupCommand;
import com.ibm.wps.puma.GroupManager;
import com.ibm.wps.puma.Principal;
import com.ibm.wps.puma.UserManager;
then you can create a User object to represent the user and you can
add user to LDAP
UserManager.instance().getPumaService().enroll(aUser);
UserManager.instance().getPumaService().sync(aUser);
update user in LDAP by first getting the user and then call settAttribute(xx)
theUser = (com.ibm.wps.puma.User)UserManager.instance().findById(uid);
delete the user
UserManager.instance().getPumaService().delete(aUser);
**Note that these APIs works with portal even if it wasn't configured to use LDAP server, it always communicates with the user registry of your portl whatever it was. But when portal is configured with LDAP then it could be a good way (or the only way) to
manage your LDAP users from the code.
And you need these JARS (already exist in <portal>/share/app) wp.user.xx
Good Luck
Best Regards
matouk
| |
|
| matouk,
Ok, tanks for the tip about PUMA. But should I use PUMA if I want to connectg to a LDAP outside the portal?
Is there any way of creating a JNDI resource in the deployment descriptor? Thats th way I have done for JDBC connections.
Could I use JNDI like this:
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://localhost:389");
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, "cn=Directory Manager");
env.put(Context.SECURITY_CREDENTIALS, "password");
DirContext ctx = new InitialDirContext(env);
Cheers!
|
|
|
|
|