Automagically setting user ID of record creator and modifier in CakePHP 1.2
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();
}
...
}
?>
Share This
Tags: automation, cake 1.2, CakePHP, CakePHP Developers, cakephp development, created, modified
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.
May 2nd, 2008 at 3:13 pmThanks for this article.
May 7th, 2008 at 9:12 amIt would be a good suggestion to add it to the core imho.