testmonitor/laravel-accountable

追踪创建、修改或删除 Eloquent 模型的用户

8.0.0 2023-06-28 10:45 UTC

This package is auto-updated.

Last update: 2024-09-15 09:54:30 UTC


README

Latest Stable Version CircleCI StyleCI codecov License

此包提供了一个特质,用于追踪创建、修改或删除 Eloquent 模型的用户。

Accountable 将观察您模型上的任何活动,并使用当前认证用户相应地设置 created_by_user_idupdated_by_user_iddeleted_by_user_id

它还为您提供了有用的作用域查询函数,允许您获取由特定用户创建、修改或删除的模型。

目录

安装

此包可以通过 Composer 安装。

$ composer require testmonitor/laravel-accountable

包将自动注册自身。

可选,发布配置文件

$ php artisan vendor:publish --provider="TestMonitor\Accountable\AccountableServiceProvider" --tag="config"

配置文件允许您设置首选认证驱动程序、数据库列名和匿名用户。后者可以用于处理未认证用户创建/更新的记录。

如果保持不变,Accountable 将使用默认认证驱动程序和默认列名(created_by_user_idupdated_by_user_iddeleted_by_user_id)。

使用方法

要将 Accountable 添加到您的 Laravel 应用程序中,您需要

  1. 将所需的列添加到您的迁移文件中。
  2. 在您的模型上使用特质 TestMonitor\Accountable\Traits\Accountable

请注意,由于 Laravel 事件系统的性质,Accountable 不会处理批量更新。

使用迁移助手

迁移助手简化了向迁移中添加列的过程

use TestMonitor\Accountable\Accountable;

class CreateProjectsTable extends Migration
{
    public function up()
    {
        Schema::create('projects', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');

            $table->timestamps();
            $table->softDeletes();

            // This will add the required columns
            Accountable::columns($table);
        });
    }
}

提示:如果您不使用软删除在您的模型上,使用 Accountable::columns($table, false) 防止助手添加 deleted_by_user_id 列。

使用特质

在您要跟踪的模型上添加 Accountable 特质

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use TestMonitor\Accountable\Traits\Accountable;

class Project extends Model
{
    use Accountable, SoftDeletes;

    protected $table = 'projects';
}

示例

设置您的模型并确保您已认证。

基础

创建一个项目并显示创建该项目的用户名称

$project = new Project(['name' => 'Awesome project']);
$project->save();

// Show the name of user that created the project
echo $project->creator->name;

获取由特定用户创建的所有项目

$user = User::findOrFail(42);

// Get all projects created by user with id 42
Project::onlyCreatedBy($user)->get();

属性

您可以使用以下属性和方法来揭示负责创建、更新或删除记录的用户。

// Get the user that created the model
$model->created_by_user_id;
$model->creator->name;

// Get the user that last updated the model
$model->updated_by_user_id;
$model->editor->name;

// Get the user that last deleted the model
$model->deleted_by_user_id;
$model->deleter->name;

作用域查询

以下作用域查询可供您使用

// Retrieve the models either created, updated,
// or deleted by $user.
Model::onlyCreatedBy($user)->get();
Model::onlyUpdatedBy($user)->get();
Model::onlyDeletedBy($user)->get();

// And one extra: get all models that were created
// by the currently authenticated user.
Model::mine()->get();

禁用日志记录

在某些情况下,您可能不想自动保存用户和模型(例如:当播种测试数据时)。您可以使用 disableUserLogging 方法禁用 accountable。

$project = new Project(['name' => 'Do not track me']);
$project->disableUserLogging()->save();

如果您想重新启用 accountable,只需随后使用 enableUserLogging 方法即可。

伪装

当认证不可用 - 例如,当在队列中运行作业时 - 您可能希望“伪装”一个用户。只需使用 actingAs 方法覆盖用户识别。

accountable()->actingAs($event->causer);

您可以通过调用 reset 方法结束伪装。

测试

该包包含集成测试。您可以使用 PHPUnit 运行它们。

$ vendor/bin/phpunit

变更日志

有关更多信息,请参阅变更日志

贡献

有关贡献详细信息,请参阅贡献

致谢

许可证

MIT许可(MIT)。有关更多信息,请参阅许可