title: Get user groups in OpenLDAP with JAVA
body: <p>I don't know java that much but I need to change a code. The background here is we have the code working in LDAP which will give us the groups assigned to the user which has logged in. Now, we have to switch to OpenLDAP for some reason and here the problem arises. Here we are not able to get the groups assigned to the user.</p>
  
  <p>Previously I was using to get the groups</p>
  
  <p>The context name is here ou=People,dc=maxcrc,dc=com</p>
  
  <pre><code>NamingEnumeration&lt;SearchResult&gt; search 
      = context.search(contextName, 
              "(sAMAccountName=" + userId + ")", constraints);
  </code></pre>
  
  <p>Now, I have tried various combination like</p>
  
  <pre><code>NamingEnumeration&lt;SearchResult&gt; search 
            = context.search(contextName, 
                     "(uid=" + userId + ")",  constraints);
  </code></pre>
  
  <p>and</p>
  
  <pre><code>NamingEnumeration&lt;SearchResult&gt; search 
          = context.search(contextName, 
                  "(&amp;(objectClass=groupOfNames)(cn=+userId)",  constraints);
  </code></pre>
  
  <p>and others.</p>
  
  <p>The problem is here I am not getting the groups name. So, is there wrong in how I am searching for groups or what I am not getting. Could anyone please help me.</p>
  
  <p>This is our code</p>
  
  <pre><code>public static HashMap getGroupList(
          DirContext context, String userId, String key) 
          throws NamingException, NullArgumentException, 
                  InvalidStringValueException, ParserException {
  
    //setting sonstraints ans searach control to subtree scope
    HashMap groupList = new HashMap();
    SearchControls constraints = new SearchControls();
    constraints.setSearchScope(SearchControls.ONELEVEL_SCOPE);
    constraints.setReturningAttributes(new String[]{"cn", MEMBER_OF_ATTRIBUTE});
  
    String contextName = parser.getConfigNodeValue("contextName");
    logger.debug("Context Name: " + contextName);
    logger.debug("Finding Group List for user ID: " + userId);
    NamingEnumeration&lt;SearchResult&gt; search 
            = context.search(contextName, 
                    SAMAC_COUNT_NAME + userId + CLOSE_BRACKET, constraints);
  
    //searching attribute
    logger.debug("searching attribute");
  
    SearchResult searchResult = null;
  
    String value = "";
    while (search.hasMoreElements()) {
        searchResult = search.next();
        String groupName = searchResult.getAttributes().get(MEMBER_OF_ATTRIBUTE).toString();
        groupList.put(groupName, groupName);
    }
  
    return groupList;
  }
  </code></pre>
  
  <p>EDIT:</p>
  
  <p>here context name is <code>ou=People,dc=maxcrc,dc=com</code> and I have applied the various search filter as <code>(uid=userId)</code>, also <code>(&amp;(objectClass=groupOfNames)(uid=userId))</code>, also <code>(&amp;(objectClass=user)(uid=userId))</code>,  but I am getting nothing out. I need to know how to search in here.</p>
  
  <p>Directory is simple here-</p>
  
  <p>In <strong>dc=maxcrc dc=com</strong> there is <strong>ou=People</strong> 
  and there is a user inside that demo and the demo has is a part of a group. Object class is inetOrgPerson for a user</p>