update user list
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Command;
|
||||
|
||||
use App\Model\User;
|
||||
use Hyperf\Command\Annotation\Command;
|
||||
use Hyperf\Command\Command as HyperfCommand;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
|
||||
#[Command]
|
||||
class UserListCommand extends HyperfCommand
|
||||
{
|
||||
public function __construct(protected ContainerInterface $container)
|
||||
{
|
||||
parent::__construct('user:list');
|
||||
}
|
||||
|
||||
public function configure(): void
|
||||
{
|
||||
parent::configure();
|
||||
$this->setDescription('List all system users');
|
||||
$this->addOption('role', 'r', InputOption::VALUE_OPTIONAL, 'Filter by role name (e.g. administrator, developer, accessor)');
|
||||
}
|
||||
|
||||
public function handle(): int
|
||||
{
|
||||
$query = User::query()->with('role');
|
||||
|
||||
$role_name = $this->input->getOption('role');
|
||||
if ($role_name) {
|
||||
$query->whereHas('role', fn ($q) => $q->where('name', $role_name));
|
||||
}
|
||||
|
||||
$users = $query->orderBy('id')->get();
|
||||
|
||||
if ($users->isEmpty()) {
|
||||
$this->info('No users found.');
|
||||
return 0;
|
||||
}
|
||||
|
||||
$rows = $users->map(fn (User $u) => [
|
||||
$u->id,
|
||||
$u->username,
|
||||
$u->email,
|
||||
$u->role?->name ?? '-',
|
||||
$this->formatStatus($u->status),
|
||||
$u->created_at?->toDateTimeString(),
|
||||
])->toArray();
|
||||
|
||||
$this->table(
|
||||
['ID', 'Username', 'Email', 'Role', 'Status', 'Created At'],
|
||||
$rows,
|
||||
);
|
||||
|
||||
$this->info(sprintf('Total: %d user(s)', count($rows)));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
private function formatStatus(int $status): string
|
||||
{
|
||||
return match ($status) {
|
||||
1 => '<fg=green>enabled</>',
|
||||
0 => '<fg=red>disabled</>',
|
||||
default => '<fg=yellow>unknown</>',
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Command;
|
||||
|
||||
use App\Model\User;
|
||||
use Hyperf\Command\Annotation\Command;
|
||||
use Hyperf\Command\Command as HyperfCommand;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
|
||||
#[Command]
|
||||
class UserResetPasswordCommand extends HyperfCommand
|
||||
{
|
||||
public function __construct(protected ContainerInterface $container)
|
||||
{
|
||||
parent::__construct('user:reset-password');
|
||||
}
|
||||
|
||||
public function configure(): void
|
||||
{
|
||||
parent::configure();
|
||||
$this->setDescription('Reset password for a specified user');
|
||||
$this->addArgument('identifier', InputArgument::REQUIRED, 'Username or user ID');
|
||||
$this->addOption('password', 'p', InputOption::VALUE_OPTIONAL, 'New password (min 6 characters). If omitted, will prompt interactively');
|
||||
}
|
||||
|
||||
public function handle(): int
|
||||
{
|
||||
$identifier = $this->input->getArgument('identifier');
|
||||
|
||||
// 按 ID(纯数字)或 username 查找用户
|
||||
$user = is_numeric($identifier)
|
||||
? User::find((int) $identifier)
|
||||
: User::query()->where('username', $identifier)->first();
|
||||
|
||||
if (! $user) {
|
||||
$this->error(sprintf('User "%s" not found.', $identifier));
|
||||
return 1;
|
||||
}
|
||||
|
||||
// 获取密码:优先 --password 选项,否则交互式隐藏输入
|
||||
$new_password = $this->input->getOption('password');
|
||||
if ($new_password === null) {
|
||||
$new_password = $this->secret('Enter new password');
|
||||
if ($new_password === null) {
|
||||
$this->error('Password cannot be empty.');
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
// 密码长度校验(bcrypt 截断超过 72 字节的输入)
|
||||
if (strlen($new_password) < 6) {
|
||||
$this->error('Password must be at least 6 characters.');
|
||||
return 1;
|
||||
}
|
||||
if (strlen($new_password) > 72) {
|
||||
$this->error('Password must not exceed 72 characters (bcrypt limit).');
|
||||
return 1;
|
||||
}
|
||||
|
||||
// password mutator 自动调用 password_hash()
|
||||
$user->password = $new_password;
|
||||
$user->save();
|
||||
|
||||
$this->info(sprintf("Password for user '%s' has been reset.", $user->username));
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user