UserToggle2FA.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Contracts\Console\PromptsForMissingInput;
  5. use App\User;
  6. class UserToggle2FA extends Command implements PromptsForMissingInput
  7. {
  8. /**
  9. * The name and signature of the console command.
  10. *
  11. * @var string
  12. */
  13. protected $signature = 'user:2fa {username}';
  14. /**
  15. * The console command description.
  16. *
  17. * @var string
  18. */
  19. protected $description = 'Disable two factor authentication for given username';
  20. /**
  21. * Prompt for missing input arguments using the returned questions.
  22. *
  23. * @return array
  24. */
  25. protected function promptForMissingArgumentsUsing()
  26. {
  27. return [
  28. 'username' => 'Which username should we disable 2FA for?',
  29. ];
  30. }
  31. /**
  32. * Execute the console command.
  33. */
  34. public function handle()
  35. {
  36. $user = User::whereUsername($this->argument('username'))->first();
  37. if(!$user) {
  38. $this->error('Could not find any user with that username');
  39. exit;
  40. }
  41. if(!$user->{'2fa_enabled'}) {
  42. $this->info('User did not have 2FA enabled!');
  43. return;
  44. }
  45. $user->{'2fa_enabled'} = false;
  46. $user->{'2fa_secret'} = null;
  47. $user->{'2fa_backup_codes'} = null;
  48. $user->save();
  49. $this->info('Successfully disabled 2FA on this account!');
  50. }
  51. }