]> BookStack Code Mirror - bookstack/blob - app/Console/Commands/ResetMfaCommand.php
Simplify ApiAuthException control flow
[bookstack] / app / Console / Commands / ResetMfaCommand.php
1 <?php
2
3 namespace BookStack\Console\Commands;
4
5 use BookStack\Users\Models\User;
6 use Illuminate\Console\Command;
7
8 class ResetMfaCommand extends Command
9 {
10     /**
11      * The name and signature of the console command.
12      *
13      * @var string
14      */
15     protected $signature = 'bookstack:reset-mfa
16                             {--id= : Numeric ID of the user to reset MFA for}
17                             {--email= : Email address of the user to reset MFA for} 
18                             ';
19
20     /**
21      * The console command description.
22      *
23      * @var string
24      */
25     protected $description = 'Reset & Clear any configured MFA methods for the given user';
26
27     /**
28      * Execute the console command.
29      */
30     public function handle(): int
31     {
32         $id = $this->option('id');
33         $email = $this->option('email');
34         if (!$id && !$email) {
35             $this->error('Either a --id=<number> or --email=<email> option must be provided.');
36
37             return 1;
38         }
39
40         $field = $id ? 'id' : 'email';
41         $value = $id ?: $email;
42
43         /** @var User $user */
44         $user = User::query()
45             ->where($field, '=', $value)
46             ->first();
47
48         if (!$user) {
49             $this->error("A user where {$field}={$value} could not be found.");
50
51             return 1;
52         }
53
54         $this->info("This will delete any configure multi-factor authentication methods for user: \n- ID: {$user->id}\n- Name: {$user->name}\n- Email: {$user->email}\n");
55         $this->info('If multi-factor authentication is required for this user they will be asked to reconfigure their methods on next login.');
56         $confirm = $this->confirm('Are you sure you want to proceed?');
57         if (!$confirm) {
58             return 1;
59         }
60
61         $user->mfaValues()->delete();
62         $this->info('User MFA methods have been reset.');
63
64         return 0;
65     }
66 }