Authentication ZF2身份验证
我正在使用ZF2开发一个应用程序。我已经用用户名和密码进行了用户身份验证。但是,我想检查身份验证中的附加列(例如:status) 我已经完成了以下代码Authentication ZF2身份验证,authentication,zend-framework2,Authentication,Zend Framework2,我正在使用ZF2开发一个应用程序。我已经用用户名和密码进行了用户身份验证。但是,我想检查身份验证中的附加列(例如:status) 我已经完成了以下代码 public function authenticate() { $this->authAdapter = new AuthAdapter($this->dbAdapter, 'usertable', 'username', 'passwor
public function authenticate()
{
$this->authAdapter = new AuthAdapter($this->dbAdapter,
'usertable',
'username',
'password'
);
$this->authAdapter->setIdentity($this->username)
->setCredential($this->password)
->setCredentialTreatment('MD5(?)');
$result = $this->authAdapter->authenticate();
return $result;
}
如何检查身份验证中的“状态”列?
注意:状态值应为1。
谢谢。当我使用zf2和doctrine构建身份验证时,我创建了授权插件并定制了用于传递额外的身份验证列。 你可能需要朝着类似的方向走
$adapter = new AuthAdapter($db,
'users',
'username',
'password',
'MD5(?)'
);
// get select object (by reference)
$select = $adapter->getDbSelect();
$select->where('active = "TRUE"');
// authenticate, this ensures that users.active = TRUE
$adapter->authenticate();
更改后,您的代码应该如下所示
public function authenticate()
{
$this->authAdapter = new AuthAdapter($this->dbAdapter,
'usertable',
'username',
'password'
);
$select = $this->authAdapter->getDbSelect();
$select->where('status= "1"');
$this->authAdapter->setIdentity($this->username)
->setCredential($this->password)
->setCredentialTreatment('MD5(?)');
$result = $this->authAdapter->authenticate();
return $result;
}
由于方法
getresultrowject
,ZF2提供了另一种使用其他列处理额外检查的方法,而不是针对标识和凭证的列。在您的示例中,usertable
的所有列都可以作为getResultRowObject()
返回的对象的属性使用。因此,您可以使用以下内容扩展代码:
if ($result->isValid()) {
$identityRowObject = $this->authAdapter->getResultRowObject();
$status = $identityRowObject->status;
// do whatever complex checking you need with $status...
}
问候,,
Marc@Developer您不是真的使用MD5作为凭证处理,是吗?@Daniel我总是使用凭证处理,但user2003356可能不使用它,上面的答案是基于问题的回答。