Esempio n. 1
0
  private Vector getAttribute(String entryDN, String attr) {
    Vector v = new Vector();
    LDAPSearchResults searchResults;
    LDAPEntry entry = null;
    try {
      searchResults = connection.search(entryDN, LDAPConnection.SCOPE_BASE, "", null, false);
      entry = searchResults.next();
    } catch (LDAPException e) {
      // e.printStackTrace();
      return v;
    }

    // System.out.println(entry);
    LDAPAttributeSet attributeSet = entry.getAttributeSet();
    // System.out.println(attributeSet);
    Iterator allAttributes = attributeSet.iterator();

    while (allAttributes.hasNext()) {
      LDAPAttribute attribute = (LDAPAttribute) allAttributes.next();
      String attributeName = attribute.getName();
      if (attributeName.equalsIgnoreCase(attr)) {
        Enumeration en = attribute.getStringValues();
        for (; en.hasMoreElements(); ) {
          v.add(en.nextElement());
        }
      }
    }
    return v;
  }
 /**
  * Executes an ldap search.
  *
  * @param conn to search with
  * @param sr to read properties from
  * @return ldap search queue
  * @throws LDAPException if an error occurs
  */
 protected LDAPSearchQueue search(final LDAPConnection conn, final SearchRequest sr)
     throws LDAPException {
   final LDAPSearchConstraints constraints = getLDAPSearchConstraints(sr);
   final LDAPControl[] lc =
       config.getControlProcessor().processRequestControls(sr.getControls());
   if (lc != null) {
     constraints.setControls(lc);
   }
   return conn.search(
       sr.getBaseDn(),
       getSearchScope(sr.getSearchScope()),
       sr.getSearchFilter() != null ? sr.getSearchFilter().format() : null,
       getReturnAttributes(sr),
       sr.getTypesOnly(),
       (LDAPSearchQueue) null,
       constraints);
 }
Esempio n. 3
0
 public LDAPUser login(String aUserName, String aPassword) throws InvalidLoginException {
   LDAPUser onlineUser = null;
   try {
     connection.connect(ldapHost, ldapPort);
     System.out.println("Connect Successfull");
     System.out.println(aUserName);
     LDAPSearchQueue searchResults =
         connection.search(
             "o=Aerothai",
             LDAPConnection.SCOPE_SUB,
             "cn=" + aUserName,
             new String[] {LDAPConnection.NO_ATTRS},
             true,
             (LDAPSearchQueue) null);
     LDAPMessage message;
     message = searchResults.getResponse();
     if (message instanceof LDAPSearchResult) {
       LDAPEntry entry = ((LDAPSearchResult) message).getEntry();
       String dn = entry.getDN();
       String[] userDn = dn.split(",");
       String fullDn = userDn[0] + "," + userDn[1] + "," + userDn[2] + ",o=Aerothai";
       connection.bind(ldapVersion, fullDn, aPassword.getBytes("UTF8"));
       System.out.println("Bind Successfull");
       onlineUser = new LDAPUser();
       try {
         onlineUser.setFirstName((String) getAttribute(fullDn, "givenName").elementAt(0));
         onlineUser.setLastName((String) getAttribute(fullDn, "sn").elementAt(0));
         try {
           Integer.parseInt((String) getAttribute(fullDn, "cn").elementAt(1));
           onlineUser.setEmployeeCode((String) getAttribute(fullDn, "cn").elementAt(1));
         } catch (NumberFormatException e) {
           onlineUser.setEmployeeCode((String) getAttribute(fullDn, "cn").elementAt(0));
         }
         onlineUser.setDepartment((String) getAttribute(fullDn, "ou").elementAt(0));
         onlineUser.setLocation((userDn[2].split("="))[1]);
       } catch (ArrayIndexOutOfBoundsException e1) {
         e1.printStackTrace();
         onlineUser = null;
         throw new InvalidLoginException(
             "à¡Ô´¤ÇÒÁ¼Ô´¾ÅÒ´ÃÐËÇèÒ§¡Òô֧¢éÍÁÙÅ ¡ÃØ³Ò Login ãËÁèÍÕ¡¤ÃÑé§");
       }
     } else {
       disconnect();
       throw new InvalidLoginException(
           "äÁ辺¼Ùéãªé§Ò¹ª×èÍ "
               + aUserName
               + " ¡ÃسÒÅͧÍÕ¡¤ÃÑé§ ËÃ×Í µÔ´µèÍà¨éÒ˹éÒ·Õè¡Í§Ç¤.¾Ç. à¾×è͵ÃǨÊͺ¢éÍÁÙŢͧ·èÒ¹");
     }
     disconnect();
   } catch (LDAPException e) {
     e.printStackTrace();
     throw new InvalidLoginException(
         "ÃËÑʼèÒ¹äÁè¶Ù¡µéͧ ¡ÃسÒÅͧÍÕ¡¤ÃÑé§ ËÃ×Í µÔ´µèÍà¨éÒ˹éÒ·Õè¡Í§Ç¤.¾Ç. à¾×è͵ÃǨÊͺ¢éÍÁÙŢͧ·èÒ¹");
   } catch (UnsupportedEncodingException e) {
     e.printStackTrace();
     throw new InvalidLoginException(e);
   } finally {
     disconnect();
   }
   return onlineUser;
 }