From 06b521335fbe2a64d9d0840fedd8bc2fdd52e535 Mon Sep 17 00:00:00 2001
From: neil_a_wilson <neil_a_wilson@localhost>
Date: Thu, 16 Aug 2007 21:37:56 +0000
Subject: [PATCH] Add a new convenience constructor for the InternalClientConnection class that allows you to create a new internal client connection authenticated as a given user by providing only that user's DN (as opposed to having to create an AuthenticationInfo object for the user in order to use the other constructor).

---
 opends/tests/unit-tests-testng/src/server/org/opends/server/core/TestModifyDNOperation.java                          |    6 +--
 opends/tests/unit-tests-testng/src/server/org/opends/server/core/GroupManagerTestCase.java                           |   12 ++----
 opends/tests/unit-tests-testng/src/server/org/opends/server/protocols/internal/InternalClientConnectionTestCase.java |    5 ++
 opends/src/messages/messages/protocol.properties                                                                     |    1 
 opends/tests/unit-tests-testng/src/server/org/opends/server/extensions/WhoAmIExtendedOperationTestCase.java          |    6 ++-
 opends/src/server/org/opends/server/protocols/internal/InternalClientConnection.java                                 |   61 ++++++++++++++++++++++++++++++
 opends/tests/unit-tests-testng/src/server/org/opends/server/core/CompareOperationTestCase.java                       |    6 +--
 7 files changed, 78 insertions(+), 19 deletions(-)

diff --git a/opends/src/messages/messages/protocol.properties b/opends/src/messages/messages/protocol.properties
index 57a21cb..5e048f2 100644
--- a/opends/src/messages/messages/protocol.properties
+++ b/opends/src/messages/messages/protocol.properties
@@ -1404,3 +1404,4 @@
 MILD_ERR_JMX_INSUFFICIENT_PRIVILEGES_439=You do not have sufficient \
  privileges to establish the connection through JMX. At least JMX_READ \
  privilege is required
