Index: src/edu/ucsb/nceas/metacat/AuthLdap.java
===================================================================
--- src/edu/ucsb/nceas/metacat/AuthLdap.java	(revision 722)
+++ src/edu/ucsb/nceas/metacat/AuthLdap.java	(revision 723)
@@ -40,6 +40,7 @@
 import javax.naming.directory.DirContext;
 import javax.naming.directory.InitialDirContext;
 import javax.naming.directory.SearchResult;
+import javax.naming.directory.SearchControls;
 import javax.naming.NamingEnumeration;
 import javax.naming.NamingException;
 import java.util.Iterator;
@@ -105,13 +106,14 @@
 
     } catch (AuthenticationException ae) {
       util.debugMessage("Error authenticating in AuthLdap.authenticate: " + ae);
-      // NEED TO THROW THE RIGHT EXCEPTION HERE
-      return false;
+      throw new ConnectException(
+      "Error authenticating in AuthLdap.authenticate: " + ae);
     } catch (NamingException e) {
       util.debugMessage("Naming exception while authenticating in " + 
                         "AuthLdap.authenticate: " + e);
-      // NEED TO THROW THE RIGHT EXCEPTION HERE
-      return false;
+      throw new ConnectException(
+      "Naming exception while authenticating in " + 
+                        "AuthLdap.authenticate: " + e);
     }
 
     return authenticated;
