Single User Admin Area Authentication with CakePHP
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.
[...] 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
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
@Shunro – Thanks for sharing this; I’ll test it out on my next project!
Comment on July 17, 2006 @ 5:00 pm
It didn’t work for me ….
“Call to undefined method BlogsController::adminAuth()”
Comment on May 8, 2007 @ 5:08 am
@Vangelis – Did you remember to add the adminAuth function to your AppController class?
Comment on May 8, 2007 @ 10:42 am
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
@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