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

时间:2012-04-23 16:40:55

标签: ldap liferay-6

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

是否有任何建议或样本。

提前谢谢

1 个答案:

答案 0 :(得分: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;
    }
}