Auth组件没有响应

时间:2015-07-27 10:23:17

标签: php cakephp

在我的情况下,Auth组件没有正确响应。当我使用错误的邮件地址登录时,它显示错误,但是当我使用正确的电子邮件地址登录但密码错误时,它不会显示错误并重定向到可靠的内页。

AppController.php

public $components = array(
    'Session',
    'Auth' => array(
        'authenticate' => array(
            'Form' => array(
                'fields' => array(
                    'username' => 'email'
                    )
                ),
                'loginRedirect' => array(
                    'controller' => 'users',
                    'action' => 'index',
                )
            ),
    );

UsersController.php

public function login() {
        $this->layout = 'login';
        if ($this->request->is('post')) {
            if ($this->Auth->login($this->data)) {
                if (!empty($this->request->data)) {
                    $access_controll = $this->{$this->modelClass}->find('first', array('conditions'=>array('email'=>$this->request->data['User']['email'])));
                    if (!empty($access_controll)) {
                    $role_id = $access_controll['User']['role_id'];
                    if( ($role_id == 1) OR ($role_id == 2) ) {
                        return $this->redirect(array('controller' => 'dashboards'));
                    } else {
                        die('You are not authenticate person.');
                        //$this->Session->setFlash(__('You are not authenticate person.'), 'message', array('class' => 'danger'), 'auth');
                    }
                } else {
                    die ('Something is wrong email id or password.');
                    //$this->Session->setFlash(__('You are not a register user still.'), 'message', array('class' => 'danger'), 'auth');
                }
                }
            } else {
                $this->Session->setFlash(__('invalidLoginDetails'), 'message', array('class' => 'danger'), 'auth');
            }
        }

login.ctp

<?php echo $this->Form->create('User',array('novalidate'=>'true','inputDefaults' => array('div' => false))); ?>
<?php echo $this->Form->input('email', array('type'=>'email', 'class'=>'form-control', 'placeholder'=>'Email', 'label'=>false)); ?>
<?php echo $this->Form->input('password', array('type'=>'password', 'class'=>'form-control', 'placeholder'=>'Password', 'label'=>false)); ?>
<?php echo $this->Form->end(__('Login'), array('class'=>'btn btn-default btn-block btn-clean'));?>

2 个答案:

答案 0 :(得分:0)

对AuthComponent :: login

的调用不好

如上所述,Auth::login method未检查密码:

  

如果提供了$ user ,该数据将作为登录用户存储。

即。无论$this->data是什么,if it is truthy,此方法调用将始终返回true。

请注意the documentation example的差异:

public function login() {
    if ($this->request->is('post')) {
        // Important: Use login() without arguments! See warning below.
        if ($this->Auth->login()) {

这是1.x的行为更改(假设您使用的是2.x),其中相同的代码尝试从请求数据中识别用户。

答案 1 :(得分:0)

这令人头疼但终于解决了。当我清除我的浏览器窗口的cookie和缓存时,它没有任何其他东西它正常工作然后我做了一个注销功能来清除缓存并且它工作。

logout():

public function logout() {
    return $this->redirect($this->Auth->logout());
}

但是我无法理解私有窗口中也出现问题,但私有窗口不存储cookie然后为什么在私人窗口中发生此问题..

感谢您的建议&amp;支持@ AD7six