|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Console\Commands\User; |
| 4 | + |
| 5 | +/** |
| 6 | + * Disables a user account, deletes information about their email address and their password hash. |
| 7 | + * Requires the user to manage zero wikis. |
| 8 | + */ |
| 9 | + |
| 10 | +use App\User; |
| 11 | +use App\WikiManager; |
| 12 | +use Illuminate\Console\Command; |
| 13 | + |
| 14 | +class Disable extends Command { |
| 15 | + protected $signature = 'wbs-user:disable {--email=}'; |
| 16 | + |
| 17 | + protected $description = 'Disable user account'; |
| 18 | + |
| 19 | + public function handle(): int { |
| 20 | + $email = $this->option('email'); |
| 21 | + |
| 22 | + $user = User::whereEmail($email)->first(); |
| 23 | + |
| 24 | + if (empty($email)) { |
| 25 | + $this->error("Error: no email address provided. usage: wbs-user:disable --email='mail@address.com'"); |
| 26 | + |
| 27 | + return 1; |
| 28 | + } |
| 29 | + |
| 30 | + if (!$user) { |
| 31 | + $this->error("Error: Could not find a user for '$email'."); |
| 32 | + |
| 33 | + return 2; |
| 34 | + } |
| 35 | + |
| 36 | + $userWikiManagers = WikiManager::whereUserId($user->id)->with('wiki')->get(); |
| 37 | + $undeletedWikis = []; |
| 38 | + |
| 39 | + foreach ($userWikiManagers as $userWikiManager) { |
| 40 | + $userWiki = $userWikiManager->wiki; |
| 41 | + |
| 42 | + if ($userWiki !== null) { |
| 43 | + $undeletedWikis[] = $userWiki->domain; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + if (!empty($undeletedWikis)) { |
| 48 | + $this->error('Error: User still has wikis: ' . print_r($undeletedWikis, true)); |
| 49 | + |
| 50 | + return 3; |
| 51 | + } |
| 52 | + |
| 53 | + $userId = $user->id; |
| 54 | + $user->email = ''; |
| 55 | + $user->password = random_bytes(10); |
| 56 | + $user->verified = false; |
| 57 | + |
| 58 | + if ($user->save()) { |
| 59 | + $this->info("Successfully disabled user account with email '$email' (id: '$userId')"); |
| 60 | + $this->info('Information about email and password hash was deleted.'); |
| 61 | + |
| 62 | + return 0; |
| 63 | + } else { |
| 64 | + $this->error('Error: Failed to save changes to the database.'); |
| 65 | + |
| 66 | + return 4; |
| 67 | + } |
| 68 | + } |
| 69 | +} |
0 commit comments