Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/app/Models/ActivityLog.php
14039 views
1
<?php
2
3
namespace Pterodactyl\Models;
4
5
use Carbon\Carbon;
6
use Illuminate\Support\Facades\Event;
7
use Pterodactyl\Events\ActivityLogged;
8
use Illuminate\Database\Eloquent\Builder;
9
use Illuminate\Database\Eloquent\MassPrunable;
10
use Pterodactyl\Contracts\Models\Identifiable;
11
use Illuminate\Database\Eloquent\Relations\HasOne;
12
use Illuminate\Database\Eloquent\Relations\HasMany;
13
use Illuminate\Database\Eloquent\Relations\MorphTo;
14
use Pterodactyl\Models\Traits\HasRealtimeIdentifier;
15
use Illuminate\Database\Eloquent\Model as IlluminateModel;
16
17
/**
18
* \Pterodactyl\Models\ActivityLog.
19
*
20
* @property int $id
21
* @property string|null $batch
22
* @property string $event
23
* @property string $ip
24
* @property string|null $description
25
* @property string|null $actor_type
26
* @property int|null $actor_id
27
* @property int|null $api_key_id
28
* @property \Illuminate\Support\Collection|null $properties
29
* @property Carbon $timestamp
30
* @property IlluminateModel|\Eloquent $actor
31
* @property \Illuminate\Database\Eloquent\Collection<int, \Pterodactyl\Models\ActivityLogSubject> $subjects
32
* @property int|null $subjects_count
33
* @property ApiKey|null $apiKey
34
*
35
* @method static Builder|ActivityLog forActor(\Illuminate\Database\Eloquent\Model $actor)
36
* @method static Builder|ActivityLog forEvent(string $action)
37
* @method static Builder|ActivityLog newModelQuery()
38
* @method static Builder|ActivityLog newQuery()
39
* @method static Builder|ActivityLog query()
40
* @method static Builder|ActivityLog whereActorId($value)
41
* @method static Builder|ActivityLog whereActorType($value)
42
* @method static Builder|ActivityLog whereApiKeyId($value)
43
* @method static Builder|ActivityLog whereBatch($value)
44
* @method static Builder|ActivityLog whereDescription($value)
45
* @method static Builder|ActivityLog whereEvent($value)
46
* @method static Builder|ActivityLog whereId($value)
47
* @method static Builder|ActivityLog whereIp($value)
48
* @method static Builder|ActivityLog whereProperties($value)
49
* @method static Builder|ActivityLog whereTimestamp($value)
50
*
51
* @mixin \Eloquent
52
*/
53
#[Attributes\Identifiable('actl')]
54
class ActivityLog extends Model implements Identifiable
55
{
56
use MassPrunable;
57
use HasRealtimeIdentifier;
58
59
public const RESOURCE_NAME = 'activity_log';
60
61
/**
62
* Tracks all the events we no longer wish to display to users. These are either legacy
63
* events or just events where we never ended up using the associated data.
64
*/
65
public const DISABLED_EVENTS = ['server:file.upload'];
66
67
public $timestamps = false;
68
69
protected $guarded = [
70
'id',
71
'timestamp',
72
];
73
74
protected $casts = [
75
'properties' => 'collection',
76
'timestamp' => 'datetime',
77
];
78
79
protected $with = ['subjects'];
80
81
public static array $validationRules = [
82
'event' => ['required', 'string'],
83
'batch' => ['nullable', 'uuid'],
84
'ip' => ['required', 'string'],
85
'description' => ['nullable', 'string'],
86
'properties' => ['array'],
87
];
88
89
/**
90
* @return \Illuminate\Database\Eloquent\Relations\MorphTo<\Illuminate\Database\Eloquent\Model, $this>
91
*/
92
public function actor(): MorphTo
93
{
94
$morph = $this->morphTo();
95
if (method_exists($morph, 'withTrashed')) { // @phpstan-ignore function.alreadyNarrowedType
96
return $morph->withTrashed();
97
}
98
99
return $morph;
100
}
101
102
/**
103
* @return \Illuminate\Database\Eloquent\Relations\HasMany<\Pterodactyl\Models\ActivityLogSubject, $this>
104
*/
105
public function subjects(): HasMany
106
{
107
return $this->hasMany(ActivityLogSubject::class);
108
}
109
110
/**
111
* @return \Illuminate\Database\Eloquent\Relations\HasOne<\Pterodactyl\Models\ApiKey, $this>
112
*/
113
public function apiKey(): HasOne
114
{
115
return $this->hasOne(ApiKey::class, 'id', 'api_key_id');
116
}
117
118
public function scopeForEvent(Builder $builder, string $action): Builder
119
{
120
return $builder->where('event', $action);
121
}
122
123
/**
124
* Scopes a query to only return results where the actor is a given model.
125
*/
126
public function scopeForActor(Builder $builder, IlluminateModel $actor): Builder
127
{
128
return $builder->whereMorphedTo('actor', $actor);
129
}
130
131
/**
132
* Returns models to be pruned.
133
*
134
* @see https://laravel.com/docs/9.x/eloquent#pruning-models
135
*/
136
public function prunable()
137
{
138
if (is_null(config('activity.prune_days'))) {
139
throw new \LogicException('Cannot prune activity logs: no "prune_days" configuration value is set.');
140
}
141
142
return static::where('timestamp', '<=', Carbon::now()->subDays(config('activity.prune_days')));
143
}
144
145
/**
146
* Boots the model event listeners. This will trigger an activity log event every
147
* time a new model is inserted which can then be captured and worked with as needed.
148
*/
149
protected static function boot()
150
{
151
parent::boot();
152
153
static::created(function (self $model) {
154
Event::dispatch(new ActivityLogged($model));
155
});
156
}
157
}
158
159