]> BookStack Code Mirror - bookstack/blob - app/Console/Commands/ResetMfa.php
Played around with a new app structure
[bookstack] / app / Console / Commands / ResetMfa.php
1 <?php
2
3 namespace BookStack\Console\Commands;
4
5 use BookStack\Users\Models\User;
6 use Illuminate\Console\Command;
7
8 class ResetMfa 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      * Create a new command instance.
29      *
30      * @return void
31      */
32     public function __construct()
33     {
34         parent::__construct();
35     }
36
37     /**
38      * Execute the console command.
39      *
40      * @return mixed
41      */
42     public function handle()
43     {
44         $id = $this->option('id');
45         $email = $this->option('email');
46         if (!$id && !$email) {
47             $this->error('Either a --id=<number> or --email=<email> option must be provided.');
48
49             return 1;
50         }
51
52         $field = $id ? 'id' : 'email';
53         $value = $id ?: $email;
54
55         /** @var User $user */
56         $user = User::query()
57             ->where($field, '=', $value)
58             ->first();
59
60         if (!$user) {
61             $this->error("A user where {$field}={$value} could not be found.");
62
63             return 1;
64         }
65
66         $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");
67         $this->info('If multi-factor authentication is required for this user they will be asked to reconfigure their methods on next login.');
68         $confirm = $this->confirm('Are you sure you want to proceed?');
69         if ($confirm) {
70             $user->mfaValues()->delete();
71             $this->info('User MFA methods have been reset.');
72
73             return 0;
74         }
75
76         return 1;
77     }
78 }