cakephp auth 组件允许重定向问题

发布于 2024-12-07 03:17:44 字数 1276 浏览 1 评论 0原文

当我使用 $this->Auth->allow('index','view'); 时,我遇到了 Auth 组件问题; 当我使用 $this->Auth->allow('*') 时,我收到 /users/login 导致了太多重定向,它工作正常。我使用 cakephp 1.3.12 这里是 app_controller.php

class AppController extends Controller {
    var $components = array('Auth','Session');
    function beforeFilter(){    
         $this->Auth->allow('index','view');
    } 
}

我更改了 app_controller.php

class AppController extends Controller {
    var $components = array('Auth','Session');
    function beforeFilter(){    
        $this->Auth->allow(array('index','view','display'));
    }
}

users_controller.php

class UsersController extends AppController {

var $name = 'Users';

function beforeFilter() {
    parent::beforeFilter();
    $this->Auth->allow(array('login','logout'));
}

function login() {
    if ($this->Session->read('Auth.User')) {
        $this->redirect('/', null, false);
    }
}

paths.php

Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));

有什么建议吗? 谢谢

I am having problem with Auth component when I use $this->Auth->allow('index','view');
I am getting /users/login has resulted in too many redirects when I use $this->Auth->allow('*') it works fine. I am using cakephp 1.3.12 here is app_controller.php

class AppController extends Controller {
    var $components = array('Auth','Session');
    function beforeFilter(){    
         $this->Auth->allow('index','view');
    } 
}

I changed the app_controller.php

class AppController extends Controller {
    var $components = array('Auth','Session');
    function beforeFilter(){    
        $this->Auth->allow(array('index','view','display'));
    }
}

users_controller.php

class UsersController extends AppController {

var $name = 'Users';

function beforeFilter() {
    parent::beforeFilter();
    $this->Auth->allow(array('login','logout'));
}

function login() {
    if ($this->Session->read('Auth.User')) {
        $this->redirect('/', null, false);
    }
}

routes.php

Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));

any suggestions?
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

作死小能手 2024-12-14 03:17:44

不知道,但您可能想检查是否有任何请求操作。

“如果您在布局或元素中使用 requestAction,则应该允许这些操作,以便能够正确打开登录页面。”
http://book.cakephp.org/1.3/en/view/1257/allow

这让我困惑了很长时间。

假设您在模板中的某处渲染了一个元素:

echo $this->element('comments');

在views/elements/comments.ctp 中,您有一些请求操作的内容,例如

$comments = $this->requestAction('comments/index');
foreach($comments as $comment) {
 // print stuff
}

在您的CommentsController 中,您必须执行以下操作:

function beforeFilter() {
    $this->Auth->allow('index');
}

请注意,您正在从元素中的注释控制器请求索引操作。这就是为什么您必须允许该特定控制器的“索引”。

我还没有在任何地方看到这个问题得到妥善解决。希望这就是导致您错误的原因。

Don't know but you might want to check if you have any request actions.

"If you are using requestAction in your layout or elements you should allow those actions in order to be able to open login page properly."
http://book.cakephp.org/1.3/en/view/1257/allow

This had me stumped for the longest time.

Let's say you render an element somewhere in your template:

echo $this->element('comments');

And in views/elements/comments.ctp you have something that requests an action like

$comments = $this->requestAction('comments/index');
foreach($comments as $comment) {
 // print stuff
}

In your CommentsController your have to:

function beforeFilter() {
    $this->Auth->allow('index');
}

Notice you are requesting an index action from your comments controller in your element. That's why you have to allow 'index' for that specific controller.

I haven't seen this problem properly addressed anywhere. Hope that's what is causing your error.

咽泪装欢 2024-12-14 03:17:44

它是一个数组 =)

$this->Auth->allow(array('index','view'));

由于 /user/login 操作无法访问,因此您会收到 too much redirects 消息。因此,服务器尝试显示登录页面,但它不能,因为常规的未连接用户无法访问 /user/login。当用户无法访问某个页面时,服务器会将他重定向到登录页面......所以你看,这是一个无限循环。

/user/login 操作应该授权给每个人。您的 Users 控制器应如下所示:

class UsersController extends AppController {

var $name = 'Users';
function beforeFilter() {
    parent::beforeFilter();
    $this->Auth->allow(array('login','logout'));
}

function login(){
    if ($this->Session->read('Auth.User')) {
        $this->redirect('/', null, false);
    }
}

    //if you're using prefix routes. 
function admin_login(){
    $this->redirect('/users/login');
}

如果这不是问题,也许您正在重定向 paths.php 中的页面

希望这会有所帮助

its an array =)

$this->Auth->allow(array('index','view'));

your getting the too many redirects message becasuse the /user/login action is not accessible. So the server tries to display the login page, but it can't, because regular non-connected users dont have acces to /user/login. And when a user doesn't have access to a page, the server will redirect him to the login page... so you see, its an infinite loop.

The /user/login action should be authorized to everyone. Your Users controller should look like this:

class UsersController extends AppController {

var $name = 'Users';
function beforeFilter() {
    parent::beforeFilter();
    $this->Auth->allow(array('login','logout'));
}

function login(){
    if ($this->Session->read('Auth.User')) {
        $this->redirect('/', null, false);
    }
}

    //if you're using prefix routes. 
function admin_login(){
    $this->redirect('/users/login');
}

if this doesn't the problem, maybe you're redirecting the page in the routes.php

Hope this helps

木槿暧夏七纪年 2024-12-14 03:17:44

你做错了。应用程序如何知道你正在尝试控制哪个控制器操作。从你的控制器执行它。

从应用程序中删除它

$this->Auth->allow(array('index','view','display'));

在您的应用程序控制器中尝试进行所需的更改,

        $this->Auth->loginError = "Wrong credentials. Please provide a valid username and password.";

        $this->Auth->authError = "You don't have sufficient privilege to access this resource.";

        $this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');

        $this->Auth->logoutRedirect = array('controller' => 'users', 'action' => 'login');

        $this->Auth->loginRedirect = array('controller' => 'users', 'action' => 'dashboard');

从您的用户控制器执行此操作

$this->Auth->userModel = 'User';
$this->Auth->allow('*');

并且在您的登录中不要执行任何操作,所有重定向都将由应用程序控制器执行。

如果您对此有任何疑问,请给我发邮件

[电子邮件受保护]

you are doing it wrong.How can app can get to know that which of your controller action you are trying to controller.Do it from your controller.

remove this from app

$this->Auth->allow(array('index','view','display'));

try this in your app controller with needed change

        $this->Auth->loginError = "Wrong credentials. Please provide a valid username and password.";

        $this->Auth->authError = "You don't have sufficient privilege to access this resource.";

        $this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');

        $this->Auth->logoutRedirect = array('controller' => 'users', 'action' => 'login');

        $this->Auth->loginRedirect = array('controller' => 'users', 'action' => 'dashboard');

do this from your user controller

$this->Auth->userModel = 'User';
$this->Auth->allow('*');

And in your login dont do anything all of your redirect and all will be doing by app controller.

If you have any doubt regarding this mail me

[email protected]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文