From ed2e2adf3a9ad9099c15abe3b31e9e7439e7ec09 Mon Sep 17 00:00:00 2001
From: "Endi S. Dewata" <edewata@redhat.com>
Date: Wed, 30 Apr 2014 10:55:30 -0400
Subject: [PATCH] Fixed UGSubsystem.getUser().

Previously the getUser() method in UGSubsystem was using findUsers()
which uses a subtree search to find users. It has been replaced with
a base search which is more accurate since the user DN is known. The
code has also been simplified to merge the two cases where the input
parameter could be a user ID or a DN.

Ticket #920
---
 .../com/netscape/cmscore/usrgrp/UGSubsystem.java   | 67 +++++++++++++---------
 1 file changed, 40 insertions(+), 27 deletions(-)

diff --git a/base/server/cmscore/src/com/netscape/cmscore/usrgrp/UGSubsystem.java b/base/server/cmscore/src/com/netscape/cmscore/usrgrp/UGSubsystem.java
index 4eaaa4758c2e297e952726ab4a8202b12efdb1fd..5f898d6e06f3bf19fe53fb18be66644bff6d9cb1 100644
--- a/base/server/cmscore/src/com/netscape/cmscore/usrgrp/UGSubsystem.java
+++ b/base/server/cmscore/src/com/netscape/cmscore/usrgrp/UGSubsystem.java
@@ -190,45 +190,54 @@ public final class UGSubsystem implements IUGSubsystem {
     /**
      * Retrieves a user from LDAP
      */
-    public IUser getUser(String userid) throws EUsrGrpException {
-        if (userid == null) {
+    public IUser getUser(String userID) throws EUsrGrpException {
+
+        if (userID == null) {
             return null;
         }
 
+        String userDN;
+
+        if (userID.indexOf('=') < 0) { // user ID is not a DN
+            userDN = getUserDN(userID);
+
+        } else { // user ID is a DN
+            // TODO: use a separate method for user ID and DN
+            userDN = userID;
+        }
+
         try {
-            if (userid.indexOf('=') == -1) {
-                Enumeration<IUser> e = findUsers(userid);
+            LDAPConnection ldapconn = null;
 
-                if (e != null && e.hasMoreElements()) {
-                    IUser u = e.nextElement();
+            try {
+                ldapconn = getConn();
 
-                    return u;
-                } else {
-                    throw new EUsrGrpException(CMS.getUserMessage("CMS_USRGRP_USER_NOT_FOUND"));
-                }
-            } else {
-                LDAPConnection ldapconn = null;
+                // use base search to find the exact user
+                LDAPSearchResults res = ldapconn.search(
+                        userDN,
+                        LDAPv2.SCOPE_BASE,
+                        "(objectclass=*)",
+                        null,
+                        false);
 
-                try {
-                    ldapconn = getConn();
-                    // read DN
-                    LDAPSearchResults res =
-                            ldapconn.search(userid,
-                                    LDAPv2.SCOPE_SUB, "(objectclass=*)", null, false);
-                    Enumeration<IUser> e = buildUsers(res);
+                // throw EUsrGrpException if result is empty
+                Enumeration<IUser> e = buildUsers(res);
 
-                    if (e.hasMoreElements()) {
-                        return e.nextElement();
-                    }
-                } finally {
-                    if (ldapconn != null)
-                        returnConn(ldapconn);
-                }
+                // user found
+                return e.nextElement();
+
+            } finally {
+                if (ldapconn != null)
+                    returnConn(ldapconn);
             }
+
         } catch (Exception e) {
+            // currently this will catch all exceptions
+            // TODO: catch user not found exception only, rethrow everything else
             log(ILogger.LL_FAILURE, CMS.getLogMessage("CMSCORE_USRGRP_GET_USER", e.toString()));
-            // throws...
         }
+
+        // user not found or other error occurs
         return null;
     }
 
@@ -1902,6 +1911,10 @@ public final class UGSubsystem implements IUGSubsystem {
         return "ou=People," + mBaseDN;
     }
 
+    public String getUserDN(String userID) {
+        return "uid=" + userID + "," + getUserBaseDN();
+    }
+
     /**
      * Retrieves group base dn.
      */
-- 
1.8.4.2

