2022-05-28 19:36:26 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Services\Activity;
|
|
|
|
|
2022-05-29 20:19:04 +00:00
|
|
|
use Illuminate\Support\Arr;
|
|
|
|
use Webmozart\Assert\Assert;
|
2022-05-28 19:36:26 +00:00
|
|
|
use Illuminate\Support\Collection;
|
|
|
|
use Pterodactyl\Models\ActivityLog;
|
|
|
|
use Illuminate\Contracts\Auth\Factory;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2022-05-28 21:03:58 +00:00
|
|
|
use Illuminate\Support\Facades\Request;
|
2022-05-29 20:19:04 +00:00
|
|
|
use Pterodactyl\Models\ActivityLogSubject;
|
2022-05-28 19:36:26 +00:00
|
|
|
use Illuminate\Database\ConnectionInterface;
|
|
|
|
|
|
|
|
class ActivityLogService
|
|
|
|
{
|
|
|
|
protected ?ActivityLog $activity = null;
|
|
|
|
|
2022-05-29 20:19:04 +00:00
|
|
|
protected array $subjects = [];
|
|
|
|
|
2022-05-28 19:36:26 +00:00
|
|
|
protected Factory $manager;
|
2022-05-29 20:19:04 +00:00
|
|
|
|
2022-05-28 19:36:26 +00:00
|
|
|
protected ConnectionInterface $connection;
|
2022-05-29 20:19:04 +00:00
|
|
|
|
2022-05-28 19:36:26 +00:00
|
|
|
protected AcitvityLogBatchService $batch;
|
2022-05-29 20:19:04 +00:00
|
|
|
|
2022-05-28 19:36:26 +00:00
|
|
|
protected ActivityLogTargetableService $targetable;
|
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
Factory $manager,
|
|
|
|
AcitvityLogBatchService $batch,
|
|
|
|
ActivityLogTargetableService $targetable,
|
|
|
|
ConnectionInterface $connection
|
|
|
|
) {
|
|
|
|
$this->manager = $manager;
|
|
|
|
$this->batch = $batch;
|
|
|
|
$this->targetable = $targetable;
|
|
|
|
$this->connection = $connection;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 21:03:58 +00:00
|
|
|
public function description(?string $description): self
|
2022-05-28 19:36:26 +00:00
|
|
|
{
|
|
|
|
$this->getActivity()->description = $description;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the subject model instance.
|
2022-05-29 20:19:04 +00:00
|
|
|
*
|
|
|
|
* @param \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Model[] $subjects
|
2022-05-28 19:36:26 +00:00
|
|
|
*/
|
2022-05-29 20:19:04 +00:00
|
|
|
public function subject(...$subjects): self
|
2022-05-28 19:36:26 +00:00
|
|
|
{
|
2022-05-29 20:19:04 +00:00
|
|
|
foreach (Arr::wrap($subjects) as $subject) {
|
|
|
|
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 19:36:26 +00:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the actor model instance.
|
|
|
|
*/
|
2022-05-28 21:03:58 +00:00
|
|
|
public function actor(Model $actor): self
|
2022-05-28 19:36:26 +00:00
|
|
|
{
|
|
|
|
$this->getActivity()->actor()->associate($actor);
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets a custom property on the activty log instance.
|
|
|
|
*
|
2022-05-29 20:19:04 +00:00
|
|
|
* @param string|array $key
|
2022-05-28 19:36:26 +00:00
|
|
|
* @param mixed $value
|
|
|
|
*/
|
2022-05-29 20:19:04 +00:00
|
|
|
public function property($key, $value = null): self
|
2022-05-28 19:36:26 +00:00
|
|
|
{
|
2022-05-29 20:19:04 +00:00
|
|
|
$properties = $this->getActivity()->properties;
|
|
|
|
$this->activity->properties = is_array($key)
|
|
|
|
? $properties->merge($key)
|
|
|
|
: $properties->put($key, $value);
|
2022-05-28 19:36:26 +00:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2022-05-28 21:03:58 +00:00
|
|
|
/**
|
|
|
|
* Attachs the instance request metadata to the activity log event.
|
|
|
|
*/
|
|
|
|
public function withRequestMetadata(): self
|
|
|
|
{
|
2022-05-29 20:19:04 +00:00
|
|
|
return $this->property([
|
|
|
|
'ip' => Request::getClientIp(),
|
|
|
|
'useragent' => Request::userAgent(),
|
|
|
|
]);
|
2022-05-28 21:03:58 +00:00
|
|
|
}
|
|
|
|
|
2022-05-28 19:36:26 +00:00
|
|
|
/**
|
|
|
|
* Logs an activity log entry with the set values and then returns the
|
|
|
|
* model instance to the caller.
|
|
|
|
*/
|
2022-05-28 21:03:58 +00:00
|
|
|
public function log(string $description = null): ActivityLog
|
2022-05-28 19:36:26 +00:00
|
|
|
{
|
2022-05-28 21:03:58 +00:00
|
|
|
$activity = $this->getActivity();
|
|
|
|
|
|
|
|
if (!is_null($description)) {
|
|
|
|
$activity->description = $description;
|
|
|
|
}
|
2022-05-28 19:36:26 +00:00
|
|
|
|
2022-05-29 20:19:04 +00:00
|
|
|
return $this->save();
|
2022-05-28 19:36:26 +00:00
|
|
|
}
|
|
|
|
|
2022-05-28 21:03:58 +00: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 19:36:26 +00:00
|
|
|
/**
|
|
|
|
* Executes the provided callback within the scope of a database transaction
|
|
|
|
* and will only save the activity log entry if everything else succesfully
|
|
|
|
* settles.
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*
|
|
|
|
* @throws \Throwable
|
|
|
|
*/
|
2022-05-29 20:19:04 +00:00
|
|
|
public function transaction(\Closure $callback)
|
2022-05-28 19:36:26 +00:00
|
|
|
{
|
|
|
|
return $this->connection->transaction(function () use ($callback) {
|
|
|
|
$response = $callback($activity = $this->getActivity());
|
|
|
|
|
2022-05-29 20:19:04 +00:00
|
|
|
$this->save($activity);
|
2022-05-28 19:36:26 +00:00
|
|
|
|
|
|
|
return $response;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the current activity log instance.
|
|
|
|
*/
|
|
|
|
protected function getActivity(): ActivityLog
|
|
|
|
{
|
|
|
|
if ($this->activity) {
|
|
|
|
return $this->activity;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->activity = new ActivityLog([
|
2022-05-29 17:56:39 +00:00
|
|
|
'ip' => Request::ip(),
|
2022-05-28 19:36:26 +00:00
|
|
|
'batch_uuid' => $this->batch->uuid(),
|
|
|
|
'properties' => Collection::make([]),
|
|
|
|
]);
|
|
|
|
|
|
|
|
if ($subject = $this->targetable->subject()) {
|
2022-05-28 21:03:58 +00:00
|
|
|
$this->subject($subject);
|
2022-05-28 19:36:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($actor = $this->targetable->actor()) {
|
2022-05-28 21:03:58 +00:00
|
|
|
$this->actor($actor);
|
2022-05-28 19:36:26 +00:00
|
|
|
} elseif ($user = $this->manager->guard()->user()) {
|
|
|
|
if ($user instanceof Model) {
|
2022-05-28 21:03:58 +00:00
|
|
|
$this->actor($user);
|
2022-05-28 19:36:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->activity;
|
|
|
|
}
|
2022-05-29 20:19:04 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Saves the activity log instance and attaches all of the subject models.
|
|
|
|
*
|
|
|
|
* @throws \Throwable
|
|
|
|
*/
|
|
|
|
protected function save(ActivityLog $activity = null): ActivityLog
|
|
|
|
{
|
|
|
|
$activity = $activity ?? $this->activity;
|
|
|
|
|
|
|
|
Assert::notNull($activity);
|
|
|
|
|
|
|
|
$response = $this->connection->transaction(function () use ($activity) {
|
|
|
|
$activity->save();
|
|
|
|
|
|
|
|
$subjects = Collection::make($this->subjects)
|
|
|
|
->map(fn (Model $subject) => [
|
|
|
|
'activity_log_id' => $this->activity->id,
|
|
|
|
'subject_id' => $subject->getKey(),
|
|
|
|
'subject_type' => $subject->getMorphClass(),
|
|
|
|
])
|
|
|
|
->values()
|
|
|
|
->toArray();
|
|
|
|
|
|
|
|
ActivityLogSubject::insert($subjects);
|
|
|
|
|
|
|
|
return $activity;
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->activity = null;
|
|
|
|
$this->subjects = [];
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
2022-05-28 19:36:26 +00:00
|
|
|
}
|