From af6b8ce09941650025c73770123efb49d0af9ef8 Mon Sep 17 00:00:00 2001 From: Justin Hanley Date: Tue, 30 Jul 2024 07:33:13 -0600 Subject: [PATCH] feat(Scripts/Commands): account set email (#19481) * added worldserver command to set email address * reverted sql, added Tail args * removed pointer * Added helper text via updatescript * fix build * fix build * fixed db update * Update data/sql/updates/db_world/2024_07_26_01.sql Co-authored-by: Kitzunu <24550914+Kitzunu@users.noreply.github.com> * Update data/sql/updates/db_world/2024_07_26_01.sql Co-authored-by: Kitzunu <24550914+Kitzunu@users.noreply.github.com> * fixed hander * refactor with named params * refactor with named params --------- Co-authored-by: Kitzunu <24550914+Kitzunu@users.noreply.github.com> --- data/sql/updates/db_world/2024_07_26_01.sql | 9 ++++ .../Database/Implementation/LoginDatabase.cpp | 1 + .../Database/Implementation/LoginDatabase.h | 1 + src/server/game/Accounts/AccountMgr.cpp | 27 ++++++++++ src/server/game/Accounts/AccountMgr.h | 3 ++ src/server/scripts/Commands/cs_account.cpp | 50 ++++++++++++++++++- 6 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 data/sql/updates/db_world/2024_07_26_01.sql diff --git a/data/sql/updates/db_world/2024_07_26_01.sql b/data/sql/updates/db_world/2024_07_26_01.sql new file mode 100644 index 000000000..55cc22a45 --- /dev/null +++ b/data/sql/updates/db_world/2024_07_26_01.sql @@ -0,0 +1,9 @@ +-- DB update 22024_07_26_00 -> 2024_07_26_01 +DELETE FROM `command` WHERE `name`='account set email'; + +INSERT INTO `command` (`name`, `security`, `help`) VALUES +('account set email', 4, 'Syntax: .account set email $account $email $email_confirmation\nAdd or change an email to the account.'); + +DELETE FROM `acore_string` WHERE `entry` IN (875); +INSERT INTO `acore_string` (`entry`, `content_default`,`locale_zhCN`) VALUES +(875, 'Your email can\'t be longer than 255 characters, email not changed!', '您的电子邮件无法超过255个字符,电子邮件没有改变!'); diff --git a/src/server/database/Database/Implementation/LoginDatabase.cpp b/src/server/database/Database/Implementation/LoginDatabase.cpp index 3dbb2ced2..5b20d8693 100644 --- a/src/server/database/Database/Implementation/LoginDatabase.cpp +++ b/src/server/database/Database/Implementation/LoginDatabase.cpp @@ -83,6 +83,7 @@ void LoginDatabaseConnection::DoPrepareStatements() PrepareStatement(LOGIN_UPD_EXPANSION, "UPDATE account SET expansion = ? WHERE id = ?", CONNECTION_ASYNC); PrepareStatement(LOGIN_UPD_ACCOUNT_LOCK, "UPDATE account SET locked = ? WHERE id = ?", CONNECTION_ASYNC); PrepareStatement(LOGIN_UPD_ACCOUNT_LOCK_COUNTRY, "UPDATE account SET lock_country = ? WHERE id = ?", CONNECTION_ASYNC); + PrepareStatement(LOGIN_UPD_EMAIL, "UPDATE account SET email = ? WHERE id = ?", CONNECTION_ASYNC); PrepareStatement(LOGIN_UPD_USERNAME, "UPDATE account SET username = ? WHERE id = ?", CONNECTION_ASYNC); PrepareStatement(LOGIN_UPD_MUTE_TIME, "UPDATE account SET mutetime = ? , mutereason = ? , muteby = ? WHERE id = ?", CONNECTION_ASYNC); PrepareStatement(LOGIN_UPD_MUTE_TIME_LOGIN, "UPDATE account SET mutetime = ? WHERE id = ?", CONNECTION_ASYNC); diff --git a/src/server/database/Database/Implementation/LoginDatabase.h b/src/server/database/Database/Implementation/LoginDatabase.h index c7fb2be48..365099913 100644 --- a/src/server/database/Database/Implementation/LoginDatabase.h +++ b/src/server/database/Database/Implementation/LoginDatabase.h @@ -67,6 +67,7 @@ enum LoginDatabaseStatements : uint32 LOGIN_UPD_EXPANSION, LOGIN_UPD_ACCOUNT_LOCK, LOGIN_UPD_ACCOUNT_LOCK_COUNTRY, + LOGIN_UPD_EMAIL, LOGIN_UPD_USERNAME, LOGIN_UPD_MUTE_TIME, LOGIN_UPD_MUTE_TIME_LOGIN, diff --git a/src/server/game/Accounts/AccountMgr.cpp b/src/server/game/Accounts/AccountMgr.cpp index 439b6b5ba..2eefbc3ee 100644 --- a/src/server/game/Accounts/AccountMgr.cpp +++ b/src/server/game/Accounts/AccountMgr.cpp @@ -58,6 +58,33 @@ namespace AccountMgr return AOR_OK; // everything's fine } + AccountOpResult ChangeEmail(uint32 accountId, std::string newEmail) + { + std::string username; + + if (!GetName(accountId, username)) + { + sScriptMgr->OnFailedEmailChange(accountId); + return AOR_NAME_NOT_EXIST; // account doesn't exist + } + + if (utf8length(newEmail) > MAX_EMAIL_STR) + { + sScriptMgr->OnFailedEmailChange(accountId); + return AOR_EMAIL_TOO_LONG; // email's too long + } + + Utf8ToUpperOnlyLatin(newEmail); + + LoginDatabasePreparedStatement* stmt = LoginDatabase.GetPreparedStatement(LOGIN_UPD_EMAIL); + stmt->SetData(0, newEmail); + stmt->SetData(1, accountId); + LoginDatabase.Execute(stmt); + + sScriptMgr->OnEmailChange(accountId); + return AOR_OK; + } + AccountOpResult DeleteAccount(uint32 accountId) { // Check if accounts exists diff --git a/src/server/game/Accounts/AccountMgr.h b/src/server/game/Accounts/AccountMgr.h index 2f203093f..49361db16 100644 --- a/src/server/game/Accounts/AccountMgr.h +++ b/src/server/game/Accounts/AccountMgr.h @@ -26,6 +26,7 @@ enum AccountOpResult AOR_OK, AOR_NAME_TOO_LONG, AOR_PASS_TOO_LONG, + AOR_EMAIL_TOO_LONG, AOR_NAME_ALREADY_EXIST, AOR_NAME_NOT_EXIST, AOR_DB_INTERNAL_ERROR @@ -33,6 +34,7 @@ enum AccountOpResult #define MAX_ACCOUNT_STR 20 #define MAX_PASS_STR 16 +#define MAX_EMAIL_STR 255 namespace AccountMgr { @@ -40,6 +42,7 @@ namespace AccountMgr AccountOpResult DeleteAccount(uint32 accountId); AccountOpResult ChangeUsername(uint32 accountId, std::string newUsername, std::string newPassword); AccountOpResult ChangePassword(uint32 accountId, std::string newPassword); + AccountOpResult ChangeEmail(uint32 accountId, std::string email); bool CheckPassword(uint32 accountId, std::string password); uint32 GetId(std::string const& username); diff --git a/src/server/scripts/Commands/cs_account.cpp b/src/server/scripts/Commands/cs_account.cpp index 94d3893d6..93092d9b2 100644 --- a/src/server/scripts/Commands/cs_account.cpp +++ b/src/server/scripts/Commands/cs_account.cpp @@ -55,7 +55,8 @@ public: { "addon", HandleAccountSetAddonCommand, SEC_GAMEMASTER, Console::Yes }, { "gmlevel", HandleAccountSetGmLevelCommand, SEC_ADMINISTRATOR, Console::Yes }, { "password", HandleAccountSetPasswordCommand, SEC_ADMINISTRATOR, Console::Yes }, - { "2fa", HandleAccountSet2FACommand, SEC_PLAYER, Console::Yes } + { "2fa", HandleAccountSet2FACommand, SEC_PLAYER, Console::Yes }, + { "email", HandleAccountSetEmailCommand, SEC_ADMINISTRATOR, Console::Yes } }; static ChatCommandTable accountLockCommandTable @@ -900,6 +901,53 @@ public: } return true; } + + /// Set email for account + static bool HandleAccountSetEmailCommand(ChatHandler* handler, AccountIdentifier account, std::string email, std::string emailConfirmation) + + { + if (!account || !email.data() || !emailConfirmation.data()) + return false; + + std::string accountName = account.GetName(); + if (!Utf8ToUpperOnlyLatin(accountName)) + { + handler->SendErrorMessage(LANG_ACCOUNT_NOT_EXIST, accountName); + return false; + } + + uint32 targetAccountId = account.GetID(); + if (!targetAccountId) + { + handler->SendErrorMessage(LANG_ACCOUNT_NOT_EXIST, accountName); + return false; + } + + if (email != emailConfirmation) + { + handler->SendErrorMessage(LANG_NEW_EMAILS_NOT_MATCH); + return false; + } + + AccountOpResult result = AccountMgr::ChangeEmail(targetAccountId, email.data()); + + switch (result) + { + case AOR_OK: + handler->SendSysMessage(LANG_COMMAND_EMAIL); + break; + case AOR_NAME_NOT_EXIST: + handler->SendErrorMessage(LANG_ACCOUNT_NOT_EXIST, accountName); + return false; + case AOR_EMAIL_TOO_LONG: + handler->SendErrorMessage(LANG_EMAIL_TOO_LONG); + return false; + default: + handler->SendErrorMessage(LANG_COMMAND_NOTCHANGEEMAIL); + return false; + } + return true; + } }; void AddSC_account_commandscript()