ResetPasswordCommand.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. namespace Dcat\Admin\Console;
  3. use Illuminate\Console\Command;
  4. class ResetPasswordCommand extends Command
  5. {
  6. /**
  7. * The name and signature of the console command.
  8. *
  9. * @var string
  10. */
  11. protected $signature = 'admin:reset-password';
  12. /**
  13. * The console command description.
  14. *
  15. * @var string
  16. */
  17. protected $description = 'Reset password for a specific admin user';
  18. /**
  19. * Execute the console command.
  20. */
  21. public function handle()
  22. {
  23. if (! config('app.debug')) {
  24. $this->error('Permission deny!');
  25. return;
  26. }
  27. $userModel = config('admin.database.users_model');
  28. $users = $userModel::all();
  29. askForUserName:
  30. $username = $this->askWithCompletion('Please enter a username who needs to reset his password', $users->pluck('username')->toArray());
  31. $user = $users->first(function ($user) use ($username) {
  32. return $user->username == $username;
  33. });
  34. if (is_null($user)) {
  35. $this->error('The user you entered is not exists');
  36. goto askForUserName;
  37. }
  38. enterPassword:
  39. $password = $this->secret('Please enter a password');
  40. if ($password !== $this->secret('Please confirm the password')) {
  41. $this->error('The passwords entered twice do not match, please re-enter');
  42. goto enterPassword;
  43. }
  44. $user->password = bcrypt($password);
  45. $user->save();
  46. $this->info('User password reset successfully.');
  47. }
  48. }