+MILD_ERR_INTERNALCONN_NO_SUCH_USER_440=User %s does not exist in the directory
diff --git a/opends/src/server/org/opends/server/protocols/internal/InternalClientConnection.java b/opends/src/server/org/opends/server/protocols/internal/InternalClientConnection.java
index fe888a4..d90aaca 100644
--- a/opends/src/server/org/opends/server/protocols/internal/InternalClientConnection.java
+++ b/opends/src/server/org/opends/server/protocols/internal/InternalClientConnection.java
@@ -311,6 +311,67 @@
 
 
   /**
+   * Creates a new internal client connection that will be
+   * authenticated as the specified user.
+   *
+   * @param  userDN  The DN of the entry to use as the
+   *                 authentication and authorization identity.
+   *
+   * @throws  DirectoryException  If a problem occurs while trying to
+   *                              get the entry for the provided user
+   *                              DN.
+   */
+  public InternalClientConnection(DN userDN)
+         throws DirectoryException
+  {
+    this(getAuthInfoForDN(userDN));
+  }
+
+
+
+  /**
+   * Creates an authentication information object for the user with
+   * the specified DN.
+   *
+   * @param  userDN  The DN of the user for whom to create an
+   *                 authentication information object.
+   *
+   * @return  The appropriate authentication information object.
+   *
+   * @throws  DirectoryException  If a problem occurs while trying to
+   *                              create the authentication
+   *                              information object, or there is no
+   *                              such user in the directory.
+   */
+  private static AuthenticationInfo getAuthInfoForDN(DN userDN)
+          throws DirectoryException
+  {
+    if ((userDN == null) || userDN.isNullDN())
+    {
+      return new AuthenticationInfo();
+    }
+
+    DN rootUserDN = DirectoryServer.getActualRootBindDN(userDN);
+    if (rootUserDN != null)
+    {
+      userDN = rootUserDN;
+    }
+
+    Entry userEntry = DirectoryServer.getEntry(userDN);
+    if (userEntry == null)
+    {
+      Message m =
+           ERR_INTERNALCONN_NO_SUCH_USER.get(String.valueOf(userDN));
+      throw new DirectoryException(ResultCode.NO_SUCH_OBJECT, m);
+    }
+
+    boolean isRoot = DirectoryServer.isRootDN(userDN);
+    return new AuthenticationInfo(userEntry, isRoot);
+  }
+
+
+
+  /**
    * Retrieves a shared internal client connection that is
    * authenticated as a root user.
    *
diff --git a/opends/tests/unit-tests-testng/src/server/org/opends/server/core/CompareOperationTestCase.java b/opends/tests/unit-tests-testng/src/server/org/opends/server/core/CompareOperationTestCase.java
index d6ad1d6..90350a4 100644
--- a/opends/tests/unit-tests-testng/src/server/org/opends/server/core/CompareOperationTestCase.java
+++ b/opends/tests/unit-tests-testng/src/server/org/opends/server/core/CompareOperationTestCase.java
@@ -123,10 +123,8 @@
          "ds-privilege-name: bypass-acl",
          "ds-privilege-name: proxied-auth");
 
-    Entry proxyUserEntry =
-               DirectoryServer.getEntry(DN.decode("uid=proxy.user,o=test"));
-    AuthenticationInfo authInfo = new AuthenticationInfo(proxyUserEntry, false);
-    proxyUserConn = new InternalClientConnection(authInfo);
+    proxyUserConn =
+         new InternalClientConnection(DN.decode("uid=proxy.user,o=test"));
   }
 
 
diff --git a/opends/tests/unit-tests-testng/src/server/org/opends/server/core/GroupManagerTestCase.java b/opends/tests/unit-tests-testng/src/server/org/opends/server/core/GroupManagerTestCase.java
index 8473b4d..01a7716 100644
--- a/opends/tests/unit-tests-testng/src/server/org/opends/server/core/GroupManagerTestCase.java
+++ b/opends/tests/unit-tests-testng/src/server/org/opends/server/core/GroupManagerTestCase.java
@@ -1203,8 +1203,7 @@
 
     // Get a client connection authenticated as user1 and make sure it handles
     // group operations correctly.
-    AuthenticationInfo authInfo = new AuthenticationInfo();
-    InternalClientConnection conn0 = new InternalClientConnection(authInfo);
+    InternalClientConnection conn0 = new InternalClientConnection(DN.nullDN());
     InternalSearchOperation searchOperation =
          new InternalSearchOperation(conn0, conn0.nextOperationID(),
                   conn0.nextMessageID(), null, DN.nullDN(),
@@ -1230,8 +1229,7 @@
 
     // Get a client connection authenticated as user1 and make sure it handles
     // group operations correctly.
-    authInfo = new AuthenticationInfo(DirectoryServer.getEntry(user1DN), false);
-    InternalClientConnection conn1 = new InternalClientConnection(authInfo);
+    InternalClientConnection conn1 = new InternalClientConnection(user1DN);
     searchOperation =
          new InternalSearchOperation(conn1, conn1.nextOperationID(),
                   conn1.nextMessageID(), null, DN.nullDN(),
@@ -1261,8 +1259,7 @@
 
     // Get a client connection authenticated as user2 and make sure it handles
     // group operations correctly.
-    authInfo = new AuthenticationInfo(DirectoryServer.getEntry(user2DN), false);
-    InternalClientConnection conn2 = new InternalClientConnection(authInfo);
+    InternalClientConnection conn2 = new InternalClientConnection(user2DN);
     searchOperation =
          new InternalSearchOperation(conn2, conn2.nextOperationID(),
                   conn2.nextMessageID(), null, DN.nullDN(),
@@ -1292,8 +1289,7 @@
 
     // Get a client connection authenticated as user3 and make sure it handles
     // group operations correctly.
-    authInfo = new AuthenticationInfo(DirectoryServer.getEntry(user3DN), false);
-    InternalClientConnection conn3 = new InternalClientConnection(authInfo);
+    InternalClientConnection conn3 = new InternalClientConnection(user3DN);
     searchOperation =
          new InternalSearchOperation(conn3, conn3.nextOperationID(),
                   conn3.nextMessageID(), null, DN.nullDN(),
diff --git a/opends/tests/unit-tests-testng/src/server/org/opends/server/core/TestModifyDNOperation.java b/opends/tests/unit-tests-testng/src/server/org/opends/server/core/TestModifyDNOperation.java
index 55fc448..884e9e8 100644
--- a/opends/tests/unit-tests-testng/src/server/org/opends/server/core/TestModifyDNOperation.java
+++ b/opends/tests/unit-tests-testng/src/server/org/opends/server/core/TestModifyDNOperation.java
@@ -153,10 +153,8 @@
          "ds-privilege-name: bypass-acl",
          "ds-privilege-name: proxied-auth");
 
-    Entry proxyUserEntry =
-               DirectoryServer.getEntry(DN.decode("uid=proxy.user,o=test"));
-    AuthenticationInfo authInfo = new AuthenticationInfo(proxyUserEntry, false);
-    proxyUserConn = new InternalClientConnection(authInfo);
+    proxyUserConn =
+         new InternalClientConnection(DN.decode("uid=proxy.user,o=test"));
   }
 
   /**
diff --git a/opends/tests/unit-tests-testng/src/server/org/opends/server/extensions/WhoAmIExtendedOperationTestCase.java b/opends/tests/unit-tests-testng/src/server/org/opends/server/extensions/WhoAmIExtendedOperationTestCase.java
index a025686..eda7234 100644
--- a/opends/tests/unit-tests-testng/src/server/org/opends/server/extensions/WhoAmIExtendedOperationTestCase.java
+++ b/opends/tests/unit-tests-testng/src/server/org/opends/server/extensions/WhoAmIExtendedOperationTestCase.java
@@ -104,12 +104,14 @@
   /**
    * Tests the use of the Who Am I? extended operation with an internal
    * unauthenticated connection.
+   *
+   * @throws  Exception  If an unexpected problem occurs.
    */
   @Test()
   public void testAsInternalAnonymous()
