Ver código fonte

Closes ZF-6813

Added Zend_Ldap_Auth_Adapter_Ldap::setIdentity() as a proxy to Zend_Ldap_Auth_Adapter_Ldap::setUsername()
Added Zend_Ldap_Auth_Adapter_Ldap::setCredential() as a proxy to Zend_Ldap_Auth_Adapter_Ldap::setPassword()

Added Zend_Ldap_Auth_Adapter_Ldap::getAccountObject() to retrieve the account data for an authenticated user (resembles Zend_Ldap_Auth_Adapter_DbTable::getResultRowObject())

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@17434 44c647ce-9c0f-0410-b52a-842ac1e357ba
sgehrig 16 anos atrás
pai
commit
b1bc06aa2f

+ 69 - 2
library/Zend/Auth/Adapter/Ldap.php

@@ -64,6 +64,13 @@ class Zend_Auth_Adapter_Ldap implements Zend_Auth_Adapter_Interface
     protected $_password = null;
 
     /**
+     * The DN of the authenticated account. Used to retrieve the account entry on request.
+     *
+     * @var string
+     */
+    protected $_authenticatedDn = null;
+
+    /**
      * Constructor
      *
      * @param  array  $options  An array of arrays of Zend_Ldap options
@@ -152,6 +159,36 @@ class Zend_Auth_Adapter_Ldap implements Zend_Auth_Adapter_Interface
     }
 
     /**
+     * setIdentity() - set the identity (username) to be used
+     *
+     * Proxies to {@see setPassword()}
+     *
+     * Closes ZF-6813
+     *
+     * @param  string $identity
+     * @return Zend_Auth_Adapter_Ldap Provides a fluent interface
+     */
+    public function setIdentity($identity)
+    {
+        return $this->setUsername($identity);
+    }
+
+    /**
+     * setCredential() - set the credential (password) value to be used
+     *
+     * Proxies to {@see setPassword()}
+     *
+     * Closes ZF-6813
+     *
+     * @param  string $credential
+     * @return Zend_Auth_Adapter_Ldap Provides a fluent interface
+     */
+    public function setCredential($credential)
+    {
+        return $this->setPassword($credential);
+    }
+
+    /**
      * Returns the LDAP Object
      *
      * @return Zend_Ldap The Zend_Ldap object used to authenticate the credentials
@@ -275,9 +312,11 @@ class Zend_Auth_Adapter_Ldap implements Zend_Auth_Adapter_Interface
                     continue;
                 }
 
-                $canonicalName = $ldap->getCanonicalAccountName($username);
+                $ldap->bind($username, $password);
 
-                $ldap->bind($canonicalName, $password);
+                $canonicalName = $ldap->getCanonicalAccountName($username);
+                $this->_authenticatedDn = $ldap->getCanonicalAccountName($username,
+                    Zend_Ldap::ACCTNAME_FORM_DN);
 
                 $messages[0] = '';
                 $messages[1] = '';
@@ -325,6 +364,34 @@ class Zend_Auth_Adapter_Ldap implements Zend_Auth_Adapter_Interface
     }
 
     /**
+     * getAccountObject() - Returns the result entry as a stdClass object
+     *
+     * This ressembles the feature {@see Zend_Auth_Adapter_DbTable::getResultRowObject()}.
+     * Closes ZF-6813
+     *
+     * @param  array $returnAttribs
+     * @return stdClass|boolean
+     */
+    public function getAccountObject(array $returnAttribs = array())
+    {
+        if (!$this->_authenticatedDn) {
+            return false;
+        }
+
+        $returnObject = new stdClass();
+
+        $entry = $this->getLdap()->getEntry($this->_authenticatedDn, $returnAttribs, true);
+        foreach ($entry as $attr => $value) {
+            if (is_array($value)) {
+                $returnObject->$attr = (count($value) > 1) ? $value : $value[0];
+            } else {
+                $returnObject->$attr = $value;
+            }
+        }
+        return $returnObject;
+    }
+
+    /**
      * Converts options to string
      *
      * @param  array $options

+ 16 - 0
tests/Zend/Auth/Adapter/Ldap/OfflineTest.php

@@ -95,4 +95,20 @@ class Zend_Auth_Adapter_Ldap_OfflineTest extends PHPUnit_Framework_TestCase
                                          ->getPassword();
         $this->assertSame($passwordExpected, $passwordActual);
     }
+
+    public function testSetIdentityProxiesToSetUsername()
+    {
+        $usernameExpected = 'someUsername';
+        $usernameActual = $this->_adapter->setIdentity($usernameExpected)
+                                         ->getUsername();
+        $this->assertSame($usernameExpected, $usernameActual);
+    }
+
+    public function testSetCredentialProxiesToSetPassword()
+    {
+        $passwordExpected = 'somePassword';
+        $passwordActual = $this->_adapter->setCredential($passwordExpected)
+                                         ->getPassword();
+        $this->assertSame($passwordExpected, $passwordActual);
+    }
 }

+ 23 - 1
tests/Zend/Auth/Adapter/Ldap/OnlineTest.php

@@ -64,16 +64,21 @@ class Zend_Auth_Adapter_Ldap_OnlineTest extends PHPUnit_Framework_TestCase
             'password' => TESTS_ZEND_LDAP_PASSWORD,
             'baseDn' => TESTS_ZEND_LDAP_BASE_DN,
         );
-        if (defined('TESTS_ZEND_LDAP_PORT') && TESTS_ZEND_LDAP_PORT != 389)
+        if (defined('TESTS_ZEND_LDAP_PORT'))
             $this->_options['port'] = TESTS_ZEND_LDAP_PORT;
+        if (defined('TESTS_ZEND_LDAP_USE_START_TLS'))
+            $this->_options['useStartTls'] = TESTS_ZEND_LDAP_USE_START_TLS;
         if (defined('TESTS_ZEND_LDAP_USE_SSL'))
             $this->_options['useSsl'] = TESTS_ZEND_LDAP_USE_SSL;
         if (defined('TESTS_ZEND_LDAP_BIND_REQUIRES_DN'))
             $this->_options['bindRequiresDn'] = TESTS_ZEND_LDAP_BIND_REQUIRES_DN;
+        if (defined('TESTS_ZEND_LDAP_ACCOUNT_FILTER_FORMAT'))
+            $this->_options['accountFilterFormat'] = TESTS_ZEND_LDAP_ACCOUNT_FILTER_FORMAT;
         if (defined('TESTS_ZEND_LDAP_ACCOUNT_DOMAIN_NAME'))
             $this->_options['accountDomainName'] = TESTS_ZEND_LDAP_ACCOUNT_DOMAIN_NAME;
         if (defined('TESTS_ZEND_LDAP_ACCOUNT_DOMAIN_NAME_SHORT'))
             $this->_options['accountDomainNameShort'] = TESTS_ZEND_LDAP_ACCOUNT_DOMAIN_NAME_SHORT;
+
         if (defined('TESTS_ZEND_LDAP_ALT_USERNAME')) {
             $this->_names[Zend_Ldap::ACCTNAME_FORM_USERNAME] = TESTS_ZEND_LDAP_ALT_USERNAME;
             if (defined('TESTS_ZEND_LDAP_ACCOUNT_DOMAIN_NAME')) {
@@ -96,6 +101,7 @@ class Zend_Auth_Adapter_Ldap_OnlineTest extends PHPUnit_Framework_TestCase
         );
 
         $result = $adapter->authenticate();
+
         $this->assertTrue($result instanceof Zend_Auth_Result);
         $this->assertTrue($result->isValid());
         $this->assertTrue($result->getCode() == Zend_Auth_Result::SUCCESS);
@@ -171,4 +177,20 @@ class Zend_Auth_Adapter_Ldap_OnlineTest extends PHPUnit_Framework_TestCase
         $messages = $result->getMessages();
         $this->assertContains('not found', $messages[0]);
     }
+
+    public function testAccountObjectRetrieval()
+    {
+        $adapter = new Zend_Auth_Adapter_Ldap(
+            array($this->_options),
+            TESTS_ZEND_LDAP_ALT_USERNAME,
+            TESTS_ZEND_LDAP_ALT_PASSWORD
+        );
+
+        $result = $adapter->authenticate();
+        $account = $adapter->getAccountObject();
+
+        $this->assertTrue($result->isValid());
+        $this->assertType('stdClass', $account);
+        $this->assertEquals(TESTS_ZEND_LDAP_ALT_DN, $account->dn);
+    }
 }