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