如何以编程方式从LDAP Liferay 6.0.5导入用户

如何以编程方式从LDAP Liferay 6.0.5导入用户,ldap,liferay-6,Ldap,Liferay 6,我想以编程方式将用户从LDAP导入Liferay 6.0.5 是否有任何建议或样品plz Advanced thanx您可以在com.liferay.portal.security.ldap.portalldappimporterImpl中查看liferay的源代码,这可能会让您更好地了解如何在liferay中执行此操作 或 您可以在自定义portlet中尝试以下代码,该代码非常基本(我已删除并仅保留了所需的基本代码,因此它将不编译,但只需很少修改即可工作): import javax.nami

我想以编程方式将用户从LDAP导入Liferay 6.0.5

是否有任何建议或样品plz


Advanced thanx

您可以在com.liferay.portal.security.ldap.portalldappimporterImpl中查看liferay的源代码,这可能会让您更好地了解如何在liferay中执行此操作

您可以在自定义portlet中尝试以下代码,该代码非常基本(我已删除并仅保留了所需的基本代码,因此它将不编译,但只需很少修改即可工作):

import javax.naming.CommunicationException;
import javax.naming.Context;
import javax.naming.NameClassPair;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;

import com.liferay.portal.model.User;

public class MyProgramaticLDAP {

    private static final Properties ENV_PROPS = new Properties();

    static {
        ENV_PROPS.setProperty(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
        ENV_PROPS.setProperty(Context.PROVIDER_URL, "ldap://url.to.my.com:389");
        ENV_PROPS.setProperty(Context.SECURITY_PRINCIPAL, "uid=myuserid,ou=people,dc=myorg,dc=com");
        ENV_PROPS.setProperty(Context.SECURITY_CREDENTIALS, "mypassword");
        ENV_PROPS.setProperty("PROVIDER_PROTOCOL", "ldap"));
        ENV_PROPS.setProperty("PROVIDER_PORT", "389");
        ENV_PROPS.setProperty("PROVIDER_HOST", "192.168.5.234");
        ENV_PROPS.setProperty("LDAP_BASE_URL", "ldap://url.to.my.com:389");
        ENV_PROPS.setProperty("CONTEXT_NAME", "ou=people,dc=myorg,dc=com"));
    }

    public User getLdapUser(String userEmail) throws PortalException,
            SystemException, WebServiceAuthenticationException {

        DirContext ctx = null;
        String userContext = StringPool.BLANK;
        String userName = null;
        NamingEnumeration results = null;

        //liferay user
        User user = new User(); //won't compile

        try {
            // context and specifying LDAP service provider parameters.
            ctx = new InitialDirContext(ENV_PROPS);

            userContext = "uid=" + userEmail + "," + ENV_PROPS.getProperty("CONTEXT_NAME");
            results = ctx.list(ENV_PROPS.getProperty("CONTEXT_NAME"));

            System.out.println("User context: " + userContext);

            Attributes attrs = null;

            while (results.hasMore()) {

                NameClassPair ncp = (NameClassPair) results.next();

                userName = ncp.getName();

                // the attributes for the record retrieved, your attributes may differ based upon the LDAP you use
                System.out.println("Fetching attributes");

                attrs = ctx.getAttributes(userName + "," + ENV_PROPS.getProperty("CONTEXT_NAME"));

                System.out.println("Attribute mail: " + attrs.get("mail").get());           
                System.out.println("Attribute sn: " + attrs.get("sn").get());
                System.out.println("Attribute title: " + attrs.get("title").get());
                System.out.println("Attribute mobile: " + attrs.get("mobile").get());

                System.out.println("Attribute firstname: " + attrs.get("firstname").get());
                user.setFirstName(attrs.get("firstname").get());

                System.out.println("Attribute department: " + attrs.get("department").get());

            }// while ends here

        } catch (CommunicationException cex) {
            cex.printStackTrace();
        } catch (Exception exp) {
            exp.printStacktrace();
        } finally {
            // close connection and other code
        }

        return user;
    }
}