title: Expand AD Group using Scala
body: <p>I am trying to expand a bunch of AD Groups using Scala. Based on the code given here</p>
  
  <p><a href="http://www.thetekblog.com/2010/06/active-directory-with-ldap-retrieving-all-members-of-a-group/" rel="nofollow noreferrer">http://www.thetekblog.com/2010/06/active-directory-with-ldap-retrieving-all-members-of-a-group/</a></p>
  
  <p>I wrote the following code</p>
  
  <pre><code>package com.abhi
  
  import java.util
  import javax.naming.ldap._
  import javax.naming._
  import java.util.Hashtable
  import javax.naming.directory.{SearchControls, SearchResult}
  object LDAPScala extends App {
     val base = "ou=Foo,dc=MYCOMPANY,dc=COM"
     val env = new util.Hashtable[String, String]()
     env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory")
     env.put(Context.SECURITY_AUTHENTICATION, "simple")
     env.put(Context.SECURITY_PRINCIPAL, "foo@mycompany.com")
     env.put(Context.SECURITY_CREDENTIALS, "Bar")
     env.put(Context.PROVIDER_URL, "ldap://ldapserver.mycompany.com:389")
     val groupList = List("Group1", "Group2", "Group3")
  
     try {
        val ctx = new InitialLdapContext(env, null)
        val searchCtls = new SearchControls()
        searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE)
        val attributes = Array("member","memberof")
        searchCtls.setReturningAttributes(attributes);
  
        for {
           group &lt;- groupList
        } {
           val searchFilter = s"(&amp;(objectCategory=group)(name=${group}))"
           val answers = ctx.search(base, searchFilter, searchCtls)
           while(answers.hasMoreElements) {
              val answer = answers.next()
              val attributes = answer.getAttributes.getAll
              while(attributes.hasMore) {
                 val attr = attributes.nextElement()
                 val everyone = attr.getAll
                 while(everyone.hasMore) {
                  val person = everyone.next()
                  println(person)
                 }
              }
           }
        }
     } catch {
        case e : Exception =&gt;
           println(e.getMessage)
           println(e.getStackTrace)
     }
  }
  </code></pre>
  
  <p>This code works and I can see a list of users in each group like this</p>
  
  <pre><code>CN=User1,OU=Users,OU=Accounts,OU=tor,OU=CA,OU=AMER,OU=Regions,DC=FOO,DC=COM
  CN=User2,OU=Users,OU=Accounts,OU=LON,OU=UK,OU=EMEA,OU=Regions,DC=FOO,DC=COM
  CN=User3,OU=Users,OU=Accounts,OU=pla,OU=US,OU=AMER,OU=Regions,DC=FOO,DC=COM
  </code></pre>
  
  <p>Three questions</p>
  
  <ol>
  <li><p>I needed the login IDs (I think they are called samAccountNames). but here the CN contains the  actual names of people not their login ids.</p></li>
  <li><p>Will this give me all the members? I remember that AD had some type of limitation where it will truncate the number of users in the group if there are too many users. </p></li>
  <li><p>I don't know if my code above will work if there are groups within groups.</p></li>
  </ol>