2

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

请问有什么建议或样品吗?

提前谢谢

4

1 回答 1

1

您可以在com.liferay.portal.security.ldap.PortalLDAPImporterImpl中查看 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;
    }
}
于 2012-04-24T09:41:24.343 回答