Files
laravel-manager-packagist/src/Console/UserPasswordCommand.php
T
2026-12-05 15:01:51 +00:00

58 lines
1.9 KiB
PHP

<?php
namespace IriksIT\Manager\Console;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
/**
* Sets a new password for an account, for someone who cannot log in. Typed hidden by default; `--generate` makes a long random
* one and shows it once. The password is never an argument (it would stay in the history of the shell).
*
* php artisan manager:user-password [email protected] [--generate]
*/
class UserPasswordCommand extends Command
{
protected $signature = 'manager:user-password {email : The email address of the account}
{--generate : Make a random password and show it once}';
protected $description = 'Set a new password for an account of the manager';
public function handle(): int
{
$model = config('manager.user_model');
$user = $model::query()->where('email', strtolower((string) $this->argument('email')))->first();
if ($user === null) {
$this->error('There is no account with that email address.');
return self::FAILURE;
}
if ($this->option('generate')) {
$password = Str::password(20, symbols: false);
} else {
$password = (string) $this->secret('New password (min 12 characters)');
if (mb_strlen($password) < 12) {
$this->error('The password must be at least 12 characters.');
return self::FAILURE;
}
if ($password !== (string) $this->secret('Once more')) {
$this->error('The two passwords are not the same.');
return self::FAILURE;
}
}
$user->forceFill(['password' => Hash::make($password)])->save();
$this->info("New password for {$user->email} is set.");
$this->option('generate') && $this->line($password);
return self::SUCCESS;
}
}