2019-10-23 10:45:57 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Console\Commands\Admin;
|
|
|
|
|
2024-02-25 09:11:15 +00:00
|
|
|
use App\Console\Commands\Concerns\AskForPassword;
|
2024-01-15 17:45:01 +00:00
|
|
|
use App\Repositories\UserRepository;
|
2019-10-23 10:45:57 +00:00
|
|
|
use Illuminate\Console\Command;
|
|
|
|
use Illuminate\Contracts\Hashing\Hasher as Hash;
|
|
|
|
|
|
|
|
class ChangePasswordCommand extends Command
|
|
|
|
{
|
2019-11-29 20:28:34 +00:00
|
|
|
use AskForPassword;
|
|
|
|
|
2022-07-29 06:47:10 +00:00
|
|
|
protected $signature = "koel:admin:change-password
|
2020-06-13 19:57:00 +00:00
|
|
|
{email? : The user's email. If empty, will get the default admin user.}";
|
|
|
|
protected $description = "Change a user's password";
|
2019-10-23 10:45:57 +00:00
|
|
|
|
2024-01-15 17:45:01 +00:00
|
|
|
public function __construct(private Hash $hash, private UserRepository $userRepository)
|
2019-10-23 10:45:57 +00:00
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
2022-07-29 06:47:10 +00:00
|
|
|
public function handle(): int
|
2019-10-23 10:45:57 +00:00
|
|
|
{
|
2020-06-13 19:57:00 +00:00
|
|
|
$email = $this->argument('email');
|
|
|
|
|
2022-08-09 18:45:11 +00:00
|
|
|
$user = $email
|
2024-01-15 17:45:01 +00:00
|
|
|
? $this->userRepository->findOneByEmail($email)
|
|
|
|
: $this->userRepository->getDefaultAdminUser();
|
2019-10-23 10:45:57 +00:00
|
|
|
|
|
|
|
if (!$user) {
|
2020-06-13 19:57:00 +00:00
|
|
|
$this->error('The user account cannot be found.');
|
2019-10-23 10:45:57 +00:00
|
|
|
|
2022-07-29 06:47:10 +00:00
|
|
|
return self::FAILURE;
|
2019-10-23 10:45:57 +00:00
|
|
|
}
|
|
|
|
|
2021-06-05 10:47:56 +00:00
|
|
|
$this->comment("Changing the user's password (ID: $user->id, email: $user->email)");
|
2019-10-23 10:45:57 +00:00
|
|
|
|
2019-11-29 20:28:34 +00:00
|
|
|
$user->password = $this->hash->make($this->askForPassword());
|
2019-10-23 10:45:57 +00:00
|
|
|
$user->save();
|
|
|
|
|
2020-06-13 19:57:00 +00:00
|
|
|
$this->comment('Alrighty, the new password has been saved. Enjoy! 👌');
|
2022-07-29 06:47:10 +00:00
|
|
|
|
|
|
|
return self::SUCCESS;
|
2019-10-23 10:45:57 +00:00
|
|
|
}
|
|
|
|
}
|