From 3f46e05d3691895b6b8a5b3832d17ecb1e210791 Mon Sep 17 00:00:00 2001 From: Kitzunu <24550914+Kitzunu@users.noreply.github.com> Date: Sun, 27 Jul 2025 08:54:16 +0200 Subject: [PATCH] refactor(Core/Account): Move account flag logic from AccountMgr to WorldSession (#22558) --- .../Database/Implementation/LoginDatabase.cpp | 3 +- .../Database/Implementation/LoginDatabase.h | 3 +- src/server/game/Accounts/AccountMgr.cpp | 33 ------------------- src/server/game/Accounts/AccountMgr.h | 4 --- src/server/game/Server/WorldSession.cpp | 27 ++++++++++++++- src/server/game/Server/WorldSession.h | 8 ++++- src/server/game/Server/WorldSocket.cpp | 6 ++-- 7 files changed, 38 insertions(+), 46 deletions(-) diff --git a/src/server/database/Database/Implementation/LoginDatabase.cpp b/src/server/database/Database/Implementation/LoginDatabase.cpp index 9c782ff3a..8821fe9d3 100644 --- a/src/server/database/Database/Implementation/LoginDatabase.cpp +++ b/src/server/database/Database/Implementation/LoginDatabase.cpp @@ -102,8 +102,7 @@ void LoginDatabaseConnection::DoPrepareStatements() PrepareStatement(LOGIN_SEL_CHECK_PASSWORD, "SELECT salt, verifier FROM account WHERE id = ?", CONNECTION_SYNCH); PrepareStatement(LOGIN_SEL_CHECK_PASSWORD_BY_NAME, "SELECT salt, verifier FROM account WHERE username = ?", CONNECTION_SYNCH); PrepareStatement(LOGIN_SEL_ACCOUNT_FLAG, "SELECT Flags FROM account WHERE id = ?", CONNECTION_SYNCH); - PrepareStatement(LOGIN_UPD_ADD_ACCOUNT_FLAG, "UPDATE account SET Flags = Flags | ? WHERE id = ?", CONNECTION_ASYNC); - PrepareStatement(LOGIN_UPD_REMOVE_ACCOUNT_FLAG, "UPDATE account SET Flags = Flags &~ ? WHERE id = ?", CONNECTION_ASYNC); + PrepareStatement(LOGIN_UPD_SET_ACCOUNT_FLAG, "UPDATE account SET Flags = ? WHERE id = ?", CONNECTION_ASYNC); PrepareStatement(LOGIN_SEL_PINFO, "SELECT a.username, aa.gmlevel, a.email, a.reg_mail, a.last_ip, DATE_FORMAT(a.last_login, '%Y-%m-%d %T'), a.mutetime, a.mutereason, a.muteby, a.failed_logins, a.locked, a.OS FROM account a LEFT JOIN account_access aa ON (a.id = aa.id AND (aa.RealmID = ? OR aa.RealmID = -1)) WHERE a.id = ?", CONNECTION_SYNCH); PrepareStatement(LOGIN_SEL_PINFO_BANS, "SELECT unbandate, bandate = unbandate, bannedby, banreason FROM account_banned WHERE id = ? AND active ORDER BY bandate ASC LIMIT 1", CONNECTION_SYNCH); PrepareStatement(LOGIN_SEL_GM_ACCOUNTS, "SELECT a.username, aa.gmlevel FROM account a, account_access aa WHERE a.id=aa.id AND aa.gmlevel >= ? AND (aa.realmid = -1 OR aa.realmid = ?)", CONNECTION_SYNCH); diff --git a/src/server/database/Database/Implementation/LoginDatabase.h b/src/server/database/Database/Implementation/LoginDatabase.h index 230e86452..e11c285b4 100644 --- a/src/server/database/Database/Implementation/LoginDatabase.h +++ b/src/server/database/Database/Implementation/LoginDatabase.h @@ -86,8 +86,7 @@ enum LoginDatabaseStatements : uint32 LOGIN_SEL_CHECK_PASSWORD, LOGIN_SEL_CHECK_PASSWORD_BY_NAME, LOGIN_SEL_ACCOUNT_FLAG, - LOGIN_UPD_ADD_ACCOUNT_FLAG, - LOGIN_UPD_REMOVE_ACCOUNT_FLAG, + LOGIN_UPD_SET_ACCOUNT_FLAG, LOGIN_SEL_PINFO, LOGIN_SEL_PINFO_BANS, LOGIN_SEL_GM_ACCOUNTS, diff --git a/src/server/game/Accounts/AccountMgr.cpp b/src/server/game/Accounts/AccountMgr.cpp index c4f0649c1..4129609db 100644 --- a/src/server/game/Accounts/AccountMgr.cpp +++ b/src/server/game/Accounts/AccountMgr.cpp @@ -299,39 +299,6 @@ namespace AccountMgr return false; } - bool HasAccountFlag(uint32 accountId, uint32 flag) - { - LoginDatabasePreparedStatement* stmt = LoginDatabase.GetPreparedStatement(LOGIN_SEL_ACCOUNT_FLAG); - stmt->SetData(0, accountId); - if (PreparedQueryResult result = LoginDatabase.Query(stmt)) - { - uint32 flags = (*result)[0].Get(); - return (flags & flag) != 0; - } - - return false; - } - - void UpdateAccountFlag(uint32 accountId, uint32 flag, bool remove /*= false*/) - { - LoginDatabasePreparedStatement* stmt = LoginDatabase.GetPreparedStatement( - remove ? LOGIN_UPD_REMOVE_ACCOUNT_FLAG : LOGIN_UPD_ADD_ACCOUNT_FLAG - ); - stmt->SetData(0, flag); - stmt->SetData(1, accountId); - LoginDatabase.Execute(stmt); - } - - void ValidateAccountFlags(uint32 accountId, uint32 flags, uint32 security) - { - bool hasGMFlag = (flags & ACCOUNT_FLAG_GM) != 0; - - if (IsGMAccount(security) && !hasGMFlag) - UpdateAccountFlag(accountId, ACCOUNT_FLAG_GM); - else if (hasGMFlag && !IsGMAccount(security)) - UpdateAccountFlag(accountId, ACCOUNT_FLAG_GM, true); - } - uint32 GetCharactersCount(uint32 accountId) { // check character count diff --git a/src/server/game/Accounts/AccountMgr.h b/src/server/game/Accounts/AccountMgr.h index d2fa715b5..3f2eb983e 100644 --- a/src/server/game/Accounts/AccountMgr.h +++ b/src/server/game/Accounts/AccountMgr.h @@ -56,10 +56,6 @@ namespace AccountMgr bool IsGMAccount(uint32 gmlevel); bool IsAdminAccount(uint32 gmlevel); bool IsConsoleAccount(uint32 gmlevel); - - bool HasAccountFlag(uint32 accountId, uint32 flag); - void UpdateAccountFlag(uint32 accountId, uint32 flag, bool remove = false); - void ValidateAccountFlags(uint32 accountId, uint32 flags, uint32 security); }; #endif diff --git a/src/server/game/Server/WorldSession.cpp b/src/server/game/Server/WorldSession.cpp index 07167f5b0..c8d336fe5 100644 --- a/src/server/game/Server/WorldSession.cpp +++ b/src/server/game/Server/WorldSession.cpp @@ -104,7 +104,7 @@ bool WorldSessionFilter::Process(WorldPacket* packet) } /// WorldSession constructor -WorldSession::WorldSession(uint32 id, std::string&& name, std::shared_ptr sock, AccountTypes sec, uint8 expansion, +WorldSession::WorldSession(uint32 id, std::string&& name, uint32 accountFlags, std::shared_ptr sock, AccountTypes sec, uint8 expansion, time_t mute_time, LocaleConstant locale, uint32 recruiter, bool isARecruiter, bool skipQueue, uint32 TotalTime) : m_muteTime(mute_time), m_timeOutTime(0), @@ -116,6 +116,7 @@ WorldSession::WorldSession(uint32 id, std::string&& name, std::shared_ptrSetData(0, _accountFlags); + stmt->SetData(1, GetAccountId()); + LoginDatabase.Execute(stmt); +} + +void WorldSession::ValidateAccountFlags() +{ + bool hasGMFlag = HasAccountFlag(ACCOUNT_FLAG_GM); + + if (IsGMAccount() && !hasGMFlag) + UpdateAccountFlag(ACCOUNT_FLAG_GM); + else if (hasGMFlag && !IsGMAccount()) + UpdateAccountFlag(ACCOUNT_FLAG_GM, true); +} + bool WorldSession::IsGMAccount() const { return GetSecurity() >= SEC_GAMEMASTER; diff --git a/src/server/game/Server/WorldSession.h b/src/server/game/Server/WorldSession.h index 73ec2b542..02948d249 100644 --- a/src/server/game/Server/WorldSession.h +++ b/src/server/game/Server/WorldSession.h @@ -329,9 +329,14 @@ struct PacketCounter class WorldSession { public: - WorldSession(uint32 id, std::string&& name, std::shared_ptr sock, AccountTypes sec, uint8 expansion, time_t mute_time, LocaleConstant locale, uint32 recruiter, bool isARecruiter, bool skipQueue, uint32 TotalTime); + WorldSession(uint32 id, std::string&& name, uint32 accountFlags, std::shared_ptr sock, AccountTypes sec, uint8 expansion, time_t mute_time, LocaleConstant locale, uint32 recruiter, bool isARecruiter, bool skipQueue, uint32 TotalTime); ~WorldSession(); + uint32 GetAccountFlags() const { return _accountFlags; } + bool HasAccountFlag(uint32 flag) const { return (_accountFlags & flag) != 0; } + void UpdateAccountFlag(uint32 flag, bool remove = false); + void ValidateAccountFlags(); + bool IsGMAccount() const; bool PlayerLoading() const { return m_playerLoading; } @@ -1158,6 +1163,7 @@ private: bool _skipQueue; uint32 _accountId; std::string _accountName; + uint32 _accountFlags; uint8 m_expansion; uint32 m_total_time; diff --git a/src/server/game/Server/WorldSocket.cpp b/src/server/game/Server/WorldSocket.cpp index 9c4d9de8e..51705d18e 100644 --- a/src/server/game/Server/WorldSocket.cpp +++ b/src/server/game/Server/WorldSocket.cpp @@ -698,8 +698,6 @@ void WorldSocket::HandleAuthSessionCallback(std::shared_ptr authSes LoginDatabase.Execute(stmt); - AccountMgr::ValidateAccountFlags(account.Id, account.Flags, account.Security); - // At this point, we can safely hook a successful login sScriptMgr->OnAccountLogin(account.Id); @@ -707,7 +705,7 @@ void WorldSocket::HandleAuthSessionCallback(std::shared_ptr authSes sScriptMgr->OnLastIpUpdate(account.Id, address); - _worldSession = new WorldSession(account.Id, std::move(authSession->Account), shared_from_this(), account.Security, + _worldSession = new WorldSession(account.Id, std::move(authSession->Account), account.Flags, shared_from_this(), account.Security, account.Expansion, account.MuteTime, account.Locale, account.Recruiter, account.IsRectuiter, account.Security ? true : false, account.TotalTime); _worldSession->ReadAddonsInfo(authSession->AddonInfo); @@ -718,6 +716,8 @@ void WorldSocket::HandleAuthSessionCallback(std::shared_ptr authSes _worldSession->InitWarden(account.SessionKey, account.OS); } + _worldSession->ValidateAccountFlags(); + sWorldSessionMgr->AddSession(_worldSession); AsyncRead();