mystertyboy
mystertyboy

Reputation: 479

Ldap searchFilter string for not equals to memberOf OU="Google app user" and OU="Contacts"

I have to get the data for the user which are not part of group OU="Google app User" and OU=Contacts I don't have any idea of creating search filter string. code is given below---

public SearchResult getUserInfo(DirContext ctx) throws NamingException{
    SearchResult sourceResult = null;
     // Create the search controls          
    SearchControls searchCtls = new SearchControls(); 

      //Specify the attributes to return 
   //1   String returnedAtts[]={"ou","description"}; // array of the object list  which is returned as the 
                                    //result of the search query on ldap active directory

     String returnedAtts[]={"cn","description","memberOf"};
    searchCtls.setReturningAttributes(returnedAtts); 
      logger.info("Specify the attributes to return ");

      //Specify the search scope 
      searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE); 
      logger.info("Specify the search scope  ");

      //specify the LDAP search filter 
  //1    String searchFilter = "(&(OU=Contacts))"; // search input  variable   require to search the  result   active directory 
      String searchFilter = "(&(OU=Contacts))";// I wanna  create search          filter for all reseult which are not group member of OU="google app user"
  and OU="contacts".          
      logger.info("specify the LDAP search filter  ");

      //Specify the Base for the search 
     //1 String searchBase ="DC=Hellowveen,DC=com";
      String searchBase ="OU=Users,OU=Matriz,OU=Brazil,DC=Hellowveen,DC=com";// "dc=dom,dc=fr";  initial basic search directory
      logger.info("Specify the Base for the search  ");

      //initialize counter to total the results 
      int totalResults = 0; 

      // Search for objects using the filter 
      NamingEnumeration<SearchResult> answer = ctx.search(searchBase, searchFilter, searchCtls); 
      logger.info("Search for objects using the filter  ");

      //Loop through the search results 
      while (answer.hasMoreElements()) 
      { 
        sourceResult = (SearchResult)answer.next(); 

        totalResults++; 

        System.out.println(" get name : >>>" + sourceResult.getName()); 
        System.out.println(" get class name :>>>" +      sourceResult.getClassName()); 
        Attributes attrs = sourceResult.getAttributes(); 
        System.out.println("member Off>>>>" + attrs.get("memberOf")); 

      } 
      logger.info("Loop  END through the search results  ");
      System.out.println("Total results: " + totalResults); 
      ctx.close(); 


    return sourceResult; 
}

How to search the data in active directory using the java on the base's of group(memberOf) . I really appreciate every suggestion solution. Thanks

Upvotes: 2

Views: 2248

Answers (1)

barthel
barthel

Reputation: 950

The LDAP filer search string representation: RFC 2254

Filter all user which are NOT a member of group "Google app User" but a member of group Contacts:

(&(!(memberOf=ou=Google app User))(memberOf=ou=Contacts))

Upvotes: 1

Related Questions