Files
pterodactyl-panel/app/Models/ActivityLog.php
T

183 lines
5.5 KiB
PHP
Raw Normal View History

<?php
2026-10-05 18:10:18 -04:00
declare(strict_types=1);
namespace Pterodactyl\Models;
2026-10-05 18:10:18 -04:00
use Database\Factories\ActivityLogFactory;
use Illuminate\Database\Eloquent\Attributes\Guarded;
use Illuminate\Database\Eloquent\Attributes\Scope;
use Illuminate\Database\Eloquent\Attributes\WithoutTimestamps;
use Illuminate\Database\Eloquent\Builder;
2026-10-05 18:10:18 -04:00
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\MassPrunable;
2026-10-05 18:10:18 -04:00
use Illuminate\Database\Eloquent\Model as IlluminateModel;
2022-10-14 10:59:20 -06:00
use Illuminate\Database\Eloquent\Relations\HasMany;
2026-10-05 18:10:18 -04:00
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\MorphTo;
2026-10-05 18:10:18 -04:00
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Event;
use LogicException;
use Pterodactyl\Contracts\Models\Identifiable;
use Pterodactyl\Events\ActivityLogged;
use Pterodactyl\Models\Traits\HasRealtimeIdentifier;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Support\JsonValueGuard;
/**
* \Pterodactyl\Models\ActivityLog.
*
* @property int $id
* @property string|null $batch
* @property string $event
2022-05-29 13:56:39 -04:00
* @property string $ip
* @property string|null $description
* @property string|null $actor_type
* @property int|null $actor_id
* @property int|null $api_key_id
2026-10-05 18:10:18 -04:00
* @property Collection<string, JsonValue> $properties
2024-10-21 19:18:20 -06:00
* @property Carbon $timestamp
2026-10-05 18:10:18 -04:00
* @property IlluminateModel|null $actor
* @property \Illuminate\Database\Eloquent\Collection<int, ActivityLogSubject> $subjects
* @property int|null $subjects_count
2024-10-21 19:18:20 -06:00
* @property ApiKey|null $apiKey
*
* @method static Builder|ActivityLog forActor(\Illuminate\Database\Eloquent\Model $actor)
* @method static Builder|ActivityLog forEvent(string $action)
* @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)
* @method static Builder|ActivityLog whereApiKeyId($value)
* @method static Builder|ActivityLog whereBatch($value)
* @method static Builder|ActivityLog whereDescription($value)
2022-05-29 13:56:39 -04:00
* @method static Builder|ActivityLog whereEvent($value)
* @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 10:59:20 -06:00
*
2026-10-05 18:10:18 -04:00
* @mixin \Illuminate\Database\Eloquent\Model
*/
#[Attributes\Identifiable('actl')]
2026-10-05 18:10:18 -04:00
#[Guarded([
'id',
'timestamp',
])]
#[WithoutTimestamps]
class ActivityLog extends Model implements Identifiable
{
2026-10-05 18:10:18 -04:00
/** @use HasFactory<ActivityLogFactory> */
use HasFactory;
use HasRealtimeIdentifier;
2026-10-05 18:10:18 -04:00
use MassPrunable;
2026-10-05 18:10:18 -04:00
public const string RESOURCE_NAME = 'activity_log';
2022-07-24 17:13:07 -04:00
/**
2022-10-14 10:59:20 -06:00
* Tracks all the events we no longer wish to display to users. These are either legacy
2022-07-24 17:13:07 -04:00
* events or just events where we never ended up using the associated data.
*/
2026-10-05 18:10:18 -04:00
public const array DISABLED_EVENTS = ['server:file.upload'];
protected $with = ['subjects'];
2026-10-05 18:10:18 -04:00
/**
* @return array<string, string>
*/
protected function casts(): array
{
return [
'properties' => 'collection',
'timestamp' => 'datetime',
];
}
/**
2026-10-05 18:10:18 -04:00
* @return MorphTo<IlluminateModel, $this>
*/
public function actor(): MorphTo
{
2022-06-05 18:28:08 -04:00
$morph = $this->morphTo();
if (method_exists($morph, 'withTrashed')) { // @phpstan-ignore function.alreadyNarrowedType
2022-06-05 18:28:08 -04:00
return $morph->withTrashed();
}
return $morph;
}
/**
2026-10-05 18:10:18 -04:00
* @return HasMany<ActivityLogSubject, $this>
*/
2022-10-14 10:59:20 -06:00
public function subjects(): HasMany
{
return $this->hasMany(ActivityLogSubject::class);
}
/**
2026-10-05 18:10:18 -04:00
* @return HasOne<ApiKey, $this>
*/
public function apiKey(): HasOne
{
return $this->hasOne(ApiKey::class, 'id', 'api_key_id');
}
2026-10-05 18:10:18 -04:00
/** @return array<string, JsonValue> */
public function propertyValues(): array
{
2026-10-05 18:10:18 -04:00
$values = $this->properties->all();
JsonValueGuard::assertValue($values);
2026-10-05 18:10:18 -04:00
return $values;
}
/**
* Returns models to be pruned.
*
* @see https://laravel.com/docs/9.x/eloquent#pruning-models
2026-10-05 18:10:18 -04:00
*
* @return Builder<static>
*/
2026-10-05 18:10:18 -04:00
public function prunable(): Builder
{
2026-10-05 18:10:18 -04:00
throw_if((config('activity.prune_days')) === null, LogicException::class, 'Cannot prune activity logs: no "prune_days" configuration value is set.');
2026-10-05 18:10:18 -04:00
return static::query()->where('timestamp', '<=', now()->subDays(JsonValueGuard::integer(config('activity.prune_days'))));
}
/**
* 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.
*/
2026-10-05 18:10:18 -04:00
protected static function boot(): void
{
parent::boot();
2026-10-05 18:10:18 -04:00
static::created(function (self $model): void {
Event::dispatch(new ActivityLogged($model));
});
}
2026-10-05 18:10:18 -04:00
/**
* @param Builder<static> $builder
*/
#[Scope]
protected function forEvent(Builder $builder, string $action): void
{
$builder->where('event', $action);
}
/**
* Scopes a query to only return results where the actor is a given model.
*
* @param Builder<static> $builder
*/
#[Scope]
protected function forActor(Builder $builder, IlluminateModel $actor): void
{
$builder->whereMorphedTo('actor', $actor);
}
}