zend-framework - Zend 框架 : How to check an additional column while using DbTable Auth Adapter?

标签 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)。我不知道这是否可以通过适配器完成。如何才能做到这一点?

最佳答案

我也有类似的情况,我扩展了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;
}

关于zend-framework - Zend 框架 : How to check an additional column while using DbTable Auth Adapter?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1871142/

相关文章:

php - Zend 导航 : Where should I load the ACL 'Role' in a private application

php - Zend Auth 锁定 session

php - 使用 Zend 框架的 iPhone Web 服务

zend-framework - Zend 框架 : strange url rewrite behavior

php - 执行此 UPDATE 查询的最佳方法

mysql - 在 zend 中使用动态数据更新查询?

php - Zend_Db 使用多个 AND OR 运算符的复杂 WHERE 子句

zend-framework - 为 Zend_Auth_Adapter_DbTable 添加条件

PHPUnit 白名单代码覆盖率

zend-framework - Zend 框架 : Route assemble and GET parameters