Zend Framework:如何在使用DbTable Auth Adapter时检查其他列?

时间:2009-12-09 01:49:15

标签: zend-framework zend-db zend-auth

目前,我正在定期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;
}

但我想检查数据库中的其他列(例如IsActive)。我不知道是否可以使用适配器完成此操作。怎么办呢?

4 个答案:

答案 0 :(得分:2)

我有类似的情况,我扩展了Zend_Auth_Adapter_DbTable以满足我的需求:

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;
    }
}

然后我就这样使用它:

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;
}

答案 1 :(得分:1)

我为Zend_Auth_Adapter_DbTable使用了两列,它看起来像这样:

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

进行身份验证时,SQL最终看起来像这样:

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,对我而言,它运行良好。我不知道这对你有帮助,因为我不知道你要做什么。

答案 2 :(得分:0)

随附的Auth Adapater Zend_Auth_Adapter_DbTable不允许您检查其他列。您可以扩展Zend_Auth_Adapter_DbTable类并添加另一列。您必须为新字段$ _otherFieldValue的值和公共函数setMemberField($ value)添加成员变量。最后,您必须覆盖:

protected function _authenticateCreateSelect()

希望有所帮助。

答案 3 :(得分: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');