orisintel / laravel-model-auditlog
Requires
- php: ^7.3|^8.0
- awobaz/compoships: ^2.0.3
- fico7489/laravel-pivot: ^3.0.1
- laravel/framework: ^8.0
- orisintel/laravel-process-stamps: ^3.0
Requires (Dev)
- doctrine/dbal: ^2.9
- larapack/dd: ^1.0
- mockery/mockery: ~1.0
- orchestra/testbench: ^6.0
- phpunit/phpunit: ^9.0
README
在修改模型记录时,能够记录所做的更改以及是谁做出的这些更改是非常好的。已经有很多这样的软件包,但这个软件包与其他软件包的不同之处在于,它将更改记录到单独的表中以提高性能,并支持真实的外键。
安装
您可以通过composer安装此软件包
composer require orisintel/laravel-model-auditlog
配置
php artisan vendor:publish --provider="\OrisIntel\AuditLog\AuditLogServiceProvider"
运行上述命令将发布配置文件。
使用方法
在您的表中添加适当的字段后,将特性添加到您的模型中。
// User model class User extends Model { use \OrisIntel\AuditLog\Traits\AuditLoggable;
要为您的模型生成审计日志模型/迁移,请使用以下命令
php artisan make:model-auditlog "\App\User"
将 \App\User
替换为您自己的模型名称。模型/表选项可以在配置文件中调整。
如果您需要忽略模型上的特定字段,则扩展 getAuditLogIgnoredFields()
方法并返回一个字段数组。
public function getAuditLogIgnoredFields() : array { return ['posted_at']; }
使用该功能,您可以在记录应该记录的内容周围添加更多自定义逻辑。一个例子可能是,如果帖子尚未发布,则不记录帖子的标题更改。
public function getAuditLogIgnoredFields() : array { if ($this->postHasBeenPublished()) { return ['title']; } return []; }
处理枢纽表
审计日志还可以支持枢纽模型上的更改。
在这个例子中,我们有一个包含 post_id
和 tag_id
的 post_tags
枢纽表,其中包含 posts
和 tags
表。
修改审计日志迁移,将 subject_id
列替换为两个枢纽列。
Schema::create('post_tag_auditlog', function (Blueprint $table) { $table->bigIncrements('id'); $table->unsignedInteger('post_id')->index(); $table->unsignedInteger('tag_id')->index(); $table->unsignedTinyInteger('event_type')->index(); $table->unsignedInteger('user_id')->nullable()->index(); $table->string('field_name')->index(); $table->text('field_value_old')->nullable(); $table->text('field_value_new')->nullable(); $table->timestamp('occurred_at')->index()->default('CURRENT_TIMESTAMP'); });
为枢纽表创建一个模型,该模型扩展Laravel的Pivot类。该类必须使用AuditLoggablePivot特性,并定义一个$audit_loggable_keys
变量,该变量用于将枢纽映射到审计日志表。
class PostTag extends Pivot { use AuditLoggablePivot; /** * The array keys are the composite key in the audit log * table while the pivot table columns are the values. * * @var array */ protected $audit_loggable_keys = [ 'post_id' => 'post_id', 'tag_id' => 'tag_id', ]; }
注意:如果枢纽列和审计日志表中的列(例如:user_id
)具有相同的名称,则更改审计日志表中的列名称(例如:audit_user_id
)并定义关系为'audit_user_id' => 'user_id'
。
通过枢纽连接的两个模型需要更新,以便在枢纽模型上触发事件。目前Laravel不支持枢纽事件,因此需要第三方软件包。
composer require fico7489/laravel-pivot
让两个模型都使用PivotEventTrait
use Fico7489\Laravel\Pivot\Traits\PivotEventTrait; use Illuminate\Database\Eloquent\Model; class Post extends Model { use PivotEventTrait;
修改两个相关模型的belongsToMany连接,包括使用函数以及枢纽模型。在Post模型中
public function tags() { return $this->belongsToMany(Tag::class) ->using(PostTag::class); }
在Tag模型中
public function posts() { return $this->belongsToMany(Post::class) ->using(PostTag::class); }
当通过 detach
或 sync
删除枢轴记录时,将为每个键(例如:post_id
和 tag_id
)添加一个审计日志记录到审计日志表中。`field_value_old` 将是记录的 id,而 `field_value_new` 将为空。记录的事件类型将为 PIVOT_DELETED
(id:6)。
如果您需要通过 auditLogs
关系(例如:$post_tag->auditLogs()->get())拉取审计日志,则需要支持复合键。
composer require awobaz/compoships
然后使用枢轴审计日志模型上的 trait
use Awobaz\Compoships\Compoships; use OrisIntel\AuditLog\Models\BaseModel; class PostTagAuditLog extends BaseModel { use Compoships;
有关带有审计日志的枢轴的示例,请参阅 laravel-model-auditlog/tests/Fakes
,其中包含工作迁移和模型。
注意:两个模型都必须使用 AuditLoggable trait(例如:Post 和 Tag),以便 $post->tags()->sync([...])
可以正常工作。
测试
composer test
使用 Docker
所有资产都设置在 docker-compose.yml 文件下。第一次运行 Docker 镜像时,您必须使用以下命令构建它:
docker-compose build
然后您可以使用以下命令将其在后台启动:
docker-compose up -d
并且该镜像被别名,因此您可以通过以下方式访问其命令行:
docker exec -it processes-stamp-app /bin/bash
从那里,您可以在隔离环境中运行测试
贡献
有关详细信息,请参阅 CONTRIBUTING。
安全
如果您发现任何与安全相关的问题,请通过电子邮件 security@orisintel.com 报告,而不是使用问题跟踪器。
鸣谢
许可
MIT 许可证(MIT)。有关更多信息,请参阅 许可文件。