title: Active Directory: Move users to different OUs
body: <p>I'm develop an app that needs create and maintain users into the active directory.</p>
  
  <p>My problem is that the users have a hierarchy and the master can create them with different password expiration values. I was reading about that and maybe it's possible do it using OUs , but I can't found some code example about it. </p>
  
  <p>Maybe exist a better way to do the requirement, but unfortunately this is the only way that I found.</p>
  
  <p><strong>Solution (that work for me)</strong></p>
  
  <p>When you are creating the distinguished name (dn), you have to add the the OU into the value. This is the code that I made:</p>
  
  <pre><code>protected String getDN(User user)
  {
    //User name
    String dn = "CN=" + user.getLogin();
  
    //OU
    String ou;
    if (user.getPasswordExpirationTime() == 1)
      ou = "PJ1"; //one day
    else if (usuario.getPasswordExpirationTime() == 30)
      ou = "PJ30"; //thirty days
    else if (usuario.getPasswordExpirationTime() == 60)
      ou = "PJ60"; //sixty days
    else
      ou = "PJ90"; //default, ninety days
  
    dn += ",OU=" + ou;
  
    //Domain
    dn += ",DC=domain,DC=local";
  
    return dn;
  }
  </code></pre>