CakePHP Auth 如何允许特定的控制器和操作

发布于 2024-08-31 23:15:25 字数 761 浏览 5 评论 0原文

我有一个“帖子”和一个“用户”控制器。我使用身份验证组件,我希望所有用户都可以访问“Post.index”,但只有登录的用户才能访问“User.index”。

在我的 app_controller.php 中,我有这个

$this->Auth->allow('signup', 'confirm', 'index');

,但所有用户都可以访问 post.index 和 user.index。如何在允许方法中指定控制器?

这对我不起作用:

$this->Auth->allow('signup', 'confirm', 'Post.index');

更新 我从 app_controller.php 中删除了“index”,并将其设置在后控制器的 beforeFilter 方法中:

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

我还在 app_controller 中设置了一个变量“loggedIn”,而不调用“parent::beforeFilter();”我收到“未定义变量”通知。

谢谢 sibidiba

I have a "Posts" and a "Users" controller. I use the Auth Component and I want that all users can visit "Post.index" but only logged in users can visit "User.index".

In my app_controller.php I have this

$this->Auth->allow('signup', 'confirm', 'index');

but with that all users can visit post.index and user.index. How can I specify a Controller in the allow-method?

This didn't work for me:

$this->Auth->allow('signup', 'confirm', 'Post.index');

update
I removed 'index' from the app_controller.php and instead set it in the beforeFilter method in the post controller:

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

I also set a variable "loggedIn" in app_controller, without calling "parent::beforeFilter();" I got an "undefined variable" notice.

thx sibidiba

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

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

发布评论

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

评论(8

仅此而已 2024-09-07 23:15:25

期间不会工作。你可以尝试用“/”代替。如果同样失败,您应该在 PostController 和 UserController 的 ::beforeFilter() 中分别设置 $this->Auth->allow('index') 。不要忘记调用parent::beforeFilter()。

The period will not work. You could try '/' instead. If that fails as well, you should set $this->Auth->allow('index') in PostController's and UserController's ::beforeFilter() individually. Don't forget to call parent::beforeFilter().

窝囊感情。 2024-09-07 23:15:25

取决于您正在处理的版本。如果是 cakephp 2.x,请将此代码放入具有您想要无需登录即可访问的操作的控制器中。作为您的问题,您应该将此代码放入 Posts 控制器:

function beforeFilter(){
     $this->Auth->allow(array('index','another action'));}

allow(array('action you want to allowed')) 而不是 allow('action you want to allowed')

Depends on the version you're working on. If it's cakephp 2.x, put this code into the controller that has the action you want give access without login. As your question, you should put this code to Posts controller:

function beforeFilter(){
     $this->Auth->allow(array('index','another action'));}

allow(array('acction you want to allow')) instead allow('acction you want to allow')

你在看孤独的风景 2024-09-07 23:15:25

我正在使用 CakePHP 2.x。斜线技巧不起作用。

如果您想允许用户无需登录即可访问“myController.myAction”,您应该将 beforeFilter() 添加到 myController.php 而不是 AppController.php

以下是添加到 myController.php 的代码:

function beforeFilter() {
    parent::beforeFilter();
    $this->Auth->allow('myAction');
}

I am using CakePHP 2.x. The slash trick doesn't work.

If you want to allow user access "myController.myAction" without login, you should add beforeFilter() into myController.php instead of AppController.php

Here is the code to add into myController.php:

function beforeFilter() {
    parent::beforeFilter();
    $this->Auth->allow('myAction');
}
飘落散花 2024-09-07 23:15:25

对于 Cakephp 2.x,有几种方法(取决于 cakephp 版本)。

从文档(http://book.cakephp.org/2.0 /en/core-libraries/components/authentication.html):

// Allow all actions. CakePHP 2.0
$this->Auth->allow('*');

// Allow all actions. CakePHP 2.1
$this->Auth->allow();

// Allow only the view and index actions.
$this->Auth->allow('view', 'index');

// Allow only the view and index actions.
$this->Auth->allow(array('view', 'index'));

For Cakephp 2.x, there are several methods (depending on the cakephp version).

From the docs (http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html):

// Allow all actions. CakePHP 2.0
$this->Auth->allow('*');

// Allow all actions. CakePHP 2.1
$this->Auth->allow();

// Allow only the view and index actions.
$this->Auth->allow('view', 'index');

// Allow only the view and index actions.
$this->Auth->allow(array('view', 'index'));
为你拒绝所有暧昧 2024-09-07 23:15:25

对于 CakePHP 开发人员来说,授权允许特定控制器的特定操作是一个常见问题

https://blog.sohelrana.me/cakephp-auth-allow-specific-actions-specific-controllers/

It’s a common problem to CakePHP developer to auth allow to specific actions of a specific controller

https://blog.sohelrana.me/cakephp-auth-allow-specific-actions-specific-controllers/

迟月 2024-09-07 23:15:25

在 cake 3.x 中,您可以使用以下代码行来允许所有操作。

    public function beforeFilter(Event $event) {
      parent::beforeFilter($event);
      $this->Auth->allow();
    }

In cake 3.x you can use below lines of code to allow all the actions.

    public function beforeFilter(Event $event) {
      parent::beforeFilter($event);
      $this->Auth->allow();
    }
吃颗糖壮壮胆 2024-09-07 23:15:25

$this->name 返回当前请求的控制器。

在 AppController::beforeFilter() 中试试这个

public function beforeFilter()
{

    // ... Basic configs 

    switch ($this->name) {
        case 'Posts':
            $this->Auth->allow('add');
            break;              
        case 'Test':
            $this->Auth->allow('test');
            break;
    }
}

抱歉,我的英语不好

$this->name returns current Controller requested.

try this in AppController::beforeFilter()

public function beforeFilter()
{

    // ... Basic configs 

    switch ($this->name) {
        case 'Posts':
            $this->Auth->allow('add');
            break;              
        case 'Test':
            $this->Auth->allow('test');
            break;
    }
}

Sorry, my english is not good

独守阴晴ぅ圆缺 2024-09-07 23:15:25

对于CakePHP 3.*,在特定控制器允许特定方法

//put this line after namespace
use Cake\Event\Event;

// in your specific controller call this function to allow specific methods
public function beforeFilter(Event $event) {
        parent::beforeFilter($event);
        $this->Auth->allow(['index','view']); //<-- here you put your methods
}

For CakePHP 3.* to allow specific methods in the specific controller

//put this line after namespace
use Cake\Event\Event;

// in your specific controller call this function to allow specific methods
public function beforeFilter(Event $event) {
        parent::beforeFilter($event);
        $this->Auth->allow(['index','view']); //<-- here you put your methods
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文