可以';t使用CakePHP';s身份验证登录


Can't login with CakePHP's Auth login

我知道这是一个过于夸张的问题,但我无法在所有答案中找到答案。也许你可以帮我。我正在尝试让一个用户登录,但我收到了"用户名/密码无效"错误,数据正确。这是我第一次使用cakeHP。

到代码。

型号:

App::uses('AppModel','Model');
Class User extends AppModel {
    public $useTable = 'Users';
    public $hasMany = array(
        'Costumer' => array(
            'className' => 'Costumer',
            'foreignKey' => 'users_id',
            'order' => 'Costumer.name ASC'
        )
    );
    //Suppressed the validation code, don't think it's important here
    public function beforeSave($options = array()){
        if (!empty($this->data['User']['pwd'])) {
            $this->data['User']['passwd'] = Security::hash($this->data['User']['pwd']);
        }
    }
}

控制器:

App::uses('AppController', 'Controller');
class UsersController extends AppController{
public $helpers = array('Html', 'Form');
public $name = 'Users';
public $components = array('Auth','Session');
public function beforeFilter(){
    parent::beforeFilter();
    $this->Auth->allow('add');
}
public function login(){
    //Tests
    $userEmail = $this->User->findByEmail($this->request->data['User']['email']);
    $userPass = $this->User->findByPasswd(Security::hash($this->request->data['User']['passwd']));
    die(var_dump($userEmail, $userPass));
    if ($this->request->is('post')) {
        $this->request->data['User']['passwd'] = Security::hash($this->request->data['User']['passwd']);
        if ($this->Auth->login()){
            return $this->redirect($this->Auth->redirectUrl());
        } else {
            $this->Session->setFlash(__('E-mail e/ou usuário incorretos, tente novamente.'));
        }
    }
}

视图:

<div class="row" style="margin-top: 40px;">
<div class="col-lg-8">
</div>
<div id="login" class="col-lg-2" style="background-color: #eee">
    <h3>Conecte-se.</h3>
    <?php 
        echo $this->Form->create('User', array(
            'label' => 'login', 
            'class' => 'form-horizontal form-group'
            )
        );
        echo $this->Form->input('email', array(
            'label' => 'E-mail',
            'class' => 'form-control',              
            )
        );
        echo $this->Form->input('passwd', array(
            'label' => 'Senha',
            'class' => 'form-control', 
            )
        );
        echo '<br />';
        echo $this->Form->end(array(
            'label' => 'Entrar', 
            'class' => 'btn btn-success'
            )
        );
     ?>
</div>

我的AppController.php有:

class AppController extends Controller {
//public $components = array('DebugKit.Toolbar');
public $components = array(
    'Session',
    'Auth' => array(
        'loginRedirect' => array('controller' => 'pages', 'action' => 'display', 'homemk'),
        'logoutRedirect' => array('controller' => 'pages', 'action' => 'display', 'homemk'),
        'authenticate' => array(
            'Form' => array(
                'fields' => array('username' => 'email', 'password' => 'passwd'),
                'passwordHasher' => 'Blowfish'
            )
        ),
        'authError' => 'Para visualizar esta página, você precisa estar logado.'
    )
);
public function beforeFilter(){
    $this->Auth->allow('display');      
    $this->set('authUser', $this->Auth->user());
}

}

疯狂的是,UsersController的两条线路都是

$userEmail = $this->User->findByEmail($this->request->data['User']['email']);

$userPass = $this->User->findByPasswd(Security::hash($this->request->data['User']['passwd']));    

返回我试图登录的用户,这样就不会出现数据错误。

伙计们!我在这里错过了什么?

谢谢。

编辑

由于我还没有找到任何"优雅"的方法,我写了一个虚拟的变通方法。它根据数据库手动检查请求->数据值,并手动登录用户。这是一个临时解决方案,我稍后会回来讨论。

public function login(){
    if ($this->request->is('post')) {
        $user = $this->User->findByEmail($this->request->data['User']['email']);            
        if (!empty($user) && ($user['User']['passwd'] == Security::hash($this->request->data['User']['passwd']))){
            $this->Auth->login($this->request->data);
            return $this->redirect($this->Auth->redirectUrl());
        } else {
            $this->Session->setFlash(__('E-mail e/ou usuário incorretos, tente novamente.'));
        }
    }
}

我不熟悉Auth组件的passwordHasher属性,也不熟悉Blowfish。

你应该使用Cake内置的密码散列

用户模型

public function beforeSave() {
    if (isset($this->data['User']['passwd'])) {
        $this->data['User']['passwd'] = AuthComponent::password($this->data['User']['passwd']);
    }
    return true;
}

AppController

public $components = array(
    'Session',
    'Auth' => array(
        'loginRedirect' => array('controller' => 'pages', 'action' => 'display', 'homemk'),
        'logoutRedirect' => array('controller' => 'pages', 'action' => 'display', 'homemk'),
        'authenticate' => array(
            'Form' => array(
                'fields' => array('username' => 'email', 'password' => 'passwd'),
            )
        ),
        'authError' => 'Para visualizar esta página, você precisa estar logado.'
    )
);

用户控制器

public function login() {
    if ($this->request->is('post')) {
        if ($this->Auth->login()){
            return $this->redirect($this->Auth->redirectUrl());
        } else {
            $this->Session->setFlash(__('E-mail e/ou usuário incorretos, tente novamente.'));
        } // end if cannot log in
    } // end if no form submitted
} // end login