diff --git a/app/Console/Commands/User/Disable.php b/app/Console/Commands/User/Disable.php new file mode 100644 index 000000000..d82237fa8 --- /dev/null +++ b/app/Console/Commands/User/Disable.php @@ -0,0 +1,69 @@ +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; + } + } +} diff --git a/tests/Commands/User/DisableTest.php b/tests/Commands/User/DisableTest.php new file mode 100644 index 000000000..1599a0e39 --- /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); + } +}