Cakephp 3 Permissions - Authentication

0

Good afternoon,

Someone who has already needed to use the restrictions for the user via the bank in an editable way that can help.

At first I followed the initial tutorial creating the users and roles tables, but the client asked me to edit the role access permissions, for example:

Roles | Permissions ADMIN - > all; EDITOR - > controller = > POST, action ADD EDITOR - > controller = > POST, action EDIT CLIENT - > deny

In this way I created the permissions and roles_permissions table and so I tried to pass in isAuthorized to allow or deny without success. If you have a plugin that you use or a better idea, thank you right away.

 public function isAuthorized($user)
 {

    $this->rolePermissionsTable = TableRegistry::get('RolePermissions');

    $rolePermissions = $this->rolePermissionsTable->find()->where(['role_id' => $user['role_id'] ])->all();
   //var_dump($rolePermissions); die;

    $this->rolePermissionsTable = TableRegistry::get('RolePermissions');

    $this->permissionsTable = TableRegistry::get("permissions");
    // Admin pode acessar todas as actions
    foreach ($rolePermissions as $authorized) {
        $permissions = $this->permissionsTable->find()->where(['id', $authorized['permission_id'] ] )->all();


        if ($user['role_id']== $authorized['role_id']) {
            $this->addPermission($permissions);
            $this->Auth->allow('*');
            return true;
        }
        if($user['role_id'] != $authorized['role_id']){
            return false;
        }
    }
   // Bloqueia acesso por padrão
     return false;
 }


 public function addPermission($permissions){
    foreach ($permissions as $permission) {
        $this->Auth->allow(['controller' => $permission['controller'], 'action' => $permission['action'] ]);
    }
 }

public function removePermission ($ permissions) {         foreach ($ permissions as $ permission) {             $ this-> Auth-> deny (['controller' => $ permission ['controller'], 'action' = > $ permission ['action']]);         }      }

    
asked by anonymous 19.12.2017 / 20:14

1 answer

1

I've created a plugin that controls application access in CakePHP 3, implementing ACL with web manager. Initially it comes with 4 groups, being super (the rain sends), admin (it can all but only in the administrative tables, which are groups, users and permissions), manager, that can all but in the business tables example) and user that can not anything, just log in.

You grant and remove permissions via the web, in forms, you can grant permission for actions of controllers or remove.

It also comes with a layout using Bootstrap.

See an online demo here: link

The demo only allows you to select. Install locally for a better experience.

Download link

Any questions tell me.

    
25.04.2018 / 17:31