sammaye/auditrail2 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

sammaye/auditrail2

Composer 安装命令:

composer require sammaye/auditrail2

包简介

A reload of the popular audit trail extension

README 文档

README

This is basically a modification of a previous extension made by MadSkillsTisdale at http://www.yiiframework.com/extension/audittrail.

I have basically cleaned up some of the code and made a few additions to the behaviour bundled within this extension.

Installing the extension

The method of installation has changed. I have removed the need to install a module since:

  • It only provided global configuration variables for the audit log widget
  • It was extra bloat that didn't justify the needs
  • I found that in a real system you wouldn't want a page showing all audit log entries since the audit logs
  • The audit log is quite easy to add to a page using CGridView

As such, for these reasons, the module itself has been deleted.

Composer

This extension is listed on packagist.

Step 1

To install you must first choose a folder in which to place this repository. I have chosen:

/root/backend/extensions/modules

Since this seems most right to me. Clone this repository to that location.

Step 2

Time to install the table. You can use the migration file provided by the original author of this extension or you can use the SQL file bundled within the migrations folder. Simply run it on your DB server (using PHPMyAdmin or something) and watch the magic unfold.

Step 3

Reference the AuditTrail model within your configuration:

'import'=>array(
	'site.backend.extensions.modules.auditTrail.models.AuditTrail',
),

Note You can move AuditTrail to your models folder preventing you from having to link it like this.

Step 4

Simply use the behaviour within a model like:

'LoggableBehavior'=> array(
	'class' => 'site.backend.extensions.modules.auditTrail.behaviors.LoggableBehavior',
)

Epilogue

If your user class is not User then you may (depending on your setup) need to change the relation within the AuditLog model to suite your needs.

API

Please note that the below snippets are snippets only and are not tested in a real environment. It is recommend that you use this section as reference and documentation only, do not copy and paste from here.

Custom User Attributes

Some people don't actually have defined users but do have an attribute of the auditable model that would define a unique identification of who edited it. For this end you can use:

'LoggableBehavior'=> array(
	'class' => 'site.backend.extensions.modules.auditTrail.behaviors.LoggableBehavior',
	'userAttribute' => 'name'
)

Storing Timestamps

The date of the audit log can be changed to used timestamps instead using:

'LoggableBehavior'=> array(
	'class' => 'site.backend.extensions.modules.auditTrail.behaviors.LoggableBehavior',
	'storeTimestamp' => true
)

Changing the date format

You can adjust the date format using the dateFormat property of the behaviour:

'LoggableBehavior'=> array(
	'class' => 'site.backend.extensions.modules.auditTrail.behaviors.LoggableBehavior',
	'dateFormat' => 'Y-m-d H:i:s'
)

Ignoring and allowing specific fields

There is one interesting addition to this version. You can now specify an allowed set of fields and a ignored set of fields...or both.

To do this include the behaviour in your models like you normally would:

'LoggableBehavior'=> 'site.backend.extensions.modules.auditTrail.behaviors.LoggableBehavior'

But then add either an ignored or allowed (or both) list of fields to the behaviour like so:

'LoggableBehavior'=> array(
	'class' => 'site.backend.extensions.modules.auditTrail.behaviors.LoggableBehavior',
	'allowed' => array(
		'version',
		'ns_purchase_description'
	),
	'ignored' => array(
		'ns_purchase_description',
		'ns_display_name',
		'update_time'
	)
)

The names put into the allowed and ignored parameters of the behaviour represent field names.

As you will notice I allow the ns_purchase_description field but also ignore it. When you use the fields in this way ignored will replace the allowed and this field will be omitted.

Ignoring a whole class

This is useful if you put the behaviour in a class that extends CActiveRecord which all your own models extend from. This is useful in cases where you want ALL your classes to share the same core (like this behaviour) without having to specify it in every model you create.

'LoggableBehavior'=> array(
	'class' => 'site.backend.extensions.modules.auditTrail.behaviors.LoggableBehavior',
  	'ignored_class' => array(
		'ErrorLog',  // I use this to log error messages to MYSQL, no need to keep a log of this
	),
)

Printing out the audit log

Since this no longer uses a module to do its work there is no global configuration for the previously inbuilt audit log to work from. Instead you can insert an audit log like (as an example only, showing an audit of changes to a book title and it's products on a book title page):

$model_ids = array(array($model->id, 'Title'));
foreach($model->products as $id => $product){
    $model_ids[] = array($product->id, 'Product');
}

$criteria=new CDbCriteria(array(
    'order'=>'stamp DESC',
    'with'=>array('user'),
));
$param_id = 0;
foreach( $model_ids as $id_pair ) {
    $criteria->addCondition( '( model_id = :id' . $param_id . ' AND model = :model' . $param_id . ' )', 'OR' );
    $criteria->params[ ':id' . $param_id ] = $id_pair[0];
    $criteria->params[ ':model' . $param_id ] = $id_pair[1];
    $param_id++;
}

$this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'title-grid',
    'dataProvider'=>new CActiveDataProvider('AuditTrail', array(
        'criteria'=>$criteria,
        'pagination'=>array(
            'pageSize'=>100,
        )
    )),
    'columns'=>array(
        array(
            'name' => 'Author',
            'value' => '$data->user ? $data->user->email : ""'
        ),
        'model',
        'model_id',
        'action',
        array(
            'name' => 'field',
            'value' => '$data->getParent()->getAttributeLabel($data->field)'
        ),
        'old_value',
        'new_value',
        array(
            'name' => 'Date Changed',
            'value' => 'date("d-m-Y H:i:s", strtotime($data->stamp))'
        )
    ),
));

For more user-friendliness in the CGridView, you can use these columns:

  1. Retrieves the changed model and shows its string representation:

     array(
         'header' => Yii::t('app', 'Name'),
         'value' => '$data->findModel()',
     ),
    
  2. If the field is a relation, find the related model and use that instead of showing the FK value:

     array(
         'name' => 'old_value',
         'value' => '$data->getOldValue()', 
     ),
     array(
         'name' => 'new_value',
         'value' => '$data->getNewValue()',
     ),
    

sammaye/auditrail2 适用场景与选型建议

sammaye/auditrail2 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 18.16k 次下载、GitHub Stars 达 33, 最近一次更新时间为 2013 年 04 月 08 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「Audit」 「yii」 「trail」 「phundament」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 sammaye/auditrail2 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 sammaye/auditrail2 我们能提供哪些服务?
定制开发 / 二次开发

基于 sammaye/auditrail2 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 18.16k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 33
  • 点击次数: 20
  • 依赖项目数: 7
  • 推荐数: 0

GitHub 信息

  • Stars: 33
  • Watchers: 9
  • Forks: 13
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-2-Clause
  • 更新时间: 2013-04-08