koel/app/Console/Commands/Admin/ChangePasswordCommand.php

48 lines
1.3 KiB
PHP
Raw Normal View History

<?php
namespace App\Console\Commands\Admin;
use App\Console\Commands\Traits\AskForPassword;
use App\Models\User;
use Illuminate\Console\Command;
use Illuminate\Contracts\Hashing\Hasher as Hash;
class ChangePasswordCommand extends Command
{
use AskForPassword;
2022-07-29 06:47:10 +00:00
protected $signature = "koel:admin:change-password
{email? : The user's email. If empty, will get the default admin user.}";
protected $description = "Change a user's password";
2022-07-29 06:47:10 +00:00
public function __construct(private Hash $hash)
{
parent::__construct();
}
2022-07-29 06:47:10 +00:00
public function handle(): int
{
$email = $this->argument('email');
2019-10-23 13:51:37 +00:00
/** @var User|null $user */
$user = $email
? User::query()->where('email', $email)->first()
: User::query()->where('is_admin', true)->first();
if (!$user) {
$this->error('The user account cannot be found.');
2022-07-29 06:47:10 +00:00
return self::FAILURE;
}
2021-06-05 10:47:56 +00:00
$this->comment("Changing the user's password (ID: $user->id, email: $user->email)");
$user->password = $this->hash->make($this->askForPassword());
$user->save();
$this->comment('Alrighty, the new password has been saved. Enjoy! 👌');
2022-07-29 06:47:10 +00:00
return self::SUCCESS;
}
}