CakePHP登录用户名/密码验证

CakePHP登录用户名/密码验证,php,cakephp,login,Php,Cakephp,Login,我的users_controller中有以下内容,因为需要进一步的逻辑来执行登录,但是在CakePHP中完成时,默认验证将停止,因此我尝试在执行帐户检查之前强制它验证用户名和密码字段。除了这个,一切都很好。我尝试添加两个if语句(如下)来检查username/password是否为空,但是一旦页面加载它们为空,验证框就会显示出来 我对如何实现这一目标感到困惑。任何帮助都将不胜感激 function login() { if($this->Auth->user()) {

我的users_controller中有以下内容,因为需要进一步的逻辑来执行登录,但是在CakePHP中完成时,默认验证将停止,因此我尝试在执行帐户检查之前强制它验证用户名和密码字段。除了这个,一切都很好。我尝试添加两个if语句(如下)来检查username/password是否为空,但是一旦页面加载它们为空,验证框就会显示出来

我对如何实现这一目标感到困惑。任何帮助都将不胜感激

    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));
        }
    }
}
用户\u控制器beforeFilter()

过滤器和组件前的app_控制器

    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());
}
如果你的“默认验证”停止了,那么那里就出了问题。如果您在模型中的before过滤器中有某些内容,请确保它们返回true。我建议您为此编写一个单元测试

你绝对不必做这个!控制器中的空检查。无论如何,整个代码块可以减少到6行。这其中的大部分应该放在模型中


检查这个插件或者查看它的代码来获得一个想法

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

这是将页面循环回登录操作,没有数据,因此没有验证。

谢谢burzum,我看过该插件,它看起来很棒。我的大多数控制器代码(以上)与该插件中的代码相同。。我花了很多时间在它上面,只是把它扔掉,然后使用这个插件lol。我已经用我的beforeFilter更新了上面的内容,如果它让它更清晰的话。。。我不明白为什么注册表单可以很好地验证,而登录表单却不能。注册表单工作时,模型也有正确的验证检查。我想我找到了答案,在登录功能结束时,我让它重定向回登录操作,因此重置了验证$此->重定向(数组('controller'=>'users','action'=>'login','admin'=>FALSE));'。只是有一个奇怪的问题,密码跳过了空的验证,并警告了长度…修复了这个问题,在模型验证中使用'last'=>true。这是cakephp迭代每个规则的方式。
 else {
        $this->Session->setFlash('Incorrect username/password combination.','default',array('class'=>'flash_red'));
        $this->redirect(array('controller'=>'users', 'action'=>'login', 'admin'=>FALSE));
    }