首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Laravel审计表中的用户类型和用户Id为空。

Laravel审计表中的用户类型和用户Id为空。
EN

Stack Overflow用户
提问于 2019-12-08 05:53:47
回答 1查看 3.1K关注 0票数 0

我使用Laravel审计来跟踪系统中的模型更改。我看到管理类型跟踪用户类型和Id,但是对于工作人员用户,user_type和user_id是NULL,下面是我的代码:

config/audit.php

代码语言:javascript
复制
<?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,
];

员工模型

代码语言:javascript
复制
<?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));
    }
}

下面是我在数据库表中看到的内容:

我知道一定有一些简单的方法来处理这个问题,但我不知道如何处理。谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-12-09 06:24:41

我找到了一种简单的方法来处理这个问题,实际上,我在GitHub中找到的解决这个问题的方法是创建一个新的自定义解析器,并从config/audit.php.中更改解析器类。这是可行的,但更简单的方法是将您想要审计的所有保护/用户类型传递到config/audit.php中的用户形态前缀& Guards节中的守护数组中。就像这样:

代码语言:javascript
复制
'user' => [
        'morph_prefix' => 'user',
        'guards'       => [
            'admin',
            'staff',
            'student',
            'api',
        ],
    ],

另一种方法是像这样定制解析器:

“重写”UserResolver类。

在config/audit.php中:

代码语言:javascript
复制
...
    'resolver' = [
        // ...
        'user' => App\Resolvers\UserResolver::class,
        // ...
    ],
...

应用程序/解析器/UserResolver.php中:

代码语言:javascript
复制
  /**
     * {@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();
            }
        }
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59232824

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档