From de034cf090d7d08fdcfcd49d20939595a1344517 Mon Sep 17 00:00:00 2001 From: SkyMulley Date: Mon, 1 Jun 2026 16:55:37 +0100 Subject: [PATCH] bugfix: user creation now properly appears in action log (#5644) ### Description User creation was not properly logged within the action log, neither from sub-user invite or manual user creation. This PR adds the relevant log and includes testing. ### Manual testing scenarios 1. Invite a user within any created server 2. Create a user in the admin 3. Verify both users appear as an action in the action log ### Questions or comments AI was used for secondary testing, QA and code review. No code was written by AI. ### Resolved issues: 1. [x] resolves pterodactyl/panel#5631 --- app/Services/Users/UserCreationService.php | 10 +++++ resources/lang/en/activity.php | 3 ++ .../Application/Users/UserControllerTest.php | 2 + .../Subuser/CreateServerSubuserTest.php | 28 +++++++++++++ .../Admin/UserController/CreateUserTest.php | 42 +++++++++++++++++++ 5 files changed, 85 insertions(+) create mode 100644 tests/Integration/Http/Controllers/Admin/UserController/CreateUserTest.php diff --git a/app/Services/Users/UserCreationService.php b/app/Services/Users/UserCreationService.php index ed92d7b58..7fe8bd795 100644 --- a/app/Services/Users/UserCreationService.php +++ b/app/Services/Users/UserCreationService.php @@ -4,6 +4,7 @@ namespace Pterodactyl\Services\Users; use Ramsey\Uuid\Uuid; use Pterodactyl\Models\User; +use Pterodactyl\Facades\Activity; use Illuminate\Contracts\Hashing\Hasher; use Illuminate\Database\ConnectionInterface; use Illuminate\Contracts\Auth\PasswordBroker; @@ -53,6 +54,15 @@ class UserCreationService $this->connection->commit(); $user->notify(new AccountCreated($user, $token ?? null)); + Activity::event('user:user.create') + ->subject($user) + ->property([ + 'email' => $user->email, + 'username' => $user->username, + 'admin' => $user->root_admin, + ]) + ->log(); + return $user; } } diff --git a/resources/lang/en/activity.php b/resources/lang/en/activity.php index 501a1dcde..f19262697 100644 --- a/resources/lang/en/activity.php +++ b/resources/lang/en/activity.php @@ -21,6 +21,9 @@ return [ ], ], 'user' => [ + 'user' => [ + 'create' => 'Created a new user :email', + ], 'account' => [ 'email-changed' => 'Changed email from :old to :new', 'password-changed' => 'Changed password', diff --git a/tests/Integration/Api/Application/Users/UserControllerTest.php b/tests/Integration/Api/Application/Users/UserControllerTest.php index a2512094b..7c70bb10b 100644 --- a/tests/Integration/Api/Application/Users/UserControllerTest.php +++ b/tests/Integration/Api/Application/Users/UserControllerTest.php @@ -231,6 +231,8 @@ class UserControllerTest extends ApplicationApiIntegrationTestCase 'resource' => route('api.application.users.view', $user->id), ], ], true); + + $this->assertActivityFor('user:user.create', $this->getApiUser(), $user); } /** diff --git a/tests/Integration/Api/Client/Server/Subuser/CreateServerSubuserTest.php b/tests/Integration/Api/Client/Server/Subuser/CreateServerSubuserTest.php index 86725c76f..547f8b864 100644 --- a/tests/Integration/Api/Client/Server/Subuser/CreateServerSubuserTest.php +++ b/tests/Integration/Api/Client/Server/Subuser/CreateServerSubuserTest.php @@ -47,6 +47,34 @@ class CreateServerSubuserTest extends ClientApiIntegrationTestCase $this->assertJsonTransformedWith($expected, $subuser); } + /** + * Test that a newly created user account correctly causes the creation of a user:user.create + * activity log entry. + */ + public function testCreatingSubuserWithNewEmailLogsUserCreation() + { + [$user, $server] = $this->generateTestAccount(); + + $response = $this->actingAs($user)->postJson($this->link($server) . '/users', [ + 'email' => $email = $this->faker->email, + 'permissions' => [ + Permission::ACTION_USER_CREATE, + ], + ]); + + $response->assertOk(); + + /** @var User $subuser */ + $subuser = User::query()->where('email', $email)->firstOrFail(); + + $this->assertActivityLogged('user:user.create'); + $this->assertDatabaseHas('activity_logs', [ + 'event' => 'user:user.create', + 'actor_type' => $user->getMorphClass(), + 'actor_id' => $user->id, + ]); + } + /** * Tests that an error is returned if a subuser attempts to create a new subuser and assign * permissions that their account does not also possess. diff --git a/tests/Integration/Http/Controllers/Admin/UserController/CreateUserTest.php b/tests/Integration/Http/Controllers/Admin/UserController/CreateUserTest.php new file mode 100644 index 000000000..d4ddceb1e --- /dev/null +++ b/tests/Integration/Http/Controllers/Admin/UserController/CreateUserTest.php @@ -0,0 +1,42 @@ +actingAs(User::factory()->create()) + ->post('/admin/users/new', [ + 'email' => 'test@example.com', + 'username' => 'testuser', + 'name_first' => 'Test', + 'name_last' => 'User', + ]) + ->assertForbidden(); + } + + public function testCreatingAdministratorAccountIsLogged(): void + { + $admin = User::factory()->admin()->create(); + + $this->actingAs($admin) + ->post('/admin/users/new', [ + 'email' => 'created.admin@example.com', + 'username' => 'createdadmin', + 'name_first' => 'Created', + 'name_last' => 'Admin', + 'root_admin' => 1, + ]) + ->assertSessionHasNoErrors(); + + /** @var User $created */ + $created = User::query()->where('username', 'createdadmin')->firstOrFail(); + $this->assertTrue($created->root_admin); + + $this->assertActivityFor('user:user.create', $admin, $created); + } +}