fix для работы с vmangos

This commit is contained in:
2026-10-04 13:02:12 +04:00
parent 028aa9e35d
commit 5952e966d2
18 changed files with 400 additions and 163 deletions
@@ -3,6 +3,7 @@
namespace App\Console\Commands;
use App\Services\VMangosSrpService;
use Carbon\CarbonImmutable;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\File;
@@ -12,102 +13,123 @@ class ImportWebAccountsToVMangos extends Command
{
protected $signature = 'vmangos:import-web-accounts {--dry-run : Show counts without writing} {--include-bots : Import RNDBOT accounts too}';
protected $description = 'Create vMaNGOS accounts for existing web users, preserving their web passwords and using random game passwords';
protected $description = 'Create vMaNGOS accounts from web.users with random game passwords; keep web passwords unchanged';
public function handle(VMangosSrpService $srp): int
{
$source = DB::connection(config('moonwell.legacy_auth_connection'));
$source = DB::connection();
$target = DB::connection(config('moonwell.auth_connection'));
$query = $source->table('account')->selectRaw('id, username, email, locked, joindate, HEX(salt) AS salt_hex, HEX(verifier) AS verifier_hex')->orderBy('id');
if (! $this->option('include-bots')) {
$query->where('username', 'not like', config('moonwell.portal.bot_account_prefix').'%');
$query = $source->table('users')->select('id', 'name', 'email', 'created_at')->orderBy('id');
$prefix = strtoupper(trim((string) config('moonwell.portal.bot_account_prefix')));
if (! $this->option('include-bots') && $prefix !== '') {
$query->whereRaw('UPPER(name) NOT LIKE ?', [$prefix.'%']);
}
$created = 0;
$linked = 0;
$skipped = 0;
foreach ($query->cursor() as $legacy) {
$username = $srp->normalizeUsername($legacy->username);
$users = $query->get();
$names = [];
foreach ($users as $user) {
$username = $srp->normalizeUsername($user->name);
if (preg_match('/^[A-Z0-9]{3,16}$/D', $username) !== 1) {
throw new RuntimeException("Unsupported legacy account name for ID {$legacy->id}.");
$this->error("Unsupported web.users name for ID {$user->id}; expected 3–16 Latin letters or digits.");
return self::FAILURE;
}
if (isset($names[$username])) {
$this->error("Duplicate normalized username for web.users IDs {$names[$username]} and {$user->id}.");
$existing = $target->table('account')->where('username', $username)->first(['id']);
$credential = $existing ? $target->table('site_credentials')->where('account_id', $existing->id)->first() : null;
if ($credential) {
if ((int) $credential->legacy_account_id !== (int) $legacy->id) {
throw new RuntimeException("Account name conflict for legacy ID {$legacy->id}.");
}
$skipped++;
continue;
return self::FAILURE;
}
if ($this->option('dry-run')) {
$existing ? $linked++ : $created++;
continue;
}
$target->transaction(function () use ($target, $source, $srp, $legacy, $username, $existing): void {
// Only the SRP verifier is stored; the random plaintext is discarded.
[$gameSalt, $gameVerifier] = $srp->makeRegistrationData($username, bin2hex(random_bytes(16)));
if ($existing) {
$accountId = (int) $existing->id;
$target->table('account')->where('id', $accountId)->update([
's' => $gameSalt,
'v' => $gameVerifier,
'sessionkey' => null,
'email' => $legacy->email,
'locked' => (int) $legacy->locked,
]);
} else {
$accountId = (int) $target->table('account')->insertGetId([
'username' => $username,
's' => $gameSalt,
'v' => $gameVerifier,
'email' => $legacy->email,
'locked' => (int) $legacy->locked,
'expansion' => 0,
'joindate' => $legacy->joindate,
]);
}
$target->table('site_credentials')->insert([
'account_id' => $accountId,
'legacy_account_id' => (int) $legacy->id,
'salt_hex' => $legacy->salt_hex,
'verifier_hex' => $legacy->verifier_hex,
'created_at' => now(),
'updated_at' => now(),
]);
foreach ($target->table('realmlist')->pluck('id') as $realmId) {
$target->table('realmcharacters')->insertOrIgnore(['realmid' => (int) $realmId, 'acctid' => $accountId, 'numchars' => 0]);
}
$gmLevel = (int) $source->table('account_access')->where('id', $legacy->id)->max('gmlevel');
if ($gmLevel > 0) {
$target->table('account_access')->updateOrInsert(
['id' => $accountId, 'RealmID' => -1],
['gmlevel' => $gmLevel],
);
}
}, 3);
$existing ? $linked++ : $created++;
$names[$username] = $user->id;
}
if (! $this->option('dry-run') && ($created + $linked) > 0) {
// Old bearer tokens and sessions contain AzerothCore IDs, which may
// now identify a different vMaNGOS account. Force fresh login.
DB::table('oauth_access_tokens')->where('revoked', false)->update(['revoked' => true]);
DB::table('oauth_refresh_tokens')->where('revoked', false)->update(['revoked' => true]);
if (config('session.driver') === 'database') {
DB::table(config('session.table', 'sessions'))->delete();
} elseif (config('session.driver') === 'file') {
foreach (File::files(config('session.files')) as $sessionFile) {
File::delete($sessionFile->getPathname());
try {
[$created, $linked, $skipped] = $source->transaction(fn (): array => $target->transaction(function () use ($target, $source, $srp, $users): array {
$created = $linked = $skipped = 0;
$plans = [];
foreach ($users as $user) {
$username = $srp->normalizeUsername($user->name);
$mapping = $target->table('site_credentials')->where('web_user_id', $user->id)->first();
$existing = $target->table('account')->where('username', $username)->lockForUpdate()->first(['id', 'email']);
if ($mapping) {
if (! $existing || (int) $mapping->account_id !== (int) $existing->id) {
throw new RuntimeException("Account name changed or mapping conflict for web.users ID {$user->id}.");
}
$skipped++;
continue;
}
$credential = $existing ? $target->table('site_credentials')->where('account_id', $existing->id)->first() : null;
if ($credential?->web_user_id !== null) {
throw new RuntimeException("Account already belongs to another web user; web.users ID {$user->id}.");
}
if ($existing && $existing->email && strcasecmp(trim($existing->email), trim($user->email)) !== 0) {
throw new RuntimeException("Existing game account email differs for web.users ID {$user->id}; resolve the conflict before importing.");
}
$existing ? $linked++ : $created++;
$plans[] = [$user, $username, $existing, $credential];
}
if ($this->option('dry-run')) {
return [$created, $linked, $skipped];
}
foreach ($plans as [$user, $username, $existing, $credential]) {
// Discard the random plaintext. Web authentication reads users.password.
[$salt, $verifier] = $srp->makeRegistrationData($username, bin2hex(random_bytes(16)));
if ($existing) {
$accountId = (int) $existing->id;
$target->table('account')->where('id', $accountId)->update([
's' => $salt, 'v' => $verifier, 'sessionkey' => null, 'email' => $user->email,
]);
$target->table('launcher_ticket')->where('account_id', $accountId)->delete();
} else {
$accountId = (int) $target->table('account')->insertGetId([
'username' => $username, 's' => $salt, 'v' => $verifier,
'email' => $user->email, 'expansion' => 0,
'joindate' => $user->created_at
? CarbonImmutable::parse($user->created_at, config('app.timezone'))->utc()->toDateTimeString()
: now('UTC')->toDateTimeString(),
]);
$target->table('account_access')->insert([
'id' => $accountId,
'RealmID' => (int) config('moonwell.registration.realm_id'),
'gmlevel' => (int) config('moonwell.registration.gmlevel'),
]);
}
$target->table('site_credentials')->updateOrInsert(['account_id' => $accountId], [
'web_user_id' => (int) $user->id,
'salt_hex' => null, 'verifier_hex' => null,
'created_at' => $credential?->created_at ?? now('UTC'),
'updated_at' => now('UTC'),
]);
foreach ($target->table('realmlist')->pluck('id') as $realmId) {
$target->table('realmcharacters')->insertOrIgnore(['realmid' => (int) $realmId, 'acctid' => $accountId, 'numchars' => 0]);
}
}
if (! $this->option('dry-run') && ($created + $linked) > 0) {
// Old sessions contain IDs from the former authentication source.
$source->table('oauth_access_tokens')->where('revoked', false)->update(['revoked' => true]);
$source->table('oauth_refresh_tokens')->where('revoked', false)->update(['revoked' => true]);
if (config('session.driver') === 'database') {
$source->table(config('session.table', 'sessions'))->delete();
}
}
return [$created, $linked, $skipped];
}));
} catch (RuntimeException $exception) {
$this->error($exception->getMessage());
return self::FAILURE;
}
if (! $this->option('dry-run') && ($created + $linked) > 0 && config('session.driver') === 'file') {
foreach (File::files(config('session.files')) as $sessionFile) {
File::delete($sessionFile->getPathname());
}
}