2021-01-17 18:49:36 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Models;
|
|
|
|
|
|
|
|
use Ramsey\Uuid\Uuid;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Container\Container;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property int $id
|
|
|
|
* @property string $uuid
|
|
|
|
* @property bool $is_system
|
|
|
|
* @property int|null $user_id
|
|
|
|
* @property int|null $server_id
|
|
|
|
* @property string $action
|
2021-01-17 19:52:44 +00:00
|
|
|
* @property string|null $subaction
|
2021-01-17 18:49:36 +00:00
|
|
|
* @property array $device
|
|
|
|
* @property array $metadata
|
|
|
|
* @property \Carbon\CarbonImmutable $created_at
|
|
|
|
*
|
|
|
|
* @property \Pterodactyl\Models\User|null $user
|
|
|
|
* @property \Pterodactyl\Models\Server|null $server
|
|
|
|
*/
|
|
|
|
class AuditLog extends Model
|
|
|
|
{
|
|
|
|
const UPDATED_AT = null;
|
|
|
|
|
2021-01-17 23:25:49 +00:00
|
|
|
const SERVER__FILESYSTEM_DOWNLOAD = 'server:filesystem.download';
|
|
|
|
const SERVER__FILESYSTEM_WRITE = 'server:filesystem.write';
|
|
|
|
const SERVER__FILESYSTEM_DELETE = 'server:filesystem.delete';
|
|
|
|
const SERVER__FILESYSTEM_RENAME = 'server:filesystem.rename';
|
|
|
|
const SERVER__FILESYSTEM_COMPRESS = 'server:filesystem.compress';
|
|
|
|
const SERVER__FILESYSTEM_DECOMPRESS = 'server:filesystem.decompress';
|
|
|
|
const SERVER__FILESYSTEM_PULL = 'server:filesystem.pull';
|
|
|
|
const SERVER__BACKUP_STARTED = 'server:backup.started';
|
|
|
|
const SERVER__BACKUP_FAILED = 'server:backup.failed';
|
|
|
|
const SERVER__BACKUP_COMPELTED = 'server:backup.completed';
|
|
|
|
const SERVER__BACKUP_DELETED = 'server:backup.deleted';
|
|
|
|
const SERVER__BACKUP_RESTORE_STARTED = 'server:backup.restore.started';
|
|
|
|
const SERVER__BACKUP_RESTORE_COMPLETED = 'server:backup.restore.completed';
|
|
|
|
const SERVER__BACKUP_RESTORE_FAILED = 'server:backup.restore.failed';
|
2021-01-17 18:49:36 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string[]
|
|
|
|
*/
|
|
|
|
public static $validationRules = [
|
|
|
|
'uuid' => 'required|uuid',
|
2021-01-17 19:52:44 +00:00
|
|
|
'action' => 'required|string|max:191',
|
|
|
|
'subaction' => 'nullable|string|max:191',
|
2021-01-17 19:46:08 +00:00
|
|
|
'device' => 'array',
|
2021-01-17 18:49:36 +00:00
|
|
|
'device.ip_address' => 'ip',
|
|
|
|
'device.user_agent' => 'string',
|
|
|
|
'metadata' => 'array',
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $table = 'audit_logs';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
protected $immutableDates = true;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string[]
|
|
|
|
*/
|
|
|
|
protected $casts = [
|
|
|
|
'device' => 'array',
|
|
|
|
'metadata' => 'array',
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string[]
|
|
|
|
*/
|
|
|
|
protected $guarded = [
|
|
|
|
'id',
|
|
|
|
'created_at',
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
|
|
*/
|
|
|
|
public function user()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(User::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
|
|
*/
|
|
|
|
public function server()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(Server::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new AuditLog model and returns it, attaching device information and the
|
|
|
|
* currently authenticated user if available. This model is not saved at this point, so
|
|
|
|
* you can always make modifications to it as needed before saving.
|
|
|
|
*
|
|
|
|
* @param string $action
|
|
|
|
* @param array $metadata
|
|
|
|
* @param bool $isSystem
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public static function factory(string $action, array $metadata, bool $isSystem = false)
|
|
|
|
{
|
|
|
|
/** @var \Illuminate\Http\Request $request */
|
|
|
|
$request = Container::getInstance()->make('request');
|
2021-01-17 19:46:08 +00:00
|
|
|
if ($isSystem || ! $request instanceof Request) {
|
2021-01-17 18:49:36 +00:00
|
|
|
$request = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (new self())->fill([
|
|
|
|
'uuid' => Uuid::uuid4()->toString(),
|
|
|
|
'is_system' => $isSystem,
|
2021-01-17 19:46:08 +00:00
|
|
|
'user_id' => ($request && $request->user()) ? $request->user()->id : null,
|
2021-01-17 18:49:36 +00:00
|
|
|
'server_id' => null,
|
|
|
|
'action' => $action,
|
|
|
|
'device' => $request ? [
|
|
|
|
'ip_address' => $request->getClientIp(),
|
|
|
|
'user_agent' => $request->userAgent(),
|
|
|
|
] : [],
|
|
|
|
'metadata' => $metadata,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|