Whoops, don't store the model until we've successfully completed the transaction internals

This commit is contained in:
Dane Everitt 2021-01-18 20:14:38 -08:00
parent 8d69a60e28
commit e700b4da78
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53

View file

@ -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 string $action
* @param array $metadata * @param array $metadata
* @return \Pterodactyl\Models\AuditLog * @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, 'server_id' => $this->id,
]); ]);
$model->save();
return $model;
} }
/** /**
@ -366,9 +364,8 @@ class Server extends Model
*/ */
public function audit(string $action, Closure $callback) public function audit(string $action, Closure $callback)
{ {
$model = $this->newAuditEvent($action, []); return $this->getConnection()->transaction(function () use ($action, $callback) {
$model = $this->newAuditEvent($action);
return $this->getConnection()->transaction(function () use ($callback, &$model) {
$response = $callback($model, $this); $response = $callback($model, $this);
$model->save(); $model->save();