power activity

This commit is contained in:
robert dennis
2026-10-12 05:18:56 -04:00
parent f8163159dd
commit 50904d0e6a
8 changed files with 64 additions and 7 deletions
@@ -7,6 +7,7 @@ namespace Pterodactyl\Actions\Servers;
use Illuminate\Http\Response;
use Pterodactyl\Contracts\Servers\SendsServerCommands;
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
use Pterodactyl\Facades\Activity;
use Pterodactyl\Facades\Daemon;
use Pterodactyl\Models\Server;
use Symfony\Component\HttpKernel\Exception\HttpException;
@@ -22,5 +23,7 @@ final readonly class SendServerCommand implements SendsServerCommands
throw $daemonConnectionException;
}
Activity::event('server:console.command')->subject($server)->property('command', $command)->log();
}
}
+3
View File
@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Pterodactyl\Actions\Servers;
use Pterodactyl\Contracts\Servers\SendsServerPower;
use Pterodactyl\Facades\Activity;
use Pterodactyl\Facades\Daemon;
use Pterodactyl\Models\Server;
@@ -13,5 +14,7 @@ final readonly class SendServerPower implements SendsServerPower
public function send(Server $server, string $signal): void
{
Daemon::server($server)->power($signal);
Activity::event(mb_strtolower("server:power.{$signal}"))->subject($server)->log();
}
}
@@ -11,7 +11,6 @@ use Knuckles\Scribe\Attributes\Response as ScribeResponse;
use Knuckles\Scribe\Attributes\Subgroup;
use Pterodactyl\Contracts\Servers\SendsServerCommands;
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
use Pterodactyl\Facades\Activity;
use Pterodactyl\Http\Controllers\Api\Client\ClientApiController;
use Pterodactyl\Http\Requests\Api\Client\Servers\SendCommandRequest;
use Pterodactyl\Models\Server;
@@ -44,8 +43,6 @@ class CommandController extends ClientApiController
$command = JsonValueGuard::string($request->validated('command'));
$commands->send($server, $command);
Activity::event('server:console.command')->property('command', $command)->log();
return $this->returnNoContent();
}
}
@@ -10,7 +10,6 @@ use Knuckles\Scribe\Attributes\Group;
use Knuckles\Scribe\Attributes\Response as ScribeResponse;
use Knuckles\Scribe\Attributes\Subgroup;
use Pterodactyl\Contracts\Servers\SendsServerPower;
use Pterodactyl\Facades\Activity;
use Pterodactyl\Http\Controllers\Api\Client\ClientApiController;
use Pterodactyl\Http\Requests\Api\Client\Servers\SendPowerRequest;
use Pterodactyl\Models\Server;
@@ -30,8 +29,6 @@ class PowerController extends ClientApiController
$signal = JsonValueGuard::string($request->validated('signal'));
$power->send($server, $signal);
Activity::event(mb_strtolower("server:power.{$signal}"))->log();
return $this->returnNoContent();
}
}
+24 -1
View File
@@ -4,10 +4,12 @@ declare(strict_types=1);
namespace Pterodactyl\Tests\Assertions;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Event;
use PHPUnit\Framework\Assert;
use Pterodactyl\Events\ActivityLogged;
use Pterodactyl\Models\ActivityLog;
use Pterodactyl\Models\ActivityLogSubject;
trait AssertsActivityLogged
@@ -22,6 +24,27 @@ trait AssertsActivityLogged
$this->assertActivitySubjects($event, ...$subjects);
}
/**
* Asserts that exactly one entry was stored for the event against all of the given
* subjects, which is what puts it on that server's or user's activity feed.
*/
public function assertActivityOn(string $event, Model ...$subjects): ActivityLog
{
$query = ActivityLog::query()->where('event', $event);
foreach ($subjects as $subject) {
$query->whereHas('subjects', fn (Builder $builder) => $builder->where('subject_type', $subject->getMorphClass())->where('subject_id', $subject->getKey()));
}
$found = $query->get();
Assert::assertCount(1, $found, sprintf(
'Failed asserting that one "%s" event was logged against %s.',
$event,
implode(', ', array_map(fn (Model $subject): string => sprintf('%s[%d]', $subject::class, $subject->getKey()), $subjects)),
));
return $found->sole();
}
/**
* Asserts that the given activity log event was stored in the database.
*/
@@ -49,7 +72,7 @@ trait AssertsActivityLogged
foreach ($subjects as $subject) {
$match = $e->model->subjects->first(function (ActivityLogSubject $model) use ($subject) {
return $model->subject_type === $subject->getMorphClass()
&& $model->subject_id = $subject->getKey();
&& $model->subject_id === $subject->getKey();
});
Assert::assertNotNull(
@@ -0,0 +1,32 @@
<?php
declare(strict_types=1);
namespace Pterodactyl\Tests\Pest\Integration\Actions\Servers\SendServerPowerTest;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Pterodactyl\Contracts\Servers\SendsServerCommands;
use Pterodactyl\Contracts\Servers\SendsServerPower;
use Pterodactyl\Tests\Integration\IntegrationTestCase;
use Pterodactyl\Tests\Support\Fakes\FakeDaemonCommand;
use Pterodactyl\Tests\Support\Fakes\FakeDaemonPower;
uses(IntegrationTestCase::class, DatabaseTransactions::class);
test('a power signal sent without the http api is logged on the server', function (): void {
new FakeDaemonPower;
$server = $this->createServerModel();
$this->app->make(SendsServerPower::class)->send($server, 'restart');
$this->assertActivityOn('server:power.restart', $server);
});
test('a command sent without the http api is logged on the server', function (): void {
new FakeDaemonCommand;
$server = $this->createServerModel();
$this->app->make(SendsServerCommands::class)->send($server, 'say hi');
expect($this->assertActivityOn('server:console.command', $server)->properties->get('command'))->toBe('say hi');
});
@@ -30,6 +30,7 @@ test('command can send to server', function () {
$response = $this->actingAs($user)->postJson("/api/client/servers/{$server->uuid}/command", ['command' => 'say Test']);
$response->assertStatus(Response::HTTP_NO_CONTENT);
$fake->assertSent('say Test');
$this->assertActivityFor('server:console.command', $user, $server);
Http::assertSent(fn (HttpRequest $request): bool => str_contains($request->url(), "/api/servers/{$server->uuid}/"));
});
test('error is returned when server is offline', function () {
@@ -45,4 +45,5 @@ test('action can be sent to server', function (string $action, string $permissio
[$user, $server] = $this->generateTestAccount([$permission]);
$this->actingAs($user)->postJson("/api/client/servers/{$server->uuid}/power", ['signal' => $action])->assertStatus(Response::HTTP_NO_CONTENT);
$fake->assertSentTo(mb_trim($action), $server->uuid);
$this->assertActivityFor('server:power.'.mb_trim($action), $user, $server);
})->with('validPowerActionDataProvider');