CakePhp 3.3 - 如何允许访问控制器中不同方法的不同用户角色?

时间:2016-10-18 21:34:14

标签: php cakephp authorization cakephp-3.0

我试图允许不同的Controller方法使用不同的用户角色,官方Dev网站上的教程期望用户角色是两个,但我想与更多的角色合作,我做了一些挖掘这个,但是没有运气。

我试着跟随:

AppController的

class AppController extends Controller
{

    /**
     * Initialization hook method.
     *
     * Use this method to add common initialization code like loading components.
     *
     * e.g. `$this->loadComponent('Security');`
     *
     * @return void
     */
    public function initialize()
    {
        parent::initialize();

        $this->loadComponent('RequestHandler');
        $this->loadComponent('Flash');
        $this->loadComponent('Auth', [
            'authorize' => 'Controller',
            'authenticate' => [
                'Form' => [
                    'fields' => [
                        'username' => 'email',
                        'password' => 'password'
                    ]
                ]
            ],
            /*'loginRedirect' => [
                'controller' => 'Dashboards',
                'action' => 'index'
            ],*/
            'logoutRedirect' => [
                'controller' => 'Pages',
                'action' => 'logout'
            ],
            'loginAction' => [
                'controller' => 'Users',
                'action' => 'login'
            ],
            //'unauthorizedRedirect' => $this->referer()
        ]);

        // Allow the display action so our pages controller
        // continues to work.
        $this->Auth->allow(['login','register']);
    }

    public function isAuthorized($user)
    {
        return true;
    }

UsersController

class UsersController extends AppController
{
    /**
     * Init method
     *
     * @return \Cake\Network\Response|null
     */
    public function initialize()
    {
        parent::initialize();
        //pr("initialize");
    }

    public function isAuthorized($user)
    {
        if (isset($user['role']) && $user['role'] === 'ADMIN' ||
            isset($user['role']) && $user['role'] === 'MANAGER' ) {
            $this->Auth->allow(['logout', 'index','delete']);
            //return;
        }
        if (isset($user['role']) && $user['role'] === 'PARTNER') {
            $this->Auth->allow(['index']);
            //return;
        }
        if (!isset($user['role'])) {
            $this->Auth->allow(['register','index']);
        //return;
        }
        return parent::isAuthorized($user);
    }

具有允许被调用方法的角色的用户能够调用方法,其他用户被重定向到不允许的方法页面。注销后,用户将全局重定向到登录页面。

我根据以下教程尝试了一些组合:

http://book.cakephp.org/3.0/en/tutorials-and-examples/blog-auth-example/auth.html

但没有运气

我怎么能以正确的方式做到这一点?

非常感谢您的任何建议。

1 个答案:

答案 0 :(得分:1)

isAuthorized是错误的调用allow的地方。将allow调用移至beforeFilter,并保留isAuthorized(应该只返回一个布尔值)以获得更细粒度的内容,例如,如果特定用户有权编辑某些项目但不能其他

这样的事情:

public function beforeFilter($event)
{
    parent::beforeFilter($event);
    $user = $this->Auth->user();

    if (isset($user['role']) && $user['role'] === 'ADMIN' ||
        isset($user['role']) && $user['role'] === 'MANAGER' ) {
        $this->Auth->allow(['logout', 'index','delete']);
    }
    if (isset($user['role']) && $user['role'] === 'PARTNER') {
        $this->Auth->allow(['index']);
    }
    if (!isset($user['role'])) {
        $this->Auth->allow(['register','index']);
    }
}