obAuth Simple Authentication
Authenticate your users and secure your controller actions from users belonging to certain groups. Or simply secure your controller actions to let any authenticated user to access it.
How to use: http://bakery.cakephp.org/articles/view/121
Some features:
1. User authentication
2. User groups supported
3. Protect action access with 1 line of code
Component Class:
<?php
class obAuthComponent extends Object
{
/* Component config variables */
var $user_model = "User";
var $group_model = "Group";
var $user_fields = array('id' => 'id', 'username' => 'username', 'password' => 'password', 'group_id' => 'group_id');
var $group_fields = array('id' => 'id', 'name' => 'name');
var $components = array('Session');
var $login_page = 'users/login'; // login action
var $logout_page = null; // Page to redirect to when user logs out
var $deny_page = null; // Page to redirect if you deny access but don't want take user to login page
var $sesskey = "mYpERsOnALhaSHkeY";
/* Don't modify these variables */
var $last_page = null;
var $user = null;
var $controller;
function startup(&$controller)
{
$this->controller = $controller;
if ($this->Session->valid() && $this->Session->check($this->sesskey))
{
$this->user = $this->Session->read($this->sesskey);
}
$this->controller->set('obAuth', $this->user);
}
// Method to check if user is logged.
function login($data)
{
$username = $data["{$this->user_fields['username']}"];
$password = $data[$this->user_fields['password']];
$conditions = array($this->user_model.".".$this->user_fields['username'] => $username, $this->user_model.".".$this->user_fields['password'] => md5($password), $this->user_model.".active" => 1);
$user = $this->controller->{$this->user_model}->find($conditions);
if (empty($user)) {
return false;
} else {
$sessdata["{$this->user_model}"]['id'] = $user["{$this->user_model}"]["{$this->user_fields['id']}"];
$sessdata["{$this->user_model}"]['username'] = $user["{$this->user_model}"]["{$this->user_fields['username']}"];
$sessdata["{$this->user_model}"]['password'] = $user["{$this->user_model}"]["{$this->user_fields['password']}"];
$sessdata["{$this->group_model}"]['id'] = $user["{$this->group_model}"]["{$this->group_fields['id']}"];
$sessdata["{$this->group_model}"]['name'] = $user["{$this->group_model}"]["{$this->group_fields['name']}"];
$sessdata["{$this->user_model}"]['login_hash'] = md5($this->sesskey . $sessdata["{$this->user_model}"]['username'] . $sessdata["{$this->user_model}"]['password'] . $sessdata["{$this->group_model}"]['id']);
$this->Session->write($this->sesskey, $sessdata);
return true;
}
}
// Logout user and destroy cookie
function logout($redirect=null)
{
$this->user = null;
$this->Session->delete($this->sesskey);
$page = (!empty($redirect)) ? $redirect : $this->logout_page;
$this->controller->redirect($page);
}
// Check is user is part of usergroup specified
function lock($groups=null, $redirect=null)
{
$hasAccess = false;
// User page tracker
if ($this->controller->action != "login")
{
$this->last_page = $this->controller->here;
}
if (!empty($this->user))
{
if (!empty($groups))
{
foreach ($groups as $group)
{
if ($this->user["{$this->group_model}"]['id'] == $group || $this->user["{$this->group_model}"]['name'] == $group)
$hasAccess = true;
}
}
else
{
$hasAccess = true;
}
}
if(!$hasAccess)
{
$page = (!empty($redirect)) ? $redirect : $this->login_page;
$this->controller->redirect($page);
}
}
function deny($redirect=null)
{
$page = (!empty($redirect)) ? $redirect : $this->deny_page;
$this->controller->redirect($page);
}
function getUserId()
{
return (!empty($this->user)) ? $this->user["{$this->user_model}"]['id'] : false;
}
function getGroupId()
{
return (!empty($this->user)) ? $this->user["{$this->group_model}"]['id'] : false;
}
}
?>

This is more efficient:$data["{$this->user_fields['username']}"];
$data[ $this->user_fields['username'] ];
Warning: Cannot modify header information - headers already sent by (output started at /home/barbier/public_html/app/controllers/components/oth_auth.php:299) in /home/barbier/public_html/cake/libs/controller/controller.php on line 447
Any ideas why? I've already checked all files on my proyect but still getting the error.
I've read on the explanation page that this component stops working with Cake 1.18. Are you planning any fixes/updates?
thanks
I have a few questions:
Thank you for this useful work.
Carlo.
As for the user information, you can access all of the user information through the $this->obAuth->user variable in the controller you're working in.
I should be updating this component soon.
Getting an Error Undefined property: obAuthComponent::$User [CORE\app\controllers\components\ob_auth.php, line 47]
I'm having a bit of trouble trying to figure out how to fix this.
$conditions = array($this->user_model.".".$this->user_fields['username'] => $username, $this->user_model.".".$this->user_fields['password'] => md5($password), $this->user_model.".active" => 1);
I ran into that too. The usergroup line is just plain wrong - $this->user is an array, not an object, and it actually generates an error anyway. Delete it.
$sesskey is used when building the session data or reading from it, but as you say, it's not used in lock(), so delete it.
As for getUserId etc, they could be used in login(), but it's probably faster to leave that lookup inlined as it is. I don't know why I'd need them apart from that.
When I released this component there were unfinished parts. For example the $this->last_page part is supposed to store the last page the user visited. For example, if a user tries to access "add members" section, they'll first be redirected to login page, once they login, they'll be then redirected to that page they were trying to access. This is still unfinished.
I see where the $usergroup and $sesskey variables are assigned, but don't see them used anywhere.
Secondly, I'm unclear about what this bit of code does in the lock method:
// User page tracker
if ($this->controller->action != "login")
{
$this->last_page = $this->controller->here;
}
One last question: where are the getUserId and getGroupId methods used? I searched around and didn't come up with anything.
Thanks!
Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /Library/WebServer/Documents/cake_1.1.12.4205/ngo_reservation/controllers/components/ob_auth.php:118) in /Library/WebServer/Documents/cake_1.1.12.4205/cake/libs/session.php on line 146