2022-05-28 15:36:26 -04:00
|
|
|
<?php
|
|
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2022-05-28 15:36:26 -04:00
|
|
|
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;
|
2022-05-28 15:36:26 -04:00
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
2026-10-05 18:10:18 -04:00
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
2022-05-29 19:45:00 -04:00
|
|
|
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;
|
2022-05-28 15:36:26 -04:00
|
|
|
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;
|
2026-02-14 11:21:57 -08:00
|
|
|
use Pterodactyl\Models\Traits\HasRealtimeIdentifier;
|
2022-05-28 15:36:26 -04:00
|
|
|
|
|
|
|
|
/**
|
2026-10-11 19:54:55 -04:00
|
|
|
* @property Collection<string, mixed> $properties
|
2022-05-28 15:36:26 -04:00
|
|
|
*/
|
2026-02-14 11:21:57 -08:00
|
|
|
#[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)]
|
2026-02-14 11:21:57 -08:00
|
|
|
class ActivityLog extends Model implements Identifiable
|
2022-05-28 15:36:26 -04:00
|
|
|
{
|
2026-10-05 18:10:18 -04:00
|
|
|
/** @use HasFactory<ActivityLogFactory> */
|
|
|
|
|
use HasFactory;
|
|
|
|
|
|
2026-02-14 11:21:57 -08:00
|
|
|
use HasRealtimeIdentifier;
|
2026-10-05 18:10:18 -04:00
|
|
|
use MassPrunable;
|
2022-05-29 19:45:00 -04:00
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
public const string RESOURCE_NAME = 'activity_log';
|
2022-05-29 20:34:48 -04:00
|
|
|
|
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'];
|
2022-05-28 15:36:26 -04:00
|
|
|
|
2022-05-29 17:07:54 -04:00
|
|
|
protected $with = ['subjects'];
|
|
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
/**
|
|
|
|
|
* @return array<string, string>
|
|
|
|
|
*/
|
|
|
|
|
protected function casts(): array
|
|
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
'properties' => 'collection',
|
|
|
|
|
'timestamp' => 'datetime',
|
|
|
|
|
];
|
|
|
|
|
}
|
2022-05-28 15:36:26 -04:00
|
|
|
|
2025-12-20 15:55:13 -08:00
|
|
|
/**
|
2026-10-05 18:10:18 -04:00
|
|
|
* @return MorphTo<IlluminateModel, $this>
|
2025-12-20 15:55:13 -08:00
|
|
|
*/
|
2022-05-28 15:36:26 -04:00
|
|
|
public function actor(): MorphTo
|
|
|
|
|
{
|
2022-06-05 18:28:08 -04:00
|
|
|
$morph = $this->morphTo();
|
2025-12-20 15:55:13 -08:00
|
|
|
if (method_exists($morph, 'withTrashed')) { // @phpstan-ignore function.alreadyNarrowedType
|
2022-06-05 18:28:08 -04:00
|
|
|
return $morph->withTrashed();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $morph;
|
2022-05-29 17:07:54 -04:00
|
|
|
}
|
|
|
|
|
|
2025-12-20 15:55:13 -08:00
|
|
|
/**
|
2026-10-05 18:10:18 -04:00
|
|
|
* @return HasMany<ActivityLogSubject, $this>
|
2025-12-20 15:55:13 -08:00
|
|
|
*/
|
2022-10-14 10:59:20 -06:00
|
|
|
public function subjects(): HasMany
|
2022-05-29 17:07:54 -04:00
|
|
|
{
|
|
|
|
|
return $this->hasMany(ActivityLogSubject::class);
|
2022-05-28 15:36:26 -04:00
|
|
|
}
|
|
|
|
|
|
2025-12-20 15:55:13 -08:00
|
|
|
/**
|
2026-10-05 18:10:18 -04:00
|
|
|
* @return HasOne<ApiKey, $this>
|
2025-12-20 15:55:13 -08:00
|
|
|
*/
|
2022-06-18 12:07:44 -04:00
|
|
|
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
|
2022-05-28 15:36:26 -04:00
|
|
|
{
|
2026-10-11 19:54:55 -04:00
|
|
|
return $this->properties->all();
|
2022-05-28 15:36:26 -04:00
|
|
|
}
|
2022-05-29 17:07:54 -04:00
|
|
|
|
2022-05-29 19:45:00 -04:00
|
|
|
/**
|
2026-10-05 18:10:18 -04:00
|
|
|
* @return Builder<static>
|
2022-05-29 19:45:00 -04:00
|
|
|
*/
|
2026-10-05 18:10:18 -04:00
|
|
|
public function prunable(): Builder
|
2022-05-29 19:45:00 -04:00
|
|
|
{
|
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.');
|
2022-05-29 19:45:00 -04:00
|
|
|
|
2026-10-11 19:06:44 -04:00
|
|
|
return static::query()->where('timestamp', '<=', now()->subDays($days));
|
2022-05-29 19:45:00 -04:00
|
|
|
}
|
|
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
protected static function boot(): void
|
2022-05-29 17:07:54 -04:00
|
|
|
{
|
|
|
|
|
parent::boot();
|
|
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
static::created(function (self $model): void {
|
2022-05-29 17:07:54 -04:00
|
|
|
Event::dispatch(new ActivityLogged($model));
|
|
|
|
|
});
|
|
|
|
|
}
|
2022-05-28 15:36:26 -04:00
|
|
|
}
|