ResetPasswordCommand.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. namespace Encore\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. $userModel = config('admin.database.users_model');
  24. $users = $userModel::all();
  25. askForUserName:
  26. $username = $this->askWithCompletion('Please enter a username who needs to reset his password', $users->pluck('username')->toArray());
  27. $user = $users->first(function ($user) use ($username) {
  28. return $user->username == $username;
  29. });
  30. if (is_null($user)) {
  31. $this->error('The user you entered is not exists');
  32. goto askForUserName;
  33. }
  34. enterPassword:
  35. $password = $this->secret('Please enter a password');
  36. if ($password !== $this->secret('Please confirm the password')) {
  37. $this->error('The passwords entered twice do not match, please re-enter');
  38. goto enterPassword;
  39. }
  40. $user->password = bcrypt($password);
  41. $user->save();
  42. $this->info('User password reset successfully.');
  43. }
  44. }