Zend_Auth - When I set a custom storage namespace it does not exist when I redirect to a new controller
In my entry
controller I set:
This works:
$authadapter = new Test_Auth_Adapter_DbTable($db);
$auth = Zend_Auth::getInstance();
$result = $auth->authenticate($authadapter);
$data = $authadapter->getResultRowObject(null, 'password');
$auth->getStorage()->write($data);
$this->_redirector->gotoUrl('/main');
This does not:
$authadapter = new Test_Auth_Adapter_DbTable($db);
$auth = Zend_Auth::getInstance();
$auth->setStorage(new Zend_Auth_Storage_Session('Test_User')); //the only difference
$result = $auth->authenticate($authadapter);
$data = $authadapter->getResultRowObject(null, 'password');
$auth->getStorage()->write($data);
$this->_redirector->gotoUrl('/main');
I can see it set in the $_SESSION var with all of the correct data when I use a debugger but after the data is set and I redirect to the desired destination the $_SESSION 开发者_C百科var is no longer set thus I cannot access things!
The page being redirected to checks auth with:
$this->auth = Zend_Auth::getInstance();
if (!$this->auth->hasIdentity()) {
$this->_redirector->gotoUrl('/entry');
}
Why doesn't this work?
Try this:
$this->auth = Zend_Auth::getInstance();
$this->auth->setStorage(new Zend_Auth_Storage_Session('Test_User'));
if (!$this->auth->hasIdentity()) {
$this->_redirector->gotoUrl('/entry');
}
the problem is if you don't set a storage class, it will default to using Zend_Auth_Storage_Session with the Zend_Auth namespace. And because your session data isn't in that namespace, Zend_Auth doesn't see it and behaves as if the user is not logged in.
Depending on how your application is structured (and how big a part of it the auth is), you might want to do this in a bootstrap method instead so you only have to do it once:
protected function _initAuth()
{
$auth = Zend_Auth::getInstance();
$auth->setStorage(new Zend_Auth_Storage_Session('Test_User'));
return $auth;
}
精彩评论