2

プログラムで LDAP から Liferay 6.0.5 にユーザーをインポートしたいと考えています。

提案やサンプルはありますか。

事前の感謝

4

1 に答える 1

1

liferay のソース コードはcom.liferay.portal.security.ldap.PortalLDAPImporterImplで確認できます。これにより、liferay でそれを行う方法についてより良いアイデアが得られる場合があります。

また

カスタム ポートレットで次のコードを試すことができます。コードは非常に基本的なものです (必要な基本的な部分だけを削除して保存したので、コンパイルは行われませんが、ほとんど変更を加えなくても動作するはずです)。

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 に答える