fix request
This commit is contained in:
@@ -4,6 +4,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Command;
|
||||
|
||||
use App\Model\Role; // phpcs:ignore -- used in handle()
|
||||
use App\Model\User;
|
||||
use Hyperf\Command\Command as HyperfCommand;
|
||||
use Hyperf\Command\Annotation\Command;
|
||||
@@ -29,29 +30,52 @@ class AppInstall extends HyperfCommand
|
||||
|
||||
// 检查 admin 用户是否已存在
|
||||
$existingUser = User::query()
|
||||
->where('username', 'adminstrator')
|
||||
->where('username', 'administrator')
|
||||
->orWhere('email', 'admin@admin.com')
|
||||
->first();
|
||||
|
||||
if ($existingUser) {
|
||||
$this->error('Admin user already exists!');
|
||||
$this->line('Admin user already exists.', 'comment');
|
||||
$this->line('Username: ' . $existingUser->username);
|
||||
$this->line('Email: ' . $existingUser->email);
|
||||
return 1;
|
||||
|
||||
// 自动修复:缺失的 role_id
|
||||
$adminRole = Role::query()->where('name', 'administrator')->first();
|
||||
if ($adminRole && $existingUser->role_id !== $adminRole->id) {
|
||||
$existingUser->role_id = $adminRole->id;
|
||||
$existingUser->save();
|
||||
$this->line('Fixed: role_id set to administrator.', 'info');
|
||||
}
|
||||
|
||||
// 自动修复:旧拼写错误的用户名
|
||||
if ($existingUser->username === 'adminstrator') {
|
||||
$existingUser->username = 'administrator';
|
||||
$existingUser->save();
|
||||
$this->line('Fixed: username corrected to "administrator".', 'info');
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 创建默认 admin 用户
|
||||
try {
|
||||
$adminRole = Role::query()->where('name', 'administrator')->first();
|
||||
if (!$adminRole) {
|
||||
$this->error('Role "administrator" not found. Please run migrations first.');
|
||||
return 1;
|
||||
}
|
||||
|
||||
$user = User::create([
|
||||
'username' => 'adminstrator',
|
||||
'username' => 'administrator',
|
||||
'email' => 'admin@admin.com',
|
||||
'password' => 'admin',
|
||||
'status' => 1,
|
||||
'role_id' => $adminRole->id,
|
||||
'ext' => [],
|
||||
]);
|
||||
|
||||
$this->line('Default admin user created successfully!', 'info');
|
||||
$this->line('Username: adminstrator', 'comment');
|
||||
$this->line('Username: administrator', 'comment');
|
||||
$this->line('Email: admin@admin.com', 'comment');
|
||||
$this->line('Password: admin', 'comment');
|
||||
$this->line('');
|
||||
|
||||
Reference in New Issue
Block a user