0

I have a Zend Application with user authentication. I need the UserID in nearly every action for database queries. Certainly I want to have quick access primarily in controllers but also in view scripts.

The way I get so far:

Zend_Auth::getInstance()->getIdentity()->User_ID

How can I write a plugin or helper to provide the UserID in an easier way?

e.g. like this:

$this->User_ID
kapa
  • 77,694
  • 21
  • 158
  • 175
danijar
  • 32,406
  • 45
  • 166
  • 297
  • Why not store your database specifics in you Zend_Db_Table objects in the `insert()` method for example – JF Dion Dec 05 '11 at 19:00
  • I want a short way of accessing the UserID. It's already stored in the Auth object. If I understand your idea is really complicated. – danijar Dec 05 '11 at 19:18
  • not really, if the only place you need to acces the user id, you can put it in all the table classes that need it and let them handle it, this way you won't have to repeat it in every controller. If you really want to make it really short, make a static class like `My_User` which acces the `Zend_Auth` object, with a method for each property you need. You could then call it like `My_User::id` which would run the full `Zend_Auth::getInstance()->getIdentity()->User_ID;` when called – JF Dion Dec 05 '11 at 21:16
  • Static class would be great! Can you give me the first step? – danijar Dec 06 '11 at 09:04

2 Answers2

3

you can make property in your controller which could be set in controller constructor:

$this->_User_ID = Zend_Auth::getInstance()->getIdentity()->User_ID;

or drop it to Zend_Registry when establish and use from there.

bensiu
  • 24,660
  • 56
  • 77
  • 117
  • It would be repeated code because I need the UserID in every controller How should I save the UserID in the registry? In my bootstrap? `Zend_Registry::get('User_ID')` is long, too. – danijar Dec 03 '11 at 16:08
  • Ok, I think it's the best way. Action Helpers and Plugins have other disadvantages. – danijar Dec 05 '11 at 18:33
1

Here is a plugin that will inject the user id into the view, or set it to null if the user has no identity.

<?php

class Application_Plugin_Identity extends Zend_Controller_Plugin_Abstract
{
    public function preDispatch(Zend_Controller_Request_Abstract $request)
    {
        $front     = Zend_Controller_Front::getInstance();
        $bootstrap = $front->getParam('bootstrap');

        $bootstrap->bootstrap('view');
        $view = $bootstrap->getResource('view');

        if (Zend_Auth::getInstance()->hasIdentity()) {
            $view->User_ID = Zend_Auth::getInstance()->getIdentity()->User_ID;
        } else {
            $view->User_ID = null;
        }
    }
}

To enable it you can register it in a bootstrap method like this:

Zend_Controller_Front::getInstance()
->registerPlugin(new Application_Plugin_Identity());

Now from any of your view scripts, you can reference $this->User_ID and it will contain the user's id or null if not logged in.

UPDATE:

An action helper works well if you mostly want to access it from your controller.

From your controller, call $userId = $this->_helper->Identity(); to get the user ID. As a bonus, I have it assign the user id to the view as well so from the view you can call $this->User_ID

<?php

class My_Helper_Identity extends Zend_Controller_Action_Helper_Abstract
{
    protected static $_userId = null;

    public function direct()
    {
        if ($this->_userId == null) {
            $request  = $this->getRequest();
            $view     = $this->getActionController()->view;

            if (Zend_Auth::getInstance()->hasIdentity()) {
                $user_id = Zend_Auth::getInstance()->getIdentity()->User_ID;
            } else {
                $user_id = null;
            }

            $view->User_ID = $user_id;
            $this->_userId = $user_id;
            $this->getActionController()->_userId = $user_id;
        }

        return $this->_userId;
    }
}
drew010
  • 68,777
  • 11
  • 134
  • 162
  • Thanks, and how to call it from a controller? – danijar Dec 03 '11 at 18:24
  • You don't need to call it from a controller, the plugin will run at preDispatch() which happens before your controller action is executed. So your view will automatically have the variable. If you are in a controller, you can reference `$this->view->User_ID` to get the user ID in the controller if that's what you meant. – drew010 Dec 03 '11 at 20:41
  • That is what I meant. But it seems not so elegant cause I almost never need it in the view but in my controller. And `$this->view->User_ID` is too long. ;-) Should be `$this->User_ID` – danijar Dec 03 '11 at 20:51
  • Thanks, great effort :-) Unfortunatly I have to call the helper once per controller to get access to `$this->_userId;` right? – danijar Dec 04 '11 at 08:52
  • If you need access to it you can/have to call it. The advantage of the plugin is that it is automatically executed with each request. The advantage of action helpers is that they are only included and run if you call them. But yes to make it available you still need to call it once per controller/action. Your only other option is to extend a base controller class from Zend_Controller_Action that does this for you and have all of your controllers extend from that class. I think the plugin and action helper is more standard though. – drew010 Dec 04 '11 at 17:40
  • I prefer the plugin because I dont need to call it manually. But is it possible to transmit the UserID to the controller, too? – danijar Dec 05 '11 at 14:04
  • I didn't find a way to assign it to the controller from the plugin. At this point I think you need to extend a class from Zend_Controller_Action, set up the identity variables in the base class init or preDispatch, and then extend all of your controllers from that class. – drew010 Dec 05 '11 at 16:48
  • Ok, thanks for your help. Thats not a solution for me but maybe I find a way with plugin... Or I go with the Registry solution. – danijar Dec 05 '11 at 18:28