CakePHP Authentication, Authorization and ACL

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.

2 Responses to “CakePHP Authentication, Authorization and ACL”

  1. Paul Gardner May 5, 2010 at 4:42 pm #

    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 May 5, 2010 at 5:22 pm #

    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 ‘/’?

Leave a Reply

  • freida pinto boyfriend
  • search xml file
  • la ink 2011 season 5
  • new england patriots 84
  • randy moss football cards
  • bea luna
  • chicago bears zip hoodie
  • tea party for kids
  • search engines other than google
  • search engines compared
  • sitcoms
  • search comcast net
  • zara phillips yachtzara phillips zimbio
  • search and seizure
  • wrestling
  • connecticut post
  • chicago bears posters
  • zara phillips baby
  • mtv 90s music videos
  • zara phillips and the queen
  • tea party young people
  • randy moss university
  • connecticut limo
  • kill
  • search engines of the world
  • chicago bears garter
  • search operatorssearch people
  • greg olsen twitter
  • battleship kirishima
  • connecticut lottery
  • greg olsen 2009 calendar
  • mtv music awards
  • search engines internet
  • stewart
  • hp support error 1005
  • chicago bears pictures
  • arrested
  • battleship aurora
  • connecticut quarter error
  • zara phillips school
  • search engines for jobs
  • hp support id
  • la ink bam margera
  • search engines before google
  • halo
  • la ink price list
  • wharehouse
  • dis x
  • hp support contact number
  • vince young 2008
  • dis boards cruise
  • greg olsen university of miami
  • vince young uncle rico
  • new england patriots needs
  • quarters
  • rinse
  • cspan michelle bachmann
  • dis windsor wi
  • battleship classes
  • moble
  • ncaa
  • c span yesterdayc span zelaya
  • tea party ribbons
  • dedham
  • tea party gifts
  • vince young depression
  • bea binene
  • chad ochocinco xpchad ochocinco youtube
  • bengals 08 schedule
  • tea party texas
  • hp support center
  • manicure
  • new england patriots espn blog
  • ipods
  • revival
  • new england patriots gillette stadium
  • zara phillips guest list
  • johanson
  • search 50 cent
  • chicago bears 96
  • hp support center
  • randy moss legal issues
  • search in vi
  • winnipeg
  • new england patriots helmet
  • jenkins
  • bea taylor
  • greg olsen mormon
  • hp support englandhp support forum
  • search engines for kids
  • dis unplugged show notes
  • charges
  • di's hallmark
  • tuneup
  • bengals kids jersey
  • connecticut department of labor
  • freida pinto glamour 2011
  • patten
  • c span yesterdayc span zelaya
  • prediction
  • chad ochocinco sisterchad ochocinco twitter
  • battleship history
  • tea party zombies download
  • disloyaldis magazine
  • christians
  • battleship egg hunt
  • hp support driver downloads
  • la ink watch online free
  • bixby
  • scopes
  • search protocol host
  • vince young z
  • parade
  • charities
  • kayak
  • badges
  • new england patriots 3 4
  • bengals hard knocks episode 1
  • mtv rivals
  • samba
  • zara phillips husband
  • bedspread
  • mtv 25 lame
  • battleship yamato wreck
  • holster
  • tumbling
  • connecticut natural gas
  • randy moss mix
  • bea rims
  • bengals undraftedbengals vs steelers
  • bea 71 16
  • avondale
  • greg olsen dustin keller
  • search dog foundation
  • attempt
  • brownie
  • cspan streaming
  • battleship excel
  • chicago bears bleacher report
  • bengals tryouts
  • hp support contact us
  • chad ochocinco bears
  • flames
  • la ink upcoming episodes
  • battleship galactica
  • mtv overdrive
  • connecticut 5 star resorts
  • connecticut education
  • bea 2011 map
  • zara phillips dating
  • hp support greece
  • hp support greece
  • battleship aurora
  • vince young yahoo stats
  • thom
  • la ink youtube pixie
  • la ink corey
  • battleship lexington
  • mounts
  • 4pm cspancspan area 51cspan 90.1
  • tea party zombies download
  • new england patriots 1997 roster
  • chicago bears schedule 2011
  • randy moss height
  • randy moss wallpaper
  • c span 4 to 5
  • pans
  • connecticut airports
  • tea party medicare
  • greg olsen vancouver
  • zara phillips royal wedding picture
  • new england patriots xxl
  • roth
  • battleship layout
  • bengals images
  • search jail inmates
  • mtv jams
  • dis pater
  • chad ochocinco 15
  • chad ochocinco and cheryl burke
  • graphs
  • goldfinger
  • disassembledis boards
  • new england patriots 1996 roster
  • search engines rankings 2011
  • tyson
  • affairs
  • certified
  • chicago bears media relations