views:

56

answers:

2

I'm having a difficult time understanding how Zend_Session_Namespace is integrated with Zend_Auth. I have this method I'm using as the action to Authenticate my login page-it is working correctly and redirecting to the /monthly view:

 public function authAction(){
    $request    = $this->getRequest();
    $registry   = Zend_Registry::getInstance();
    $auth       = Zend_Auth::getInstance(); 

    $DB = $registry['DB'];

    $authAdapter = new Zend_Auth_Adapter_DbTable($DB);
    $authAdapter->setTableName('users')
                ->setIdentityColumn('UserName')
                ->setCredentialColumn('Password');    

// Set the input credential values
    $uname = $request->getParam('UserName');
    $paswd = $request->getParam('Password');
    $authAdapter->setIdentity($uname);
    $authAdapter->setCredential($paswd);

   // Perform the authentication query, saving the result
    $result = $auth->authenticate($authAdapter);

       // TRYING TO SET THE NAMESPACE
        $this->session = new Zend_Session_Namspace('UserName');

    if($result->isValid()){
        $data = $authAdapter->getResultRowObject(null,'password');
        $auth->getStorage()->write($data);
        $this->_redirect('/monthly');

    }else{
        $this->_redirect('/login');
    }
}

But I need to be able to store UserName as a Zend_session and call it from monthly controller. I'm not doing things right because I just get a blank screen when I try and do this:

public function indexAction()
{

$this->view->userName = Zend_Session_Namespace('UserName');
}
+1  A: 

Youre not useing the correct namespace. Zend_Auth use the Zend_Auth namespace. The namespace is the structure, not the key for a value. so your session looks something like this:

Array('Zend_Auth' => array ('UserName' => 'myname')

Well thats not accurate exactly because you havent stored the user name unless youve provided for this directly in your adapter. youll need to do something like:

$auth->getStorage()->UserName = 'myusername';

Then you can access with $authData = new Zend_Session_Namespace('Zend_Auth'); $username = $authData->UserName;.

Take a closer look at how the Zend_Auth_Adapter_Db works.

prodigitalson
The user name is un the $uname, so can I do this: '$this->session = new Zend_Session_Namspace($uname);' ? Edit: ah-I see that variable is only the unauthenticaated username.
Joel
Looking closer at your example-I understand for the most part. What I don't understand is with this line : $authData = new Zend_Session_Namespace('Zend_Auth'); do I want that still in the authAction, and then would I also need to put that in monthlyAction? I'm confused about how to pass this session variable from Auth controller to Monthly controller.
Joel
No... if youre using an auth adapter that is the namespace it uses, it will be created by default by your auth adapter. in the index action you jsut need to rehydrate the namespace with `new Zend_Session_Namespace('Zend_Auth')` or if youve otherwise made the auth or session obect available globally (like through the registry) you can pull it form there. I forget what the array keys are it normally stores everythign in. After an auth jsut dump $_SESSION and youll see... i dont remeber if it stores as `identity` or `userid` or what.
prodigitalson
+1  A: 

With the lines:

$data = $authAdapter->getResultRowObject(null,'password');
$auth->getStorage()->write($data);

You're writing all the user's information, except the password, which is OK. Where ever you need to access the logged in user's details, you can do something like (updated as per your comment):

public function indexAction() {
    $auth = Zend_Auth::getInstance();
    if($auth->hasIdentity()) {
        $userData = $auth->getIdentity();
        $this->view->user = $userData;
    }
}

in the view file (index.phtml) just: echo $this->user->firstname

That's it. If one day you decide to change the storage for Zend_Auth from session, to, for example, database, this piece of code will still work.

robertbasic
Well dang-I'm not having any luck with this. I'm thinking I'm not correctly instantiating things in the indexAction of the Monthly controller (that is where the authAction redirects to). WOuld you be willing to post what that indexActin should look like? Does the above code go in the indexAction or in an initAction?
Joel
I've updated the example. You can assign the user's info in (almost) whatever place you want. If you need it only in one action, this will suffice. If you need it in more than one place, a front controller plugin or a view helper would be better.
robertbasic
I seem to be having no luck at all with this. Nothing is passing to the view. I'm also confused-where is the "user->firstname" variable coming from? IE: what is making Zend_Auth think that the userName should be found at user->firstname? I'm not sure if this is part of my problem or what, but I'm just not figuring this out. :( Could the issue also be that authAction is in a different controller than monthlyAction?
Joel
in the `indexAction`, do a `var_dump($userData);` just to see what you have in there. I put `$this->user->firstname` just for an example. All columns from the `users` table, except for the password, should be in the `$userData` variable.
robertbasic
GOT IT!!! Woo hoo! It was as stupid as me echoing 'userName' instead of 'UserName'. Works great now. Thank you!!
Joel
glad it's working ;)
robertbasic