dongmen9517 2014-04-10 17:13
浏览 38
已采纳

Zend \ Authentication是否提供了检查身份是否存在的方法?

Its simple enough to use Zend\Authentication to authenticate against a DB:

$isAuthenticated = $this->getAuthService($db)->getAdapter()
    ->setIdentity($this->request->getPost('username'))
    ->setCredential($this->request->getPost('password'))
    ->authenticate()
    ->isValid()
    ;

But this just returns a Boolean value if authentication was successful. Does Zend\Authentication offer a way to check if the identity is present in a DB and return this info as well? I could just do an sql query first to see if its in the DB, but before I do that I wanted to be sure there wasn't an in box solution the class offered for this.

This way I could have an "Incorrect password please try again" message vs "User account does not exist" . Or I could just say that not displaying this info is a security feature and not bother :P

  • 写回答

1条回答 默认 最新

  • douxiong4892 2014-04-10 17:29
    关注

    Yes, you have the code. And you can use as this:

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

    Check the Authenticacion document http://framework.zend.com/manual/2.3/en/modules/zend.authentication.intro.html

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?