php - Laravel 审计表中的用户类型和用户 ID 为 NULL
问题描述
我使用 Laravel 审计来跟踪系统中的模型更改。我看到管理员类型跟踪用户类型和 ID,但对于员工用户,user_type 和 user_id 为NULL,这是我的代码:
配置/审计.php
<?php
return [
'enabled' => env('AUDITING_ENABLED', true),
/*
|--------------------------------------------------------------------------
| Audit Implementation
|--------------------------------------------------------------------------
|
| Define which Audit model implementation should be used.
|
*/
'implementation' => OwenIt\Auditing\Models\Audit::class,
/*
|--------------------------------------------------------------------------
| User Morph prefix & Guards
|--------------------------------------------------------------------------
|
| Define the morph prefix and authentication guards for the User resolver.
|
*/
'user' => [
'morph_prefix' => 'user',
'guards' => [
'admin',
'api',
],
],
/*
|--------------------------------------------------------------------------
| Audit Resolvers
|--------------------------------------------------------------------------
|
| Define the User, IP Address, User Agent and URL resolver implementations.
|
*/
'resolver' => [
'user' => OwenIt\Auditing\Resolvers\UserResolver::class,
'ip_address' => OwenIt\Auditing\Resolvers\IpAddressResolver::class,
'user_agent' => OwenIt\Auditing\Resolvers\UserAgentResolver::class,
'url' => OwenIt\Auditing\Resolvers\UrlResolver::class,
],
/*
|--------------------------------------------------------------------------
| Audit Events
|--------------------------------------------------------------------------
|
| The Eloquent events that trigger an Audit.
|
*/
'events' => [
'created',
'updated',
'deleted',
'restored',
],
/*
|--------------------------------------------------------------------------
| Strict Mode
|--------------------------------------------------------------------------
|
| Enable the strict mode when auditing?
|
*/
'strict' => false,
/*
|--------------------------------------------------------------------------
| Audit Timestamps
|--------------------------------------------------------------------------
|
| Should the created_at, updated_at and deleted_at timestamps be audited?
|
*/
'timestamps' => false,
/*
|--------------------------------------------------------------------------
| Audit Threshold
|--------------------------------------------------------------------------
|
| Specify a threshold for the amount of Audit records a model can have.
| Zero means no limit.
|
*/
'threshold' => 0,
/*
|--------------------------------------------------------------------------
| Audit Driver
|--------------------------------------------------------------------------
|
| The default audit driver used to keep track of changes.
|
*/
'driver' => 'database',
/*
|--------------------------------------------------------------------------
| Audit Driver Configurations
|--------------------------------------------------------------------------
|
| Available audit drivers and respective configurations.
|
*/
'drivers' => [
'database' => [
'table' => 'audits',
'connection' => null,
],
],
/*
|--------------------------------------------------------------------------
| Audit Console
|--------------------------------------------------------------------------
|
| Whether console events should be audited (eg. php artisan db:seed).
|
*/
'console' => false,
];
员工模型
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use App\Notifications\StaffsResetPasswordNotification;
use OwenIt\Auditing\Contracts\Auditable;
class Staff extends Authenticatable implements Auditable
{
use Notifiable;
use \OwenIt\Auditing\Auditable;
protected $guard = 'staff';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password','type'
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
public function sendPasswordResetNotification($token)
{
$this->notify(new StaffsResetPasswordNotification($token));
}
}
这是我在数据库表中看到的:
我知道必须有一些简单的方法来处理这个问题,但不确定如何。谢谢
解决方案
我找到了一种简单的方法来处理这个问题,实际上,我在GitHub中找到的解决这个问题的答案是创建一个新的自定义解析器并从config/audit.php更改解析器类。这是可行的,但更简单的方法是将要审核的所有警卫/用户类型传递到User Morph prefix & Guards 部分的config/audit.php中的警卫数组中。像这样:
'user' => [
'morph_prefix' => 'user',
'guards' => [
'admin',
'staff',
'student',
'api',
],
],
另一种方法是像这样自定义解析器:
“覆盖” UserResolver 类。
在 config/audit.php 中:
...
'resolver' = [
// ...
'user' => App\Resolvers\UserResolver::class,
// ...
],
...
在 app/Resolvers/UserResolver.php 中:
/**
* {@inheritdoc}
*/
public static function resolve()
{
$guards = config('audit.user.guards'); // I only removed the default array value
foreach ($guards as $guard) {
if (Auth::guard($guard)->check()) {
return Auth::guard($guard)->user();
}
}
}
推荐阅读
- node.js - Node.js 的新条带实现
- android - 分享 Android 应用 Play 商店统计信息
- objective-c - 根据浮点值对对象数组进行排序
- google-analytics - 为什么谷歌分析在热门事件报告中只显示 99999 个结果?
- javascript - Javascript将位置记录到文件中
- macos - 在 Mac OS X Sierra 上调整 ulimit [打开文件句柄限制] 以运行 GATK 工具
- unit-testing - 如何对编译器可以强制执行的需求进行单元测试
- angular - 混合角度应用程序中的 events.js 中的解析错误
- qt - 用于旋转 QWidget 的 QPropertyAnimation
- ios - 以编程方式将navigationController添加到指定viewController而无需故事板swift