All of our projects use as much of standard CakePHP libraries as possible. That’s why all of our CakePHP projects use AuthComponent for authentication.
The first thing we needed to do to was to create ability to access ID of a currently logged in user anywhere in the application. This was achieved by overloading the core AuthComponent:
controllers/components/loadsys_auth.php
<?php uses('controller/components/auth'); class LoadsysAuthComponent extends AuthComponent { function initialize(&$controller) { ClassRegistry::addObject('LoadsysAuthComponent', $this); parent::initialize($controller); } } class LoadsysAuth { function &getInstance() { static $instance = array(); if (!$instance) { $instance[0] =& ClassRegistry::getObject('LoadsysAuthComponent'); } return $instance[0]; } static function getUser() { $_this =& LoadsysAuth::getInstance(); return $_this->user(); } static function getUserId() { $_this =& LoadsysAuth::getInstance(); $user = $_this->user(); return Set::extract($user, 'User.id'); } static function getUserGroupId() { $_this =& LoadsysAuth::getInstance(); $user = $_this->user(); return Set::extract($user, 'User.user_group_id'); } } ?>
This structure will allow us to access user ID and user group ID from anywhere in the application by calling LoadsysAuth::getUserId() and LoadsysAuth::getUserGroupId(). This could be applied to many situations.
Now we need to include the component in our project:
app_controller.php
class AppController extends Controller { ... var $components = array('LoadsysAuth'); ... }
In order to set the creator_id and modifier_id for, we will have to put it in the base class:
app_model.php
<?php class AppModel extends Model { function beforeSave() { $exists = $this->exists(); if ( !$exists && $this->hasField('creator_id') && empty($this->data[$this->alias]['creator_id']) ) { $this->data[$this->alias]['creator_id'] = LoadsysAuth::getUserId(); } if ( $this->hasField('modifier_id') && empty($this->data[$this->alias]['modifier_id']) ) { $this->data[$this->alias]['modifier_id'] = LoadsysAuth::getUserId(); } return true; } } ?>
In your application models, if you need to override the beforeSave callback, make sure you call the parent function:
Example:
create table articles ( id int unsigned not null auto_increment, title varchar(255) not null, article varchar(255) not null, created datetime, creator_id int unsigned, modified datetime, modifier_id int unsigned, primary key(id) );
models/article.php
<?php class Article extends AppModel { ... function beforeSave() { ... return parent::beforeSave(); } ... } ?>
Nice… I’ve been using a very similar trick from long time. Its really amazing that once you start developing keeping reusability in mind, you start producing some great code. I liked the idea of overloading of the component.
Thanks for this article.
It would be a good suggestion to add it to the core imho.
This is a *really* nice component. I did have a problem with this in that my passwords were being double hashed by the Auth component. The solution was to only load this component in the controllers I’d be using it in.
Actually, on closer inspection this seems to break other parts of the Auth component as well. $this->Auth->allow() ceases to work when this component is loaded as well as the double hashing of passwords as mentioned above. Does the author or anyone else have a possible solution to this?
Nevermind. It seems I was loading Auth as well as LoadsysAuth in my app controller causing the conflicts. $this->LoadsysAuth->allow() now works fine.
Do you mind if I quote a couple of your posts as long as I provide credit and sources back to your site? My blog site is in the exact same area of interest as yours and my users would truly benefit from some of the information you provide here. Please let me know if this ok with you. Many thanks!