From 291c65275ae9e83bee7db6d3c187a9ec345e853a Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Sun, 17 Jan 2021 11:52:44 -0800 Subject: [PATCH] Update audit design --- .../Api/Client/Servers/FileController.php | 19 +++++++------------ app/Models/AuditLog.php | 4 +++- ...1_01_17_102401_create_audit_logs_table.php | 8 ++++++-- 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/app/Http/Controllers/Api/Client/Servers/FileController.php b/app/Http/Controllers/Api/Client/Servers/FileController.php index 782d948f..2a2d96e0 100644 --- a/app/Http/Controllers/Api/Client/Servers/FileController.php +++ b/app/Http/Controllers/Api/Client/Servers/FileController.php @@ -146,10 +146,8 @@ class FileController extends ClientApiController public function write(WriteFileContentRequest $request, Server $server): JsonResponse { $server->audit(AuditLog::ACTION_SERVER_FILESYSTEM_WRITE, function (AuditLog $audit, Server $server) use ($request) { - $audit->metadata = [ - 'file' => $request->get('file'), - 'sub_action' => 'write_content', - ]; + $audit->subaction = 'write_content'; + $audit->metadata = ['file' => $request->get('file')]; $this->fileRepository ->setServer($server) @@ -171,10 +169,8 @@ class FileController extends ClientApiController public function create(CreateFolderRequest $request, Server $server): JsonResponse { $server->audit(AuditLog::ACTION_SERVER_FILESYSTEM_WRITE, function (AuditLog $audit, Server $server) use ($request) { - $audit->metadata = [ - 'file' => $request->input('root', '/') . $request->input('name'), - 'sub_action' => 'create_folder', - ]; + $audit->subaction = 'create_folder'; + $audit->metadata = ['file' => $request->input('root', '/') . $request->input('name')]; $this->fileRepository ->setServer($server) @@ -218,10 +214,9 @@ class FileController extends ClientApiController public function copy(CopyFileRequest $request, Server $server): JsonResponse { $server->audit(AuditLog::ACTION_SERVER_FILESYSTEM_WRITE, function (AuditLog $audit, Server $server) use ($request) { - $audit->metadata = [ - 'file' => $request->input('location'), - 'sub_action' => 'copy_file', - ]; + $audit->subaction = 'copy_file'; + $audit->metadata = ['file' => $request->input('location')]; + $this->fileRepository ->setServer($server) ->copyFile($request->input('location')); diff --git a/app/Models/AuditLog.php b/app/Models/AuditLog.php index 7a056326..e5a9204e 100644 --- a/app/Models/AuditLog.php +++ b/app/Models/AuditLog.php @@ -13,6 +13,7 @@ use Illuminate\Container\Container; * @property int|null $user_id * @property int|null $server_id * @property string $action + * @property string|null $subaction * @property array $device * @property array $metadata * @property \Carbon\CarbonImmutable $created_at @@ -49,7 +50,8 @@ class AuditLog extends Model */ public static $validationRules = [ 'uuid' => 'required|uuid', - 'action' => 'required|string', + 'action' => 'required|string|max:191', + 'subaction' => 'nullable|string|max:191', 'device' => 'array', 'device.ip_address' => 'ip', 'device.user_agent' => 'string', diff --git a/database/migrations/2021_01_17_102401_create_audit_logs_table.php b/database/migrations/2021_01_17_102401_create_audit_logs_table.php index a13274c0..f67e7d64 100644 --- a/database/migrations/2021_01_17_102401_create_audit_logs_table.php +++ b/database/migrations/2021_01_17_102401_create_audit_logs_table.php @@ -17,12 +17,16 @@ class CreateAuditLogsTable extends Migration $table->id(); $table->char('uuid', 36); $table->boolean('is_system')->default(false); - $table->bigInteger('user_id')->nullable(); - $table->bigInteger('server_id')->nullable(); + $table->unsignedInteger('user_id')->nullable(); + $table->unsignedInteger('server_id')->nullable(); $table->string('action'); + $table->string('subaction')->nullable(); $table->json('device'); $table->json('metadata'); $table->timestamp('created_at', 0); + + $table->foreign('user_id')->references('id')->on('users')->onDelete('set null'); + $table->foreign('server_id')->references('id')->on('servers')->onDelete('cascade'); }); }