У меня есть форма для входа, LoginForm.class.php:
public function configure()
{
$this->setWidgets(array(
'username' => new sfWidgetFormInput(array(), array('style' => 'width:130px;')),
'password' => new sfWidgetFormInputPassword(array(), array('style' => 'width:130px;'))
));
$this->setValidators(array(
'username' => new sfValidatorString(array('required' => TRUE), array('required' => 'Please provide your username.')),
'password' => new sfValidatorString(array('required' => TRUE), array('required' => 'Please provide your password.'))
));
$this->validatorSchema->setPostValidator(new sfValidatorCallback(array('callback' => array($this, 'checkUserdata'))));
$this->widgetSchema->setNameFormat('user[%s]');
}
public function checkUserdata($validator, $values)
{
if ($values['username'] && $values['password'])
{
$currUser = sfContext::getInstance()->getRequest()->getParameter('user');
$oUser = Doctrine_Core::getTable('user')->findOneByUsernameAndPassword($values['username'], md5($values['password']));
if($oUser)
{
if($oUser->getSuspend() == 0)
{
//Previous credentials are removed
if(sfContext::getInstance()->getUser()->isAuthenticated())
{
sfContext::getInstance()->getUser()->getAttributeHolder()->removeNamespace('ns_user');
sfContext::getInstance()->getUser()->setAuthenticated(false);
sfContext::getInstance()->getUser()->clearCredentials();
}
//This new user is authenticated
sfContext::getInstance()->getUser()->setAuthenticated(true);
//All info about the user is stored into a session variable
sfContext::getInstance()->getUser()->setAttribute('id', $oUser->getId(), 'ns_user');
sfContext::getInstance()->getUser()->setAttribute('username', $oUser->getUsername(), 'ns_user');
sfContext::getInstance()->getUser()->setAttribute('name', $oUser->getName(), 'ns_user');
sfContext::getInstance()->getUser()->setAttribute('type', $oUser->getType(), 'ns_user');
//credentials are set
sfContext::getInstance()->getUser()->addCredential('user');
}
else
{
throw new sfValidatorError($validator, 'This user is suspended. Please activate before login.');
}
}
else
{
throw new sfValidatorError($validator, 'Wrong username or password.');
}
}
return $values;
}
и в логин Действия:
public function executeIndex(sfRequest $request)
{
$this->setTitle('Authentication');
$this->form = new LoginForm(); //A new form object is created
if($request->isMethod('post')) //It checks if it comes from Post
{
$this->form->bind($request->getParameter('user'));
$user = $request->getParameter('user');
if($this->form->isValid()) //If form validation is ok
{
if(($user['username'] == "admin" && $user['password'] == "admin") || $this->getUser()->getAttribute('type','','ns_user') == 'admin')
return $this->redirect('admin/index');
else
return $this->redirect('home/index');
}
}
}
public function executeLogout($request)
{
//Authentication data is removed
if($this->getUser()->isAuthenticated())
{
$this->getUser()->getAttributeHolder()->removeNamespace('ns_user');
$this->getUser()->setAuthenticated(false);
$this->getUser()->clearCredentials();
}
return $this->redirect('login/index');
}
Если сеанс входа в систему истек, и я нажимаю любую кнопку, появляется исключение - Ошибка 404.
Как сохранить вход в систему после истечения сеанса?
Спасибо