2012-04-23 7 views

答えて

1

ありがとう

アドバンス、これはあなたの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; 
    } 
} 
+0

ありがとうPrakash – radhakrishna

関連する問題