mirror of
https://github.com/TrinityCore/TrinityCore.git
synced 2026-01-15 23:20:36 +01:00
AccountMgr singleton converted to namespace with free functions as it has no internal state
This commit is contained in:
@@ -23,10 +23,10 @@
|
||||
#include "Util.h"
|
||||
#include "SHA1.h"
|
||||
|
||||
AccountMgr::AccountMgr() {}
|
||||
AccountMgr::~AccountMgr() {}
|
||||
namespace AccountMgr
|
||||
{
|
||||
|
||||
AccountOpResult AccountMgr::CreateAccount(std::string username, std::string password)
|
||||
AccountOpResult CreateAccount(std::string username, std::string password)
|
||||
{
|
||||
if (utf8length(username) > MAX_ACCOUNT_STR)
|
||||
return AOR_NAME_TOO_LONG; // username's too long
|
||||
@@ -43,7 +43,7 @@ AccountOpResult AccountMgr::CreateAccount(std::string username, std::string pass
|
||||
return AOR_OK; // everything's fine
|
||||
}
|
||||
|
||||
AccountOpResult AccountMgr::DeleteAccount(uint32 accountId)
|
||||
AccountOpResult DeleteAccount(uint32 accountId)
|
||||
{
|
||||
QueryResult result = LoginDatabase.PQuery("SELECT 1 FROM account WHERE id='%d'", accountId);
|
||||
if (!result)
|
||||
@@ -89,7 +89,7 @@ AccountOpResult AccountMgr::DeleteAccount(uint32 accountId)
|
||||
return AOR_OK;
|
||||
}
|
||||
|
||||
AccountOpResult AccountMgr::ChangeUsername(uint32 accountId, std::string newUsername, std::string newPassword)
|
||||
AccountOpResult ChangeUsername(uint32 accountId, std::string newUsername, std::string newPassword)
|
||||
{
|
||||
QueryResult result = LoginDatabase.PQuery("SELECT 1 FROM account WHERE id='%d'", accountId);
|
||||
if (!result)
|
||||
@@ -113,7 +113,7 @@ AccountOpResult AccountMgr::ChangeUsername(uint32 accountId, std::string newUser
|
||||
return AOR_OK;
|
||||
}
|
||||
|
||||
AccountOpResult AccountMgr::ChangePassword(uint32 accountId, std::string newPassword)
|
||||
AccountOpResult ChangePassword(uint32 accountId, std::string newPassword)
|
||||
{
|
||||
std::string username;
|
||||
|
||||
@@ -133,20 +133,20 @@ AccountOpResult AccountMgr::ChangePassword(uint32 accountId, std::string newPass
|
||||
return AOR_OK;
|
||||
}
|
||||
|
||||
uint32 AccountMgr::GetId(std::string username)
|
||||
uint32 GetId(std::string username)
|
||||
{
|
||||
LoginDatabase.EscapeString(username);
|
||||
QueryResult result = LoginDatabase.PQuery("SELECT id FROM account WHERE username = '%s'", username.c_str());
|
||||
return (result) ? (*result)[0].GetUInt32() : 0;
|
||||
}
|
||||
|
||||
uint32 AccountMgr::GetSecurity(uint32 accountId)
|
||||
uint32 GetSecurity(uint32 accountId)
|
||||
{
|
||||
QueryResult result = LoginDatabase.PQuery("SELECT gmlevel FROM account_access WHERE id = '%u'", accountId);
|
||||
return (result) ? (*result)[0].GetUInt32() : 0;
|
||||
}
|
||||
|
||||
uint32 AccountMgr::GetSecurity(uint64 accountId, int32 realmId)
|
||||
uint32 GetSecurity(uint64 accountId, int32 realmId)
|
||||
{
|
||||
QueryResult result = (realmId == -1)
|
||||
? LoginDatabase.PQuery("SELECT gmlevel FROM account_access WHERE id = '%u' AND RealmID = '%d'", accountId, realmId)
|
||||
@@ -154,7 +154,7 @@ uint32 AccountMgr::GetSecurity(uint64 accountId, int32 realmId)
|
||||
return (result) ? (*result)[0].GetUInt32() : 0;
|
||||
}
|
||||
|
||||
bool AccountMgr::GetName(uint32 accountId, std::string& name)
|
||||
bool GetName(uint32 accountId, std::string& name)
|
||||
{
|
||||
QueryResult result = LoginDatabase.PQuery("SELECT username FROM account WHERE id = '%u'", accountId);
|
||||
if (result)
|
||||
@@ -166,7 +166,7 @@ bool AccountMgr::GetName(uint32 accountId, std::string& name)
|
||||
return false;
|
||||
}
|
||||
|
||||
bool AccountMgr::CheckPassword(uint32 accountId, std::string password)
|
||||
bool CheckPassword(uint32 accountId, std::string password)
|
||||
{
|
||||
std::string username;
|
||||
|
||||
@@ -180,14 +180,14 @@ bool AccountMgr::CheckPassword(uint32 accountId, std::string password)
|
||||
return (result) ? true : false;
|
||||
}
|
||||
|
||||
uint32 AccountMgr::GetCharactersCount(uint32 accountId)
|
||||
uint32 GetCharactersCount(uint32 accountId)
|
||||
{
|
||||
// check character count
|
||||
QueryResult result = CharacterDatabase.PQuery("SELECT COUNT(guid) FROM characters WHERE account = '%d'", accountId);
|
||||
return (result) ? (*result)[0].GetUInt32() : 0;
|
||||
}
|
||||
|
||||
bool AccountMgr::normalizeString(std::string& utf8String)
|
||||
bool normalizeString(std::string& utf8String)
|
||||
{
|
||||
wchar_t buffer[MAX_ACCOUNT_STR+1];
|
||||
|
||||
@@ -205,7 +205,7 @@ bool AccountMgr::normalizeString(std::string& utf8String)
|
||||
return WStrToUtf8(buffer, maxLength, utf8String);
|
||||
}
|
||||
|
||||
std::string AccountMgr::CalculateShaPassHash(std::string& name, std::string& password)
|
||||
std::string CalculateShaPassHash(std::string& name, std::string& password)
|
||||
{
|
||||
SHA1Hash sha;
|
||||
sha.Initialize();
|
||||
@@ -220,3 +220,4 @@ std::string AccountMgr::CalculateShaPassHash(std::string& name, std::string& pas
|
||||
return encoded;
|
||||
}
|
||||
|
||||
} // Namespace AccountMgr
|
||||
|
||||
@@ -19,10 +19,9 @@
|
||||
#ifndef _ACCMGR_H
|
||||
#define _ACCMGR_H
|
||||
|
||||
#include "Define.h"
|
||||
#include <string>
|
||||
|
||||
#include "Common.h"
|
||||
|
||||
enum AccountOpResult
|
||||
{
|
||||
AOR_OK,
|
||||
@@ -35,12 +34,8 @@ enum AccountOpResult
|
||||
|
||||
#define MAX_ACCOUNT_STR 16
|
||||
|
||||
class AccountMgr
|
||||
namespace AccountMgr
|
||||
{
|
||||
public:
|
||||
AccountMgr();
|
||||
~AccountMgr();
|
||||
|
||||
AccountOpResult CreateAccount(std::string username, std::string password);
|
||||
AccountOpResult DeleteAccount(uint32 accountId);
|
||||
AccountOpResult ChangeUsername(uint32 accountId, std::string newUsername, std::string newPassword);
|
||||
@@ -54,8 +49,7 @@ class AccountMgr
|
||||
uint32 GetCharactersCount(uint32 accountId);
|
||||
std::string CalculateShaPassHash(std::string& name, std::string& password);
|
||||
|
||||
static bool normalizeString(std::string& utf8String);
|
||||
bool normalizeString(std::string& utf8String);
|
||||
};
|
||||
|
||||
#define sAccountMgr ACE_Singleton<AccountMgr, ACE_Null_Mutex>::instance()
|
||||
#endif
|
||||
|
||||
@@ -110,7 +110,7 @@ void AuctionHouseMgr::SendAuctionWonMail(AuctionEntry *auction, SQLTransaction&
|
||||
else
|
||||
{
|
||||
bidder_accId = sObjectMgr->GetPlayerAccountIdByGUID(bidder_guid);
|
||||
bidder_security = sAccountMgr->GetSecurity(bidder_accId, realmID);
|
||||
bidder_security = AccountMgr::GetSecurity(bidder_accId, realmID);
|
||||
|
||||
if (bidder_security > SEC_PLAYER) // not do redundant DB requests
|
||||
{
|
||||
|
||||
@@ -516,7 +516,7 @@ bool ChatHandler::HasLowerSecurityAccount(WorldSession* target, uint32 target_ac
|
||||
if (target)
|
||||
target_sec = target->GetSecurity();
|
||||
else if (target_account)
|
||||
target_sec = sAccountMgr->GetSecurity(target_account, realmID);
|
||||
target_sec = AccountMgr::GetSecurity(target_account, realmID);
|
||||
else
|
||||
return true; // caller must report error for (target == NULL && target_account == 0)
|
||||
|
||||
|
||||
@@ -3065,7 +3065,7 @@ bool ChatHandler::HandleBanInfoAccountCommand(const char *args)
|
||||
return false;
|
||||
}
|
||||
|
||||
uint32 accountid = sAccountMgr->GetId(account_name);
|
||||
uint32 accountid = AccountMgr::GetId(account_name);
|
||||
if (!accountid)
|
||||
{
|
||||
PSendSysMessage(LANG_ACCOUNT_NOT_EXIST, account_name.c_str());
|
||||
@@ -3342,7 +3342,7 @@ bool ChatHandler::HandleBanListHelper(QueryResult result)
|
||||
account_name = fields[1].GetString();
|
||||
// "character" case, name need extract from another DB
|
||||
else
|
||||
sAccountMgr->GetName (account_id, account_name);
|
||||
AccountMgr::GetName (account_id, account_name);
|
||||
|
||||
// No SQL injection. id is uint32.
|
||||
QueryResult banInfo = LoginDatabase.PQuery("SELECT bandate, unbandate, bannedby, banreason FROM account_banned WHERE id = %u ORDER BY unbandate", account_id);
|
||||
@@ -3505,7 +3505,7 @@ bool ChatHandler::HandlePDumpLoadCommand(const char *args)
|
||||
return false;
|
||||
}
|
||||
|
||||
uint32 account_id = sAccountMgr->GetId(account_name);
|
||||
uint32 account_id = AccountMgr::GetId(account_name);
|
||||
if (!account_id)
|
||||
{
|
||||
account_id = atoi(account); // use original string
|
||||
@@ -3517,7 +3517,7 @@ bool ChatHandler::HandlePDumpLoadCommand(const char *args)
|
||||
}
|
||||
}
|
||||
|
||||
if (!sAccountMgr->GetName(account_id, account_name))
|
||||
if (!AccountMgr::GetName(account_id, account_name))
|
||||
{
|
||||
PSendSysMessage(LANG_ACCOUNT_NOT_EXIST, account_name.c_str());
|
||||
SetSentErrorMessage(true);
|
||||
|
||||
@@ -169,7 +169,7 @@ bool ChatHandler::HandleGMTicketAssignToCommand(const char* args)
|
||||
// Get target information
|
||||
uint64 targetGuid = sObjectMgr->GetPlayerGUIDByName(target.c_str());
|
||||
uint64 targetAccId = sObjectMgr->GetPlayerAccountIdByGUID(targetGuid);
|
||||
uint32 targetGmLevel = sAccountMgr->GetSecurity(targetAccId, realmID);
|
||||
uint32 targetGmLevel = AccountMgr::GetSecurity(targetAccId, realmID);
|
||||
// Target must exist and have administrative rights
|
||||
if (!targetGuid || targetGmLevel == SEC_PLAYER)
|
||||
{
|
||||
@@ -227,7 +227,7 @@ bool ChatHandler::HandleGMTicketUnAssignCommand(const char* args)
|
||||
{
|
||||
uint64 guid = ticket->GetAssignedToGUID();
|
||||
uint32 accountId = sObjectMgr->GetPlayerAccountIdByGUID(guid);
|
||||
security = sAccountMgr->GetSecurity(accountId, realmID);
|
||||
security = AccountMgr::GetSecurity(accountId, realmID);
|
||||
}
|
||||
// Check security
|
||||
Player* player = m_session->GetPlayer();
|
||||
|
||||
@@ -560,7 +560,7 @@ void WorldSession::HandleAddFriendOpcodeCallBack(QueryResult result, std::string
|
||||
team = Player::TeamForRace((*result)[1].GetUInt8());
|
||||
friendAcctid = (*result)[2].GetUInt32();
|
||||
|
||||
if (GetSecurity() >= SEC_MODERATOR || sWorld->getBoolConfig(CONFIG_ALLOW_GM_FRIEND) || sAccountMgr->GetSecurity(friendAcctid, realmID) < SEC_MODERATOR)
|
||||
if (GetSecurity() >= SEC_MODERATOR || sWorld->getBoolConfig(CONFIG_ALLOW_GM_FRIEND) || AccountMgr::GetSecurity(friendAcctid, realmID) < SEC_MODERATOR)
|
||||
{
|
||||
if (friendGuid)
|
||||
{
|
||||
|
||||
@@ -382,7 +382,7 @@ void fixNULLfields(std::string &line)
|
||||
|
||||
DumpReturn PlayerDumpReader::LoadDump(const std::string& file, uint32 account, std::string name, uint32 guid)
|
||||
{
|
||||
uint32 charcount = sAccountMgr->GetCharactersCount(account);
|
||||
uint32 charcount = AccountMgr::GetCharactersCount(account);
|
||||
if (charcount >= 10)
|
||||
return DUMP_TOO_MANY_CHARS;
|
||||
|
||||
|
||||
@@ -2298,7 +2298,7 @@ bool World::RemoveBanAccount(BanMode mode, std::string nameOrIP)
|
||||
{
|
||||
uint32 account = 0;
|
||||
if (mode == BAN_ACCOUNT)
|
||||
account = sAccountMgr->GetId(nameOrIP);
|
||||
account = AccountMgr::GetId(nameOrIP);
|
||||
else if (mode == BAN_CHARACTER)
|
||||
account = sObjectMgr->GetPlayerAccountIdByPlayerName(nameOrIP);
|
||||
|
||||
|
||||
@@ -99,11 +99,11 @@ public:
|
||||
if (!szAcc || !szPassword)
|
||||
return false;
|
||||
|
||||
// normalized in sAccountMgr->CreateAccount
|
||||
// normalized in AccountMgr::CreateAccount
|
||||
std::string account_name = szAcc;
|
||||
std::string password = szPassword;
|
||||
|
||||
AccountOpResult result = sAccountMgr->CreateAccount(account_name, password);
|
||||
AccountOpResult result = AccountMgr::CreateAccount(account_name, password);
|
||||
switch(result)
|
||||
{
|
||||
case AOR_OK:
|
||||
@@ -150,7 +150,7 @@ public:
|
||||
return false;
|
||||
}
|
||||
|
||||
uint32 account_id = sAccountMgr->GetId(account_name);
|
||||
uint32 account_id = AccountMgr::GetId(account_name);
|
||||
if (!account_id)
|
||||
{
|
||||
handler->PSendSysMessage(LANG_ACCOUNT_NOT_EXIST, account_name.c_str());
|
||||
@@ -164,7 +164,7 @@ public:
|
||||
if (handler->HasLowerSecurityAccount (NULL, account_id, true))
|
||||
return false;
|
||||
|
||||
AccountOpResult result = sAccountMgr->DeleteAccount(account_id);
|
||||
AccountOpResult result = AccountMgr::DeleteAccount(account_id);
|
||||
switch(result)
|
||||
{
|
||||
case AOR_OK:
|
||||
@@ -282,7 +282,7 @@ public:
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!sAccountMgr->CheckPassword(handler->GetSession()->GetAccountId(), std::string(old_pass)))
|
||||
if (!AccountMgr::CheckPassword(handler->GetSession()->GetAccountId(), std::string(old_pass)))
|
||||
{
|
||||
handler->SendSysMessage(LANG_COMMAND_WRONGOLDPASSWORD);
|
||||
handler->SetSentErrorMessage(true);
|
||||
@@ -296,7 +296,7 @@ public:
|
||||
return false;
|
||||
}
|
||||
|
||||
AccountOpResult result = sAccountMgr->ChangePassword(handler->GetSession()->GetAccountId(), std::string(new_pass));
|
||||
AccountOpResult result = AccountMgr::ChangePassword(handler->GetSession()->GetAccountId(), std::string(new_pass));
|
||||
switch(result)
|
||||
{
|
||||
case AOR_OK:
|
||||
@@ -342,7 +342,7 @@ public:
|
||||
return false;
|
||||
|
||||
account_id = player->GetSession()->GetAccountId();
|
||||
sAccountMgr->GetName(account_id, account_name);
|
||||
AccountMgr::GetName(account_id, account_name);
|
||||
szExp = szAcc;
|
||||
}
|
||||
else
|
||||
@@ -356,7 +356,7 @@ public:
|
||||
return false;
|
||||
}
|
||||
|
||||
account_id = sAccountMgr->GetId(account_name);
|
||||
account_id = AccountMgr::GetId(account_name);
|
||||
if (!account_id)
|
||||
{
|
||||
handler->PSendSysMessage(LANG_ACCOUNT_NOT_EXIST, account_name.c_str());
|
||||
@@ -429,17 +429,17 @@ public:
|
||||
}
|
||||
|
||||
// handler->getSession() == NULL only for console
|
||||
targetAccountId = (isAccountNameGiven) ? sAccountMgr->GetId(targetAccountName) : handler->getSelectedPlayer()->GetSession()->GetAccountId();
|
||||
targetAccountId = (isAccountNameGiven) ? AccountMgr::GetId(targetAccountName) : handler->getSelectedPlayer()->GetSession()->GetAccountId();
|
||||
int32 gmRealmID = (isAccountNameGiven) ? atoi(arg3) : atoi(arg2);
|
||||
uint32 plSecurity;
|
||||
if (handler->GetSession())
|
||||
plSecurity = sAccountMgr->GetSecurity(handler->GetSession()->GetAccountId(), gmRealmID);
|
||||
plSecurity = AccountMgr::GetSecurity(handler->GetSession()->GetAccountId(), gmRealmID);
|
||||
else
|
||||
plSecurity = SEC_CONSOLE;
|
||||
|
||||
// can set security level only for target with less security and to less security that we have
|
||||
// This is also reject self apply in fact
|
||||
targetSecurity = sAccountMgr->GetSecurity(targetAccountId, gmRealmID);
|
||||
targetSecurity = AccountMgr::GetSecurity(targetAccountId, gmRealmID);
|
||||
if (targetSecurity >= plSecurity || gm >= plSecurity)
|
||||
{
|
||||
handler->SendSysMessage(LANG_YOURS_SECURITY_IS_LOW);
|
||||
@@ -501,7 +501,7 @@ public:
|
||||
return false;
|
||||
}
|
||||
|
||||
uint32 targetAccountId = sAccountMgr->GetId(account_name);
|
||||
uint32 targetAccountId = AccountMgr::GetId(account_name);
|
||||
if (!targetAccountId)
|
||||
{
|
||||
handler->PSendSysMessage(LANG_ACCOUNT_NOT_EXIST, account_name.c_str());
|
||||
@@ -521,7 +521,7 @@ public:
|
||||
return false;
|
||||
}
|
||||
|
||||
AccountOpResult result = sAccountMgr->ChangePassword(targetAccountId, szPassword1);
|
||||
AccountOpResult result = AccountMgr::ChangePassword(targetAccountId, szPassword1);
|
||||
|
||||
switch (result)
|
||||
{
|
||||
|
||||
@@ -155,7 +155,7 @@ bool ChatHandler::GetDeletedCharacterInfoList(DeletedInfoList& foundList, std::s
|
||||
info.accountId = fields[2].GetUInt32();
|
||||
|
||||
// account name will be empty for not existed account
|
||||
sAccountMgr->GetName(info.accountId, info.accountName);
|
||||
AccountMgr::GetName(info.accountId, info.accountName);
|
||||
|
||||
info.deleteDate = time_t(fields[3].GetUInt32());
|
||||
|
||||
@@ -278,7 +278,7 @@ void ChatHandler::HandleCharacterDeletedRestoreHelper(DeletedInfo const& delInfo
|
||||
}
|
||||
|
||||
// check character count
|
||||
uint32 charcount = sAccountMgr->GetCharactersCount(delInfo.accountId);
|
||||
uint32 charcount = AccountMgr::GetCharactersCount(delInfo.accountId);
|
||||
if (charcount >= 10)
|
||||
{
|
||||
PSendSysMessage(LANG_CHARACTER_DELETED_SKIP_FULL, delInfo.name.c_str(), delInfo.lowguid, delInfo.accountId);
|
||||
@@ -350,7 +350,7 @@ bool ChatHandler::HandleCharacterDeletedRestoreCommand(const char* args)
|
||||
if (newAccount && newAccount != delInfo.accountId)
|
||||
{
|
||||
delInfo.accountId = newAccount;
|
||||
sAccountMgr->GetName(newAccount, delInfo.accountName);
|
||||
AccountMgr::GetName(newAccount, delInfo.accountName);
|
||||
}
|
||||
|
||||
HandleCharacterDeletedRestoreHelper(delInfo);
|
||||
@@ -466,7 +466,7 @@ bool ChatHandler::HandleCharacterEraseCommand(const char* args){
|
||||
}
|
||||
|
||||
std::string account_name;
|
||||
sAccountMgr->GetName (account_id, account_name);
|
||||
AccountMgr::GetName (account_id, account_name);
|
||||
|
||||
Player::DeleteFromDB(character_guid, account_id, true, true);
|
||||
PSendSysMessage(LANG_CHARACTER_DELETED, character_name.c_str(), GUID_LOPART(character_guid), account_name.c_str(), account_id);
|
||||
|
||||
@@ -213,7 +213,7 @@ int RASocket::check_password(const std::string& user, const std::string& pass)
|
||||
AccountMgr::normalizeString(safe_pass);
|
||||
LoginDatabase.EscapeString(safe_pass);
|
||||
|
||||
std::string hash = sAccountMgr->CalculateShaPassHash(safe_user, safe_pass);
|
||||
std::string hash = AccountMgr::CalculateShaPassHash(safe_user, safe_pass);
|
||||
|
||||
QueryResult check = LoginDatabase.PQuery(
|
||||
"SELECT 1 FROM account WHERE username = '%s' AND sha_pass_hash = '%s'",
|
||||
|
||||
@@ -82,20 +82,20 @@ int ns1__executeCommand(soap* soap, char* command, char** result)
|
||||
return 401;
|
||||
}
|
||||
|
||||
uint32 accountId = sAccountMgr->GetId(soap->userid);
|
||||
uint32 accountId = AccountMgr::GetId(soap->userid);
|
||||
if(!accountId)
|
||||
{
|
||||
sLog->outDebug(LOG_FILTER_NETWORKIO, "TCSoap: Client used invalid username '%s'", soap->userid);
|
||||
return 401;
|
||||
}
|
||||
|
||||
if(!sAccountMgr->CheckPassword(accountId, soap->passwd))
|
||||
if(!AccountMgr::CheckPassword(accountId, soap->passwd))
|
||||
{
|
||||
sLog->outDebug(LOG_FILTER_NETWORKIO, "TCSoap: invalid password for account '%s'", soap->userid);
|
||||
return 401;
|
||||
}
|
||||
|
||||
if(sAccountMgr->GetSecurity(accountId) < SEC_ADMINISTRATOR)
|
||||
if(AccountMgr::GetSecurity(accountId) < SEC_ADMINISTRATOR)
|
||||
{
|
||||
sLog->outDebug(LOG_FILTER_NETWORKIO, "TCSoap: %s's gmlevel is too low", soap->userid);
|
||||
return 403;
|
||||
|
||||
Reference in New Issue
Block a user