2011-11-30 234 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