views:

373

answers:

2

Hi,

So i'm using a simple Zend_Auth mechanism to ensure my users login before they can access certain controllers. My AdminController contains this method

function preDispatch()
{
    $auth = Zend_Auth::getInstance();
    if (!$auth->hasIdentity()) {
        $this->_redirect('auth/login');
    }
}

The user gets redirected to the AuthController, but after a success login when get redirected to default 'index' page for my site, and not the 'admin' page.

function loginAction()
{
    if (strtolower($_SERVER['REQUEST_METHOD']) == 'post') {
        // collect the data from the user
        Zend_Loader::loadClass('Zend_Filter_StripTags');
        $filter = new Zend_Filter_StripTags();
        $username = $filter->filter($this->_request->getPost('username'));
        $password = $filter->filter($this->_request->getPost('password'));

        if (empty($username)) {
            $this->view->message = 'Please provide a username.';
        } else {
            // setup Zend_Auth adapter for a database table
            $dbAdapter = Zend_Db_Table::getDefaultAdapter();

            //Zend_Loader::loadClass('Zend_Auth_Adapter_DbTable');
            $authAdapter = new Zend_Auth_Adapter_DbTable($dbAdapter);
            $authAdapter->setTableName('login');
            $authAdapter->setIdentityColumn('email');
            $authAdapter->setCredentialColumn('password');

            // Set the input credential values to authenticate against
            $authAdapter->setIdentity($username);
            $authAdapter->setCredential($password);

            // do the authentication 
            $auth = Zend_Auth::getInstance();
            $result = $auth->authenticate($authAdapter);
            if ($result->isValid()) {
                // success : store database row to auth's storage system (not the password though!)
                $data = $authAdapter->getResultRowObject(null, 'password');
                $auth->getStorage()->write($data);
                // I THINK I NEED TO CHANGE THIS LINE
                $this->_redirect('/'); 
            } else {
                // failure: clear database row from session
                $this->view->message = 'Login failed.';
            }
        }
    }
    $this->render();   
}

What the correct way in Zend to configure the preDispatch() method so that i can tell the AuthController to redirect to page requested initially?

+1  A: 

You should remember current url in preDispatch and then redirect to that url in your login action. Example:

public function preDispatch()
{
    $requestUri = Zend_Controller_Front::getInstance()->getRequest()->getRequestUri();

    $session = new Zend_Session_Namespace('lastRequest');
    $session->lastRequestUri = requestUri;

    // ...
}

public function loginAction()
{
    // ...

    $session = new Zend_Session_Namespace('lastRequest');
    if (isset($session->lastRequestUri)) {
        $this->_redirect($session->lastRequestUri);
        return;
    }

    // ...
}

You can write your own Zend_Controller_Action_Helper for this purpose. You can also check HTTP_REFERRER, but it seems for me that session is more reliable way to store such data.

Vladimir
Are you going to go for the bonus points and show us the Action_Helper solution? I've never got one working so far.
emeraldjava
+1  A: 

You can start from this one http://pastebin.com/f3472a877 and write your own. Usage:

public function preDispatch()
{
    $this->_helper->lastDecline()->saveRequestUri();

    // redirect to login action
}

public function loginAction()
{
    // authorize user

    // this will redirect us to the saved in preDispatch uri
    // if there is no any saved uris in session we will go to the root "/"
    $this->_helper->lastDecline();
    return;
}
Vladimir