Skip to content

Commit 0b2c0db

Browse files
committed
Remove last references to audit logs
1 parent 0621d84 commit 0b2c0db

File tree

4 files changed

+43
-83
lines changed

4 files changed

+43
-83
lines changed

app/Http/Controllers/Api/Remote/Servers/ServerDetailsController.php

Lines changed: 37 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,22 @@
44

55
use Illuminate\Http\Request;
66
use Pterodactyl\Models\Server;
7-
use Pterodactyl\Models\Backup;
8-
use Pterodactyl\Models\AuditLog;
97
use Illuminate\Http\JsonResponse;
108
use Pterodactyl\Facades\Activity;
11-
use Illuminate\Database\Query\Builder;
12-
use Illuminate\Database\Query\JoinClause;
139
use Pterodactyl\Http\Controllers\Controller;
14-
use Pterodactyl\Repositories\Eloquent\NodeRepository;
10+
use Illuminate\Database\ConnectionInterface;
1511
use Pterodactyl\Services\Eggs\EggConfigurationService;
1612
use Pterodactyl\Repositories\Eloquent\ServerRepository;
1713
use Pterodactyl\Http\Resources\Wings\ServerConfigurationCollection;
1814
use Pterodactyl\Services\Servers\ServerConfigurationStructureService;
1915

