CakePHP Auth->登录不生成请求数据的查询

时间:2013-12-05 23:54:28

标签: authentication login cakephp-2.0 components

我让Auth组件完美地运行了运行Cake 2.4.1的应用程序,我需要将它复制到另一个运行Cake 2.4.1的应用程序。我复制了我能想到的与Auth的东西有关的一切,但它只是在第二个应用程序中不起作用。它拒绝登录。对$this->Auth->login()的调用失败,即使它获得的数据与有效的应用程序完全相同。

两个应用程序都使用完全相同的数据库和用户登录。 两个应用程序在POST请求中获得完全相同的请求数据。

首先,这是我的设置。

在AppController.php中:

public $uses = array ('User');
public $components = array    ( 'Session',
                                'CPRACL.CPRACL' => array (),
                                'Auth' => array (
//                                        'loginAction' => array ('controller' => 'users', 'action' => 'login'),
//                                        'logoutRedirect' => '/users/login',
//                                        'loginRedirect' => '/',
                                        'authenticate' => array (
                                            'Form' => array (
                                                'fields' => array ('username' => 'email_address'),
                                                'scope' => array ('User.active' => 1)
                                            )
                                        ),
                                        'authorize' => array ('Controller'),
//                                        'unauthorizedRedirect' => '/' // when going to a page the user is not authorized to go to
                                    )
                                );

注释掉的行使用默认值,但它们包含在内以供参考其默认值。这在两个应用程序中完全相同。 CPRACL是我使用的插件组件,而不是Cake的ACL组件,但它很快就会失败。

同样在 AppController.php

public function beforeFilter ()
{
    $login_user = $this->User->find ('first', array ('conditions' => array ('id' => $this->Auth->user ('id'))));
    if (!empty ($login_user))
        $this->set ('login_user', $login_user ['User']);
}

public function isAuthorized ($user = null)
{
    // This never gets called in the application that does not work, but it does get called in the one that works.
}

在两个应用程序中,它按照AppController.php中的定义设置Auth组件,然后调用beforeFilter来检查用户是否已经登录。这是一种方便,以便当它们被重定向到不同的页面时,应用程序可以显示登录用户的用户名。它工作正常,我不认为这会导致任何问题。

接下来发生的事情是调用UsersController::login()

    public function login ()
    {
        $redirect_url = $this->Auth->redirectUrl ();
        if ($this->Auth->loggedIn ())
            return $this->redirect ($redirect_url);

//die (print_r ($this->request->data, true));
        if ($this->request->is ('post'))
        {
//die (Debugger::dump ($this->Auth));
            if ($this->Auth->login ())
                return $this->redirect ($redirect_url);
            else
                return $this->redirect ($this->Auth->loginAction);
        }
    }

我使用两条注释掉的行来比较两个应用程序之间发生的情况。这两个应用程序在帖子中获得完全相同的数据。对于Auth对象的转储,我得到的是:

object(AuthComponent) {
    components => array(
        (int) 0 => 'Session',
        (int) 1 => 'RequestHandler'
    )
    authenticate => array(
        'Form' => array(
            [maximum depth reached]
        )
    )
    authorize => array(
        (int) 0 => 'Controller'
    )
    ajaxLogin => null
    flash => array(
        'element' => 'default',
        'key' => 'auth',
        'params' => array([maximum depth reached])
    )
    loginAction => array(
        'controller' => 'users',
        'action' => 'login',
        'plugin' => null
    )
    loginRedirect => null
    logoutRedirect => array(
        'controller' => 'users',
        'action' => 'login',
        'plugin' => null
    )
    authError => 'You are not authorized to access that location.'
    unauthorizedRedirect => true
    allowedActions => array(
        (int) 0 => 'register',
        (int) 1 => 'login',
        (int) 2 => 'logout'
    )
    request => object(CakeRequest) {}
    response => object(CakeResponse) {}
    settings => array(
        'authenticate' => array(
            [maximum depth reached]
        ),
        'authorize' => array(
            [maximum depth reached]
        )
    )
    Session => object(SessionComponent) {}
    [protected] _authenticateObjects => array()
    [protected] _authorizeObjects => array()
    [protected] _user => array()
    [protected] _methods => array(
        (int) 0 => 'index',
        (int) 1 => 'view',
        (int) 2 => 'add',
        (int) 3 => 'register',
        (int) 4 => 'login',
        (int) 5 => 'logout',
        (int) 6 => 'toggleactive',
        (int) 7 => 'changePassword',
        (int) 8 => 'passwordHash',
        (int) 10 => 'edit_pwd',
        (int) 11 => 'edit_your_pwd',
        (int) 12 => 'isAuthorized'
    )
    [protected] _Collection => object(ComponentCollection) {}
    [protected] _componentMap => array(
        'Session' => array(
            [maximum depth reached]
        ),
        'RequestHandler' => array(
            [maximum depth reached]
        )
    )
}

这是我发送登录请求时两个应用程序从Auth对象转储返回的内容。它说用户没有被授权访问该位置,但我认为这是因为它还没有实际登录用户,所以我不认为该Auth对象的转储有很大价值,但我把它包括在内无论如何。

但这是最重要的部分,因为它是两个应用程序之间唯一明显不同的东西。当我在工作应用程序上执行登录时,它会成功登录我,我的布局(显示最新查询)显示此查询。注意id字段。 id字段在查询中是正确的,并且有效。

SELECT `User`.`id`, `User`.`user_id`, `User`.`email_address`, `User`.`first_name`, `User`.`last_name`, `User`.`password`, `User`.`active`, `User`.`created`, `User`.`modified` FROM `brian_cake_test`.`users` AS `User` WHERE `id` = '529f9a8c-3460-46a2-a97c-6a6242a26b88' LIMIT 1

但是当我为不起作用的应用程序执行完全相同的操作时,一切都以完全相同的方式发生,除了它将我重定向回登录页面,如果它无法登录它应该执行,并且它显示此在我的布局中查询。

SELECT `User`.`id`, `User`.`user_id`, `User`.`email_address`, `User`.`first_name`, `User`.`last_name`, `User`.`password`, `User`.`active`, `User`.`created`, `User`.`modified` FROM `brian_cake_test`.`users` AS `User` WHERE `id` IS NULL LIMIT 1

注意第二个查询中id是如何为NULL。我不知道为什么会这样。

我的 User.php 模型只是一个空类,在两个应用程序中完全相同,但一个完美运行而另一个不运行。

<?php
App::uses ('AppModel', 'Model');
class User extends AppModel
{
    public $name = 'User';
}

我绞尽脑汁试图弄清楚这两个应用程序之间有什么不同,我想不出任何东西,我尝试的任何东西都不会让它发挥作用。当我知道99%的代码与此问题无关时,我不想发布数百行代码,但我不知道为什么它在一个而不是另一个中工作。如果您认为您可能知道问题是什么,我会很乐意发布更多信息。任何帮助将非常感激。这对我来说非常重要。

1 个答案:

答案 0 :(得分:1)

实际上,如果它的代码和数据库完全相同,则不应该成为不起作用的理由。

您是否尝试过清除缓存文件夹?这些文件有时会弄乱应用程序。