Skip to content

Commit

Permalink
Standardize Account Handling (mangoszero#182)
Browse files Browse the repository at this point in the history
Co-authored-by: PargeLenis <[email protected]>
  • Loading branch information
PargeLenis and PargeLenis authored Jan 29, 2023
1 parent aa2b762 commit 0562367
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 25 deletions.
52 changes: 30 additions & 22 deletions src/game/ChatCommands/AccountCommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,30 +222,38 @@ bool ChatHandler::HandleAccountCreateCommand(char* args)
std::string password = szPassword;

AccountOpResult result;
result = sAccountMgr.CreateAccount(account_name, password);
switch (result)
uint32 expansion = 0;
if(ExtractUInt32(&args, expansion))
{
case AOR_OK:
PSendSysMessage(LANG_ACCOUNT_CREATED, account_name.c_str());
break;
case AOR_NAME_TOO_LONG:
SendSysMessage(LANG_ACCOUNT_TOO_LONG);
SetSentErrorMessage(true);
return false;
case AOR_NAME_ALREADY_EXIST:
SendSysMessage(LANG_ACCOUNT_ALREADY_EXIST);
SetSentErrorMessage(true);
return false;
case AOR_DB_INTERNAL_ERROR:
PSendSysMessage(LANG_ACCOUNT_NOT_CREATED_SQL_ERROR, account_name.c_str());
SetSentErrorMessage(true);
return false;
default:
PSendSysMessage(LANG_ACCOUNT_NOT_CREATED, account_name.c_str());
SetSentErrorMessage(true);
return false;
// No point in assigning to result as it's never used on this side of the if/else branch
sAccountMgr.CreateAccount(account_name, password, expansion);
}
else
{
result = sAccountMgr.CreateAccount(account_name, password);
switch (result)
{
case AOR_OK:
PSendSysMessage(LANG_ACCOUNT_CREATED, account_name.c_str());
break;
case AOR_NAME_TOO_LONG:
SendSysMessage(LANG_ACCOUNT_TOO_LONG);
SetSentErrorMessage(true);
return false;
case AOR_NAME_ALREADY_EXIST:
SendSysMessage(LANG_ACCOUNT_ALREADY_EXIST);
SetSentErrorMessage(true);
return false;
case AOR_DB_INTERNAL_ERROR:
PSendSysMessage(LANG_ACCOUNT_NOT_CREATED_SQL_ERROR, account_name.c_str());
SetSentErrorMessage(true);
return false;
default:
PSendSysMessage(LANG_ACCOUNT_NOT_CREATED, account_name.c_str());
SetSentErrorMessage(true);
return false;
}
}

return true;
}

Expand Down
37 changes: 35 additions & 2 deletions src/game/WorldHandlers/AccountMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ AccountOpResult AccountMgr::CreateAccount(std::string username, std::string pass
if (GetId(username))
{
{
return AOR_NAME_ALREADY_EXIST; // username does already exist
return AOR_NAME_ALREADY_EXIST; // username does already exist
}
}

Expand All @@ -83,7 +83,40 @@ AccountOpResult AccountMgr::CreateAccount(std::string username, std::string pass
}
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");

return AOR_OK; // everything's fine
return AOR_OK; // everything's fine
}

/**
* It creates an account
*
* @param username The username of the account to create.
* @param password The password you want to set for the account.
* @param expansion 0 = Classic, 1 = TBC, 2 = WOTLK, 3 = Cataclysm
*
* @return AOR_OK
*/
AccountOpResult AccountMgr::CreateAccount(std::string username, std::string password, uint32 expansion)
{
if (utf8length(username) > MAX_ACCOUNT_STR)
{
return AOR_NAME_TOO_LONG; // username's too long
}

Utf8ToUpperOnlyLatin(username);
Utf8ToUpperOnlyLatin(password);

if (GetId(username))
{
return AOR_NAME_ALREADY_EXIST; // username does already exist
}

if (!LoginDatabase.PExecute("INSERT INTO `account`(`username`,`sha_pass_hash`,`joindate`,`expansion`) VALUES('%s','%s',NOW(),'%u')", username.c_str(), CalculateShaPassHash(username, password).c_str(), expansion))
{
return AOR_DB_INTERNAL_ERROR; // unexpected error
}
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");

return AOR_OK; // everything's fine
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/game/WorldHandlers/AccountMgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ enum AccountOpResult
AOR_DB_INTERNAL_ERROR
};

#define MAX_ACCOUNT_STR 16
#define MAX_ACCOUNT_STR 16
#define MAX_PASSWORD_STR 16

/* A class that is used to manage accounts. */
Expand All @@ -49,6 +49,7 @@ class AccountMgr
~AccountMgr();

AccountOpResult CreateAccount(std::string username, std::string password);
AccountOpResult CreateAccount(std::string username, std::string password, uint32 expansion);
AccountOpResult DeleteAccount(uint32 accid);
AccountOpResult ChangeUsername(uint32 accid, std::string new_uname, std::string new_passwd);
AccountOpResult ChangePassword(uint32 accid, std::string new_passwd);
Expand Down

0 comments on commit 0562367

Please sign in to comment.