2022-05-28 19:36:26 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Models;
|
|
|
|
|
2022-05-29 23:45:00 +00:00
|
|
|
use Carbon\Carbon;
|
2022-05-29 21:07:54 +00:00
|
|
|
use Illuminate\Support\Facades\Event;
|
|
|
|
use Pterodactyl\Events\ActivityLogged;
|
2022-05-28 19:36:26 +00:00
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
2022-05-29 23:45:00 +00:00
|
|
|
use Illuminate\Database\Eloquent\MassPrunable;
|
2022-06-18 16:07:44 +00:00
|
|
|
use Illuminate\Database\Eloquent\Relations\HasOne;
|
2022-10-14 16:59:20 +00:00
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
2022-05-28 19:36:26 +00:00
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphTo;
|
|
|
|
use Illuminate\Database\Eloquent\Model as IlluminateModel;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \Pterodactyl\Models\ActivityLog.
|
|
|
|
*
|
|
|
|
* @property int $id
|
|
|
|
* @property string|null $batch
|
|
|
|
* @property string $event
|
2022-05-29 17:56:39 +00:00
|
|
|
* @property string $ip
|
2022-05-28 19:36:26 +00:00
|
|
|
* @property string|null $description
|
|
|
|
* @property string|null $actor_type
|
|
|
|
* @property int|null $actor_id
|
2022-06-18 16:07:44 +00:00
|
|
|
* @property int|null $api_key_id
|
2022-05-29 21:07:54 +00:00
|
|
|
* @property \Illuminate\Support\Collection|null $properties
|
2022-05-30 00:34:48 +00:00
|
|
|
* @property \Carbon\Carbon $timestamp
|
2022-05-29 17:56:39 +00:00
|
|
|
* @property IlluminateModel|\Eloquent $actor
|
2022-05-29 21:07:54 +00:00
|
|
|
* @property \Illuminate\Database\Eloquent\Collection|\Pterodactyl\Models\ActivityLogSubject[] $subjects
|
|
|
|
* @property int|null $subjects_count
|
2022-06-18 16:07:44 +00:00
|
|
|
* @property \Pterodactyl\Models\ApiKey|null $apiKey
|
2022-05-28 19:36:26 +00:00
|
|
|
*
|
|
|
|
* @method static Builder|ActivityLog forActor(\Illuminate\Database\Eloquent\Model $actor)
|
2022-05-29 21:07:54 +00:00
|
|
|
* @method static Builder|ActivityLog forEvent(string $action)
|
2022-05-28 19:36:26 +00:00
|
|
|
* @method static Builder|ActivityLog newModelQuery()
|
|
|
|
* @method static Builder|ActivityLog newQuery()
|
|
|
|
* @method static Builder|ActivityLog query()
|
|
|
|
* @method static Builder|ActivityLog whereActorId($value)
|
|
|
|
* @method static Builder|ActivityLog whereActorType($value)
|
2022-06-18 16:07:44 +00:00
|
|
|
* @method static Builder|ActivityLog whereApiKeyId($value)
|
2022-05-28 19:36:26 +00:00
|
|
|
* @method static Builder|ActivityLog whereBatch($value)
|
|
|
|
* @method static Builder|ActivityLog whereDescription($value)
|
2022-05-29 17:56:39 +00:00
|
|
|
* @method static Builder|ActivityLog whereEvent($value)
|
2022-05-28 19:36:26 +00:00
|
|
|
* @method static Builder|ActivityLog whereId($value)
|
|
|
|
* @method static Builder|ActivityLog whereIp($value)
|
|
|
|
* @method static Builder|ActivityLog whereProperties($value)
|
|
|
|
* @method static Builder|ActivityLog whereTimestamp($value)
|
2022-10-14 16:59:20 +00:00
|
|
|
*
|
2022-05-28 19:36:26 +00:00
|
|
|
* @mixin \Eloquent
|
|
|
|
*/
|
|
|
|
class ActivityLog extends Model
|
|
|
|
{
|
2022-05-29 23:45:00 +00:00
|
|
|
use MassPrunable;
|
|
|
|
|
2022-05-30 00:34:48 +00:00
|
|
|
public const RESOURCE_NAME = 'activity_log';
|
|
|
|
|
2022-07-24 21:13:07 +00:00
|
|
|
/**
|
2022-10-14 16:59:20 +00:00
|
|
|
* Tracks all the events we no longer wish to display to users. These are either legacy
|
2022-07-24 21:13:07 +00:00
|
|
|
* events or just events where we never ended up using the associated data.
|
|
|
|
*/
|
|
|
|
public const DISABLED_EVENTS = ['server:file.upload'];
|
|
|
|
|
2022-05-28 19:36:26 +00:00
|
|
|
public $timestamps = false;
|
|
|
|
|
|
|
|
protected $guarded = [
|
|
|
|
'id',
|
|
|
|
'timestamp',
|
|
|
|
];
|
|
|
|
|
|
|
|
protected $casts = [
|
|
|
|
'properties' => 'collection',
|
2022-05-30 00:34:48 +00:00
|
|
|
'timestamp' => 'datetime',
|
2022-05-28 19:36:26 +00:00
|
|
|
];
|
|
|
|
|
2022-05-29 21:07:54 +00:00
|
|
|
protected $with = ['subjects'];
|
|
|
|
|
2022-10-14 16:59:20 +00:00
|
|
|
public static array $validationRules = [
|
2022-05-28 19:36:26 +00:00
|
|
|
'event' => ['required', 'string'],
|
|
|
|
'batch' => ['nullable', 'uuid'],
|
2022-05-29 17:56:39 +00:00
|
|
|
'ip' => ['required', 'string'],
|
2022-05-28 19:36:26 +00:00
|
|
|
'description' => ['nullable', 'string'],
|
2022-05-29 17:56:39 +00:00
|
|
|
'properties' => ['array'],
|
2022-05-28 19:36:26 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
public function actor(): MorphTo
|
|
|
|
{
|
2022-06-05 22:28:08 +00:00
|
|
|
$morph = $this->morphTo();
|
|
|
|
if (method_exists($morph, 'withTrashed')) {
|
|
|
|
return $morph->withTrashed();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $morph;
|
2022-05-29 21:07:54 +00:00
|
|
|
}
|
|
|
|
|
2022-10-14 16:59:20 +00:00
|
|
|
public function subjects(): HasMany
|
2022-05-29 21:07:54 +00:00
|
|
|
{
|
|
|
|
return $this->hasMany(ActivityLogSubject::class);
|
2022-05-28 19:36:26 +00:00
|
|
|
}
|
|
|
|
|
2022-06-18 16:07:44 +00:00
|
|
|
public function apiKey(): HasOne
|
|
|
|
{
|
|
|
|
return $this->hasOne(ApiKey::class, 'id', 'api_key_id');
|
|
|
|
}
|
|
|
|
|
2022-05-29 20:19:04 +00:00
|
|
|
public function scopeForEvent(Builder $builder, string $action): Builder
|
2022-05-28 19:36:26 +00:00
|
|
|
{
|
2022-05-29 20:19:04 +00:00
|
|
|
return $builder->where('event', $action);
|
2022-05-28 19:36:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Scopes a query to only return results where the actor is a given model.
|
|
|
|
*/
|
|
|
|
public function scopeForActor(Builder $builder, IlluminateModel $actor): Builder
|
|
|
|
{
|
|
|
|
return $builder->whereMorphedTo('actor', $actor);
|
|
|
|
}
|
2022-05-29 21:07:54 +00:00
|
|
|
|
2022-05-29 23:45:00 +00:00
|
|
|
/**
|
|
|
|
* Returns models to be pruned.
|
|
|
|
*
|
|
|
|
* @see https://laravel.com/docs/9.x/eloquent#pruning-models
|
|
|
|
*/
|
|
|
|
public function prunable()
|
|
|
|
{
|
|
|
|
if (is_null(config('activity.prune_days'))) {
|
2022-11-29 17:53:59 +00:00
|
|
|
throw new \LogicException('Cannot prune activity logs: no "prune_days" configuration value is set.');
|
2022-05-29 23:45:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return static::where('timestamp', '<=', Carbon::now()->subDays(config('activity.prune_days')));
|
|
|
|
}
|
|
|
|
|
2022-05-29 21:07:54 +00:00
|
|
|
/**
|
|
|
|
* Boots the model event listeners. This will trigger an activity log event every
|
|
|
|
* time a new model is inserted which can then be captured and worked with as needed.
|
|
|
|
*/
|
|
|
|
protected static function boot()
|
|
|
|
{
|
|
|
|
parent::boot();
|
|
|
|
|
|
|
|
static::created(function (self $model) {
|
|
|
|
Event::dispatch(new ActivityLogged($model));
|
|
|
|
});
|
|
|
|
}
|
2022-05-28 19:36:26 +00:00
|
|
|
}
|