When designing a system for multiuser environment, it is always important to know who created and who modified a record. When using proper field names, such as created and modified, CakePHP automatically sets the proper dates values. We wanted to use the same principal for automatically setting user ID of who created and modified a record. We created a standard for ourself by naming the fields creator_id and modifier_id which goes together with Cake’s date fields created and modified.

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();
	}

	...
}

?>