2011-11-30 311 views
0

我在我的users_controller中有以下內容,因爲執行登錄需要進一步的邏輯,但是在CakePHP中完成默認驗證會停止,因此我試圖強制它驗證用戶名和密碼在進行帳戶檢查之前,請先填寫字段一切正常,但這。我已經嘗試添加兩個if語句(下面)來檢查用戶名/密碼是否爲空,但是隻要頁面加載它們爲空並且驗證框顯示。CakePHP登錄用戶名/密碼驗證

我很難爲如何實現這一目標。任何幫助將不勝感激。

function login() { 
    if($this->Auth->user()) { 
     $this->redirect(array('controller'=>'shows')); 
    } 
    if(empty($this->data['User']['username'])) { 
     $this->User->validationErrors['username'] = "Please enter a username"; 
    } 
    if(empty($this->data['User']['password'])) { 
     $this->User->validationErrors['password'] = "Please enter a password"; 
    } 
    if(!empty($this->data['User']['username'])) { 
     // unset unrequired validation rules 
     unset($this->User->validate['username']['unique']); 

     // validate form 
     $this->User->set($this->data); 
     if($this->User->validates()) { 
      // update Last Login date 
      $this->User->id = $this->User->_user['User']['id']; 
      $this->User->saveField('last_login',date("Y-m-d H:i:s")); 

      // save User to Session and redirect 
      $this->Session->write('User', $this->User->_user); 
      $this->Session->setFlash('You have successfully logged in.','default',array('class'=>'flash_green')); 
      //$this->redirect(array('controller'=>'shows', 'admin'=>FALSE)); 
     } else { 
      $this->Session->setFlash('Incorrect username/password combination.','default',array('class'=>'flash_red')); 
      $this->redirect(array('controller'=>'users', 'action'=>'login', 'admin'=>FALSE)); 
     } 
    } 
} 

users_controller beforeFilter()

function beforeFilter(){ 
    parent::beforeFilter(); 
    $this->Auth->allow('register'); 
} 

app_controller beforeFilter和組件

var $components = array('Session', 'Auth' => array(
    'loginAction' => array('controller'=>'users','action'=>'login', 'admin'=>false), 
    //'logoutRedirect' => array('controller'=>'users','action'=>'logout'), 
    'loginRedirect' => array('controller'=>'shows', 'action'=>'index'), 
    'autoRedirect' => false, 
    'authorize' => 'controller') 
); 

function beforeFilter() { 
    $this->Auth->allow('home'); 
    $this->set('admin', $this->_isAdmin()); 
    $this->set('logged_in', $this->_loggedIn()); 
    $this->set('users_username', $this->_usersUsername()); 
} 
+0

我想我知道了,在登錄功能結束後,我將它重定向回登錄操作,因此重置了驗證。 '$ this-> redirect(array('controller'=>'users','action'=>'login','admin'=> FALSE));'。只是有一個奇怪的問題與密碼跳過空的驗證和長度的警告,而不是... – medoix

+0

修復了這一點,'模型驗證'的'最後'=>真。這是cakephp對每個規則進行迭代的方式。 – medoix

回答

0

從user_controller.php文件中刪除下面的代碼。

else { 
     $this->Session->setFlash('Incorrect username/password combination.','default',array('class'=>'flash_red')); 
     $this->redirect(array('controller'=>'users', 'action'=>'login', 'admin'=>FALSE)); 
    } 

這是循環回登錄操作,沒有數據,因此沒有驗證。

0

如果你的 「默認的驗證」,然後停止什麼是錯在那裏。如果你在模型中的過濾器中有某些東西,確保它們返回true。我建議你爲此編寫一個單元測試。

您絕對不必在控制器中執行!空檢查。無論如何,整個代碼塊可以減少到6行。大部分應該進入模型。

選中此插件或查看其代碼以獲取想法。 https://github.com/CakeDC/users/

+0

感謝burzum,我看了一下這個插件,它看起來很棒。我的控制器代碼(上圖)大部分與該插件中的代碼相同..我花費了大量的時間來將它扔掉並使用此插件哈哈。我已經用我的beforeFilter更新了上述內容,如果它使得它更清晰......我難以理解爲什麼註冊表格驗證正確,但不是登錄表單。此外,該模型具有正確的驗證檢查,如註冊表形式。 – medoix