我知道这是一个过度的问题,但我无法在所有答案中找到解决方案。也许你可以帮助我。我正在尝试登录用户,并且我收到“用户名/密码无效”错误,并且数据正确。这是我第一次使用cakePHP。
代码。
型号:
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.'));
}
}
}
答案 0 :(得分:0)
我不熟悉Auth组件的passwordHasher属性,不熟悉Blowfish。
你应该使用Cake的内置密码hasher
用户模型
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.'
)
);
<强> UsersController 强>
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