In this post i am going to explain how to implement Access Control List (ACL) in CakePHP Application. If you want to learn some more about CakePHP please follow this link. To implement ACL in CakePHP application you need have good basic knowledge of CakePHP. Now without wasting any time we can start creating CakePHP application. Please follow this step.

 

Step 1. Now create new project using composer

Cakephp3 ACL Implementation

Step 2. Now download CakePHP ACL plugin from github using composer. Go to your project directory and run the following command

Step 3. Open your bootstrap.php file and add your plugin

Step 4. Create ACL table by running below command

Step 5. Now we need to create some more table for our application like Users, Groups, Posts run below schemas on your server

Step 6. Now bake your application using cake bake run bellow command one after one

Step 7. Now once you bake your MVC open your UsersControllers.php and add login() and logout() function in it and also create view for login.

Login.ctp

Step 8. Now open your UsersTable.php and replace code with below one

and after this open your User.php inside Entity and paste bellow code

Step 9. Now open your GroupsTables.php and paste below code in it

after this open your Group.php and add below code

Step 10.  Now we can override Auth Temporary for some time add below method in your GroupsController.php and UsersController.php to create Group and add User.

Step 11. Now open your AppController.php and configure the AuthComponent and the AclComponent

Now after this go to browser and type : localhost/cakeacl/groups/add and create group like Admin, User etc. And after adding this go to: localhost/cakeacl/users/add and create two user with specific role.

Create ACL ACOs

Now we need to run command to automatically sync our table in ACL ACOs run below command to do that

Now remove that temporary Auth we add in step 10.

Permissions using the ACL shell

Now we can set permission to group which what can see like Admin can do anything but user can have limited access.

  • Grant permission to all controller:  bin/cake acl grant Groups.1 controllers
  • Deny permission for all controller: bin/cake acl deny Groups.1 controllers
  • For specific controller: bin/cake acl grant Groups.2 controllers/Posts
  • For specific method: bin/cake acl grant Groups.3 controllers/Posts/index

If you face any permission issue on ubuntu run below command

Now you can login and check That’s all hope this will help you to create your Cake Application. 🙂