如何使用CakePHP Users插件创建公共方法

时间:2016-05-06 19:24:29

标签: php cakephp cakephp-3.0

我使用CakeDC Users插件在CakePHP 3.x上创建简单的基于角色的授权。

我有一堆/ admin / routes需要登录才能访问。我创建了一个我希望公开的新动作,但出于某种原因,我一直被发送到登录状态。这是我现有的permissions.php:

return [
    'Users.SimpleRbac.permissions' => [
        # Admins can do anything
        [
            'role' => 'admin',
            'plugin' => '*',
            'prefix' => '*',
            'controller' => '*',
            'action' => '*',
        ],
        # Users can only see a list and view an item
        [
            'role' => 'user',
            'plugin' => false,
            'prefix' => 'admin',
            'controller' => '*',
            'action' => ['index', 'view', 'search'],
        ],
        # Only allow a user to logout within the CakeDC/User's plugin
        [
            'role' => 'user',
            'prefix' => '*',
            'plugin' => 'CakeDC/Users',
            'controller' => 'Users',
            'action' => ['logout'],
         ],
    ],
];

以下是我尝试添加没有工作的内容:

        ...
        # anyone is allowed to do the .../dispatch method
        [
            'role' => '*',
            'controller' => '*',
            'action' => ['dispatch'],
         ],

我错过了什么?

1 个答案:

答案 0 :(得分:1)

事实证明我的问题与CakeDC / Users插件几乎没什么关系。看来我的"非CRUD"动作名称导致CakePHP不允许它。我不完全明白为什么。但是当我加上......

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

一切都按预期工作。

相关问题