我正在使用CakePHP 2.2,这是我使用的教程的链接:link
非常重要:我关掉了Inflector
我不关心ACL(它有效:D),我的AUTH不起作用... $this->Auth->login()
返回false ...
用户控制器:
App::uses('AppController', 'Controller');
class UsersController extends AppController {
public $helpers = array('Html','Form');
public $components = array('Auth' => array('authenticate' => array('form' => array('fields' => array('username' => 'login')))),'Session');
function beforeFilter() {
//$this->Auth->allow('logout', 'view');
$this->Auth->allow('*');
parent::beforeFilter();
}
function login() {
if ($this->Auth->login())
{
$this->redirect($this->Auth->redirect());
} else
{
$this->Session->setFlash(__('Invalid username or password, try again'));
}
App控制器:
App::uses('Alc', 'Controller', 'Controller');
class AppController extends Controller {
public $components = array('Auth'=>array('authorize' => array('Actions' => array('actionPath' => 'controllers'))), 'Session');
public $helpers = array('Html', 'Form', 'Session');
function beforeFilter() {
$this->Auth->userModel = 'Users';
$this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
$this->Auth->allow('index', 'view', 'admin', 'edit', 'login', 'logout', 'add');
$this->Auth->logoutRedirect = array('controller' => 'novosti', 'action' => 'index');
$this->Auth->loginRedirect = array('controller' => 'novosti', 'action' => 'index');
}
用户型号:
App::uses('AuthComponent', 'Controller/Component');
class Users extends AppModel {
public function beforeSave($options = array()) {
if (isset($this->data[$this->alias]['password'])) {
$this->data['Users']['password'] = AuthComponent::password($this->data['Users']['password']);
}
return true;
}
public function bindNode($user) {
return array('model' => 'Groups', 'foreign_key' => $user['Users']['groups_id']);
}
查看文件:
<?php
echo $this->Session->flash('auth');
echo $this->Form->create('Users', array('action' => 'login'));
echo $this->Form->inputs(array(
'legend' => __('Login', true),
'Login',
'password'
));
echo $this->Form->end('Login');
?>
没有SQL DUMPS可用
我转到lib/controller/components/Authcomponents.php
和lib/controller/components/auth/*
并查看所有这些文件....并将所有Auth.User
更改为Auth.Users
;我还发现设置变量和我发现的所有地方都将模型名称从User
更改为Users
,并且登录字段也从username
更改为Login
答案 0 :(得分:2)
if (debug($this->Auth->login()))
Debug不返回任何内容,因此该行总是会失败。
您的用户名是Login
字段,但默认为username
,您尚未为此配置验证。
public $components = [
'Auth' => [
'authenticate' => [
'Form' => [
'userModel' => 'Users',
'fields' => [
'username' => 'Login'
],
],
],
],
];
在您的beforeSave中,您使用的是Users
密钥,而不是User
。模型是单数。
添加<?php echo $this->element('sql_dump'); ?>
并查看生成的查询。确保密码正确,密码与数据库值匹配。
我注意到的一些东西。
答案 1 :(得分:0)
在登录视图中尝试此操作:
<?php
echo $this->Session->flash('auth');
echo $this->Form->create('Users', array('action' => 'login'));
echo $this->Form->inputs(array(
'legend' => __('Login', true),
'username',
'password'
));
echo $this->Form->end('Login');
?>
Auth接受默认授权文件作为用户名/密码,如果你想使用登录,然后在控制器中覆盖这样的auth授权:
$this->Auth->fields = array('username' => 'login', 'password' => 'password');
或在控制器中你可以这样做:
$this->Auth->authenticate = array(
'Form' => array(
'fields' => array('username' => 'login', 'password' =>'password')
)
);
if($this->Auth->login($this->request->data['Users'])){
......your code...
}