1

我使用的是 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::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 转储

我去看看所有这些文件....并将所有文件lib/controller/components/Authcomponents.php更改为; 还查看了设置变量和我发现的所有地方我将模型名称从更改为,并且登录字段也从更改为lib/controller/components/auth/*Auth.UserAuth.UsersUserUsersusernameLogin

4

2 回答 2

2

if (debug($this->Auth->login()))

调试不返回任何东西,所以这条线总是会失败。

您的用户名是字段,Login但默认值是username,您尚未为此配置身份验证。

public $components = [
    'Auth' => [
        'authenticate' => [
            'Form' => [
                'userModel' => 'Users',
                'fields' => [
                    'username' => 'Login'
                ],
            ],
        ],
    ],
];

在您的 beforeSave 中,您使用的是Userskey 而不是User. 模型是单一的。

添加<?php echo $this->element('sql_dump'); ?>并查看生成的查询。确保它是正确的,并且密码与您的数据库值匹配。

只是我注意到的一些东西。

于 2012-09-10T07:46:19.823 回答
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...
    }
于 2012-09-10T08:30:25.387 回答