Update backup logic to use activity logs, not audit logs

This commit is contained in:
DaneEveritt 2022-05-29 16:19:04 -04:00
parent cbecfff6da
commit 2fc5a734f9
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
12 changed files with 222 additions and 159 deletions

View file

@ -2,20 +2,28 @@
namespace Pterodactyl\Services\Activity;
use Illuminate\Support\Arr;
use Webmozart\Assert\Assert;
use Illuminate\Support\Collection;
use Pterodactyl\Models\ActivityLog;
use Illuminate\Contracts\Auth\Factory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Request;
use Pterodactyl\Models\ActivityLogSubject;
use Illuminate\Database\ConnectionInterface;
class ActivityLogService
{
protected ?ActivityLog $activity = null;
protected array $subjects = [];
protected Factory $manager;
protected ConnectionInterface $connection;
protected AcitvityLogBatchService $batch;
protected ActivityLogTargetableService $targetable;
public function __construct(
@ -65,10 +73,22 @@ class ActivityLogService
/**
* Sets the subject model instance.
*
* @param \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Model[] $subjects
*/
public function subject(Model $subject): self
public function subject(...$subjects): self
{
$this->getActivity()->subject()->associate($subject);
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;
}
return $this;
}
@ -83,26 +103,18 @@ class ActivityLogService
return $this;
}
/**
* Sets the custom properties for the activity log instance.
*
* @param \Illuminate\Support\Collection|array $properties
*/
public function withProperties($properties): self
{
$this->getActivity()->properties = Collection::make($properties);
return $this;
}
/**
* Sets a custom property on the activty log instance.
*
* @param string|array $key
* @param mixed $value
*/
public function property(string $key, $value): self
public function property($key, $value = null): self
{
$this->getActivity()->properties = $this->getActivity()->properties->put($key, $value);
$properties = $this->getActivity()->properties;
$this->activity->properties = is_array($key)
? $properties->merge($key)
: $properties->put($key, $value);
return $this;
}
@ -112,10 +124,10 @@ class ActivityLogService
*/
public function withRequestMetadata(): self
{
$this->property('ip', Request::getClientIp());
$this->property('useragent', Request::userAgent());
return $this;
return $this->property([
'ip' => Request::getClientIp(),
'useragent' => Request::userAgent(),
]);
}
/**
@ -130,11 +142,7 @@ class ActivityLogService
$activity->description = $description;
}
$activity->save();
$this->activity = null;
return $activity;
return $this->save();
}
/**
@ -155,17 +163,12 @@ class ActivityLogService
*
* @throws \Throwable
*/
public function transaction(\Closure $callback, string $description = null)
public function transaction(\Closure $callback)
{
if (!is_null($description)) {
$this->description($description);
}
return $this->connection->transaction(function () use ($callback) {
$response = $callback($activity = $this->getActivity());
$activity->save();
$this->activity = null;
$this->save($activity);
return $response;
});
@ -200,4 +203,38 @@ class ActivityLogService
return $this->activity;
}
/**
* 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;
}
}