2022-05-30 00:34:48 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Transformers\Api\Client;
|
|
|
|
|
2022-06-18 19:28:42 +00:00
|
|
|
use Illuminate\Support\Str;
|
2022-05-30 00:34:48 +00:00
|
|
|
use Pterodactyl\Models\User;
|
|
|
|
use Pterodactyl\Models\ActivityLog;
|
2022-07-10 18:56:19 +00:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2022-05-30 00:34:48 +00:00
|
|
|
|
|
|
|
class ActivityLogTransformer extends BaseClientTransformer
|
|
|
|
{
|
|
|
|
protected array $availableIncludes = ['actor'];
|
|
|
|
|
|
|
|
public function getResourceName(): string
|
|
|
|
{
|
|
|
|
return ActivityLog::RESOURCE_NAME;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function transform(ActivityLog $model): array
|
|
|
|
{
|
|
|
|
return [
|
2022-07-10 18:53:29 +00:00
|
|
|
// This is not for security, it is only to provide a unique identifier to
|
|
|
|
// the front-end for each entry to improve rendering performance since there
|
|
|
|
// is nothing else sufficiently unique to key off at this point.
|
|
|
|
'id' => sha1($model->id),
|
2022-05-30 00:34:48 +00:00
|
|
|
'batch' => $model->batch,
|
|
|
|
'event' => $model->event,
|
2022-06-18 16:07:44 +00:00
|
|
|
'is_api' => !is_null($model->api_key_id),
|
2022-07-10 18:56:19 +00:00
|
|
|
'ip' => $this->canViewIP($model->actor) ? $model->ip : null,
|
2022-05-30 00:34:48 +00:00
|
|
|
'description' => $model->description,
|
2022-06-18 19:28:42 +00:00
|
|
|
'properties' => $this->properties($model),
|
2022-06-12 19:30:49 +00:00
|
|
|
'has_additional_metadata' => $this->hasAdditionalMetadata($model),
|
2022-10-14 16:59:20 +00:00
|
|
|
'timestamp' => $model->timestamp->toAtomString(),
|
2022-05-30 00:34:48 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function includeActor(ActivityLog $model)
|
|
|
|
{
|
|
|
|
if (!$model->actor instanceof User) {
|
|
|
|
return $this->null();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->item($model->actor, $this->makeTransformer(UserTransformer::class), User::RESOURCE_NAME);
|
|
|
|
}
|
2022-06-12 19:30:49 +00:00
|
|
|
|
2022-06-18 19:28:42 +00:00
|
|
|
/**
|
|
|
|
* Transforms any array values in the properties into a countable field for easier
|
|
|
|
* use within the translation outputs.
|
|
|
|
*/
|
2022-11-21 20:28:46 +00:00
|
|
|
protected function properties(ActivityLog $model): object
|
2022-06-18 19:28:42 +00:00
|
|
|
{
|
|
|
|
if (!$model->properties || $model->properties->isEmpty()) {
|
2022-11-21 20:28:46 +00:00
|
|
|
return (object) [];
|
2022-06-18 19:28:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$properties = $model->properties
|
2022-06-28 00:52:27 +00:00
|
|
|
->mapWithKeys(function ($value, $key) use ($model) {
|
|
|
|
if ($key === 'ip' && !optional($model->actor)->is($this->request->user())) {
|
|
|
|
return [$key => '[hidden]'];
|
|
|
|
}
|
|
|
|
|
2022-06-18 19:28:42 +00:00
|
|
|
if (!is_array($value)) {
|
2022-07-01 01:06:50 +00:00
|
|
|
// Perform some directory normalization at this point.
|
|
|
|
if ($key === 'directory') {
|
|
|
|
$value = str_replace('//', '/', '/' . trim($value, '/') . '/');
|
|
|
|
}
|
|
|
|
|
2022-06-18 19:28:42 +00:00
|
|
|
return [$key => $value];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [$key => $value, "{$key}_count" => count($value)];
|
|
|
|
});
|
|
|
|
|
|
|
|
$keys = $properties->keys()->filter(fn ($key) => Str::endsWith($key, '_count'))->values();
|
|
|
|
if ($keys->containsOneItem()) {
|
|
|
|
$properties = $properties->merge(['count' => $properties->get($keys[0])])->except($keys[0]);
|
|
|
|
}
|
|
|
|
|
2022-11-21 20:28:46 +00:00
|
|
|
return (object) $properties->toArray();
|
2022-06-18 19:28:42 +00:00
|
|
|
}
|
|
|
|
|
2022-06-12 19:30:49 +00:00
|
|
|
/**
|
|
|
|
* Determines if there are any log properties that we've not already exposed
|
|
|
|
* in the response language string and that are not just the IP address or
|
|
|
|
* the browser useragent.
|
|
|
|
*
|
|
|
|
* This is used by the front-end to selectively display an "additional metadata"
|
|
|
|
* button that is pointless if there is nothing the user can't already see from
|
|
|
|
* the event description.
|
|
|
|
*/
|
|
|
|
protected function hasAdditionalMetadata(ActivityLog $model): bool
|
|
|
|
{
|
|
|
|
if (is_null($model->properties) || $model->properties->isEmpty()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$str = trans('activity.' . str_replace(':', '.', $model->event));
|
2022-06-18 20:36:19 +00:00
|
|
|
preg_match_all('/:(?<key>[\w.-]+\w)(?:[^\w:]?|$)/', $str, $matches);
|
2022-06-12 19:30:49 +00:00
|
|
|
|
2022-07-09 23:30:38 +00:00
|
|
|
$exclude = array_merge($matches['key'], ['ip', 'useragent', 'using_sftp']);
|
2022-06-12 19:30:49 +00:00
|
|
|
foreach ($model->properties->keys() as $key) {
|
|
|
|
if (!in_array($key, $exclude, true)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2022-07-10 18:56:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines if the user can view the IP address in the output either because they are the
|
|
|
|
* actor that performed the action, or because they are an administrator on the Panel.
|
|
|
|
*/
|
|
|
|
protected function canViewIP(Model $actor = null): bool
|
|
|
|
{
|
|
|
|
return optional($actor)->is($this->request->user()) || $this->request->user()->root_admin;
|
|
|
|
}
|
2022-05-30 00:34:48 +00:00
|
|
|
}
|