2022-05-28 15:36:26 -04:00
|
|
|
<?php
|
|
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2022-05-28 15:36:26 -04:00
|
|
|
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;
|
2022-05-28 15:36:26 -04:00
|
|
|
use Illuminate\Support\Collection;
|
2022-05-29 17:07:54 -04:00
|
|
|
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;
|
2022-05-29 16:19:04 -04:00
|
|
|
use Pterodactyl\Models\ActivityLogSubject;
|
2026-10-05 18:10:18 -04:00
|
|
|
use Pterodactyl\Support\JsonEmptyObject;
|
|
|
|
|
use Pterodactyl\Support\JsonValueGuard;
|
|
|
|
|
use Throwable;
|
2022-05-28 15:36:26 -04:00
|
|
|
|
|
|
|
|
class ActivityLogService
|
|
|
|
|
{
|
|
|
|
|
protected ?ActivityLog $activity = null;
|
|
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
/** @var list<Model> */
|
2022-05-29 16:19:04 -04:00
|
|
|
protected array $subjects = [];
|
|
|
|
|
|
2022-05-28 15:36:26 -04:00
|
|
|
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,
|
|
|
|
|
) {}
|
2022-05-28 15:36:26 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
2022-05-28 17:03:58 -04:00
|
|
|
public function description(?string $description): self
|
2022-05-28 15:36:26 -04:00
|
|
|
{
|
|
|
|
|
$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.
|
2022-05-29 16:19:04 -04:00
|
|
|
*
|
2026-10-05 18:10:18 -04:00
|
|
|
* @param Model|Authenticatable|null ...$subjects
|
2022-05-28 15:36:26 -04:00
|
|
|
*/
|
2022-05-29 16:19:04 -04:00
|
|
|
public function subject(...$subjects): self
|
2022-05-28 15:36:26 -04:00
|
|
|
{
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-29 16:19:04 -04:00
|
|
|
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;
|
|
|
|
|
}
|
2022-05-28 15:36:26 -04:00
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sets the actor model instance.
|
|
|
|
|
*/
|
2022-05-28 17:03:58 -04:00
|
|
|
public function actor(Model $actor): self
|
2022-05-28 15:36:26 -04:00
|
|
|
{
|
|
|
|
|
$this->getActivity()->actor()->associate($actor);
|
|
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2022-11-21 13:41:26 -07:00
|
|
|
* Sets a custom property on the activity log instance.
|
2022-05-28 15:36:26 -04:00
|
|
|
*
|
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.
|
2022-05-28 15:36:26 -04:00
|
|
|
*/
|
2026-10-05 18:10:18 -04:00
|
|
|
public function property(array|string $key, array|bool|float|int|string|JsonEmptyObject|null $value = null): self
|
2022-05-28 15:36:26 -04:00
|
|
|
{
|
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;
|
2022-05-28 15:36:26 -04:00
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-28 17:03:58 -04:00
|
|
|
/**
|
2022-11-21 13:41:26 -07:00
|
|
|
* Attaches the instance request metadata to the activity log event.
|
2022-05-28 17:03:58 -04:00
|
|
|
*/
|
|
|
|
|
public function withRequestMetadata(): self
|
|
|
|
|
{
|
2022-05-29 16:19:04 -04:00
|
|
|
return $this->property([
|
2026-10-05 18:10:18 -04:00
|
|
|
'ip' => $this->request->ip(),
|
|
|
|
|
'useragent' => $this->request->userAgent(),
|
2022-05-29 16:19:04 -04:00
|
|
|
]);
|
2022-05-28 17:03:58 -04:00
|
|
|
}
|
|
|
|
|
|
2022-05-28 15:36:26 -04:00
|
|
|
/**
|
|
|
|
|
* Logs an activity log entry with the set values and then returns the
|
2022-05-29 17:07:54 -04:00
|
|
|
* 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.
|
2022-05-28 15:36:26 -04:00
|
|
|
*/
|
2024-10-21 19:18:20 -06:00
|
|
|
public function log(?string $description = null): ActivityLog
|
2022-05-28 15:36:26 -04:00
|
|
|
{
|
2022-05-28 17:03:58 -04:00
|
|
|
$activity = $this->getActivity();
|
|
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
if (($description) !== null) {
|
2022-05-28 17:03:58 -04:00
|
|
|
$activity->description = $description;
|
|
|
|
|
}
|
2022-05-28 15:36:26 -04:00
|
|
|
|
2022-05-29 17:07:54 -04:00
|
|
|
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]);
|
2022-05-29 17:07:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $activity;
|
2022-05-28 15:36:26 -04:00
|
|
|
}
|
|
|
|
|
|
2022-05-28 17:03:58 -04:00
|
|
|
/**
|
|
|
|
|
* 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;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-28 15:36:26 -04:00
|
|
|
/**
|
|
|
|
|
* 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
|
2022-05-28 15:36:26 -04:00
|
|
|
* 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
|
2022-05-28 15:36:26 -04:00
|
|
|
*/
|
2026-10-05 18:10:18 -04:00
|
|
|
public function transaction(Closure $callback): array|bool|float|int|object|string|null
|
2022-05-28 15:36:26 -04:00
|
|
|
{
|
|
|
|
|
return $this->connection->transaction(function () use ($callback) {
|
2022-05-29 17:07:54 -04:00
|
|
|
$response = $callback($this);
|
2022-05-28 15:36:26 -04:00
|
|
|
|
2022-05-29 17:07:54 -04:00
|
|
|
$this->save();
|
2022-05-28 15:36:26 -04:00
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-29 19:26:28 -04:00
|
|
|
/**
|
|
|
|
|
* Resets the instance and clears out the log.
|
|
|
|
|
*/
|
|
|
|
|
public function reset(): void
|
|
|
|
|
{
|
|
|
|
|
$this->activity = null;
|
|
|
|
|
$this->subjects = [];
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-28 15:36:26 -04:00
|
|
|
/**
|
|
|
|
|
* Returns the current activity log instance.
|
|
|
|
|
*/
|
|
|
|
|
protected function getActivity(): ActivityLog
|
|
|
|
|
{
|
2026-10-05 18:10:18 -04:00
|
|
|
if ($this->activity instanceof ActivityLog) {
|
2022-05-28 15:36:26 -04:00
|
|
|
return $this->activity;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->activity = new ActivityLog([
|
2026-10-05 18:10:18 -04:00
|
|
|
'ip' => $this->request->ip(),
|
2025-12-25 00:27:38 +02:00
|
|
|
'batch' => $this->batch->uuid(),
|
2022-05-28 15:36:26 -04:00
|
|
|
'properties' => Collection::make([]),
|
2022-06-18 12:07:44 -04:00
|
|
|
'api_key_id' => $this->targetable->apiKeyId(),
|
2022-05-28 15:36:26 -04:00
|
|
|
]);
|
|
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
if (($subject = $this->targetable->subject()) instanceof Model) {
|
2022-05-28 17:03:58 -04:00
|
|
|
$this->subject($subject);
|
2022-05-28 15:36:26 -04:00
|
|
|
}
|
|
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
if (($actor = $this->targetable->actor()) instanceof Model) {
|
2022-05-28 17:03:58 -04:00
|
|
|
$this->actor($actor);
|
2026-10-05 18:10:18 -04:00
|
|
|
} elseif (($user = $this->manager->guard()->user()) !== null) {
|
2025-12-20 15:55:13 -08:00
|
|
|
$this->actor($user);
|
2022-05-28 15:36:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->activity;
|
|
|
|
|
}
|
2022-05-29 16:19:04 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Saves the activity log instance and attaches all of the subject models.
|
|
|
|
|
*
|
2026-10-05 18:10:18 -04:00
|
|
|
* @throws Throwable
|
2022-05-29 16:19:04 -04:00
|
|
|
*/
|
2022-05-29 17:07:54 -04:00
|
|
|
protected function save(): ActivityLog
|
2022-05-29 16:19:04 -04:00
|
|
|
{
|
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.');
|
2022-05-29 16:19:04 -04:00
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
$activity = $this->activity;
|
|
|
|
|
$response = $this->connection->transaction(function () use ($activity): ActivityLog {
|
|
|
|
|
$activity->save();
|
2022-05-29 16:19:04 -04:00
|
|
|
|
|
|
|
|
$subjects = Collection::make($this->subjects)
|
2026-10-05 18:10:18 -04:00
|
|
|
->map(fn (Model $subject): array => [
|
|
|
|
|
'activity_log_id' => $activity->id,
|
2022-05-29 16:19:04 -04:00
|
|
|
'subject_id' => $subject->getKey(),
|
|
|
|
|
'subject_type' => $subject->getMorphClass(),
|
|
|
|
|
])
|
|
|
|
|
->values()
|
2026-10-05 18:10:18 -04:00
|
|
|
->all();
|
2022-05-29 16:19:04 -04:00
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
ActivityLogSubject::query()->insert($subjects);
|
2022-05-29 16:19:04 -04:00
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
return $activity;
|
2022-05-29 16:19:04 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$this->activity = null;
|
|
|
|
|
$this->subjects = [];
|
|
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
|
}
|
2022-05-28 15:36:26 -04:00
|
|
|
}
|