views:

68

answers:

3

I am working on a web application which requires the user to login before they see or do anything. No part of this app should be accessible without being logged in. (Except of course, the login controller)

Currently I am using sessions to handle the authentication and I have put code in each controller in the init() function to check if their session is valid.

This was a temporary workaround, but it is redundant and inefficient.

I would like my init() function to be similar to the following, but I am not sure how to achieve it:

public function init()
{
    // If user not logged in redirect to login controller
    $myLibrary = Zend_Library_MyLibrary();
    $myLibrary->CheckAuth();
}

So my question really has two parts:

  1. Where is the best place to store code that will be used in multiple controllers?
  2. How do I then call that function from a controller?

Thanks.

+3  A: 

http://zendframework.com/manual/en/zend.controller.plugins.html

Registering a front controller plugin and hooking into an earlier part of the dispatch process is how I do it.

$front = Zend_Controller_Front::getInstance();
$front->registerPlugin(new Zend_Controller_Plugin_AuthCheck());

Put that in your index.php.

class AuthCheck extends Zend_Controller_Plugin_Abstract {
    public function preDispatch($request){
        // Check Auth

    }
}
Ballsacian1
To add to the explanation, if it's not already clear; whatever you put in `preDispatch` will always be run, so you don't have to directly call it.
manyxcxi
+3  A: 

Code that is reused across multiple controllers is best placed into an ActionHelper. However, for your case, I suggest to write a Controller plugin. Those hook into the Dispatch process at various stages:

public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request)
{
    if(!Zend_Auth::getInstance()->hasIdentity())
    {
        $request->setControllerName('auth');
        $request->setActionName('login');
        // Set the module if you need to as well.
    }
}

The above assumes you are using Zend_Auth to authenticate and manage your user identities.

You want a plugin over a helper, because checking if the user is logged in should happen automatically, without you having to call a checkAuth() method somewhere. Of course, nothing stops you to add an ActionHelper too, e.g.

class My_Helper_CheckAuth extends Zend_Controller_Action_Helper_Abstract
{
    public function checkAuth()
    {
        return Zend_Auth::getInstance()->hasIdentity();
    }
    public function direct()
    {
        return $this->checkAuth();
    }
}

Once you registered your helper in the bootstrap, you could use it in every controller to check if the user is already authenticated:

if ( $this->_helper->checkAuth() === FALSE) {
    // do something
}

Also see these tutorials:

Gordon
There is one part that I do not understand. If authentication is checked during the dispatch using a ControllerPlugin, wouldn't that cause an infinite loop?Example: They try to access a page -> Dispatch detects no auth -> Redirects to the login page -> Dispatch detects no auth -> Redirects to the login page -> etc... etc...PS: There are other instances where an ActionHelper will make my life much easier, thanks for the great response.
Mario
@Mario yes, that would create an infinite loop indeed. You'd either use an ACL to get around this (that would be good other question) or simply add some code to the `dispatchLoopStartup` method, checking whether the current `$request->getAction/ControllerName()` is the login page. Sorry for not pointing it out in the first place.
Gordon
@Gordon: That's what I figured ... I would just have to accommodate for the login page during my authentication checking. Anyway, I can't wait to utilize both the Controller plugin and ActionHelper. Thanks for your all your help.
Mario
A: 

Though, for this particular example your best bet is (probably) to use a Front Controller Plugin, you can also reuse code by extending the Zend_Controller_Action. Below is a contrived example, had you have been using Zend_Auth. This would go in library/Application/Controller and be named Action.php. If you were using a different namespace you would swap the name of the Application directory for that (library/[namespace]/Controller/Action.php) and rename the class accordingly.

class Application_Controller_Action extends Zend_Controller_Action
{
    protected $_loggedIn;
    protected $_username;
    protected $_flashMessenger = null;

    public function init()
    {
        $auth = Zend_Auth::getInstance();
        $this->_loggedIn = $auth->hasIdentity();
        if($this->_loggedIn)
        {
            $user = $auth->getIdentity();
            $this->_username = $this->view->escape($user->username);
        }
        $this->_flashMessenger = $this->_helper->getHelper('FlashMessenger');
        $this->initView();
    }
    ...
}
manyxcxi