Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/app/Transformers/Api/Client/ActivityLogTransformer.php
10284 views
1
<?php
2
3
namespace Pterodactyl\Transformers\Api\Client;
4
5
use Illuminate\Support\Str;
6
use Pterodactyl\Models\User;
7
use Pterodactyl\Models\ActivityLog;
8
use Illuminate\Database\Eloquent\Model;
9
10
class ActivityLogTransformer extends BaseClientTransformer
11
{
12
protected array $availableIncludes = ['actor'];
13
14
public function getResourceName(): string
15
{
16
return ActivityLog::RESOURCE_NAME;
17
}
18
19
public function transform(ActivityLog $model): array
20
{
21
return [
22
// This is not for security, it is only to provide a unique identifier to
23
// the front-end for each entry to improve rendering performance since there
24
// is nothing else sufficiently unique to key off at this point.
25
'id' => sha1($model->id),
26
'batch' => $model->batch,
27
'event' => $model->event,
28
'is_api' => !is_null($model->api_key_id),
29
'ip' => $this->canViewIP($model->actor) ? $model->ip : null,
30
'description' => $model->description,
31
'properties' => $this->properties($model),
32
'has_additional_metadata' => $this->hasAdditionalMetadata($model),
33
'timestamp' => $model->timestamp->toAtomString(),
34
];
35
}
36
37
public function includeActor(ActivityLog $model)
38
{
39
if (!$model->actor instanceof User) {
40
return $this->null();
41
}
42
43
return $this->item($model->actor, $this->makeTransformer(UserTransformer::class), User::RESOURCE_NAME);
44
}
45
46
/**
47
* Transforms any array values in the properties into a countable field for easier
48
* use within the translation outputs.
49
*/
50
protected function properties(ActivityLog $model): object
51
{
52
if (!$model->properties || $model->properties->isEmpty()) {
53
return (object) [];
54
}
55
56
$properties = $model->properties
57
->mapWithKeys(function ($value, $key) use ($model) {
58
if ($key === 'ip' && !optional($model->actor)->is($this->request->user())) {
59
return [$key => '[hidden]'];
60
}
61
62
if (!is_array($value)) {
63
// Perform some directory normalization at this point.
64
if ($key === 'directory') {
65
$value = str_replace('//', '/', '/' . trim($value, '/') . '/');
66
}
67
68
return [$key => $value];
69
}
70
71
return [$key => $value, "{$key}_count" => count($value)];
72
});
73
74
$keys = $properties->keys()->filter(fn ($key) => Str::endsWith($key, '_count'))->values();
75
if ($keys->containsOneItem()) {
76
$properties = $properties->merge(['count' => $properties->get($keys[0])])->except($keys[0]);
77
}
78
79
return (object) $properties->toArray();
80
}
81
82
/**
83
* Determines if there are any log properties that we've not already exposed
84
* in the response language string and that are not just the IP address or
85
* the browser useragent.
86
*
87
* This is used by the front-end to selectively display an "additional metadata"
88
* button that is pointless if there is nothing the user can't already see from
89
* the event description.
90
*/
91
protected function hasAdditionalMetadata(ActivityLog $model): bool
92
{
93
if (is_null($model->properties) || $model->properties->isEmpty()) {
94
return false;
95
}
96
97
$str = trans('activity.' . str_replace(':', '.', $model->event));
98
preg_match_all('/:(?<key>[\w.-]+\w)(?:[^\w:]?|$)/', $str, $matches);
99
100
$exclude = array_merge($matches['key'], ['ip', 'useragent', 'using_sftp']);
101
foreach ($model->properties->keys() as $key) {
102
if (!in_array($key, $exclude, true)) {
103
return true;
104
}
105
}
106
107
return false;
108
}
109
110
/**
111
* Determines if the user can view the IP address in the output either because they are the
112
* actor that performed the action, or because they are an administrator on the Panel.
113
*/
114
protected function canViewIP(?Model $actor = null): bool
115
{
116
return optional($actor)->is($this->request->user()) || $this->request->user()->root_admin;
117
}
118
}
119
120