PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Results

Zend_Auth adapters return an instance of Zend_Auth_Result with authenticate() in order to represent the results of an authentication attempt. Adapters populate the Zend_Auth_Result object upon construction, so that the following four methods provide a basic set of user-facing operations that are common to the results of Zend_Auth adapters:

  • isValid() - returns TRUE if and only if the result represents a successful authentication attempt

  • getCode() - returns a Zend_Auth_Result constant identifier for determining the type of authentication failure or whether success has occurred. This may be used in situations where the developer wishes to distinguish among several authentication result types. This allows developers to maintain detailed authentication result statistics, for example. Another use of this feature is to provide specific, customized messages to users for usability reasons, though developers are encouraged to consider the risks of providing such detailed reasons to users, instead of a general authentication failure message. For more information, see the notes below.

  • getIdentity() - returns the identity of the authentication attempt

  • getMessages() - returns an array of messages regarding a failed authentication attempt

A developer may wish to branch based on the type of authentication result in order to perform more specific operations. Some operations developers might find useful are locking accounts after too many unsuccessful password attempts, flagging an IP address after too many nonexistent identities are attempted, and providing specific, customized authentication result messages to the user. The following result codes are available:

<?php
Zend_Auth_Result
::SUCCESS
Zend_Auth_Result
::FAILURE
Zend_Auth_Result
::FAILURE_IDENTITY_NOT_FOUND
Zend_Auth_Result
::FAILURE_IDENTITY_AMBIGUOUS
Zend_Auth_Result
::FAILURE_CREDENTIAL_INVALID
Zend_Auth_Result
::FAILURE_UNCATEGORIZED

The following example illustrates how a developer may branch on the result code:

<?php
// inside of AuthController / loginAction
$result $this->_auth->authenticate($adapter);

switch (
$result->getCode()) {

    case 
Zend_Auth_Result::FAILURE_IDENTITY_NOT_FOUND:
        
/** do stuff for nonexistent identity **/
        
break;

    case 
Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID:
        
/** do stuff for invalid credential **/
        
break;

    case 
Zend_Auth_Result::SUCCESS:
        
/** do stuff for successful authentication **/
        
break;

    default:
        
/** do stuff for other failure **/
        
break;
}

Zend Framework