Add basic auditing for filesystem actions

Specifically skipping read actions since there isn't much to say there, and it generally wouldn't be very helpful (plus, likely to generate lots of logs).
This commit is contained in:
Dane Everitt 2021-01-17 11:46:08 -08:00
parent b15679d3bb
commit ccecaa6694
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
5 changed files with 164 additions and 64 deletions

View file

@ -2,6 +2,7 @@
namespace Pterodactyl\Models;
use Closure;
use Illuminate\Notifications\Notifiable;
use Illuminate\Database\Query\JoinClause;
use Znck\Eloquent\Traits\BelongsToThrough;
@ -335,7 +336,7 @@ class Server extends Model
* @param array $metadata
* @return \Pterodactyl\Models\AuditLog
*/
public function audit(string $action, array $metadata): AuditLog
public function newAuditEvent(string $action, array $metadata): AuditLog
{
$model = AuditLog::factory($action, $metadata)->fill([
'server_id' => $this->id,
@ -345,6 +346,32 @@ class Server extends Model
return $model;
}
/**
* Stores a new audit event for a server by using a transaction. If the transaction
* fails for any reason everything executed within will be rolled back. The callback
* passed in will receive the AuditLog model before it is saved and the second argument
* will be the current server instance. The callback should modify the audit entry as
* needed before finishing, any changes will be persisted.
*
* The response from the callback is returned to the caller.
*
* @param string $action
* @param \Closure $callback
* @return mixed
* @throws \Throwable
*/
public function audit(string $action, Closure $callback)
{
$model = $this->newAuditEvent($action, []);
return $this->getConnection()->transaction(function () use ($callback, &$model) {
$response = $callback($model, $this);
$model->save();
return $response;
});
}
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/