CakePhp2.4:Auth->;login()总是返回false
虽然我的应用程序比Cake主站点上的应用程序有更多的模型和动作,但我还是不折不扣地遵循了它,只是增加了一两点。尽管如此,我不能授权任何登录操作,我不明白为什么。这里的任何帮助都会非常有帮助,我一直在讨论API,类似的stackOverflow问题,等等,我完全被卡住了。 下面是来自的相关代码CakePhp2.4:Auth->;login()总是返回false,cakephp,cakephp-2.0,acl,cakephp-2.4,Cakephp,Cakephp 2.0,Acl,Cakephp 2.4,虽然我的应用程序比Cake主站点上的应用程序有更多的模型和动作,但我还是不折不扣地遵循了它,只是增加了一两点。尽管如此,我不能授权任何登录操作,我不明白为什么。这里的任何帮助都会非常有帮助,我一直在讨论API,类似的stackOverflow问题,等等,我完全被卡住了。 下面是来自的相关代码 应用控制器 用户控制器 用户模型 Users.login视图 组控制器 群模型 更新-已解决 如果这有帮助的话,根据stackOverflow的回答,我试了一下: pr(AuthComponent::pas
pr(AuthComponent::password($this->data[$this->alias]['password'])代码>
在调用$this->Auth->login()
之前,可以肯定的是,密码散列与我数据库中的密码散列根本不匹配。仔细检查后,我的密码
字段被限制为32个字符,而实际上它需要36个字符。真正的掌心时刻
1。AppController
class AppController extends Controller {
public $components = array( 'Session',
'Quick',
'Acl',
'Auth' => array(
'loginAction' => array('controller' => 'users', 'action' => 'login'),
'authenticate' => array(
'Form'=> array(
'fields' => array(
'username' => 'email',
'password' => 'password'))),
'authorize' => array(
'Actions' => array('actionPath' => 'controllers')
)
)
);
public $helpers = array( 'Html', 'Form', 'Session', "Js"=>array("SafejQuery"), "Foundation");
public function beforeFilter() {
$this->Auth->allow('display');
$this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
$this->Auth->logoutRedirect = array('controller' => 'pages', 'action' => 'home');
$this->Auth->loginRedirect = array('controller' => 'users', 'action' => 'home');
}
}
public function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow('index','view');
}
public function login() {
if ($this->request->is('post')) {
if ($this->Auth->login()) {
return $this->redirect($this->Auth->redirectUrl());
}
$this->Session->setFlash(__('Your username or password was incorrect.'));
}
}
public function logout() {
$this->Session->setFlash('Good-Bye');
$this->redirect($this->Auth->logout());
}
/* USED FOR ACL ASSIGNMENT ONLY */
public function initDB() {
$group = $this->User->Group;
$group->id = ADMINISTRATOR_ID;
$this->Acl->allow($group, 'controllers');
$group->id = EDUCATOR_ID;
$this->Acl->allow($group, 'controllers');
$group->id = STUDENT_ID;
$this->Acl->deny($group, 'controllers');
$this->Acl->allow($group, 'controllers/Users/home');
$this->Acl->allow($group, 'controllers/Modules/run');
$this->Acl->allow($group, 'controllers/Modules/consent');
echo "all done";
exit;
}
public function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow('index','view');
}
2。用户控制器
class AppController extends Controller {
public $components = array( 'Session',
'Quick',
'Acl',
'Auth' => array(
'loginAction' => array('controller' => 'users', 'action' => 'login'),
'authenticate' => array(
'Form'=> array(
'fields' => array(
'username' => 'email',
'password' => 'password'))),
'authorize' => array(
'Actions' => array('actionPath' => 'controllers')
)
)
);
public $helpers = array( 'Html', 'Form', 'Session', "Js"=>array("SafejQuery"), "Foundation");
public function beforeFilter() {
$this->Auth->allow('display');
$this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
$this->Auth->logoutRedirect = array('controller' => 'pages', 'action' => 'home');
$this->Auth->loginRedirect = array('controller' => 'users', 'action' => 'home');
}
}
public function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow('index','view');
}
public function login() {
if ($this->request->is('post')) {
if ($this->Auth->login()) {
return $this->redirect($this->Auth->redirectUrl());
}
$this->Session->setFlash(__('Your username or password was incorrect.'));
}
}
public function logout() {
$this->Session->setFlash('Good-Bye');
$this->redirect($this->Auth->logout());
}
/* USED FOR ACL ASSIGNMENT ONLY */
public function initDB() {
$group = $this->User->Group;
$group->id = ADMINISTRATOR_ID;
$this->Acl->allow($group, 'controllers');
$group->id = EDUCATOR_ID;
$this->Acl->allow($group, 'controllers');
$group->id = STUDENT_ID;
$this->Acl->deny($group, 'controllers');
$this->Acl->allow($group, 'controllers/Users/home');
$this->Acl->allow($group, 'controllers/Modules/run');
$this->Acl->allow($group, 'controllers/Modules/consent');
echo "all done";
exit;
}
public function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow('index','view');
}
3。用户模型
// $belongsTo built with bake, but present
public $actsAs = array('Acl' => array('type' => 'requester'));
public function beforeSave($options = array()) {
/* Note: I tried to use the SimplePasswordHasher object as per the 2.4 API,
* but the class wasn't found (but does exist at
* lib/Cake/Controller/Component/Auth/SimplePasswordHasher.php */
$this->data['User']['password'] = AuthComponent::password($this->data['User']['password']);
return true;
}
public function bindNode() {
$data = AuthComponent::user();
return array('model' => 'Group', 'foreign_key' => $data['User']['group_id']);
}
public function parentNode() {
if (!$this->id && empty($this->data)) {
return null;
}
if (isset($this->data['User']['group_id'])) {
$groupId = $this->data['User']['group_id'];
} else {
$groupId = $this->field('group_id');
}
if (!$groupId) {
return null;
} else {
return array('Group' => array('id' => $groupId));
}
}
public $actsAs = array('Acl' => array('type' => 'requester'));
public function parentNode() {
return null;
}
4。User.login视图
<h2>Login</h2>
<?php
echo $this->Form->create('User', array('url' => array('controller' => 'users', 'action' => 'login')));
echo $this->Form->input('User.email');
echo $this->Form->input('User.password');
echo $this->Form->end('Login');
?>
6。集团模式
// $belongsTo built with bake, but present
public $actsAs = array('Acl' => array('type' => 'requester'));
public function beforeSave($options = array()) {
/* Note: I tried to use the SimplePasswordHasher object as per the 2.4 API,
* but the class wasn't found (but does exist at
* lib/Cake/Controller/Component/Auth/SimplePasswordHasher.php */
$this->data['User']['password'] = AuthComponent::password($this->data['User']['password']);
return true;
}
public function bindNode() {
$data = AuthComponent::user();
return array('model' => 'Group', 'foreign_key' => $data['User']['group_id']);
}
public function parentNode() {
if (!$this->id && empty($this->data)) {
return null;
}
if (isset($this->data['User']['group_id'])) {
$groupId = $this->data['User']['group_id'];
} else {
$groupId = $this->field('group_id');
}
if (!$groupId) {
return null;
} else {
return array('Group' => array('id' => $groupId));
}
}
public $actsAs = array('Acl' => array('type' => 'requester'));
public function parentNode() {
return null;
}
“注意:我试图按照2.4API使用SimplePasswordHasher对象,
但是找不到该类(但确实存在于
lib/Cake/Controller/Component/Auth/SimplePasswordHasher.php“
您在尝试使用该文件之前是否包含了该文件
App::uses('SimplePasswordHasher', 'Controller/Component/Auth');
$this->request->data['Login']['password'] = (new SimplePasswordHasher)->hash( $this->request->data['Login']['password'] );
不用了,再来一次,谢谢。