34 lines
1.2 KiB
PHP
34 lines
1.2 KiB
PHP
<?php
|
|
|
|
namespace app\command;
|
|
|
|
use app\common\model\User;
|
|
use Symfony\Component\Console\Command\Command;
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
|
class InitUserPasswordCommand extends Command
|
|
{
|
|
protected static $defaultName = 'init:user-password';
|
|
protected static $defaultDescription = '初始化user表密码,如果密码为空则用sn加密后写入password字段';
|
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int
|
|
{
|
|
$users = User::whereNull('password')->orWhere('password', '')->get();
|
|
$count = 0;
|
|
|
|
foreach ($users as $user) {
|
|
if (!empty($user->sn)) {
|
|
$user->password = password_hash($user->sn, PASSWORD_DEFAULT);
|
|
$user->save();
|
|
$output->writeln("用户ID {$user->id} 密码已初始化");
|
|
$count++;
|
|
} else {
|
|
$output->writeln("<error>用户ID {$user->id} 的sn字段为空,无法初始化密码</error>");
|
|
}
|
|
}
|
|
|
|
$output->writeln("共初始化 {$count} 个用户密码");
|
|
return self::SUCCESS;
|
|
}
|
|
} |