2016
class ServerDetailsController extends Controller
2117
{
18+
/**
19+
* @var \Illuminate\Database\ConnectionInterface
20+
*/
21+
protected ConnectionInterface $connection;
22+
2223
/**
2324
* @var \Pterodactyl\Services\Eggs\EggConfigurationService
2425
*/
@@ -38,14 +39,15 @@ class ServerDetailsController extends Controller
3839
* ServerConfigurationController constructor.
3940
*/
4041
public function __construct(
42+
ConnectionInterface $connection,
4143
ServerRepository $repository,
4244
ServerConfigurationStructureService $configurationStructureService,
43-
EggConfigurationService $eggConfigurationService,
44-
NodeRepository $nodeRepository
45+
EggConfigurationService $eggConfigurationService
4546
) {
4647
$this->eggConfigurationService = $eggConfigurationService;
4748
$this->repository = $repository;
4849
$this->configurationStructureService = $configurationStructureService;
50+
$this->connection = $connection;
4951
}
5052

5153
/**
@@ -110,45 +112,38 @@ public function resetState(Request $request)
110112
// For each of those servers we'll track a new audit log entry to mark them as
111113
// failed and then update them all to be in a valid state.
112114
$servers = Server::query()
113-
->select('servers.*')
114-
->selectRaw('JSON_UNQUOTE(JSON_EXTRACT(started.metadata, "$.backup_uuid")) as backup_uuid')
115-
->leftJoinSub(function (Builder $builder) {
116-
$builder->select('*')->from('audit_logs')
117-
->where('action', AuditLog::SERVER__BACKUP_RESTORE_STARTED)
118-
->orderByDesc('created_at')
119-
->limit(1);
120-
}, 'started', 'started.server_id', '=', 'servers.id')
121-
->leftJoin('audit_logs as completed', function (JoinClause $clause) {
122-
$clause->whereColumn('completed.created_at', '>', 'started.created_at')
123-
->whereIn('completed.action', [
124-
AuditLog::SERVER__BACKUP_RESTORE_COMPLETED,
125-
AuditLog::SERVER__BACKUP_RESTORE_FAILED,
126-
]);
127-
})
128-
->whereNotNull('started.id')
129-
->whereNull('completed.id')
130-
->where('servers.node_id', $node->id)
131-
->where('servers.status', Server::STATUS_RESTORING_BACKUP)
115+
->with([
116+
'activity' => fn ($builder) => $builder
117+
->where('activity_logs.event', 'server:backup.restore-started')
118+
->latest('timestamp'),
119+
])
120+
->where('node_id', $node->id)
121+
->where('status', Server::STATUS_RESTORING_BACKUP)
132122
->get();
133123

134-
$backups = Backup::query()->whereIn('uuid', $servers->pluck('backup_uuid'))->get();
135-
136-
/** @var \Pterodactyl\Models\Server $server */
137-
foreach ($servers as $server) {
138-
$server->update(['status' => null]);
139-
140-
if ($backup = $backups->where('uuid', $server->getAttribute('backup_uuid'))->first()) {
141-
// Just create a new audit entry for this event and update the server state
142-
// so that power actions, file management, and backups can resume as normal.
143-
Activity::event('server:backup.restore-failed')->subject($server, $backup)->log();
124+
$this->connection->transaction(function () use ($node, $servers) {
125+
/** @var \Pterodactyl\Models\Server $server */
126+
foreach ($servers as $server) {
127+
/** @var \Pterodactyl\Models\ActivityLog|null $activity */
128+
$activity = $server->activity->first();
129+
if (!is_null($activity)) {
130+
if ($subject = $activity->subjects->where('subject_type', 'backup')->first()) {
131+
// Just create a new audit entry for this event and update the server state
132+
// so that power actions, file management, and backups can resume as normal.
133+
Activity::event('server:backup.restore-failed')
134+
->subject($server, $subject->subject)
135+
->property('name', $subject->subject->name)
136+
->log();
137+
}
138+
}
144139
}
145-
}
146140

147-
// Update any server marked as installing or restoring as being in a normal state
148-
// at this point in the process.
149-
Server::query()->where('node_id', $node->id)
150-
->whereIn('status', [Server::STATUS_INSTALLING, Server::STATUS_RESTORING_BACKUP])
151-
->update(['status' => null]);
141+
// Update any server marked as installing or restoring as being in a normal state
142+
// at this point in the process.
143+
Server::query()->where('node_id', $node->id)
144+
->whereIn('status', [Server::STATUS_INSTALLING, Server::STATUS_RESTORING_BACKUP])
145+
->update(['status' => null]);
146+
});
152147

153148
return new JsonResponse([], JsonResponse::HTTP_NO_CONTENT);
154149
}

app/Models/AuditLog.php

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,41 +7,12 @@
77
use Illuminate\Container\Container;
88

99
/**
10-
* @property int $id
11-
* @property string $uuid
12-
* @property bool $is_system
13-
* @property int|null $user_id
14-
* @property int|null $server_id
15-
* @property string $action
16-
* @property string|null $subaction
17-
* @property array $device
18-
* @property array $metadata
19-
* @property \Carbon\CarbonImmutable $created_at
20-
* @property \Pterodactyl\Models\User|null $user
21-
* @property \Pterodactyl\Models\Server|null $server
10+
* @deprecated — this class will be dropped in a future version, use the activity log
2211
*/
2312
class AuditLog extends Model
2413
{
2514
public const UPDATED_AT = null;
2615

27-
public const SERVER__FILESYSTEM_DOWNLOAD = 'server:filesystem.download';
28-
public const SERVER__FILESYSTEM_WRITE = 'server:filesystem.write';
29-
public const SERVER__FILESYSTEM_DELETE = 'server:filesystem.delete';
30-
public const SERVER__FILESYSTEM_RENAME = 'server:filesystem.rename';
31-
public const SERVER__FILESYSTEM_COMPRESS = 'server:filesystem.compress';
32-
public const SERVER__FILESYSTEM_DECOMPRESS = 'server:filesystem.decompress';
33-
public const SERVER__FILESYSTEM_PULL = 'server:filesystem.pull';
34-
public const SERVER__BACKUP_STARTED = 'server:backup.started';
35-
public const SERVER__BACKUP_FAILED = 'server:backup.failed';
36-
public const SERVER__BACKUP_COMPELTED = 'server:backup.completed';
37-
public const SERVER__BACKUP_DELETED = 'server:backup.deleted';
38-
public const SERVER__BACKUP_DOWNLOADED = 'server:backup.downloaded';
39-
public const SERVER__BACKUP_LOCKED = 'server:backup.locked';
40-
public const SERVER__BACKUP_UNLOCKED = 'server:backup.unlocked';
41-
public const SERVER__BACKUP_RESTORE_STARTED = 'server:backup.restore.started';
42-
public const SERVER__BACKUP_RESTORE_COMPLETED = 'server:backup.restore.completed';
43-
public const SERVER__BACKUP_RESTORE_FAILED = 'server:backup.restore.failed';
44-
4516
/**
4617
* @var string[]
4718
*/
@@ -104,6 +75,8 @@ public function server()
10475
* you can always make modifications to it as needed before saving.
10576
*
10677
* @return $this
78+
*
79+
* @deprecated
10780
*/
10881
public static function instance(string $action, array $metadata, bool $isSystem = false)
10982
{

app/Models/Backup.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,4 @@ public function server()
9999
{
100100
return $this->belongsTo(Server::class);
101101
}
102-
103-
/**
104-
* @return \Illuminate\Database\Eloquent\Relations\HasMany
105-
*/
106-
public function audits()
107-
{
108-
return $this->hasMany(AuditLog::class, 'metadata->backup_uuid', 'uuid')
109-
->where('action', 'LIKE', 'server:backup.%');
110-
// ->where('metadata->backup_uuid', $this->uuid);
111-
}
112102
}

app/Models/Server.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use Pterodactyl\Exceptions\Http\Server\ServerStateConflictException;
1010

1111
/**
12-
* Pterodactyl\Models\Server.
12+
* \Pterodactyl\Models\Server.
1313
*
1414
* @property int $id
1515
* @property string|null $external_id
@@ -38,6 +38,8 @@
3838
* @property int $backup_limit
3939
* @property \Illuminate\Support\Carbon|null $created_at
4040
* @property \Illuminate\Support\Carbon|null $updated_at
41+
* @property \Illuminate\Database\Eloquent\Collection|\Pterodactyl\Models\ActivityLog[] $activity
42+
* @property int|null $activity_count
4143
* @property \Pterodactyl\Models\Allocation|null $allocation
4244
* @property \Illuminate\Database\Eloquent\Collection|\Pterodactyl\Models\Allocation[] $allocations
4345
* @property int|null $allocations_count

0 commit comments

Comments
 (0)