mirror of
https://github.com/TrinityCore/TrinityCore.git
synced 2026-01-23 10:26:28 +01:00
Core/Account: Code style and cleanups in AccountMgr
This commit is contained in:
@@ -35,9 +35,7 @@ AccountOpResult AccountMgr::CreateAccount(std::string username, std::string pass
|
||||
normalizeString(password);
|
||||
|
||||
if (GetId(username))
|
||||
{
|
||||
return AOR_NAME_ALREDY_EXIST; // username does already exist
|
||||
}
|
||||
|
||||
LoginDatabase.PExecute("INSERT INTO account(username, sha_pass_hash, joindate) VALUES('%s', '%s', NOW())", username.c_str(), CalculateShaPassHash(username, password).c_str());
|
||||
LoginDatabase.Execute("INSERT INTO realmcharacters (realmid, acctid, numchars) SELECT realmlist.id, account.id, 0 FROM realmlist, account LEFT JOIN realmcharacters ON acctid=account.id WHERE acctid IS NULL");
|
||||
@@ -45,23 +43,22 @@ AccountOpResult AccountMgr::CreateAccount(std::string username, std::string pass
|
||||
return AOR_OK; // everything's fine
|
||||
}
|
||||
|
||||
AccountOpResult AccountMgr::DeleteAccount(uint32 accid)
|
||||
AccountOpResult AccountMgr::DeleteAccount(uint32 accountId)
|
||||
{
|
||||
QueryResult result = LoginDatabase.PQuery("SELECT 1 FROM account WHERE id='%d'", accid);
|
||||
QueryResult result = LoginDatabase.PQuery("SELECT 1 FROM account WHERE id='%d'", accountId);
|
||||
if (!result)
|
||||
return AOR_NAME_NOT_EXIST; // account doesn't exist
|
||||
|
||||
// existed characters list
|
||||
result = CharacterDatabase.PQuery("SELECT guid FROM characters WHERE account='%d'", accid);
|
||||
result = CharacterDatabase.PQuery("SELECT guid FROM characters WHERE account='%d'", accountId);
|
||||
if (result)
|
||||
{
|
||||
do
|
||||
{
|
||||
Field *fields = result->Fetch();
|
||||
uint32 guidlo = fields[0].GetUInt32();
|
||||
uint64 guid = MAKE_NEW_GUID(guidlo, 0, HIGHGUID_PLAYER);
|
||||
uint32 guidLow = (*result)[0].GetUInt32();
|
||||
uint64 guid = MAKE_NEW_GUID(guidLow, 0, HIGHGUID_PLAYER);
|
||||
|
||||
// kick if player currently
|
||||
// kick if player is online
|
||||
if (Player* p = ObjectAccessor::FindPlayer(guid))
|
||||
{
|
||||
WorldSession* s = p->GetSession();
|
||||
@@ -69,69 +66,69 @@ AccountOpResult AccountMgr::DeleteAccount(uint32 accid)
|
||||
s->LogoutPlayer(false); // logout player without waiting next session list update
|
||||
}
|
||||
|
||||
Player::DeleteFromDB(guid, accid, false); // no need to update realm characters
|
||||
Player::DeleteFromDB(guid, accountId, false); // no need to update realm characters
|
||||
} while (result->NextRow());
|
||||
}
|
||||
|
||||
// table realm specific but common for all characters of account for realm
|
||||
PreparedStatement* stmt = CharacterDatabase.GetPreparedStatement(CHAR_DEL_TUTORIALS);
|
||||
stmt->setUInt32(0, accid);
|
||||
stmt->setUInt32(0, accountId);
|
||||
CharacterDatabase.Execute(stmt);
|
||||
stmt = CharacterDatabase.GetPreparedStatement(CHAR_DEL_ACCOUNT_DATA);
|
||||
stmt->setUInt32(0, accid);
|
||||
stmt->setUInt32(0, accountId);
|
||||
CharacterDatabase.Execute(stmt);
|
||||
|
||||
SQLTransaction trans = LoginDatabase.BeginTransaction();
|
||||
|
||||
trans->PAppend("DELETE FROM account WHERE id='%d'", accid);
|
||||
trans->PAppend("DELETE FROM account_access WHERE id ='%d'", accid);
|
||||
trans->PAppend("DELETE FROM realmcharacters WHERE acctid='%d'", accid);
|
||||
trans->PAppend("DELETE FROM account WHERE id='%d'", accountId);
|
||||
trans->PAppend("DELETE FROM account_access WHERE id ='%d'", accountId);
|
||||
trans->PAppend("DELETE FROM realmcharacters WHERE acctid='%d'", accountId);
|
||||
|
||||
LoginDatabase.CommitTransaction(trans);
|
||||
|
||||
return AOR_OK;
|
||||
}
|
||||
|
||||
AccountOpResult AccountMgr::ChangeUsername(uint32 accid, std::string new_uname, std::string new_passwd)
|
||||
AccountOpResult AccountMgr::ChangeUsername(uint32 accountId, std::string newUsername, std::string newPassword)
|
||||
{
|
||||
QueryResult result = LoginDatabase.PQuery("SELECT 1 FROM account WHERE id='%d'", accid);
|
||||
QueryResult result = LoginDatabase.PQuery("SELECT 1 FROM account WHERE id='%d'", accountId);
|
||||
if (!result)
|
||||
return AOR_NAME_NOT_EXIST; // account doesn't exist
|
||||
|
||||
if (utf8length(new_uname) > MAX_ACCOUNT_STR)
|
||||
if (utf8length(newUsername) > MAX_ACCOUNT_STR)
|
||||
return AOR_NAME_TOO_LONG;
|
||||
|
||||
if (utf8length(new_passwd) > MAX_ACCOUNT_STR)
|
||||
if (utf8length(newPassword) > MAX_ACCOUNT_STR)
|
||||
return AOR_PASS_TOO_LONG;
|
||||
|
||||
normalizeString(new_uname);
|
||||
normalizeString(new_passwd);
|
||||
normalizeString(newUsername);
|
||||
normalizeString(newPassword);
|
||||
|
||||
std::string safe_new_uname = new_uname;
|
||||
LoginDatabase.EscapeString(safe_new_uname);
|
||||
std::string safeNewUsername = newUsername;
|
||||
LoginDatabase.EscapeString(safeNewUsername);
|
||||
|
||||
LoginDatabase.PExecute("UPDATE account SET v='0', s='0', username='%s', sha_pass_hash='%s' WHERE id='%d'", safe_new_uname.c_str(),
|
||||
CalculateShaPassHash(new_uname, new_passwd).c_str(), accid);
|
||||
LoginDatabase.PExecute("UPDATE account SET v='0', s='0', username='%s', sha_pass_hash='%s' WHERE id='%d'", safeNewUsername.c_str(),
|
||||
CalculateShaPassHash(newUsername, newPassword).c_str(), accountId);
|
||||
|
||||
return AOR_OK;
|
||||
}
|
||||
|
||||
AccountOpResult AccountMgr::ChangePassword(uint32 accid, std::string new_passwd)
|
||||
AccountOpResult AccountMgr::ChangePassword(uint32 accountId, std::string newPassword)
|
||||
{
|
||||
std::string username;
|
||||
|
||||
if (!GetName(accid, username))
|
||||
if (!GetName(accountId, username))
|
||||
return AOR_NAME_NOT_EXIST; // account doesn't exist
|
||||
|
||||
if (utf8length(new_passwd) > MAX_ACCOUNT_STR)
|
||||
if (utf8length(newPassword) > MAX_ACCOUNT_STR)
|
||||
return AOR_PASS_TOO_LONG;
|
||||
|
||||
normalizeString(username);
|
||||
normalizeString(new_passwd);
|
||||
normalizeString(newPassword);
|
||||
|
||||
// also reset s and v to force update at next realmd login
|
||||
LoginDatabase.PExecute("UPDATE account SET v='0', s='0', sha_pass_hash='%s' WHERE id='%d'",
|
||||
CalculateShaPassHash(username, new_passwd).c_str(), accid);
|
||||
CalculateShaPassHash(username, newPassword).c_str(), accountId);
|
||||
|
||||
return AOR_OK;
|
||||
}
|
||||
@@ -140,44 +137,26 @@ uint32 AccountMgr::GetId(std::string username)
|
||||
{
|
||||
LoginDatabase.EscapeString(username);
|
||||
QueryResult result = LoginDatabase.PQuery("SELECT id FROM account WHERE username = '%s'", username.c_str());
|
||||
if (!result)
|
||||
return 0;
|
||||
else
|
||||
{
|
||||
uint32 id = (*result)[0].GetUInt32();
|
||||
return id;
|
||||
}
|
||||
return (result) ? (*result)[0].GetUInt32() : 0;
|
||||
}
|
||||
|
||||
uint32 AccountMgr::GetSecurity(uint32 acc_id)
|
||||
uint32 AccountMgr::GetSecurity(uint32 accountId)
|
||||
{
|
||||
QueryResult result = LoginDatabase.PQuery("SELECT gmlevel FROM account_access WHERE id = '%u'", acc_id);
|
||||
if (result)
|
||||
{
|
||||
uint32 sec = (*result)[0].GetUInt32();
|
||||
return sec;
|
||||
}
|
||||
|
||||
return 0;
|
||||
QueryResult result = LoginDatabase.PQuery("SELECT gmlevel FROM account_access WHERE id = '%u'", accountId);
|
||||
return (result) ? (*result)[0].GetUInt32() : 0;
|
||||
}
|
||||
|
||||
uint32 AccountMgr::GetSecurity(uint64 acc_id, int32 realm_id)
|
||||
uint32 AccountMgr::GetSecurity(uint64 accountId, int32 realmId)
|
||||
{
|
||||
QueryResult result = (realm_id == -1)
|
||||
? LoginDatabase.PQuery("SELECT gmlevel FROM account_access WHERE id = '%u' AND RealmID = '%d'", acc_id, realm_id)
|
||||
: LoginDatabase.PQuery("SELECT gmlevel FROM account_access WHERE id = '%u' AND (RealmID = '%d' OR RealmID = '-1')", acc_id, realm_id);
|
||||
if (result)
|
||||
{
|
||||
uint32 sec = (*result)[0].GetUInt32();
|
||||
return sec;
|
||||
}
|
||||
|
||||
return 0;
|
||||
QueryResult result = (realmId == -1)
|
||||
? LoginDatabase.PQuery("SELECT gmlevel FROM account_access WHERE id = '%u' AND RealmID = '%d'", accountId, realmId)
|
||||
: LoginDatabase.PQuery("SELECT gmlevel FROM account_access WHERE id = '%u' AND (RealmID = '%d' OR RealmID = '-1')", accountId, realmId);
|
||||
return (result) ? (*result)[0].GetUInt32() : 0;
|
||||
}
|
||||
|
||||
bool AccountMgr::GetName(uint32 acc_id, std::string &name)
|
||||
bool AccountMgr::GetName(uint32 accountId, std::string& name)
|
||||
{
|
||||
QueryResult result = LoginDatabase.PQuery("SELECT username FROM account WHERE id = '%u'", acc_id);
|
||||
QueryResult result = LoginDatabase.PQuery("SELECT username FROM account WHERE id = '%u'", accountId);
|
||||
if (result)
|
||||
{
|
||||
name = (*result)[0].GetString();
|
||||
@@ -187,51 +166,43 @@ bool AccountMgr::GetName(uint32 acc_id, std::string &name)
|
||||
return false;
|
||||
}
|
||||
|
||||
bool AccountMgr::CheckPassword(uint32 accid, std::string passwd)
|
||||
bool AccountMgr::CheckPassword(uint32 accountId, std::string password)
|
||||
{
|
||||
std::string username;
|
||||
if (!GetName(accid, username))
|
||||
|
||||
if (!GetName(accountId, username))
|
||||
return false;
|
||||
|
||||
normalizeString(username);
|
||||
normalizeString(passwd);
|
||||
normalizeString(password);
|
||||
|
||||
QueryResult result = LoginDatabase.PQuery("SELECT 1 FROM account WHERE id='%d' AND sha_pass_hash='%s'", accid, CalculateShaPassHash(username, passwd).c_str());
|
||||
if (result)
|
||||
return true;
|
||||
|
||||
return false;
|
||||
QueryResult result = LoginDatabase.PQuery("SELECT 1 FROM account WHERE id='%d' AND sha_pass_hash='%s'", accountId, CalculateShaPassHash(username, password).c_str());
|
||||
return (result) ? true : false;
|
||||
}
|
||||
|
||||
uint32 AccountMgr::GetCharactersCount(uint32 acc_id)
|
||||
uint32 AccountMgr::GetCharactersCount(uint32 accountId)
|
||||
{
|
||||
uint32 charcount = 0;
|
||||
// check character count
|
||||
QueryResult result = CharacterDatabase.PQuery("SELECT COUNT(guid) FROM characters WHERE account = '%d'", acc_id);
|
||||
if (result)
|
||||
{
|
||||
Field *fields=result->Fetch();
|
||||
charcount = fields[0].GetUInt32();
|
||||
}
|
||||
return charcount;
|
||||
QueryResult result = CharacterDatabase.PQuery("SELECT COUNT(guid) FROM characters WHERE account = '%d'", accountId);
|
||||
return (result) ? (*result)[0].GetUInt32() : 0;
|
||||
}
|
||||
|
||||
bool AccountMgr::normalizeString(std::string& utf8str)
|
||||
bool AccountMgr::normalizeString(std::string& utf8String)
|
||||
{
|
||||
wchar_t wstr_buf[MAX_ACCOUNT_STR+1];
|
||||
wchar_t buffer[MAX_ACCOUNT_STR+1];
|
||||
|
||||
size_t wstr_len = MAX_ACCOUNT_STR;
|
||||
if (!Utf8toWStr(utf8str, wstr_buf, wstr_len))
|
||||
size_t maxLength = MAX_ACCOUNT_STR;
|
||||
if (!Utf8toWStr(utf8String, buffer, maxLength))
|
||||
return false;
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning(disable: 4996)
|
||||
#endif
|
||||
std::transform(&wstr_buf[0], wstr_buf+wstr_len, &wstr_buf[0], wcharToUpperOnlyLatin);
|
||||
std::transform(&buffer[0], buffer+maxLength, &buffer[0], wcharToUpperOnlyLatin);
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning(default: 4996)
|
||||
#endif
|
||||
|
||||
return WStrToUtf8(wstr_buf, wstr_len, utf8str);
|
||||
return WStrToUtf8(buffer, maxLength, utf8String);
|
||||
}
|
||||
|
||||
std::string AccountMgr::CalculateShaPassHash(std::string& name, std::string& password)
|
||||
|
||||
@@ -42,19 +42,19 @@ class AccountMgr
|
||||
~AccountMgr();
|
||||
|
||||
AccountOpResult CreateAccount(std::string username, std::string password);
|
||||
AccountOpResult DeleteAccount(uint32 accid);
|
||||
AccountOpResult ChangeUsername(uint32 accid, std::string new_uname, std::string new_passwd);
|
||||
AccountOpResult ChangePassword(uint32 accid, std::string new_passwd);
|
||||
bool CheckPassword(uint32 accid, std::string passwd);
|
||||
AccountOpResult DeleteAccount(uint32 accountId);
|
||||
AccountOpResult ChangeUsername(uint32 accountId, std::string newUsername, std::string newPassword);
|
||||
AccountOpResult ChangePassword(uint32 accountId, std::string newPassword);
|
||||
bool CheckPassword(uint32 accountId, std::string password);
|
||||
|
||||
uint32 GetId(std::string username);
|
||||
uint32 GetSecurity(uint32 acc_id);
|
||||
uint32 GetSecurity(uint64 acc_id, int32 realm_id);
|
||||
bool GetName(uint32 acc_id, std::string &name);
|
||||
uint32 GetCharactersCount(uint32 acc_id);
|
||||
uint32 GetSecurity(uint32 accountId);
|
||||
uint32 GetSecurity(uint64 accountId, int32 realmId);
|
||||
bool GetName(uint32 accountId, std::string& name);
|
||||
uint32 GetCharactersCount(uint32 accountId);
|
||||
std::string CalculateShaPassHash(std::string& name, std::string& password);
|
||||
|
||||
static bool normalizeString(std::string& utf8str);
|
||||
static bool normalizeString(std::string& utf8String);
|
||||
};
|
||||
|
||||
#define sAccountMgr ACE_Singleton<AccountMgr, ACE_Null_Mutex>::instance()
|
||||
|
||||
Reference in New Issue
Block a user