robwilkerson/cakephp-audit-log-plugin

CakePHP 审计插件

安装数: 60,533

依赖: 0

建议者: 0

安全: 0

星标: 104

关注者: 13

分支: 78

开放问题: 17

类型:cakephp-plugin

2.30 2019-02-09 21:16 UTC

This package is not auto-updated.

Last update: 2024-09-28 18:47:58 UTC


README

CakePHP 2.x 的日志插件。包含的 AuditableBehavior 会为每个附加到模型的实例创建审计历史。

行为在两个层面上跟踪更改。在更改完成后,它会捕捉完全填充的对象快照,并在更新操作的情况下记录每个单独的更改。

特性

  • CakePHP 2.2+ 支持。感谢 @jasonsnider。
  • 跟踪对象快照以及单个属性更改。
  • 允许将每个修订记录附加到更改的责任来源 -- 通常是一个用户。
  • 允许开发者忽略特定属性的更改。默认情况下,忽略名为 createdupdatedmodified 的属性,但这些值可以被覆盖。
  • 处理 HABTM 关联的更改。
  • PolymorphicBehavior 完全兼容。
  • SoftDelete 完全兼容。

版本

CakePHP 2.x

使用当前主分支,并按照以下说明操作。

CakePHP 3.x

现在 dev-3.x 分支专门用于任何 CakePHP 3.x 开发,但仍处于工作状态。如果您正在寻找工作解决方案,可以查看 Awesome CakePHP 中列出的插件。

CakePHP 1.3.x

使用 1.3 分支的代码,并按照该 README 文件中的说明操作。请注意,此版本的开发已结束。

安装(2.x)

安装选项

使用 Composer

从您的 /app/ 文件夹运行

$ composer require robwilkerson/cakephp-audit-log-plugin

这将自动更新您的 composer.json 文件,下载并安装所需的包。

作为存档

  1. 点击项目描述旁边的“下载”按钮。
  2. 将存档提取到 app/Plugin/AuditLog

作为子模块

  1. $ git submodule add git://github.com/robwilkerson/CakePHP-Audit-Log-Plugin.git <path_to>/app/Plugin/AuditLog
  2. $ git submodule init
  3. $ git submodule update

加载插件

app/Config/bootstrap.php 中添加以下行

CakePlugin::load('AuditLog');

设置数据库

要创建必要的表,您可以使用模式外壳。从您的 /app/ 文件夹运行

php ./Console/cake.php schema create -p AuditLog

这将创建存储每个对象的更改历史的 auditsaudit_deltas 表。

下一步

  1. 如有需要,创建一个 currentUser() 方法。

    AuditableBehavior 允许每个更改集由一个“来源”拥有,通常是负责更改的用户。由于用户和认证模型差异很大,该行为支持一个回调方法,该方法应返回要存储为更改来源的值,如果有的话。

    currentUser() 方法必须对所有关心跟踪更改来源的模型可用,因此我建议创建 CakePHP 的 AppModel.php 文件的副本并添加该方法。保持 DRY,对吧?

    如果核心Auth组件正在使用,存储变更集源可能会有些棘手,因为用户数据并不在行为所在的模型层 readily 可用。一个选项是将这些数据从控制器转发过来。一种实现方式是在AppController::beforeFilter()中包含以下代码。

    if (!empty($this->request->data) && empty($this->request->data[$this->Auth->userModel])) {
    	$user['User']['id'] = $this->Auth->user('id');
    	$this->request->data[$this->Auth->userModel] = $user;
    }

    该行为期望currentUser()方法返回一个包含id键的关联数组。也可以设置一个description键来添加关于用户的额外细节。从上面的例子继续,以下代码可能会出现在AppModel中。

    /**
     * Get the current user
     *
     * Necessary for logging the "owner" of a change set,
     * when using the AuditLog behavior.
     *
     * @return mixed|null User record. or null if no user is logged in.
     */
    public function currentUser() {
        App::uses('AuthComponent', 'Controller/Component');
        return array(
            'id' => AuthComponent::user('id'),
            'description' => AuthComponent::user('username'),
        );
    }
  2. 将行为附加到任何所需的模型并配置以满足您的需求。

配置

AuditableBehavior应用于模型基本上与将任何其他CakePHP行为应用于模型相同。该行为确实提供了一些配置选项。

  • ignore 一个要忽略的属性名称数组,在记录创建在delta表中时使用。
  • habtm 一个模型数组,这些模型与操作模型有HABTM关系,并且其更改应与模型一起监控。如果HABTM模型本身可审计,请不要将其包含在此处。此选项用于仅相对于操作模型跟踪更改的相关模型。
/**
 * A model that uses the AuditLog default settings
 */
class SomeModel extends AppModel {

	/**
	 * Loading the AuditLog behavior without explicit settings.
	 * 
	 * @var array
	 */
	public $actsAs = array('AuditLog.Auditable');

	// More model code here.
}

/**
 * A model that sets explicit AuditLog settings
 */
class AnotherModel extends AppModel {

	/**
	 * Loading the AuditLog behavior with explicit settings.
	 * 
	 * @var array
	 */
	public $actsAs = array(
		'AuditLog.Auditable' => array(
			'ignore' => array('active', 'name', 'updated'),
			'habtm' => array('Type', 'Project'),
		)
	);

	// More model code here.
}

回调

插件提供了多个回调,允许在执行此插件的操作之前或之后执行额外的逻辑。

要添加自定义回调,请在其审计模型中创建以下名称之一的方法。

afterAuditCreate

afterAuditCreate(Model $Model)

当在审计表中插入新记录时触发。

  • $Model 创建记录的模型的名称。

afterAuditUpdate

afterAuditCreate(Model $Model, array $original, array $updates, int $auditId)

当在审计表中更新记录时触发。

  • $Model 更新记录的模型的名称。
  • $original 包含保存前对象的副本。
  • $updates 已更新的记录集。
  • $auditId Audit表中记录的ID。

afterAuditProperty

afterAuditCreate(Model $Model, string $propertyName, string $oldValue, string $newValue)

每次将属性插入到audit_deltas表时都会触发。

  • $Model 创建/更新记录的模型的名称。
  • $propertyName 属性(字段名)的名称。
  • $oldValue 属性的原始值。
  • $newValue 属性的新值。

许可协议

此代码根据MIT许可协议许可。

备注

请随时在票据中提交错误报告或提出改进建议,或者分叉此项目并自行改进。欢迎贡献。