Demo – Zend Auth and Zend ACL (Part 2)

So far, we have a working ZF application that displays the login page. Note that the form action points to /auth/identify. So, control is transferred to the identify Action in the AuthController class once the submit button is clicked.

public function identifyAction()
    {
        $errors=array();
        $request = $this->getRequest();
        if ($request->isPost())
        {
            $username = $request->getPost(‘username’);
            $password = $request->getPost(‘password’);
                $authAdapter = $this->_getAuthAdapter($username,$password);
                $auth=Zend_Auth::getInstance();
                $result = $auth->authenticate($authAdapter);
                if ($result->isValid())
                {
                    //success.. store row for future use..
                    $data = $authAdapter->getResultRowObject();
                    $auth->getStorage()->write($data);
                    $this->_redirect(‘/’);
                }
                else
                {
                    echo "Login Failed.. Please retry";
                }
        }
        $this->_redirect(‘/auth/login’);
    }

Note the call to $this->_getAuthAdapter($username,$password). This is where the Zend Auth Adapter is initialized and put to use. This function takes the entered username and password, checks for invalid input, then uses the Zend_Auth library to verify the user and transfer control appropriately

private function _getAuthAdapter($username, $password)
    {
        $dbAdapter = self::$db;
        $authAdapter = new Zend_Auth_Adapter_DbTable($dbAdapter,’users’,’username’,’password’);
        $authAdapter->setIdentity($username);
        $authAdapter->setCredential($password);
        return $authAdapter;
    }

The function getResultRowObject() returns the entire row corresponding to the username that is authenticated in the users table. This is stored in the session for later retrieval (Note that the user_type column is also stored along with the user and is used subsequently for authorization purposes).

Go ahead and fire up the login action and ensure that you see the whole process (Login form to Index page on successful auth and login form to login form on unsuccessful auth)

There is actually a small problem with the above form.. The “Login Failed..” message is never really displayed on the screen. This is because of the redirect command. It clears the output buffer before transferring control. We will now fix this with appropriate error messages:

Modify the login.phtml form to include error messages

<h1>Login</h1>
<div id="error"> <?php if (isset($this->errors[‘auth’])) echo $this->errors[‘auth’]?> </div>
<p>Please log in here</p>
<form method="post" action='<?php echo $this->baseUrl?>/auth/identify’>
<div>
<label>Username</label>
<input type="text" name="username" vale=""/>
<div id="error"> <?php if (isset($this->errors[‘username’])) echo $this->errors[‘username’]?> </div>
</div>
<div>
<label>Password</label>
<input type="password" name="password" value="" />
</div>
<div>
<input type="submit" name="login" value="Login" />
</div>
</form>

Also modify the identify action to store the appropriate error messages and finally store it in the view:

public function identifyAction()
    {
        $errors=array();
        $request = $this->getRequest();
        if ($request->isPost())
        {
            $username = $request->getPost(‘username’);
            $password = $request->getPost(‘password’);
            if (strlen($username)==0)
               $errors[‘username’]=’Username is required’;
            if (count($errors)==0)
            {
                //no errors with form.. process
                $authAdapter = $this->_getAuthAdapter($username,$password);
                $auth=Zend_Auth::getInstance();
                $result = $auth->authenticate($authAdapter);
                if ($result->isValid())
                {
                    //success.. store row
                    $data = $authAdapter->getResultRowObject();
                    $auth->getStorage()->write($data);
                    $this->_redirect(‘/’);
                }
                else
                {
                    $errors[‘auth’]= "Login Failed.. Please retry";
                }
            }
        }
        $this->view->errors=$errors;
        $this->forward(‘login’);
    }

There.. now you have a fully functional authentication framework. The next part will focus on Ajax-ing the error messages.

Click here to go over to the next part in this series…

Advertisements

2 thoughts on “Demo – Zend Auth and Zend ACL (Part 2)

  1. Hi Shankar,

    I have been reading your blogs for Zend Framework, It’s very useful and thanks a ton!!

    In this Zend Auth & Zend ACL I don’t think you have discussed anything for Zend ACL.

    If you have did apart from thing. Could you please share with me.

    Thanks,
    Kannan

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s