没有使用CakePHP身份验证组件的查询
我正在使用CakePHP2.2.4,我已经开始使用Atuh Componenet 这是我的AppController:没有使用CakePHP身份验证组件的查询,php,cakephp,cakephp-2.0,Php,Cakephp,Cakephp 2.0,我正在使用CakePHP2.2.4,我已经开始使用Atuh Componenet 这是我的AppController: class AppController extends Controller { public $components = array('Auth', 'Session'); public function beforeFilter() { $this->Auth->authorize = array('Controller');
class AppController extends Controller {
public $components = array('Auth', 'Session');
public function beforeFilter() {
$this->Auth->authorize = array('Controller');
$this->Auth->authenticate = array(
'Form' => array (
'scope' => array('User.active' => 1),
'fields' => array('username' => 'email', 'password' => 'password'),
)
);
}
public function isAuthorized($user) {
debug($user);
return true;
}
}
这是我的User.php模型
class User extends AppModel {
public $name = 'User';
/* Relazioni */
public $hasOne = 'Profile';
public $belongsTo = 'Role';
public $hasMany = array(
'Lead' => array(
'className' => 'Lead'
)
);
}
这是我的UserController.php
<?php
App::uses('AppController', 'Controller');
class UsersController extends AppController
{
public $name = 'Users';
public $uses = array();
public function beforeFilter()
{
parent::beforeFilter();
}
public function login()
{
if ($this->request->is('post'))
{
if ($this->Auth->login())
{
debug('Logged');
}
else
{
$this->Session->setFlash('Login non autorizzato', 'default', array('class' => 'errore'), 'login');
}
}
}
public function logout()
{
$this->redirect($this->Auth->logout());
}
}
AppController中的代码错误
public function beforeFilter() {
$this->Auth->authorize = array('Controller');
$this->Auth->authenticate = array(
'Form' => array (
'scope' => array('User.active' => 1),
// password != pwd as you post it
'fields' => array('username' => 'email', 'password' => 'password'),
)
);
}
换成
'fields' => array('username' => 'email', 'password' => 'pwd'),
或者确保在表单中发布密码而不是pwd
有关此问题的文档,请参阅我想为到达这里的任何人发布此答案,这些人无法使用本示例不直接适用的Auth登录 需要记住的一点是,Auth期望底层数据库列为username和password。如果出于任何原因,您推迟了此操作,例如,如果您希望在用户电子邮件上验证非常常见的内容,并且您更改了表的列名以反映这一点,那么您必须告诉Auth这一点 原因是基础查询将失败。最终,所有发生在场景后面的事情都只是一个匹配指定字段的简单查询。例如,不精确-仅用于演示目的-选择*不正确:
SELECT * FROM users WHERE username = 'blahblahblah' AND password = 'someInsan31yh4sh3dpassw0rd'
如果您的基础表在电子邮件列的loo中缺少用户名列,那么此查询显然将失败。导致无法登录,并且通常没有查询失败的迹象,甚至从SQL转储中忽略了该查询。但是,AppController中的以下代码将解决您的问题:
public $components = array(
'Auth' => array(
'authenticate' => array(
'Form' => array(
'fields' => array('username' => 'columnUsedForValidatingUsername', 'password' => 'columnUserForValidatingPassword')
)
)
)
);
在这种情况下,Jippi的回答是完全正确的。但我觉得作为StackOverflow的回答者,你应该向任何发现这一点的人解释问题发生的原因,并提供一个不具体的答案
干杯您确定您作为活动用户登录的用户已设置为true吗?@TimJoyce-sure,i-ma-sure..设置为1。顺便说一句,我认为查询没有执行,正如我在sql_dump元素中告诉您的那样,我在其他页面中没有看到任何输出,我看到转储正确地添加了pr$this->request->data;在顶部筛选之前,请先访问appController,然后尝试登录,让我们看看那里的输出。@TimJoyce请查看我的编辑,但您能否告诉我,在文件结构中用户名='blahblahblah'和密码='SomeInAsna31YH4sh3dpassw0rd'存在的用户中,此选项从何处选择*
public $components = array(
'Auth' => array(
'authenticate' => array(
'Form' => array(
'fields' => array('username' => 'columnUsedForValidatingUsername', 'password' => 'columnUserForValidatingPassword')
)
)
)
);