give everyone a working code snippet to play off of, here, just because this
was a nightmare for me:
in my indexController:
function loginAction()
{
$salt = "abchefghjkmnpqrstuvwxyz0123456789";
$registry = Zend_Registry::getInstance();
$configuration = $registry->configuration;
require_once 'default/forms/LoginForm.php'; //Include
the form
$form = new LoginForm();
//Create a new object of the form class
if ($this->_request->isPost())
//Check for empty form
{
$formData = $this->_request->getPost();
if ($form->isValid($formData)) //Check
for valid inputs
{
$email=$formData['user_email'];
$password=md5($salt.$formData["user_password"]);
$db = Zend_Db::factory($configuration->database); //Connection to
the database
$authAdapter = new Zend_Auth_Adapter_DbTable($db);
$authAdapter->setTableName('users');
$authAdapter->setIdentityColumn('user_email');
$authAdapter->setCredentialColumn('user_password'); //Verify
table values with form values
$authAdapter->setIdentity($email);
$authAdapter->setCredential($password);
$auth = Zend_Auth::getInstance();
$result = $auth->authenticate($authAdapter); //Authenticate
using the adapter
if ($result->isValid())
{
// store the identity as an object where only the username and
// real_name have been returned
Zend_Session::setOptions($configuration->session->toArray());
Zend_Session::start ();
$storage = $auth->getStorage();
//Store all user table data except password as session variables
$storage->write($authAdapter->getResultRowObject(null, 'password'));
$this->view->user = Zend_Auth::getInstance()->getIdentity();
$this->view->title = 'Welcome';
$this->_helper->layout->setLayout('dynamiclayout');
$this->_helper->redirector('index');
//$this->_helper->redirector->gotoUrl('/project/project/index');
}
else
{
$this->view->message="sorry,login failed";
$this->_helper->layout->setLayout('login');
$this->view->form = $form;
$form->populate($formData);
return $this->render('login');
}
}
} else {
$this->_helper->layout->setLayout('login');
$this->view->form = $form;
}
}
my base controller:
class Athena_Controller_Action extends Zend_Controller_Action
{
function init()
{
parent::init();
$this->_helper->actionStack('setnav', 'menu', 'system');
}
public function preDispatch()
{
if (Zend_Auth::getInstance()->hasIdentity()) {
// If the user is logged in, we extend his session except in the
case of logout
if ('logout' != $this->getRequest()->getActionName()) {
$registry = Zend_Registry::getInstance();
$configuration = $registry->configuration;
$authSession = new Zend_Session_Namespace('Zend_Auth');
$authSession->setExpirationSeconds($configuration->session->remember_me_seconds);
}
} else {
if (!((($this->getRequest()->getActionName() == 'index' ||
$this->getRequest()->getActionName() == 'login')
&& $this->getRequest()->getControllerName() == 'index')
|| (($this->getRequest()->getActionName() ==
'registration-submitted' ||
$this->getRequest()->getActionName() == 'register')
&& $this->getRequest()->getControllerName() == 'management'
&& $this->getRequest()->getModuleName() == 'user'))) {
$this->_helper->redirector('default', 'index', 'index'); // back to
login page
}
}
}
}
and in my app.ini, I have a line:
session.remember_me_seconds=7200
My problem, I think, was that I wasn't using 'Zend_Auth' as the namespace of
the session. The preDispatch method basically resets the
remember_me_seconds any time the user does anything (including AJAX calls)
and it redirects to the login page otherwise. It's a pretty simple code
example, but I think it's pretty powerful. The reason I didn't use a Plugin
helper for this was because not all my controllers inherit from my base
controller, so I couldn't make it universal like that.
I'd love to see any feedback, and hopefully this example helps people avoid
the problems that I had.
Thanks again, Cory, for your help.
Cheers,
Kevin
--
View this message in context: http://www.nabble.com/Configuring-Session-in-Zend_Auth-tp22652278p22861415.html
Sent from the Zend Auth mailing list archive at Nabble.com.
没有评论:
发表评论