Zend Framework: how to check additional column when using DbTable Auth adapter?

I am currently getting the usual DbTable Auth Adapter:

protected function _getAuthAdapter($formData)
{       
    $dbAdapter = Zend_Db_Table::getDefaultAdapter();
    $authAdapter = new Zend_Auth_Adapter_DbTable($dbAdapter);
    $authAdapter->setTableName('users')
        ->setIdentityColumn('username')
        ->setCredentialColumn('password');
    $authAdapter->setIdentity($formData['username']);
    $authAdapter->setCredential(md5($formData['password']));
    return $authAdapter;
}

But I want to check an additional column in the database (for example, IsActive). I do not know if this can be done with an adapter. How can I do that?

+5
source share
4 answers

I have a similar situation, and I expanded Zend_Auth_Adapter_DbTableto meet my needs:

class My_Auth_Adapter_DbTable extends Zend_Auth_Adapter_DbTable
{
    protected $_customerIdColumn = 'customer_id';
    protected $_customerId = false;

    public function setCustomerId($id) {
        $this->_customerId = $id;
        return $this;
    }

    public function getCustomerId() {
        return ($this->_customerId !== false) ? $this->_customerId : '';
    }

    public function _authenticateCreateSelect() {
        $dbSelect = parent::_authenticateCreateSelect();
        $dbSelect->where($this->_zendDb->quoteIdentifier($this->_customerIdColumn, true) . ' = ?',
            $this->getCustomerId());
        return $dbSelect;
    }
}

Then I use it as follows:

public function getAuthAdapter(array $params)
{
    $authAdapter = new My_Auth_Adapter_DbTable(
        $params['db_adapter'],
        'users',
        'username',
        'password',
        '? AND active = 1'
    );

    $authAdapter->setIdentity($params['username'])
        ->setCustomerId($params['customer_id'])
        ->setCredential($params['password']);

    return $authAdapter;
}
+2
source

I use two columns for my Zend_Auth_Adapter_DbTable, and it looks like this:

$authAdapter = new Zend_Auth_Adapter_DbTable(
  Zend_Registry::get('database'),
  "user",
  "username",
  "password_hash", // column 1
  "MD5( CONCAT(?,password_salt) )" // column 2
);

When authenticating, SQL ends up as follows:

SELECT `user`.*, 
(CASE WHEN `password_hash` = MD5( CONCAT('password entered',password_salt) )
  THEN 1 ELSE 0 END) AS `zend_auth_credential_match`
FROM `user` WHERE (`username` = 'username entered')

, password_salt, . , , , .

+1

Auth Adapater Zend_Auth_Adapter_DbTable . Zend_Auth_Adapter_DbTable . - $_otherFieldValue setMemberField ($ value). , :

protected function _authenticateCreateSelect()

, .

0

, , , getDbSelect() where:

    $authAdapter = new Zend_Auth_Adapter_DbTable(Model_Users::getDefaultAdapter());

    $authAdapter->setTableName('users');
    $authAdapter->setIdentityColumn('email');
    $authAdapter->setCredentialColumn('password')->setCredentialTreatment('md5(?)');
    $authAdapter->setIdentity($email);
    $authAdapter->setCredential($password);
    $authAdapter->getDbSelect()->where('active = 1');
0

All Articles