@@ -123,8 +125,71 @@
   public String[] getUsers(String user, String password) 
          throws ConnectException
   {
-    // NOT IMPLEMENTED YET
-    return null;
+    MetaCatUtil util = new MetaCatUtil();
+    String ldapUrl = util.getOption("ldapurl");
+    String ldapBase = util.getOption("ldapbase");
+
+    String[] users = null;
+
+    // Identify service provider to use
+    Hashtable env = new Hashtable(11);
+    env.put(Context.INITIAL_CONTEXT_FACTORY, 
+            "com.sun.jndi.ldap.LdapCtxFactory");
+    env.put(Context.PROVIDER_URL, ldapUrl + ldapBase);
+
+    try {
+
+      // Get the dn for this uid or cn 
+      String identifier = getIdentifyingName(user);
+      if (identifier != null) {
+        env.put(Context.SECURITY_AUTHENTICATION, "simple");
+        env.put(Context.SECURITY_PRINCIPAL, identifier + "," + ldapBase);
+        env.put(Context.SECURITY_CREDENTIALS, password);
+
+        // Create the initial directory context
+        DirContext ctx = new InitialDirContext(env);
+
+        // Specify the ids of the attributes to return
+        String[] attrIDs = {"uid"};
+
+        // Search for objects in the current context
+        // All users should be put in subcontext like "ou=Users"
+        // NamingEnumeration answer = ctx.search("ou=Users", null, attrIDs);
+        NamingEnumeration enum = ctx.search("", null, attrIDs);
+
+        // Print the users
+        Vector uvec = new Vector();
+        while (enum.hasMore()) {
+          SearchResult sr = (SearchResult)enum.next();
+          Attributes attrs = sr.getAttributes();
+          NamingEnumeration enum1 = attrs.getAll(); // only "uid" attr
+          while (enum1.hasMore()) {
+            Attribute attr = (Attribute)enum1.next();
+            System.out.print(attr.getID() + "=");
+            System.out.println((String)attr.get());
+            uvec.add(attr.get());
+          }
+        }
+
+        // initialize users[]; fill users[]
+        users = new String[uvec.size()];
+        for (int i=0; i < uvec.size(); i++) {
+          users[i] = (String)uvec.elementAt(i); 
+        }
+
+        // Close the context when we're done
+        ctx.close();
+      } else {
+        util.debugMessage("User not found!");
+      }
+
+    } catch (NamingException e) {
+      System.err.println("Problem getting users in AuthLdap.getUsers:" + e);
+      throw new ConnectException(
+      "Problem getting groups in AuthLdap.getUsers:" + e);
+    }
+
+    return users;
   }
 
   /**
@@ -133,8 +198,76 @@
   public String[] getUsers(String user, String password, String group) 
          throws ConnectException
   {
-    // NOT IMPLEMENTED YET
-    return null;
+    MetaCatUtil util = new MetaCatUtil();
+    String ldapUrl = util.getOption("ldapurl");
+    String ldapBase = util.getOption("ldapbase");
+
+    String[] users = null;
+
+    // Identify service provider to use
+    Hashtable env = new Hashtable(11);
+    env.put(Context.INITIAL_CONTEXT_FACTORY, 
+            "com.sun.jndi.ldap.LdapCtxFactory");
+    env.put(Context.PROVIDER_URL, ldapUrl + ldapBase);
+
+    try {
+
+      // Get the dn for this uid or cn 
+      String identifier = getIdentifyingName(user);
+      if (identifier != null) {
+        env.put(Context.SECURITY_AUTHENTICATION, "simple");
+        env.put(Context.SECURITY_PRINCIPAL, identifier + "," + ldapBase);
+        env.put(Context.SECURITY_CREDENTIALS, password);
+
+        // Create the initial directory context
+        DirContext ctx = new InitialDirContext(env);
+
+        // Specify the ids of the attributes to return
+        String[] attrIDs = {"uid"};
+
+        // Specify the attributes to match.
+        // Ask for objects that have the attribute gid == @group.
+        Attributes matchAttrs = new BasicAttributes(true); // ignore case
+        matchAttrs.put(new BasicAttribute("gid", group));
+
+        // Search for objects in the current context
+        // All users should be put in subcontext like "ou=Users"
+        // NamingEnumeration answer=ctx.search("ou=Users",matchAttrs,attrIDs);
+        NamingEnumeration enum = ctx.search("", matchAttrs, attrIDs);
+
+        // Print the users
+        Vector uvec = new Vector();
+        while (enum.hasMore()) {
+          SearchResult sr = (SearchResult)enum.next();
+          Attributes attrs = sr.getAttributes();
+          NamingEnumeration enum1 = attrs.getAll(); // only "uid" attr
+          while (enum1.hasMore()) {
+            Attribute attr = (Attribute)enum1.next();
+            System.out.print(attr.getID() + "=");
+            System.out.println((String)attr.get());
+            uvec.add(attr.get());
+          }
+        }
+
+        // initialize users[]; fill users[]
+        users = new String[uvec.size()];
+        for (int i=0; i < uvec.size(); i++) {
+          users[i] = (String)uvec.elementAt(i); 
+        }
+
+        // Close the context when we're done
+        ctx.close();
+      } else {
+        util.debugMessage("User not found!");
+      }
+
+    } catch (NamingException e) {
+      System.err.println("Problem getting users in AuthLdap.getUsers:" + e);
+      throw new ConnectException(
+      "Problem getting groups in AuthLdap.getUsers:" + e);
+    }
+
+    return users;
   }
 
   /**
@@ -143,8 +276,78 @@
   public String[] getGroups(String user, String password) 
          throws ConnectException
   {
-    // NOT IMPLEMENTED YET
-    return null;
+    MetaCatUtil util = new MetaCatUtil();
+    String ldapUrl = util.getOption("ldapurl");
+    String ldapBase = util.getOption("ldapbase");
+
+    String[] groups = null;
+
+    // Identify service provider to use
+    Hashtable env = new Hashtable(11);
+    env.put(Context.INITIAL_CONTEXT_FACTORY, 
+            "com.sun.jndi.ldap.LdapCtxFactory");
+    env.put(Context.PROVIDER_URL, ldapUrl + ldapBase);
+
+    try {
+
+      // Get the dn for this uid or cn 
+      String identifier = getIdentifyingName(user);
+      if (identifier != null) {
+        env.put(Context.SECURITY_AUTHENTICATION, "simple");
+        env.put(Context.SECURITY_PRINCIPAL, identifier + "," + ldapBase);
+        env.put(Context.SECURITY_CREDENTIALS, password);
+
+        // Create the initial directory context
+        DirContext ctx = new InitialDirContext(env);
+
+        // Specify the ids of the attributes to return
+        String[] attrIDs = {"gid"};
+        SearchControls ctls = new SearchControls();
+        ctls.setReturningAttributes(attrIDs);
+
+        // Specify the search filter to match.
+        // Ask for objects that do not have "uid".
+        // This will assure that the directory entries are groups.
+        String filter = "(!(uid=*))";
+          
+        // Search for objects in the current context
+        // All users should be put in subcontext like "ou=Users"
+        // NamingEnumeration answer = ctx.search("ou=Users", filter,ctls);
+        NamingEnumeration enum = ctx.search("", filter, ctls);
+
+        // Print the users
+        Vector uvec = new Vector();
+        while (enum.hasMore()) {
+          SearchResult sr = (SearchResult)enum.next();
+          Attributes attrs = sr.getAttributes();
+          NamingEnumeration enum1 = attrs.getAll(); // only "gid" attr
+          while (enum1.hasMore()) {
+            Attribute attr = (Attribute)enum1.next();
+            System.out.print(attr.getID() + "=");
+            System.out.println((String)attr.get());
+            uvec.add(attr.get());
+          }
+        }
+
+        // initialize groups[] and fill it
+        groups = new String[uvec.size()];
+        for (int i=0; i < uvec.size(); i++) {
+          groups[i] = (String)uvec.elementAt(i); 
+        }
+
+        // Close the context when we're done
+        ctx.close();
+      } else {
+        util.debugMessage("User not found!");
+      }
+
+    } catch (NamingException e) {
+      System.err.println("Problem getting groups in AuthLdap.getGroups:" + e);
+      throw new ConnectException(
+      "Problem getting groups in AuthLdap.getGroups:" + e);
+    }
+
+    return groups;
   }
 
   /**
@@ -153,8 +356,76 @@
   public String[] getGroups(String user, String password, String foruser) 
          throws ConnectException
   {
-    // NOT IMPLEMENTED YET
-    return null;
+    MetaCatUtil util = new MetaCatUtil();
+    String ldapUrl = util.getOption("ldapurl");
+    String ldapBase = util.getOption("ldapbase");
+
+    String[] groups = null;
+
+    // Identify service provider to use
+    Hashtable env = new Hashtable(11);
+    env.put(Context.INITIAL_CONTEXT_FACTORY, 
+            "com.sun.jndi.ldap.LdapCtxFactory");
+    env.put(Context.PROVIDER_URL, ldapUrl + ldapBase);
+
+    try {
+
+      // Get the dn for this uid or cn 
+      String identifier = getIdentifyingName(user);
+      if (identifier != null) {
+        env.put(Context.SECURITY_AUTHENTICATION, "simple");
+        env.put(Context.SECURITY_PRINCIPAL, identifier + "," + ldapBase);
+        env.put(Context.SECURITY_CREDENTIALS, password);
+
+        // Create the initial directory context
+        DirContext ctx = new InitialDirContext(env);
+
+        // Specify the ids of the attributes to return
+        String[] attrIDs = {"gid"};
+
+        // Specify the attributes to match.
+        // Ask for objects that have the attribute uid == @foruser.
+        Attributes matchAttrs = new BasicAttributes(true); // ignore case
+        matchAttrs.put(new BasicAttribute("uid", foruser));
+
+        // Search for objects in the current context
+        // All users should be put in subcontext like "ou=Users"
+        // NamingEnumeration answer=ctx.search("ou=Users",matchAttrs,attrIDs);
+        NamingEnumeration enum = ctx.search("", matchAttrs, attrIDs);
+
+        // Print the users
+        Vector uvec = new Vector();
+        while (enum.hasMore()) {
+          SearchResult sr = (SearchResult)enum.next();
+          Attributes attrs = sr.getAttributes();
+          NamingEnumeration enum1 = attrs.getAll(); // only "gid" attr
+          while (enum1.hasMore()) {
+            Attribute attr = (Attribute)enum1.next();
+            System.out.print(attr.getID() + "=");
+            System.out.println((String)attr.get());
+            uvec.add(attr.get());
+          }
+        }
+
+        // initialize groups[] and fill it
+        groups = new String[uvec.size()];
+        for (int i=0; i < uvec.size(); i++) {
+          groups[i] = (String)uvec.elementAt(i); 
+        }
+
+        // Close the context when we're done
+        ctx.close();
+      } else {
+        util.debugMessage("User not found!");
+      }
+
+    } catch (NamingException e) {
+      System.err.println("Problem getting groups in AuthLdap.getGroups:" + e);
+      throw new ConnectException(
+      "Problem getting groups in AuthLdap.getGroups:" + e);
+    }
+
+    return groups;
   }
 
   /**
@@ -193,24 +464,25 @@
     env.put(Context.PROVIDER_URL, ldapUrl + ldapBase);
 
     // Authentication information
-    if ((user != null) && (password != null)) {
-      String identifier = getIdentifyingName(user);
-      env.put(Context.SECURITY_AUTHENTICATION, "simple");
-      env.put(Context.SECURITY_PRINCIPAL, identifier + "," + ldapBase);
-      env.put(Context.SECURITY_CREDENTIALS, password);
-    }
  
     try {
   
-      // Find out the identifying attribute for the user
-      String userident = getIdentifyingName(foruser);
+      if ((user != null) && (password != null)) {
+        String identifier = getIdentifyingName(user);
+        env.put(Context.SECURITY_AUTHENTICATION, "simple");
+        env.put(Context.SECURITY_PRINCIPAL, identifier + "," + ldapBase);
+        env.put(Context.SECURITY_CREDENTIALS, password);
+      }
 
       // Create the initial directory context
       DirContext ctx = new InitialDirContext(env);
         
+      // Find out the identifying attribute for the user
+      String userident = getIdentifyingName(foruser);
+
       // Ask for all attributes of the user 
       Attributes attrs = ctx.getAttributes(userident);
-  
+ 
       // Print all of the attributes
       NamingEnumeration en = attrs.getAll();
       while (en.hasMore()) {
@@ -230,7 +502,8 @@
     } catch (NamingException e) {
       System.err.println("Problem getting attributes in AuthLdap.getAttributes:" 
                           + e);
-      // NEED TO THROW THE RIGHT EXCEPTION HERE
+      throw new ConnectException(
+      "Problem getting attributes in AuthLdap.getAttributes:" + e);
     }
 
     return attributes;
@@ -246,7 +519,7 @@
    *          or null if not found
    */
   private String getIdentifyingName(String user) 
