From e700b4da78df5d80084932b8519459c466acc64f Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Mon, 18 Jan 2021 20:14:38 -0800 Subject: [PATCH] Whoops, don't store the model until we've successfully completed the transaction internals --- app/Models/Server.php | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/app/Models/Server.php b/app/Models/Server.php index 58e5a9760..fe653d174 100644 --- a/app/Models/Server.php +++ b/app/Models/Server.php @@ -334,20 +334,18 @@ class Server extends Model } /** - * Saves an audit entry to the database for the server. + * Returns a fresh AuditLog model for the server. This model is not saved to the + * database when created, so it is up to the caller to correctly store it as needed. * * @param string $action * @param array $metadata * @return \Pterodactyl\Models\AuditLog */ - public function newAuditEvent(string $action, array $metadata): AuditLog + public function newAuditEvent(string $action, array $metadata = []): AuditLog { - $model = AuditLog::factory($action, $metadata)->fill([ + return AuditLog::factory($action, $metadata)->fill([ 'server_id' => $this->id, ]); - $model->save(); - - return $model; } /** @@ -366,9 +364,8 @@ class Server extends Model */ public function audit(string $action, Closure $callback) { - $model = $this->newAuditEvent($action, []); - - return $this->getConnection()->transaction(function () use ($callback, &$model) { + return $this->getConnection()->transaction(function () use ($action, $callback) { + $model = $this->newAuditEvent($action); $response = $callback($model, $this); $model->save();