Search by

jeffersonsimaogoncalves / cakephp-user-activity

jeffersongoncalves

CakePHP 3 UserActivity Logging plugin

Package info

github.com/jeffersongoncalves/cakephp-user-activity

Issues

Type:cakephp-plugin

pkg:composer/jeffersonsimaogoncalves/cakephp-user-activity

Statistics

Installs: 20

Dependents: 0

Suggesters: 0

Stars: 1

1.1.0 2025-10-20 14:06 UTC

README

CakePHP User Activity

CakePHP 3 UserActivity Logging plugin

Buy Me A Coffee

Logs every Create, Update and Delete made through the CakePHP ORM: attach UserActivityBehavior to a table and every save/delete on it is recorded in a logs row (who, when, which table/row, which action) plus one logs_details row per changed field (old value / new value).

Based on crabstudio/UserActivity.

Requirements

  • PHP >=7.0
  • CakePHP cakephp/cakephp ^3.6

Installation

composer require jeffersonsimaogoncalves/cakephp-user-activity

Load the plugin in config/bootstrap.php:

Plugin::load('JeffersonSimaoGoncalves/UserActivity');

Create the logs and logs_details tables in your database (bake a migration for them with cakephp/migrations, or create them manually) — LogsTable expects at least: id, table_name, database_name, primary_key, action, description, created_by, name, recycle, created, modified; LogsDetailsTable expects: id, log_id, field_name, old_value, new_value, created, modified.

Usage

Attach the behavior to any table you want to log:

// src/Model/Table/UsersTable.php
public function initialize(array $config)
{
    parent::initialize($config);

    $this->addBehavior('JeffersonSimaoGoncalves/UserActivity.UserActivity');
}

From then on, every save() (create or update) and delete() on that table writes a Log entity (action = C/U/D, table_name, database_name, primary_key as JSON, a Portuguese description such as "Criado um registro em users com sucesso") plus one LogsDetail row per changed column, capturing old_value/new_value (JSON-encoded for json-typed columns). Deletes are recorded as recycle = true with every column's current value logged as old_value.

Attributing the current user

created_by/name on each log entry come from an app-defined getUserAuth() function, if one exists in the global namespace:

function getUserAuth(string $field)
{
    $user = \Cake\Routing\Router::getRequest()->getAttribute('identity');

    return $user ? $user->get($field) : null;
}

Without a getUserAuth() function, created_by/name are left null.

Using a separate database connection

Configure::write('JeffersonSimaoGoncalves/UserActivity.connection', 'log_db');

Querying recent activity

$Logs = TableRegistry::getTableLocator()->get('JeffersonSimaoGoncalves/UserActivity.Logs');
$recent = $Logs->find('latest', ['limit' => 50]);

Credits

License

The MIT License (MIT). Please see LICENSE for more information.