-         throws ConnectException
+         throws NamingException
   {
     MetaCatUtil util = new MetaCatUtil();
     String ldapUrl = util.getOption("ldapurl");
@@ -300,8 +573,8 @@
     } catch (NamingException e) {
       util.debugMessage("Naming exception while getting dn: " + e);
       System.out.println("Naming exception in AuthLdap.getIdentifyingName");
-      // NEED TO THROW THE RIGHT EXCEPTION HERE
-      return null;
+      throw new NamingException(
+      "Naming exception in AuthLdap.getIdentifyingName: " + e);
     }
 
     return identifier;
@@ -327,7 +600,7 @@
       } else {
         System.out.println("Authentication failed for: " + user);
       }
-
+/*
       if (isValid) {
         HashMap userInfo = authservice.getAttributes(user, password, user);
 
@@ -342,8 +615,26 @@
             System.out.println(att + ": " + value);
           }
         }
+
       }
-
+*/
+      // get the whole list of users
+      if (isValid) {
+        String[] users = authservice.getUsers(user, password);
+        for (int i=0; i < users.length; i++) {
+          System.out.println(users[i]);          
+        }
+      }
+/*
+      // get the whole list of users for a group
+      if (isValid) {
+        String group = args[2];
+        String[] users = authservice.getUsers(user, password, group);
+        for (int i=0; i < users.length; i++) {
+          System.out.println(users[i]);          
+        }
+      }
+*/
     } catch (ConnectException ce) {
       System.err.println("Error connecting to LDAP server in authldap.main");
     }
