shepherdweb.com

Single User Admin Area Authentication with CakePHP

July 14th, 2006

This script is mostly the work of Shunro Dozono with some tweaks of my own to make it work with a path to /admin. It handles simple administrator authentication for a single user. Ok, here it goes…

Pre-requisite: CakePHP

First, download Shuro’s component, sd_auth. Copy and paste the code into a file called sd_auth.php and put it inside app/controllers/components.

Now, create app_controller.php in app/ and paste the code below:

class AppController extends Controller
{
var $components = array('SdAuth');
function adminAuth()
{
// Auth Check.
if($this->SdAuth->isloggedin() == FALSE)
{
$this->layout = "admin_login";
} else {
$this->layout = "admin";
}
}
}

Next, uncomment the following line in app/config/core.php:

define('CAKE_ADMIN', 'admin');

Now, if you pre-pend a function in any of your controllers with “admin_” (eg: admin_add) it will now be accessed via the path /admin/add. Insert the authentication call at the beginning of each one of those functions:

function admin_add()
{
$this->adminAuth();
}

…and that method will be authenticated before continuing! Thanks for the tip Shunro.

Technorati Tags: , ,

7 Comments »

  1. Richard@Home » Blog Archive » links for 2006-07-16 wrote,

    [...] shepherdweb.com » Single User Admin Area Authentication with CakePHP » Shane Shepherd: web design and development; music Simple admin pages with CakePHP (tags: cakephp admin tutorial) [...]

    Pingback on July 15, 2006 @ 11:18 pm

  2. shunro dozono wrote,

    Hello, I just tested this code. By this code, you don’t have to call adminAuth().
    ———————————–
    class AppController extends Controller {

    var $components = array(’SdAuth’);

    function beforeFilter()
    {

    //for CAKE_ADMIN(admin) rooting.
    if(defined(’CAKE_ADMIN’) && !empty($this->params['admin'])){
    //start admin check.
    if($this->SdAuth->isloggedin() == FALSE)
    {
    $this->layout = “login”;
    } else {
    $this->layout = “admin_default”;
    }
    }

    Comment on July 16, 2006 @ 11:27 pm

  3. Shane wrote,

    @Shunro - Thanks for sharing this; I’ll test it out on my next project!

    Comment on July 17, 2006 @ 5:00 pm

  4. Vangelis wrote,

    It didn’t work for me ….
    “Call to undefined method BlogsController::adminAuth()”

    Comment on May 8, 2007 @ 5:08 am

  5. Shane wrote,

    @Vangelis - Did you remember to add the adminAuth function to your AppController class?

    Comment on May 8, 2007 @ 10:42 am

  6. sganassa wrote,

    link to http://cakeforge.org/snippet/detail.php?type=snippet&id=92 is broken!

    How make I download sd_auth component?

    Comment on July 13, 2008 @ 12:01 pm

  7. Shane wrote,

    @sganassa - You can still download it directly from the authors site:

    http://cakephp.seesaa.net/article/20317502.html

    Comment on July 13, 2008 @ 12:28 pm

Leave a comment

RSS feed for comments on this post.

TrackBack URI

Bad Behavior has blocked 659 access attempts in the last 7 days.