83 lines
2.0 KiB
PHP
83 lines
2.0 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Models;
|
||
|
|
|
||
|
|
use Illuminate\Database\Eloquent\Model;
|
||
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||
|
|
|
||
|
|
class ActivityLog extends Model
|
||
|
|
{
|
||
|
|
public $timestamps = false;
|
||
|
|
|
||
|
|
protected $fillable = [
|
||
|
|
'user_id',
|
||
|
|
'action',
|
||
|
|
'module',
|
||
|
|
'description',
|
||
|
|
'details',
|
||
|
|
'ip_address',
|
||
|
|
'created_at',
|
||
|
|
];
|
||
|
|
|
||
|
|
protected $casts = [
|
||
|
|
'details' => 'array',
|
||
|
|
'created_at' => 'datetime',
|
||
|
|
];
|
||
|
|
|
||
|
|
public function user(): BelongsTo
|
||
|
|
{
|
||
|
|
return $this->belongsTo(User::class);
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function log(
|
||
|
|
string $action,
|
||
|
|
string $module,
|
||
|
|
?string $description = null,
|
||
|
|
?array $details = null
|
||
|
|
): self {
|
||
|
|
return static::create([
|
||
|
|
'user_id' => auth()->id(),
|
||
|
|
'action' => $action,
|
||
|
|
'module' => $module,
|
||
|
|
'description' => $description,
|
||
|
|
'details' => $details,
|
||
|
|
'ip_address' => request()->ip(),
|
||
|
|
'created_at' => now(),
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function logCreate(Model $model, ?string $description = null): self
|
||
|
|
{
|
||
|
|
return static::log(
|
||
|
|
'create',
|
||
|
|
$model::class,
|
||
|
|
$description ?? 'Creato ' . class_basename($model),
|
||
|
|
['new' => $model->toArray()]
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function logUpdate(Model $model, array $old, ?string $description = null): self
|
||
|
|
{
|
||
|
|
return static::log(
|
||
|
|
'update',
|
||
|
|
$model::class,
|
||
|
|
$description ?? 'Modificato ' . class_basename($model),
|
||
|
|
['old' => $old, 'new' => $model->toArray()]
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function logDelete(Model $model, ?string $description = null): self
|
||
|
|
{
|
||
|
|
return static::log(
|
||
|
|
'delete',
|
||
|
|
$model::class,
|
||
|
|
$description ?? 'Eliminato ' . class_basename($model),
|
||
|
|
['deleted' => $model->toArray()]
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function logLogin(): self
|
||
|
|
{
|
||
|
|
return static::log('login', 'auth', 'Login effettuato');
|
||
|
|
}
|
||
|
|
}
|