+         throws Exception
   {
-    InternalClientConnection conn =
-         new InternalClientConnection(new AuthenticationInfo());
+    InternalClientConnection conn = new InternalClientConnection(DN.nullDN());
     ExtendedOperation extOp =
          conn.processExtendedOperation(OID_WHO_AM_I_REQUEST, null);
     assertEquals(extOp.getResultCode(), ResultCode.SUCCESS);
diff --git a/opends/tests/unit-tests-testng/src/server/org/opends/server/protocols/internal/InternalClientConnectionTestCase.java b/opends/tests/unit-tests-testng/src/server/org/opends/server/protocols/internal/InternalClientConnectionTestCase.java
index f833255..62250cb 100644
--- a/opends/tests/unit-tests-testng/src/server/org/opends/server/protocols/internal/InternalClientConnectionTestCase.java
+++ b/opends/tests/unit-tests-testng/src/server/org/opends/server/protocols/internal/InternalClientConnectionTestCase.java
@@ -136,7 +136,10 @@
       new Object[] { new InternalClientConnection(
            new AuthenticationInfo(dmEntry, true)) },
       new Object[] { new InternalClientConnection(
-           new AuthenticationInfo(userEntry, false)) }
+           new AuthenticationInfo(userEntry, false)) },
+      new Object[] { new InternalClientConnection(dmDN) },
+      new Object[] { new InternalClientConnection(DN.nullDN()) },
+      new Object[] { new InternalClientConnection((DN) null) }
     };
   }
 

--
Gitblit v1.10.0