Authorization and ACL in cakephp 3

旧城冷巷雨未停 提交于 2019-12-04 08:38:51

问题


I search the document but I don't find anything about ACL implementation in cakephp 3. How can I implement authorization with ACL in cakephp 3?


回答1:


ACL is not built into CakePHP 3 as it was in CakePHP 2. It is now available as a separate plugin.

Quote from http://book.cakephp.org/3.0/en/appendices/3-0-migration-guide.html

ACL related classes were moved to a separate plugin. Password hashers, Authentication and Authorization providers where moved to the \Cake\Auth namespace. You are required to move your providers and hashers to the App\Auth namespace as well.

You can find the plugin at https://github.com/cakephp/acl, but note that it's not yet stable.




回答2:


Great question, as Daniel Castro said the plugin is at https://github.com/cakephp/acl.

The part that is missing is to override 'isAuthorized' in your 'AppController.php' with something like:

...
use Acl\Controller\Component\AclComponent;
use Cake\Controller\ComponentRegistry;
...



public function isAuthorized($user){
      $Collection = new ComponentRegistry();
      $acl= new AclComponent($Collection);
      $username=$user['username'];
      $controller=$this->request->controller;
      $action=$this->request->action;
      $check=$acl->check($user['username'],"$controller/$action");
      return $check;
    }

Someone wiser than I will know better if the user/action/controller bits could be better sanitized. There are lots of warnings about the stability of this plugin and 'gotchas' on acl in terms of performance.

I am cutting over from a 1.3 implementation, it was helpful to add in the AppController 'initialize' info from http://book.cakephp.org/3.0/en/controllers/components/authentication.html



来源:https://stackoverflow.com/questions/27728798/authorization-and-acl-in-cakephp-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!