Thursday, November 19, 2009

Authentication with a custom adapter

In this example, we implement a simple authentication with a custom adapter. The user names and passwords are stored inside. And we use the default persistence mechanism.

Components used in this example
Implementation of the authentication adapter
  • User names and passwords are hard coded for the purpose of this example.

class MyAuthAdapter implements Zend_Auth_Adapter_Interface
// User names and passwords are hard coded for the purpose of this example.
private $_userPasswords = array('john' => '123''jane' => '456');
Construction of the adapter
  • The user name and password are passed to the adapter.

    public function __construct($username$password)
// The user name and password are passed to the adapter.
$this->_username $username;
$this->_password $password;
Authentication of the user
  • We check if the user exists. We prepare the error message if not.
  • We check if the password is valid. We prepare the error message if not.
  • We mark the result as successful if the user name and password are valid.
  • We return the authentication result.

    public function authenticate()
        if (!isset(
$this->_userPasswords[$this->_username])) {
// We check if the user exists. We prepare the error message if not.
$result = new Zend_Auth_Result(
'Unknown user!'));
        } else if (
$this->_userPasswords[$this->_username] != $this->_password) {
// We check if the password is valid. We prepare the error message if not.
$result =  new Zend_Auth_Result(
'Bad password!'));
        } else {
// We mark the result as successful if the user name and password are valid.
$result =  new Zend_Auth_Result(

// We return the authentication result.
return $result;


No comments:

Post a Comment