Files

90 lines
3.1 KiB
PHP
Raw Permalink Normal View History

<?php
2026-10-05 18:10:18 -04:00
declare(strict_types=1);
2026-10-11 21:01:23 -04:00
namespace Pterodactyl\Http\Resources\Api\Client;
2022-07-10 14:56:19 -04:00
use Illuminate\Database\Eloquent\Model;
2026-10-11 21:01:23 -04:00
use Illuminate\Http\Request;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Extensions\Scribe\Attributes\ResponseField;
2026-10-11 21:01:23 -04:00
use Pterodactyl\Http\Resources\Api\ApiResource;
use Pterodactyl\Http\Resources\Api\Concerns\FormatsActivityLogs;
use Pterodactyl\Http\Resources\Api\Includes\IncludedResource;
use Pterodactyl\Http\Resources\Api\NullResource;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Models\ActivityLog;
use Pterodactyl\Models\ActivityLogSubject;
use Pterodactyl\Models\User;
2026-10-11 21:01:23 -04:00
/**
* @extends ClientResource<ActivityLog>
*/
2026-10-05 18:10:18 -04:00
#[ResponseField('ip', example: '192.0.2.10', nullable: true)]
#[ResponseField('description', example: 'Changed account email address.', nullable: true)]
#[ResponseField('properties', schema: ['type' => 'object', 'additionalProperties' => ['oneOf' => [['type' => 'string'], ['type' => 'integer'], ['type' => 'number'], ['type' => 'boolean'], ['type' => 'array', 'items' => ['type' => 'string']], ['type' => 'object', 'additionalProperties' => ['type' => 'string']]]], 'example' => ['old' => 'old-user@example.com', 'new' => 'new-user@example.com']])]
2026-10-11 21:01:23 -04:00
class ActivityLogResource extends ClientResource
{
2026-10-05 18:10:18 -04:00
use FormatsActivityLogs;
protected array $includeRelations = [
2026-10-11 21:01:23 -04:00
'actor' => ['relation' => 'actor', 'resource' => UserResource::class],
2026-10-05 18:10:18 -04:00
];
protected array $availableIncludes = ['actor'];
2026-10-11 21:01:23 -04:00
public function resourceName(): string
{
return ActivityLog::RESOURCE_NAME;
}
2026-10-11 21:01:23 -04:00
public function toArray(Request $request): array
{
2026-10-11 21:01:23 -04:00
return $this->activityAttributes($this->resource, $this->canViewIP());
}
2026-10-11 21:01:23 -04:00
protected function resolveInclude(string $include): ?IncludedResource
{
return match ($include) {
'actor' => $this->includeActor(),
default => parent::resolveInclude($include),
};
}
/**
* @return ApiResource<User>|NullResource
*/
private function includeActor(): ApiResource|NullResource
{
2026-10-11 21:01:23 -04:00
if (! $this->resource->actor instanceof User) {
return $this->null();
}
2026-10-11 21:01:23 -04:00
return $this->item($this->resource->actor, UserResource::class, User::RESOURCE_NAME);
}
/**
2026-10-05 18:10:18 -04:00
* Determines if the user can view the IP address in the output because they are an
* administrator, because they are the actor that performed the action, or because
* the entry never had an actor and the action was performed against them. That last
2026-10-05 18:10:18 -04:00
* case covers anonymous attempts such as a failed log in, where the address belongs
* to whoever made the attempt rather than to an identified user.
*/
2026-10-11 21:01:23 -04:00
private function canViewIP(): bool
{
2026-10-05 18:10:18 -04:00
$user = $this->getUser();
if ($user->root_admin) {
return true;
}
2026-10-11 21:01:23 -04:00
if ($this->resource->actor_id !== null) {
$actor = $this->resource->actor;
return $actor instanceof Model && $actor->is($user);
}
2026-10-11 21:01:23 -04:00
return $this->resource->subjects->contains(
2026-10-05 18:10:18 -04:00
fn (ActivityLogSubject $subject): bool => $subject->subject_type === $user->getMorphClass()
&& $subject->subject_id === $user->getKey()
);
2022-07-10 14:56:19 -04:00
}
}