1

我已按照教程和所有 CakePHP 授权指南进行操作,但无法调用我的 isAuthorized() 方法。我的理解(如果我错了,请纠正我,这极有可能)是通过'authorize'->['Controller']in将授权委托给特定的控制器AppController.php,当调用 UsersController 中的方法时,在本例中为“add”,UsersController 将运行isAuthorized()我定义的方法。我正在测试这个方法是否运行,在isAuthorized()被调用时输出一个 flash->error 消息,但没有任何反应。如果我显式调用isAuthorized($hardcodeduser)我的beforeFilter()方法,它将起作用,但前提是我对用户进行硬编码。

该方法应该工作的方式是:如果注册用户请求添加/创建新用户,系统会检查用户是否具有管理员/员工级别的权限(数据库中只是一个 0 或 1 值)如果用户没有权限,那么它会重定向到主屏幕,并显示一条错误消息,显示“您无权访问该功能”。

任何帮助或建议或其他链接将不胜感激!

class AppController extends Controller {

    public $components = ['Flash', 'Auth', 'Session'];

    public function initialize() {

        $this->loadComponent('Flash');
        $this->loadComponent('Auth', [
            'authorize' => ['Controller'],
            'loginRedirect' => [
                'controller' => 'Articles',
                'action' => 'index'
            ],
            'logoutRedirect' => [
                'controller' => 'Pages',
                'action' => 'display',
                'home'
            ]

        ]);
    }

    public function beforeFilter(Event $event) {
        $this->Auth->authorize = 'Controller';
    }

    public function isAuthorized($user) {

        if(isset($user['is_staff']))
            return true;

        return false;
    }
}


class UsersController extends AppController {

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

    public function isAuthorized($user) {

        $this->Flash->error(__('Test Message PLEASE WORK'));
        if($this->request->action === 'add') {
            $isStaff = $user['is_staff'];
            if($isStaff == 0) {
                $this->redirect($this->Auth->redirectUrl());
                $this->Flash->error(__('Not authorized to access this function'));  
                return false;
            }
        }
        return parent ::isAuthorized($user);
    }
}
4

3 回答 3

9

通常,您的假设是正确的,Controller::isAuthorized()将在使用控制器授权处理程序时自动调用。

您的代码的问题在于,在您的UsersController::beforeFilter()方法中,您明确允许add每个人都可以访问该方法(它甚至不需要身份验证):

$this->Auth->allow(['logout', 'add']);

您必须了解,一旦允许使用方法,auth 组件将不会进行进一步检查,请参阅AuthComponent::startup()

另请注意,您不需要手动重定向和设置 Flash 消息,该组件会为您执行此操作,您只需要使用authErrorunauthorizedRedirect选项对其进行适当配置,请参阅Cookbook > Components > Authentication > Configuration options

于 2014-12-02T22:19:20.597 回答
8

当我们关注 Cake 博客教程时,他们犯了一个小错误,永远不会调用函数“isAuthorized”。我确实花了一些时间来研究它。解决方案是在加载组件“Auth”时添加此行:

'authorize' => array('Controller'),

所以代码应该是这样的:

$this->loadComponent('Auth', [
    'loginRedirect' => [
        'controller' => 'Articles',
        'action' => 'index'
    ],
    'logoutRedirect' => [
        'controller' => 'Pages',
        'action' => 'display',
        'home'
    ],
    'authorize' => array('Controller'),                
]);

希望它可以帮助一些人节省时间:)

于 2015-04-16T04:51:50.367 回答
1

来自 cakephp 3.x 文档:您可以使用数组在控制器的 beforeFilter() 或 initialize() 方法中配置授权处理程序:

// Basic setup
$this->Auth->config('authorize', ['Controller']);

// Pass settings in
$this->Auth->config('authorize', [
'Actions' => ['actionPath' => 'controllers/'],
'Controller'
]);
于 2016-02-08T11:14:21.403 回答