Question

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');
}
Was it helpful?

Solution

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.

OTHER TIPS

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.

This is my approach and it s working nice: 1-i start by defining an init function in the bootstrap

protected function _initSession()

{

$UserSession = new Zend_Session_Namespace('UserSession');
$UserSession->setExpirationSeconds(/* you may fix a limit */);
Zend_Registry::set('UserSession', $UserSession);

}

/* in the Login action,after correct username & pwd */

// Create session
$UserSession = Zend_Registry::get('UserSession'); 
// Get the user from database or just from fields
//you have to make sure that the psswd is encrypted use MD5 for example..
 $db = Zend_Db_Table::getDefaultAdapter();
$user = $db->fetchRow("SELECT * FROM user_table WHERE user_email = '".$user_email."'");
//then you assign to $user to $UserSession variable : 
$UserSession->user = $user;
//finaly don't forget to unset session variable in the Logout action ...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top