Skip to content

Commit bfc6f34

Browse files
committed
Audit when a backup is successful or fails
1 parent 291c652 commit bfc6f34

File tree

1 file changed

+24
-16
lines changed

1 file changed

+24
-16
lines changed

app/Http/Controllers/Api/Remote/Backups/BackupStatusController.php

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Carbon\CarbonImmutable;
66
use Pterodactyl\Models\Backup;
7+
use Pterodactyl\Models\AuditLog;
78
use Illuminate\Http\JsonResponse;
89
use League\Flysystem\AwsS3v3\AwsS3Adapter;
910
use Pterodactyl\Http\Controllers\Controller;
@@ -44,7 +45,7 @@ public function __construct(BackupRepository $repository, BackupManager $backupM
4445
* @param string $backup
4546
* @return \Illuminate\Http\JsonResponse
4647
*
47-
* @throws \Exception
48+
* @throws \Throwable
4849
*/
4950
public function __invoke(ReportBackupCompleteRequest $request, string $backup)
5051
{
@@ -57,21 +58,28 @@ public function __invoke(ReportBackupCompleteRequest $request, string $backup)
5758
);
5859
}
5960

60-
$successful = $request->input('successful') ? true : false;
61-
62-
$model->fill([
63-
'is_successful' => $successful,
64-
'checksum' => $successful ? ($request->input('checksum_type') . ':' . $request->input('checksum')) : null,
65-
'bytes' => $successful ? $request->input('size') : 0,
66-
'completed_at' => CarbonImmutable::now(),
67-
])->save();
68-
69-
// Check if we are using the s3 backup adapter. If so, make sure we mark the backup as
70-
// being completed in S3 correctly.
71-
$adapter = $this->backupManager->adapter();
72-
if ($adapter instanceof AwsS3Adapter) {
73-
$this->completeMultipartUpload($model, $adapter, $successful);
74-
}
61+
$action = $request->input('successful')
62+
? AuditLog::ACTION_SERVER_BACKUP_COMPELTED
63+
: AuditLog::ACTION_SERVER_BACKUP_FAILED;
64+
65+
$model->server->audit($action, function (AuditLog $audit) use ($model, $request) {
66+
$audit->metadata = ['backup_uuid' => $model->uuid];
67+
68+
$successful = $request->input('successful') ? true : false;
69+
$model->fill([
70+
'is_successful' => $successful,
71+
'checksum' => $successful ? ($request->input('checksum_type') . ':' . $request->input('checksum')) : null,
72+
'bytes' => $successful ? $request->input('size') : 0,
73+
'completed_at' => CarbonImmutable::now(),
74+
])->save();
75+
76+
// Check if we are using the s3 backup adapter. If so, make sure we mark the backup as
77+
// being completed in S3 correctly.
78+
$adapter = $this->backupManager->adapter();
79+
if ($adapter instanceof AwsS3Adapter) {
80+
$this->completeMultipartUpload($model, $adapter, $successful);
81+
}
82+
});
7583

7684
return new JsonResponse([], JsonResponse::HTTP_NO_CONTENT);
7785
}

0 commit comments

Comments
 (0)