A blog post

CakePHP Authentication, Authorization and ACL

Posted on the 19 August, 2009 at 12:59 am Written by admin in CakePHP

What’s the difference between Authentication and Authorization?

These two terms are quite often misunderstood. Let’s put it this way, suppose you work in a 100-story office building, each floor has different offices of other companies. You are working on the 49th Floor, cubicle number 20. Authentication is when the security personnel at the building’s front door allows you to come in. Authorization on the other hand, is when you have permission to get to your cubicle, log in to your computer and perform the tasks that you are permitted to do.

So being Authenticated does not automatically mean Authorized, but being Authorized would have to mean you are Authenticated.

Using Basic CakePHP Authentication

The first step is to include the Auth component in your controller. If you are going to use Auth for the whole site, you would want to put this in your /app/app_controller.php.

class AppController extends Controller {
 var $components = array('Auth');

 function beforeFilter() {
 //Configure Auth settings
 $this->Auth->loginError = "No, you fool! That's not the right password!";
 $this->Auth->authError = "Please sign in.";

 //$this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
 //$this->Auth->loginRedirect = array('controller' => 'users', 'action' => 'login');
 //$this->Auth->logoutRedirect = array('controller' => 'users', 'action' => 'login');

 if($this->Auth->user()) {
 //Our user is logged in.
 //User data is stored in $this->Session->read('Auth');
 }
}

Then in your controllers, for example users_controller.php.

function beforeFilter() {
 parent::beforeFilter(); //This line inherits beforeFilter() from app_controller.php
 $this->Auth->deny('*'); //Disallow access to all actions
 $this->Auth->allow('register','logout'); //Allow access to register() and logout()
}

Using CakePHP Authorization

Add the following code to /app/app_controller.php beforeFilter(). Auth->authorize accepts four types, “controller”, “model”, “actions”, “crud”. See the CakePHP manual on Authorization for details.

 //Auth method. If set to controller,
 $this->Auth->authorize = 'controller';

If Auth->authorize is set to “controller”, you’ll need to add a method called isAuthorized() to your controller. This method allows you to do some more authentication checks and then return either true or false. In this case, we put isAuthorized() into our users_controller.php

function isAuthorized() {
 if( $this->action=='index' || $this->action=='view' || $this->action=='add' || $this->action=='edit' || $this->action=='delete') {
 if($this->Auth->user('group_id')==4)
 return true;
 }
 //Redirect to error notification page
 $this->Session->setFlash('Sorry, you don\'t have permission to access that page.');
 $this->redirect('/');
 return false;
}

Using CakePHP ACL (Access Control Lists)

This topic is a wee bit more complicated. I did not proceed using ACL because CakePHP’s Authorization was enough for my needs. ACL is a lot more flexible in granting and denying permissions. Granting and denying access to our users using ACL is quite complicated, fortunately somebody created a CakePHP ACL Management Plugin. It might still take a little while for CakePHP’s ACL to get easier to implement.

some comments

There are currently 2 of them
  1. Paul Gardner 5 May 2010 at 4:42 pm permalink

    I am having problems with where the page gets redirected if isAuthorized returns false. I want my page to go back to the previous page, but it keeps going back to ‘/’.

    In tryign to find a solution for this I came across your page and was inteterested to see that you included some extra logic in your isAuthorized() function. Whilst that would still give me the same result I am curious as to why you include the lines:

    //Redirect to error notification page
    $this->Session->setFlash(‘Sorry, you don\’t have permission to access that page.’);
    $this->redirect(‘/’);

    Paul

  2. Paul Gardner 5 May 2010 at 5:22 pm permalink

    DOH!, my issue was that Controller::redirect did not have a value as I was trying to directly access a controller/action pair that I knew a user was not supposed to have access to, hence it passed the user back to ‘/’.

    To test this properly I had add a link to the controller/action I wanted to test and lo and behold without any extra code isAuthorized = false returns the user back to the page they made the request from.

    Still interested as to why you want all your failed isAuthorized() requests to go back to ‘/’?


reply