承接 testmonitor/laravel-accountable 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

testmonitor/laravel-accountable

Composer 安装命令:

composer require testmonitor/laravel-accountable

包简介

Tracks the user responsible for creating, modifying, or deleting an Eloquent model

README 文档

README

Latest Stable Version CircleCI StyleCI codecov License

This package provides a trait that tracks the user responsible for creating, modifying, or deleting an Eloquent model.

Accountable will observe any activity on your models and it sets the created_by_user_id, updated_by_user_id, and deleted_by_user_id accordingly using the currently authenticated user.

It also provides you with some useful scope query functions, allowing you to fetch models that were either created, modified, or deleted by a specific user.

Table of Contents

Installation

This package can be installed through Composer:

$ composer require testmonitor/laravel-accountable

The package will automatically register itself.

Optionally, publish the configuration file:

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

The configuration file allows you to set the preferred authentication driver, the database column names, and anonymous user. The latter can be used to deal with records created/updated by unauthenticated users.

When left untouched, Accountable will use the default authentication driver and the default column names (created_by_user_id, updated_by_user_id, and deleted_by_user_id).

Usage

In order to add Accountable to your Laravel application, you'll need to:

  1. Add the required columns to your migration file(s).
  2. Use the trait TestMonitor\Accountable\Traits\Accountable on your model(s).

Please note that due to the nature of Laravel event system, mass updates will not be handled by Accountable.

Using the Migration helper

The migration helper simplifies the process of adding columns to your migration:

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);
        });
    }
}

Tip: if you do not use soft-deletes on your model, use Accountable::columns($table, false) to prevent the helper from adding a deleted_by_user_id column.

Using the Trait

Add the Accountable trait on the models you want to track:

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';
}

Examples

Set up your model and make sure you are authenticated.

Basics

Create a project and show the name of the user that created it:

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

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

Get all projects created by a specific user:

$user = User::findOrFail(42);

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

Properties

You can use the following properties and methods to reveal the user responsible for creating, updating or deleting the record.

// 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;

Scope Queries

The following scope queries are at your disposal:

// 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();

Disable Logging

In some cases, you don't want to automatically save the user along with the model (for example: when seeding test data). You can disable accountable by using the disableUserLogging method.

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

If you want to re-enable accountable, simply use the enableUserLogging method afterwards.

Impersonation

When authentication is not available - for example, when running jobs in a queue - you might want to "impersonate" a user. Simply override user identification with the actingAs method:

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

You can end the impersonation by calling the reset method.

Tests

The package contains integration tests. You can run them using PHPUnit.

$ vendor/bin/phpunit

Changelog

Refer to CHANGELOG for more information.

Contributing

Refer to CONTRIBUTING for contributing details.

Credits

License

The MIT License (MIT). Refer to the License for more information.

testmonitor/laravel-accountable 适用场景与选型建议

testmonitor/laravel-accountable 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 11.35k 次下载、GitHub Stars 达 8, 最近一次更新时间为 2019 年 07 月 14 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 testmonitor/laravel-accountable 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 11.35k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 8
  • 点击次数: 27
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 8
  • Watchers: 2
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-07-14