2012-01-18 47 views
0

我有我的登錄表單,LoginForm.class.php:如何保持登錄symfony的

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; 
} 

而且在loginActions:

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. 如何在會話過期後保持登錄狀態?

謝謝

回答

1

及早去apps/[your_application_name]/[your_module_name],在那裏你會看到Actions文件夾和模板文件夾。然後在模塊文件夾(與動作文件夾和模板文件夾並行)中添加名爲config(如果沒有)的文件夾,並在config文件夾內添加一個名爲security.yml的文件。

添加此下面的代碼security.yml文件中的模塊級禁用身份驗證:您要刪除的認證您的應用程序級

all: 
    is_secure: false 

還有你apps/[your_application_name]文件夾內的config文件夾中的情況下,加這下面的代碼在安全yml中apps/[your_application_name]/config/security.yml

default: 
    is_secure: false