From a74edc89735fd5f44dfd3352650122068e07d43a Mon Sep 17 00:00:00 2001 From: dena Date: Thu, 6 Nov 2025 12:11:29 +0100 Subject: [PATCH 1/5] adds wbs-user:disable command --- app/Console/Commands/User/Disable.php | 70 +++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 app/Console/Commands/User/Disable.php diff --git a/app/Console/Commands/User/Disable.php b/app/Console/Commands/User/Disable.php new file mode 100644 index 000000000..6259dea34 --- /dev/null +++ b/app/Console/Commands/User/Disable.php @@ -0,0 +1,70 @@ +option('email'); + + $user = User::whereEmail($email)->first(); + + if (empty($email)) { + $this->error("Error: no email address provided. usage: wbs-user:disable --email='mail@address.com'"); + + return 1; + } + + if (!$user) { + $this->error("Error: Could not find a user for '$email'."); + + return 2; + } + + $userWikiManagers = WikiManager::whereUserId($user->id)->with('wiki')->get(); + $undeletedWikis = []; + + foreach($userWikiManagers as $userWikiManager) { + $userWiki = $userWikiManager->wiki; + + if ($userWiki !== null) { + $undeletedWikis[] = $userWiki->domain; + } + } + + if (! empty($undeletedWikis)) { + $this->error('Error: User still has wikis: '.print_r($undeletedWikis, true)); + + return 3; + } + + $userId = $user->id; + $user->email = ''; + $user->password = random_bytes(10); + $user->verified = false; + + if ($user->save()) { + $this->info("Successfully disabled user account with email '$email' (id: '$userId')"); + $this->info("Information about email and password hash was deleted."); + + return 0; + } else { + $this->error('Error: Failed to save changes to the database.'); + + return 4; + } + } +} From afac9f28ed8bb1d4dd1ba0d1e28d61def9d9d4f8 Mon Sep 17 00:00:00 2001 From: dena Date: Thu, 6 Nov 2025 12:23:11 +0100 Subject: [PATCH 2/5] linting --- app/Console/Commands/User/Disable.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/Console/Commands/User/Disable.php b/app/Console/Commands/User/Disable.php index 6259dea34..941648f75 100644 --- a/app/Console/Commands/User/Disable.php +++ b/app/Console/Commands/User/Disable.php @@ -37,7 +37,7 @@ public function handle(): int { $userWikiManagers = WikiManager::whereUserId($user->id)->with('wiki')->get(); $undeletedWikis = []; - foreach($userWikiManagers as $userWikiManager) { + foreach ($userWikiManagers as $userWikiManager) { $userWiki = $userWikiManager->wiki; if ($userWiki !== null) { @@ -45,8 +45,8 @@ public function handle(): int { } } - if (! empty($undeletedWikis)) { - $this->error('Error: User still has wikis: '.print_r($undeletedWikis, true)); + if (!empty($undeletedWikis)) { + $this->error('Error: User still has wikis: ' . print_r($undeletedWikis, true)); return 3; } @@ -58,7 +58,7 @@ public function handle(): int { if ($user->save()) { $this->info("Successfully disabled user account with email '$email' (id: '$userId')"); - $this->info("Information about email and password hash was deleted."); + $this->info('Information about email and password hash was deleted.'); return 0; } else { From 64f0720b7a67178c01bee388f1fb28e2be69c6fe Mon Sep 17 00:00:00 2001 From: dena Date: Thu, 6 Nov 2025 12:27:35 +0100 Subject: [PATCH 3/5] cleanup --- app/Console/Commands/User/Disable.php | 1 - 1 file changed, 1 deletion(-) diff --git a/app/Console/Commands/User/Disable.php b/app/Console/Commands/User/Disable.php index 941648f75..d82237fa8 100644 --- a/app/Console/Commands/User/Disable.php +++ b/app/Console/Commands/User/Disable.php @@ -9,7 +9,6 @@ use App\User; use App\WikiManager; -use App\Wiki; use Illuminate\Console\Command; class Disable extends Command { From 83263904a2ebe87e660200dd3af2732e595fc796 Mon Sep 17 00:00:00 2001 From: dena Date: Thu, 6 Nov 2025 12:36:39 +0100 Subject: [PATCH 4/5] add unit test --- tests/Commands/User/DisableTest.php | 48 +++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 tests/Commands/User/DisableTest.php diff --git a/tests/Commands/User/DisableTest.php b/tests/Commands/User/DisableTest.php new file mode 100644 index 000000000..4966f9b8f --- /dev/null +++ b/tests/Commands/User/DisableTest.php @@ -0,0 +1,48 @@ + $email, + 'password' => 'worldsstrongestpassword', + ]); + $user->save(); + + return $user; + } + + public function testSuccess() { + $oldUser = $this->createUser(self::EMAIL); + $oldUserId = $oldUser->id; + + $this->artisan('wbs-user:disable', + [ + '--email' => self::EMAIL + ] + )->assertExitCode(0); + + $newUser = User::firstWhere('id', $oldUserId); + + $this->assertSame($oldUser->id, $newUser->id); + $this->assertSame($newUser->email, ''); + $this->assertFalse($newUser->hasVerifiedEmail()); + } + + public function testUserNotFound() { + $this->artisan('wbs-user:disable', + [ + '--email' => self::EMAIL + ] + )->assertExitCode(2); + } +} From 45860d39c72b27a787d4409f69a6730d3003a40e Mon Sep 17 00:00:00 2001 From: dena Date: Thu, 6 Nov 2025 12:41:59 +0100 Subject: [PATCH 5/5] linting fix --- tests/Commands/User/DisableTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Commands/User/DisableTest.php b/tests/Commands/User/DisableTest.php index 4966f9b8f..1599a0e39 100644 --- a/tests/Commands/User/DisableTest.php +++ b/tests/Commands/User/DisableTest.php @@ -27,7 +27,7 @@ public function testSuccess() { $this->artisan('wbs-user:disable', [ - '--email' => self::EMAIL + '--email' => self::EMAIL, ] )->assertExitCode(0); @@ -41,7 +41,7 @@ public function testSuccess() { public function testUserNotFound() { $this->artisan('wbs-user:disable', [ - '--email' => self::EMAIL + '--email' => self::EMAIL, ] )->assertExitCode(2); }