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

120 lines
3.2 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;
2026-10-12 00:57:27 -04:00
use Illuminate\Database\Eloquent\Attributes\UseEloquentBuilder;
2026-10-05 18:10:18 -04:00
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\Collection;
use Illuminate\Support\Facades\Event;
use LogicException;
use Pterodactyl\Contracts\Models\Identifiable;
use Pterodactyl\Events\ActivityLogged;
2026-10-12 00:57:27 -04:00
use Pterodactyl\Models\Builders\ActivityLogBuilder;
use Pterodactyl\Models\Traits\HasRealtimeIdentifier;
/**
2026-10-11 19:54:55 -04:00
* @property Collection<string, mixed> $properties
*/
#[Attributes\Identifiable('actl')]
2026-10-05 18:10:18 -04:00
#[Guarded([
'id',
'timestamp',
])]
#[WithoutTimestamps]
2026-10-12 00:57:27 -04:00
#[UseEloquentBuilder(ActivityLogBuilder::class)]
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-11 19:54:55 -04:00
/** @return array<string, mixed> */
2026-10-05 18:10:18 -04:00
public function propertyValues(): array
{
2026-10-11 19:54:55 -04:00
return $this->properties->all();
}
/**
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-11 19:15:22 -04:00
$days = config('activity.prune_days');
2026-10-11 19:06:44 -04:00
throw_unless($days > 0, LogicException::class, 'Cannot prune activity logs: no "prune_days" configuration value is set.');
2026-10-11 19:06:44 -04:00
return static::query()->where('timestamp', '<=', now()->subDays($days));
}
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));
});
}
}