使用Zend''Authentication对DB:进行身份验证就足够简单了
$isAuthenticated = $this->getAuthService($db)->getAdapter()
->setIdentity($this->request->getPost('username'))
->setCredential($this->request->getPost('password'))
->authenticate()
->isValid()
;
但是,如果身份验证成功,这只会返回一个布尔值。Zend''Authentication是否提供了一种检查身份是否存在于数据库中并返回此信息的方法?我可以先做一个sql查询,看看它是否在数据库中,但在这样做之前,我想确保该类没有为此提供开箱即用的解决方案。
这样,我可能会收到一条"密码不正确,请重试"的消息,而不是"用户帐户不存在"。或者我可以说,不显示这些信息是一种安全功能,不需要麻烦:P
是的,您有代码。你可以这样使用:
// inside of AuthController / loginAction
$result = $this->auth->authenticate($adapter);
switch ($result->getCode()) {
case Result::FAILURE_IDENTITY_NOT_FOUND:
/** do stuff for nonexistent identity **/
break;
case Result::FAILURE_CREDENTIAL_INVALID:
/** do stuff for invalid credential **/
break;
case Result::SUCCESS:
/** do stuff for successful authentication **/
break;
default:
/** do stuff for other failure **/
break;
}
检查Authentication文档http://framework.zend.com/manual/2.3/en/modules/zend.authentication.intro.html