Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
Merge branch 'master' of git://git.zendframework.com/zf
Browse files Browse the repository at this point in the history
  • Loading branch information
mikaelkael committed Jul 15, 2010
4 parents 932d62d + e16759a + 3997018 + ee3fe36 commit 215d68f
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 19 deletions.
33 changes: 29 additions & 4 deletions src/Adapter/DbTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
Zend\DB\DB,
Zend\DB\Adapter\AbstractAdapter as AbstractDBAdapter,
Zend\DB\Expr as DBExpr,
Zend\DB\Select as DBSelect;
Zend\DB\Select as DBSelect,
Zend\DB\Table\AbstractTable;

/**
* @uses Zend\Authentication\Adapter\Exception
Expand Down Expand Up @@ -123,10 +124,10 @@ class DbTable implements AuthenticationAdapter
* @param string $credentialTreatment
* @return void
*/
public function __construct(AbstractDBAdapter $zendDb, $tableName = null, $identityColumn = null,
public function __construct(AbstractDBAdapter $zendDb = null, $tableName = null, $identityColumn = null,
$credentialColumn = null, $credentialTreatment = null)
{
$this->_zendDb = $zendDb;
$this->_setDbAdapter($zendDb);

if (null !== $tableName) {
$this->setTableName($tableName);
Expand All @@ -145,6 +146,30 @@ public function __construct(AbstractDBAdapter $zendDb, $tableName = null, $ident
}
}

/**
* _setDbAdapter() - set the database adapter to be used for quering
*
* @param Zend_Db_Adapter_Abstract
* @throws Zend_Auth_Adapter_Exception
* @return Zend_Auth_Adapter_DbTable
*/
protected function _setDbAdapter(AbstractDBAdapter $zendDb = null)
{
$this->_zendDb = $zendDb;

/**
* If no adapter is specified, fetch default database adapter.
*/
if(null === $this->_zendDb) {
$this->_zendDb = AbstractTable::getDefaultAdapter();
if (null === $this->_zendDb) {
throw new Exception('No database adapter present');
}
}

return $this;
}

/**
* setTableName() - set the table name to be used in the select query
*
Expand Down Expand Up @@ -303,7 +328,7 @@ public function authenticate()
$dbSelect = $this->_authenticateCreateSelect();
$resultIdentities = $this->_authenticateQuerySelect($dbSelect);

if ( ($authResult = $this->_authenticateValidateResultset($resultIdentities)) instanceof AuthenticationResult) {
if ( ($authResult = $this->_authenticateValidateResultSet($resultIdentities)) instanceof AuthenticationResult) {
return $authResult;
}

Expand Down
30 changes: 15 additions & 15 deletions src/Adapter/Ldap.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ public function setCredential($credential)
public function getLdap()
{
if ($this->_ldap === null) {
$this->_ldap = new \Zend_Ldap();
$this->_ldap = new \Zend\LDAP\LDAP();
}

return $this->_ldap;
Expand All @@ -216,7 +216,7 @@ public function getLdap()
* @param Zend_Ldap $ldap An existing Ldap object
* @return Zend\Authentication\Adapter\Ldap Provides a fluent interface
*/
public function setLdap(\Zend_Ldap $ldap)
public function setLdap(\Zend\LDAP\LDAP $ldap)
{
$this->_ldap = $ldap;

Expand Down Expand Up @@ -321,7 +321,7 @@ public function authenticate()
$ldap->bind();
$requireRebind = true;
}
$dn = $ldap->getCanonicalAccountName($canonicalName, Ldap\Ldap::ACCTNAME_FORM_DN);
$dn = $ldap->getCanonicalAccountName($canonicalName, \Zend\LDAP\LDAP::ACCTNAME_FORM_DN);

$groupResult = $this->_checkGroupMembership($ldap, $canonicalName, $dn, $adapterOptions);
if ($groupResult === true) {
Expand All @@ -339,7 +339,7 @@ public function authenticate()
$messages[1] = $groupResult;
$failedAuthorities[$dname] = $groupResult;
}
} catch (\Zend_Ldap_Exception $zle) {
} catch (\Zend\LDAP\Exception $zle) {

/* LDAP based authentication is notoriously difficult to diagnose. Therefore
* we bend over backwards to capture and record every possible bit of
Expand All @@ -348,18 +348,18 @@ public function authenticate()

$err = $zle->getCode();

if ($err == \Zend_Ldap_Exception::LDAP_X_DOMAIN_MISMATCH) {
if ($err == \Zend\LDAP\Exception::LDAP_X_DOMAIN_MISMATCH) {
/* This error indicates that the domain supplied in the
* username did not match the domains in the server options
* and therefore we should just skip to the next set of
* server options.
*/
continue;
} else if ($err == \Zend_Ldap_Exception::LDAP_NO_SUCH_OBJECT) {
} else if ($err == \Zend\LDAP\Exception::LDAP_NO_SUCH_OBJECT) {
$code = AuthenticationResult::FAILURE_IDENTITY_NOT_FOUND;
$messages[0] = "Account not found: $username";
$failedAuthorities[$dname] = $zle->getMessage();
} else if ($err == \Zend_Ldap_Exception::LDAP_INVALID_CREDENTIALS) {
} else if ($err == \Zend\LDAP\Exception::LDAP_INVALID_CREDENTIALS) {
$code = AuthenticationResult::FAILURE_CREDENTIAL_INVALID;
$messages[0] = 'Invalid credentials';
$failedAuthorities[$dname] = $zle->getMessage();
Expand All @@ -386,12 +386,12 @@ public function authenticate()
* @param array $options
* @return array of auth-adapter specific options
*/
protected function _prepareOptions(\Zend_Ldap $ldap, array $options)
protected function _prepareOptions(\Zend\LDAP\LDAP $ldap, array $options)
{
$adapterOptions = array(
'group' => null,
'groupDn' => $ldap->getBaseDn(),
'groupScope' => \Zend_Ldap::SEARCH_SCOPE_SUB,
'groupScope' => \Zend\LDAP\LDAP::SEARCH_SCOPE_SUB,
'groupAttr' => 'cn',
'groupFilter' => 'objectClass=groupOfUniqueNames',
'memberAttr' => 'uniqueMember',
Expand All @@ -404,8 +404,8 @@ protected function _prepareOptions(\Zend_Ldap $ldap, array $options)
switch ($key) {
case 'groupScope':
$value = (int)$value;
if (in_array($value, array(\Zend_Ldap::SEARCH_SCOPE_BASE,
\Zend_Ldap::SEARCH_SCOPE_ONE, \Zend_Ldap::SEARCH_SCOPE_SUB), true)) {
if (in_array($value, array(\Zend\LDAP\LDAP::SEARCH_SCOPE_BASE,
\Zend\LDAP\LDAP::SEARCH_SCOPE_ONE, \Zend\LDAP\LDAP::SEARCH_SCOPE_SUB), true)) {
$adapterOptions[$key] = $value;
}
break;
Expand All @@ -432,7 +432,7 @@ protected function _prepareOptions(\Zend_Ldap $ldap, array $options)
* @param array $adapterOptions
* @return string|true
*/
protected function _checkGroupMembership(\Zend_Ldap $ldap, $canonicalName, $dn, array $adapterOptions)
protected function _checkGroupMembership(\Zend\LDAP\LDAP $ldap, $canonicalName, $dn, array $adapterOptions)
{
if ($adapterOptions['group'] === null) {
return true;
Expand All @@ -444,9 +444,9 @@ protected function _checkGroupMembership(\Zend_Ldap $ldap, $canonicalName, $dn,
$user = $dn;
}

$groupName = \Zend_Ldap_Filter::equals($adapterOptions['groupAttr'], $adapterOptions['group']);
$membership = \Zend_Ldap_Filter::equals($adapterOptions['memberAttr'], $user);
$group = \Zend_Ldap_Filter::andFilter($groupName, $membership);
$groupName = \Zend\LDAP\Filter\Filter::equals($adapterOptions['groupAttr'], $adapterOptions['group']);
$membership = \Zend\LDAP\Filter\Filter::equals($adapterOptions['memberAttr'], $user);
$group = \Zend\LDAP\Filter\Filter::andFilter($groupName, $membership);
$groupFilter = $adapterOptions['groupFilter'];
if (!empty($groupFilter)) {
$group = $group->addAnd($groupFilter);
Expand Down
45 changes: 45 additions & 0 deletions test/Adapter/DbTableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,51 @@ public function testDbTableAdapterUsesCaseFolding()
$this->_adapter->authenticate();
}


/**
* Test fallback to default database adapter, when no such adapter set
*
* @group ZF-7510
*/
public function testAuthenticateWithDefaultDbAdapterNoAdapterException()
{
$this->setExpectedException('Zend\Authentication\Adapter\Exception', "No database adapter present");

// make sure that no default adapter exists
\Zend\DB\Table\AbstractTable::setDefaultAdapter(null);
$this->_adapter = new Adapter\DbTable();
}

/**
* Test fallback to default database adapter
*
* @group ZF-7510
*/
public function testAuthenticateWithDefaultDbAdapter()
{
// preserve default adapter between cases
$tmp = \Zend\DB\Table\AbstractTable::getDefaultAdapter();

// make sure that default db adapter exists
\Zend\DB\Table\AbstractTable::setDefaultAdapter($this->_db);

// check w/o passing adapter
$this->_adapter = new Adapter\DbTable($this->_db);
$this->_adapter
->setTableName('users')
->setIdentityColumn('username')
->setCredentialColumn('password')
->setTableName('users')
->setIdentity('my_username')
->setCredential('my_password');
$result = $this->_adapter->authenticate();
$this->assertTrue($result->isValid());

// restore adapter
\Zend\DB\Table\AbstractTable::setDefaultAdapter($tmp);
}


protected function _setupDbAdapter($optionalParams = array())
{
$params = array('dbname' => TESTS_ZEND_AUTH_ADAPTER_DBTABLE_PDO_SQLITE_DATABASE);
Expand Down

0 comments on commit 215d68f

Please sign in to comment.