add command user:generate

This commit is contained in:
lgb
2024-02-26 12:08:57 +08:00
parent 9efa5b2ae5
commit 86dd3ef4bf
2 changed files with 81 additions and 12 deletions

View File

@@ -0,0 +1,53 @@
<?php
namespace App\Console\Commands;
use App\Models\User;
use Database\Factories\UserFactory;
use Illuminate\Console\Command;
class UserGenerate extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'user:generate {--num=}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Generate some user. options: --num=?';
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$num = $this->option("num");
$log = "num: $num";
if (!$num) {
$this->error("$log, no num!");
}
$size = 1000;
$total = 0;
do {
if ($num < $size) {
$size = $num;
}
if ($total + $size > $num) {
$size = $num - $total;
}
User::factory($size)->create();
$total += $size;
$this->info("$log, success create $size !");
} while($total < $num);
$this->info("$log, total: $total, ALL DONE!");
return Command::SUCCESS;
}
}

View File

@@ -4,10 +4,19 @@ namespace Database\Factories;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
class UserFactory extends Factory
{
private string $defaultStyleSheet;
public function __construct()
{
do_log("UserFactory __construct");
$this->defaultStyleSheet = get_setting("main.defstylesheet");
}
/**
* The name of the factory's corresponding model.
*
@@ -22,12 +31,19 @@ class UserFactory extends Factory
*/
public function definition()
{
$password = "123456";
$secret = mksecret();
$passhash = md5($secret . $password . $secret);
return [
'name' => $this->faker->name,
'username' => $this->faker->name,
'email' => $this->faker->unique()->safeEmail,
'email_verified_at' => now(),
'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
'remember_token' => Str::random(10),
'secret' => mksecret(),
'editsecret' => "",
'passhash' => $passhash,
'stylesheet' => $this->defaultStyleSheet,
'added' => now()->toDateTimeString(),
'status' => User::STATUS_CONFIRMED,
'class' => random_int(intval(User::CLASS_USER), intval(User::CLASS_SYSOP))
];
}
@@ -36,12 +52,12 @@ class UserFactory extends Factory
*
* @return \Illuminate\Database\Eloquent\Factories\Factory
*/
public function unverified()
{
return $this->state(function (array $attributes) {
return [
'email_verified_at' => null,
];
});
}
// public function unverified()
// {
// return $this->state(function (array $attributes) {
// return [
// 'email_verified_at' => null,
// ];
// });
// }
}