Sammaye/yii-audittrail

语言: PHP

git: https://github.com/Sammaye/yii-audittrail

AuditTrail Yii扩展的一个分支
A fork of the AuditTrail Yii Extension
README.md (中文)

跟踪线索

这基本上是对MadSkillsTisdale在http://www.yiiframework.com/extension/audittrail上进行的扩展的修改。

我基本上已经清理了一些代码,并对此扩展中捆绑的行为做了一些补充。

安装扩展

安装方法已经改变。我已经删除了安装模块的需要,因为:

  • 它仅为审计日志窗口小部件提供全局配置变量
  • 这是额外的膨胀,没有证明需要
  • 我发现在实际系统中,您不希望显示自审核日志以来所有审核日志条目的页面
  • 使用CGridView将审计日志添加到页面非常容易

因此,由于这些原因,模块本身已被删除。

作曲家

此扩展名列在packagist上。

步骤1

要安装,您必须首先选择放置此存储库的文件夹。我已经选择:

/root/backend/extensions/modules

因为这对我来说似乎最合适。将此存储库克隆到该位置。

第2步

是时候安装表了。您可以使用此扩展的原始作者提供的迁移文件,也可以使用迁移文件夹中捆绑的SQL文件。只是 在你的数据库服务器上运行它(使用PHPMyAdmin或其他东西)并观看魔术展开。

第3步

在您的配置中引用AuditTrail模型:

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

注意您可以将AuditTrail移动到模型文件夹,以防止您必须像这样链接它。

步骤4

只需使用模型中的行为,例如:

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

结语

如果您的用户类不是用户,那么您可以(根据您的设置)更改AuditLog模型中的关系以满足您的需求。

API

请注意,以下代码段仅为片段,未在真实环境中进行测试。建议您仅将本部分用作参考和文档,不要 从这里复制并粘贴。

自定义用户属性

有些人实际上没有定义用户,但确实具有可审计模型的属性,该属性将定义编辑它的人的唯一标识。为此,您可以使用:

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

存储时间戳

可以使用以下命令将审核日志的日期更改为使用的时间戳:

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

更改日期格式

您可以使用行为的dateFormat属性调整日期格式:

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

忽略并允许特定字段

这个版本有一个有趣的补充。您现在可以指定一组允许的字段和一组被忽略的字段......或两者。

要做到这一点,包括模型中的行为,就像通常那样:

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

但是然后在行为中添加一个被忽略或允许(或两者)的字段列表,如下所示:

'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'
    )
)

放入行为的允许和忽略参数中的名称表示字段名称。

正如您将注意到我允许ns_purchase_description字段但也忽略它。当您以这种方式使用字段时,忽略将替换允许,并且将省略此字段。

无视整个班级

如果将行为放在扩展CActiveRecord的类中,这是非常有用的,CActiveRecord是您自己的模型扩展而来的。这在您希望所有类共享相同内核(如此行为)而不必在您创建的每个模型中指定它的情况下非常有用。

'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
    ),
)

打印出审核日志

由于这不再使用模块来完成其工作,因此没有用于以前内置的审计日志的全局配置。相反,您可以插入审核日志 喜欢(仅作为示例,显示对书名及其书籍标题页上的产品的更改的审核):

$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))'
        )
    ),
));

为了在CGridView中获得更多用户友好性,您可以使用以下列:

  1. 检索已更改的模型并显示其字符串表示形式: 阵列(     'header'=> Yii :: t('app','Name'),     'value'=>'$ data-> findModel()', )
  2. 如果该字段是关系,请找到相关模型并使用该模型而不是显示FK值: 阵列(     'name'=>'old_value',     'value'=>'$ data-> getOldValue()', ) 阵列(     'name'=>'new_value',     'value'=>'$ data-> getNewValue()', )

本文使用googletrans自动翻译,仅供参考, 原文来自github.com

en_README.md

audittrail

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()',
    ),