roketin/laravel-auditing 问题修复 & 功能扩展

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

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

roketin/laravel-auditing

Composer 安装命令:

composer require roketin/laravel-auditing

包简介

personal used for Roketin Engine. Keep a change history for your models using laravel version 5.*

README 文档

README

Latest Stable Version Total Downloads Latest Unstable Version License

Laravel Auditing allows you to record changes to an Eloquent model's set of data by simply adding its trait to your model. Laravel Auditing also provides a simple interface for retreiving an audit trail for a piece of data and allows for a great deal of customization in how that data is provided.

Auditing is based on the package auditing

Installation

Laravel Auditing can be installed with Composer, more details about this package in Composer can be found here.

Run the following command to get the latest version package:

composer require roketin/laravel-auditing

Open the file config/app.php and then add the service provider, this step is required.

'providers' => [
    // ...
    Roketin\Auditing\AuditingServiceProvider::class,
],

Note: This provider is important for the publication of configuration files.

Only after complete the step before, use the following command to publish configuration settings:

php artisan vendor:publish --provider="Roketin\Auditing\AuditingServiceProvider"

Finally, execute the migration to create the logs table in your database. This table is used to save audit the logs.

php artisan migrate

Docs

Implementation

Implementation using Trait

To register the change log, use the trait OwnerIt\Auditing\AuditingTrait in the model you want to audit

// app/Team.php
namespace App;

use Illuminate\Database\Eloquent\Model;
use Roketin\Auditing\AuditingTrait;

class Team extends Model 
{
    use AuditingTrait;
    //...
}

Base implementation Legacy Class

It is also possible to have your model extend the OwnerIt\Auditing\Auditing class to enable auditing. Example:

// app/Team.php
namespace App;

use Roketin\Auditing\Auditing;

class Team extends Auditing 
{
    //...    
}

Configuration

Auditing behavior settings

The Auditing behavior settings are carried out with the declaration of attributes in the model. See the examples below:

  • Turn off logging after a number of logs: $historyLimit = 500
  • Disable / enable logging: $auditEnabled = false
  • Turn off logging for specific fields: $dontKeepLogOf = ['field1', 'field2']
// app/Team.php
namespace App;

use Illuminate\Database\Eloquent\Model;

class Team extends Model 
{
    use Roketin\Auditing\AuditingTrait;
    // Disables the log record in this model.
    protected $auditEnabled  = false;
    // Disables the log record after 500 records.
    protected $historyLimit = 500; 
    // Fields you do NOT want to register.
    protected $dontKeepLogOf = ['created_at', 'updated_at'];
    // Tell what actions you want to audit.
    protected $auditableTypes = ['created', 'saved', 'deleted'];
}

Auditing settings

Using the configuration file, you can define:

  • The Model used to represent the current user of application.
  • A different database connection for audit.
  • The table name used for log registers.

The configuration file can be found at config/auditing.php

// config/auditing.php
return [

    // Authentication Model
    'model' => App\User::class,

    // Database Connection
    'connection' => null,

    // Table Name
    'table' => 'logs',
];

Getting the Logs

// app/Http/Controller/MyAppController.php
namespace App\Http\Controllers;

use App\Team;

class MyAppController extends BaseController 
{
    public function index()
    {
        $team = Team::find(1); // Get team
        $team->logs; // Get all logs
        $team->logs->first(); // Get first log
        $team->logs->last();  // Get last log
        $team->logs->find(2); // Selects log
    }
    //...
}

Getting logs with user responsible for the change.

use Roketin\Auditing\Log;

$logs = Log::with(['user'])->get();

or

use App\Team;

$logs = Team::logs->with(['user'])->get();

Note: Remember to properly define the user model in the file config/auditing.php

...
'model' => App\User::class,
... 

Customizing log message

You can define your own log messages for presentation. These messages can be defined for both the model as well as for each one of fields.The dynamic part of the message can be done by targeted fields per dot segmented as{object.property.property} or {object.property|Default value} or {object.property||callbackMethod}.

Note: This implementation is optional, you can make these customizations where desired.

Set messages to the model

// app/Team.php
namespace App;

use Roketin\Auditing\Auditing;

class Team extends Auditing 
{
    //...
    public static $logCustomMessage = '{user.name|Anonymous} {type} a team {elapsed_time}'; // with default value
    public static $logCustomFields = [
        'name'  => 'The name was defined as {new.name||getNewName}', // with callback method
        'owner' => [
            'updated' => '{new.owner} owns the team',
            'created' => '{new.owner|No one} was defined as owner'
        ],
    ];
    
    public function getNewName($log)
    {
        return $log->new['name'];
    }
    //...
}

Getting change logs

// app/Http/Controllers/MyAppController.php 
//...
public function auditing()
{
    $logs = Team::find(1)->logs; // Get logs of team
    return view('auditing', compact('logs'));
}
//...
    

Featuring log records:

    // resources/views/my-app/auditing.blade.php
    ...
    <ol>
        @forelse ($logs as $log)
            <li>
                {{ $log->customMessage }}
                <ul>
                    @forelse ($log->customFields as $custom)
                        <li>{{ $custom }}</li>
                    @empty
                        <li>No details</li>
                    @endforelse
                </ul>
            </li>
        @empty
            <p>No logs</p>
        @endforelse
    </ol>
    ...
    

Result:

  1. Antério Vieira created a team 1 day ago
    • The name was defined as gestao
    • No one was defined as owner
  2. Rafael França deleted a team 2 day ago
    • No details
  3. ...

Examples

Spark Auditing

For convenience we decided to use the spark for this example, the demonstration of auditing is simple and self explanatory. Click here and see for yourself.

Dreams

Dreams is a developed api to serve as an example or direction for developers using laravel-auditing. You can access the application here. The back-end (api) was developed in laravel 5.1 and the front-end (app) in angularjs, the detail are these:

Contributing

Contributions are welcomed; to keep things organized, all bugs and requests should be opened on github issues tab for the main project in the roketin/laravel-auditing/issues.

All pull requests should be made to the branch Develop, so they can be tested before being merged into the master branch.

Having problems?

If you are having problems with the use of this package, there is likely someone has faced the same problem. You can find common answers to their problems:

License

The laravel-audit package is open source software licensed under the license MIT

roketin/laravel-auditing 适用场景与选型建议

roketin/laravel-auditing 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5.27k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2016 年 03 月 17 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 5.27k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 29
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 404
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-03-17