Files
pterodactyl-panel/app/Services/Activity/ActivityLogService.php
T

274 lines
7.7 KiB
PHP
Raw Normal View History

<?php
2026-10-05 18:10:18 -04:00
declare(strict_types=1);
namespace Pterodactyl\Services\Activity;
2026-10-05 18:10:18 -04:00
use Closure;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Contracts\Auth\Factory as AuthFactory;
use Illuminate\Database\ConnectionInterface;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;
2026-10-05 18:10:18 -04:00
use InvalidArgumentException;
2022-05-29 18:48:35 -04:00
use Pterodactyl\Models\ActivityLog;
use Pterodactyl\Models\ActivityLogSubject;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Support\JsonEmptyObject;
use Pterodactyl\Support\JsonValueGuard;
use Throwable;
class ActivityLogService
{
protected ?ActivityLog $activity = null;
2026-10-05 18:10:18 -04:00
/** @var list<Model> */
protected array $subjects = [];
public function __construct(
2022-10-14 10:59:20 -06:00
protected AuthFactory $manager,
protected ActivityLogBatchService $batch,
protected ActivityLogTargetableService $targetable,
2024-10-21 19:18:20 -06:00
protected ConnectionInterface $connection,
2026-10-05 18:10:18 -04:00
protected Request $request,
) {}
/**
* Sets the activity logger as having been caused by an anonymous
* user type.
*/
public function anonymous(): self
{
$this->getActivity()->actor_id = null;
$this->getActivity()->actor_type = null;
$this->getActivity()->setRelation('actor', null);
return $this;
}
/**
* Sets the action for this activity log.
*/
public function event(string $action): self
{
$this->getActivity()->event = $action;
return $this;
}
/**
* Set the description for this activity.
*/
public function description(?string $description): self
{
$this->getActivity()->description = $description;
return $this;
}
/**
2026-10-05 18:10:18 -04:00
* Sets the subject model instance(s) for this activity log entry. Null entries
* are ignored, which lets callers pass an optional subject conditionally. A
* non-Model Authenticatable (accepted here because auth events type their
* user as the interface) is also skipped -- every Authenticatable this app
* actually issues is a Model too, but this only ever tracks morphable models.
*
2026-10-05 18:10:18 -04:00
* @param Model|Authenticatable|null ...$subjects
*/
public function subject(...$subjects): self
{
2026-10-05 18:10:18 -04:00
foreach ($subjects as $subject) {
if (! $subject instanceof Model) {
2022-10-14 10:59:20 -06:00
continue;
}
foreach ($this->subjects as $entry) {
// If this subject is already tracked in our array of subjects just skip over
// it and move on to the next one in the list.
if ($entry->is($subject)) {
continue 2;
}
}
$this->subjects[] = $subject;
}
return $this;
}
/**
* Sets the actor model instance.
*/
public function actor(Model $actor): self
{
$this->getActivity()->actor()->associate($actor);
return $this;
}
/**
* Sets a custom property on the activity log instance.
*
2026-10-05 18:10:18 -04:00
* @param string|array<string, JsonValue> $key
* @param JsonInputValue $value Ignored when an array of properties is provided.
*/
2026-10-05 18:10:18 -04:00
public function property(array|string $key, array|bool|float|int|string|JsonEmptyObject|null $value = null): self
{
2026-10-05 18:10:18 -04:00
$activity = $this->getActivity();
$properties = $activity->properties;
if (is_array($key)) {
foreach ($key as $name => $property) {
$properties->put($name, $property);
}
} else {
JsonValueGuard::assertValue($value);
$properties->put($key, $value);
}
$activity->properties = $properties;
return $this;
}
/**
* Attaches the instance request metadata to the activity log event.
*/
public function withRequestMetadata(): self
{
return $this->property([
2026-10-05 18:10:18 -04:00
'ip' => $this->request->ip(),
'useragent' => $this->request->userAgent(),
]);
}
/**
* Logs an activity log entry with the set values and then returns the
* model instance to the caller. If there is an exception encountered while
* performing this action it will be logged to the disk but will not interrupt
* the code flow.
*/
2024-10-21 19:18:20 -06:00
public function log(?string $description = null): ActivityLog
{
$activity = $this->getActivity();
2026-10-05 18:10:18 -04:00
if (($description) !== null) {
$activity->description = $description;
}
try {
return $this->save();
2026-10-05 18:10:18 -04:00
} catch (Throwable $throwable) {
/* @noinspection PhpUnhandledExceptionInspection */
throw_if(config('app.env') !== 'production', $throwable);
2022-05-29 18:48:35 -04:00
2026-10-05 18:10:18 -04:00
Log::error($throwable->getMessage(), ['exception' => $throwable]);
}
return $activity;
}
/**
* Returns a cloned instance of the service allowing for the creation of a base
* activity log with the ability to change values on the fly without impact.
*/
public function clone(): self
{
return clone $this;
}
/**
* Executes the provided callback within the scope of a database transaction
2022-10-14 10:59:20 -06:00
* and will only save the activity log entry if everything else successfully
* settles.
*
2026-10-05 18:10:18 -04:00
* @template TReturn of object|ApiValue10
2026-01-06 02:05:38 +02:00
*
2026-10-05 18:10:18 -04:00
* @param Closure($this): TReturn $callback
* @return TReturn
*
* @throws Throwable
*/
2026-10-05 18:10:18 -04:00
public function transaction(Closure $callback): array|bool|float|int|object|string|null
{
return $this->connection->transaction(function () use ($callback) {
$response = $callback($this);
$this->save();
return $response;
});
}
/**
* Resets the instance and clears out the log.
*/
public function reset(): void
{
$this->activity = null;
$this->subjects = [];
}
/**
* Returns the current activity log instance.
*/
protected function getActivity(): ActivityLog
{
2026-10-05 18:10:18 -04:00
if ($this->activity instanceof ActivityLog) {
return $this->activity;
}
$this->activity = new ActivityLog([
2026-10-05 18:10:18 -04:00
'ip' => $this->request->ip(),
'batch' => $this->batch->uuid(),
'properties' => Collection::make([]),
'api_key_id' => $this->targetable->apiKeyId(),
]);
2026-10-05 18:10:18 -04:00
if (($subject = $this->targetable->subject()) instanceof Model) {
$this->subject($subject);
}
2026-10-05 18:10:18 -04:00
if (($actor = $this->targetable->actor()) instanceof Model) {
$this->actor($actor);
2026-10-05 18:10:18 -04:00
} elseif (($user = $this->manager->guard()->user()) !== null) {
$this->actor($user);
}
return $this->activity;
}
/**
* Saves the activity log instance and attaches all of the subject models.
*
2026-10-05 18:10:18 -04:00
* @throws Throwable
*/
protected function save(): ActivityLog
{
2026-10-05 18:10:18 -04:00
throw_unless($this->activity instanceof ActivityLog, InvalidArgumentException::class, 'Cannot save an activity log before one has been initialized.');
2026-10-05 18:10:18 -04:00
$activity = $this->activity;
$response = $this->connection->transaction(function () use ($activity): ActivityLog {
$activity->save();
$subjects = Collection::make($this->subjects)
2026-10-05 18:10:18 -04:00
->map(fn (Model $subject): array => [
'activity_log_id' => $activity->id,
'subject_id' => $subject->getKey(),
'subject_type' => $subject->getMorphClass(),
])
->values()
2026-10-05 18:10:18 -04:00
->all();
2026-10-05 18:10:18 -04:00
ActivityLogSubject::query()->insert($subjects);
2026-10-05 18:10:18 -04:00
return $activity;
});
$this->activity = null;
$this->subjects = [];
return $response;
}
}