mirror of
https://github.com/mod-playerbots/azerothcore-wotlk.git
synced 2026-01-26 23:26:23 +00:00
First Commit
For Azeroth!
This commit is contained in:
151
src/server/game/Accounts/AccountMgr.cpp
Normal file
151
src/server/game/Accounts/AccountMgr.cpp
Normal file
@@ -0,0 +1,151 @@
|
||||
/*
|
||||
* Copyright (C)
|
||||
* Copyright (C)
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation; either version 2 of the License, or (at your
|
||||
* option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "AccountMgr.h"
|
||||
#include "DatabaseEnv.h"
|
||||
#include "ObjectAccessor.h"
|
||||
#include "Player.h"
|
||||
#include "Util.h"
|
||||
#include "SHA1.h"
|
||||
#include "WorldSession.h"
|
||||
|
||||
namespace AccountMgr
|
||||
{
|
||||
|
||||
uint32 GetId(std::string const& username)
|
||||
{
|
||||
PreparedStatement* stmt = LoginDatabase.GetPreparedStatement(LOGIN_GET_ACCOUNT_ID_BY_USERNAME);
|
||||
stmt->setString(0, username);
|
||||
PreparedQueryResult result = LoginDatabase.Query(stmt);
|
||||
|
||||
return (result) ? (*result)[0].GetUInt32() : 0;
|
||||
}
|
||||
|
||||
uint32 GetSecurity(uint32 accountId)
|
||||
{
|
||||
PreparedStatement* stmt = LoginDatabase.GetPreparedStatement(LOGIN_GET_ACCOUNT_ACCESS_GMLEVEL);
|
||||
stmt->setUInt32(0, accountId);
|
||||
PreparedQueryResult result = LoginDatabase.Query(stmt);
|
||||
|
||||
return (result) ? (*result)[0].GetUInt8() : uint32(SEC_PLAYER);
|
||||
}
|
||||
|
||||
uint32 GetSecurity(uint32 accountId, int32 realmId)
|
||||
{
|
||||
PreparedStatement* stmt = LoginDatabase.GetPreparedStatement(LOGIN_GET_GMLEVEL_BY_REALMID);
|
||||
stmt->setUInt32(0, accountId);
|
||||
stmt->setInt32(1, realmId);
|
||||
PreparedQueryResult result = LoginDatabase.Query(stmt);
|
||||
|
||||
return (result) ? (*result)[0].GetUInt8() : uint32(SEC_PLAYER);
|
||||
}
|
||||
|
||||
bool GetName(uint32 accountId, std::string& name)
|
||||
{
|
||||
PreparedStatement* stmt = LoginDatabase.GetPreparedStatement(LOGIN_GET_USERNAME_BY_ID);
|
||||
stmt->setUInt32(0, accountId);
|
||||
PreparedQueryResult result = LoginDatabase.Query(stmt);
|
||||
|
||||
if (result)
|
||||
{
|
||||
name = (*result)[0].GetString();
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool CheckPassword(uint32 accountId, std::string password)
|
||||
{
|
||||
std::string username;
|
||||
|
||||
if (!GetName(accountId, username))
|
||||
return false;
|
||||
|
||||
normalizeString(username);
|
||||
normalizeString(password);
|
||||
|
||||
PreparedStatement* stmt = LoginDatabase.GetPreparedStatement(LOGIN_SEL_CHECK_PASSWORD);
|
||||
stmt->setUInt32(0, accountId);
|
||||
stmt->setString(1, CalculateShaPassHash(username, password));
|
||||
PreparedQueryResult result = LoginDatabase.Query(stmt);
|
||||
|
||||
return (result) ? true : false;
|
||||
}
|
||||
|
||||
uint32 GetCharactersCount(uint32 accountId)
|
||||
{
|
||||
// check character count
|
||||
PreparedStatement* stmt = CharacterDatabase.GetPreparedStatement(CHAR_SEL_SUM_CHARS);
|
||||
stmt->setUInt32(0, accountId);
|
||||
PreparedQueryResult result = CharacterDatabase.Query(stmt);
|
||||
|
||||
return (result) ? (*result)[0].GetUInt64() : 0;
|
||||
}
|
||||
|
||||
bool normalizeString(std::string& utf8String)
|
||||
{
|
||||
wchar_t buffer[MAX_ACCOUNT_STR+1];
|
||||
|
||||
size_t maxLength = MAX_ACCOUNT_STR;
|
||||
if (!Utf8toWStr(utf8String, buffer, maxLength))
|
||||
return false;
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning(disable: 4996)
|
||||
#endif
|
||||
std::transform(&buffer[0], buffer+maxLength, &buffer[0], wcharToUpperOnlyLatin);
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning(default: 4996)
|
||||
#endif
|
||||
|
||||
return WStrToUtf8(buffer, maxLength, utf8String);
|
||||
}
|
||||
|
||||
std::string CalculateShaPassHash(std::string const& name, std::string const& password)
|
||||
{
|
||||
SHA1Hash sha;
|
||||
sha.Initialize();
|
||||
sha.UpdateData(name);
|
||||
sha.UpdateData(":");
|
||||
sha.UpdateData(password);
|
||||
sha.Finalize();
|
||||
|
||||
return ByteArrayToHexStr(sha.GetDigest(), sha.GetLength());
|
||||
}
|
||||
|
||||
bool IsPlayerAccount(uint32 gmlevel)
|
||||
{
|
||||
return gmlevel == SEC_PLAYER;
|
||||
}
|
||||
|
||||
bool IsGMAccount(uint32 gmlevel)
|
||||
{
|
||||
return gmlevel >= SEC_GAMEMASTER && gmlevel <= SEC_CONSOLE;
|
||||
}
|
||||
|
||||
bool IsAdminAccount(uint32 gmlevel)
|
||||
{
|
||||
return gmlevel >= SEC_ADMINISTRATOR && gmlevel <= SEC_CONSOLE;
|
||||
}
|
||||
|
||||
bool IsConsoleAccount(uint32 gmlevel)
|
||||
{
|
||||
return gmlevel == SEC_CONSOLE;
|
||||
}
|
||||
|
||||
} // Namespace AccountMgr
|
||||
Reference in New Issue
Block a user