Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/app/Models/ActivityLog.php
7432 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|\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
public function actor(): MorphTo
86
{
87
$morph = $this->morphTo();
88
if (method_exists($morph, 'withTrashed')) {
89
return $morph->withTrashed();
90
}
91
92
return $morph;
93
}
94
95
public function subjects(): HasMany
96
{
97
return $this->hasMany(ActivityLogSubject::class);
98
}
99
100
public function apiKey(): HasOne
101
{
102
return $this->hasOne(ApiKey::class, 'id', 'api_key_id');
103
}
104
105
public function scopeForEvent(Builder $builder, string $action): Builder
106
{
107
return $builder->where('event', $action);
108
}
109
110
/**
111
* Scopes a query to only return results where the actor is a given model.
112
*/
113
public function scopeForActor(Builder $builder, IlluminateModel $actor): Builder
114
{
115
return $builder->whereMorphedTo('actor', $actor);
116
}
117
118
/**
119
* Returns models to be pruned.
120
*
121
* @see https://laravel.com/docs/9.x/eloquent#pruning-models
122
*/
123
public function prunable()
124
{
125
if (is_null(config('activity.prune_days'))) {
126
throw new \LogicException('Cannot prune activity logs: no "prune_days" configuration value is set.');
127
}
128
129
return static::where('timestamp', '<=', Carbon::now()->subDays(config('activity.prune_days')));
130
}
131
132
/**
133
* Boots the model event listeners. This will trigger an activity log event every
134
* time a new model is inserted which can then be captured and worked with as needed.
135
*/
136
protected static function boot()
137
{
138
parent::boot();
139
140
static::created(function (self $model) {
141
Event::dispatch(new ActivityLogged($model));
142
});
143
}
144
}
145
146