From 0d189c79a2d3be98e176987e6aca15fd2c618ef0 Mon Sep 17 00:00:00 2001 From: Guillaume Melquiond Date: Fri, 4 Aug 2006 07:53:56 +0000 Subject: Strengthened checks for already existing character name or email address. --- src/accounthandler.cpp | 10 +++---- src/dal/recordset.cpp | 10 ------- src/dalstorage.cpp | 73 +++++++++++++++++--------------------------------- src/dalstorage.h | 10 +++---- src/storage.h | 10 +++---- 5 files changed, 37 insertions(+), 76 deletions(-) (limited to 'src') diff --git a/src/accounthandler.cpp b/src/accounthandler.cpp index 9287fa22..992f7888 100644 --- a/src/accounthandler.cpp +++ b/src/accounthandler.cpp @@ -111,9 +111,9 @@ void AccountHandler::processMessage(NetComputer *comp, MessageIn &message) result.writeByte(ERRMSG_INVALID_ARGUMENT); LOG_INFO(email << ": has got double quotes in it.", 1); } - else if (store.getSameEmailNumber(email) > 1) // Search if Email already exists, - { // Except for the one already that is to - result.writeByte(EMAILCHG_EXISTS_EMAIL); // be changed. + else if (store.doesEmailAddressExist(email)) + { + result.writeByte(EMAILCHG_EXISTS_EMAIL); LOG_INFO(email << ": New Email already exists.", 1); } else @@ -182,7 +182,7 @@ void AccountHandler::processMessage(NetComputer *comp, MessageIn &message) break; } // Check if the character's name already exists - if (store.doesCharacterNameExists(name)) + if (store.doesCharacterNameExist(name)) { result.writeByte(CREATE_EXISTS_NAME); LOG_INFO(name << ": Character's name already exists.", 1); @@ -630,7 +630,7 @@ AccountHandler::handleRegisterMessage(AccountClient &computer, MessageIn &msg) reply.writeByte(REGISTER_EXISTS_USERNAME); } // Find out whether the email is already in use. - else if (store.getSameEmailNumber(email) > 0) + else if (store.doesEmailAddressExist(email)) { LOG_INFO(email << ": Email already exists.", 1); reply.writeByte(REGISTER_EXISTS_EMAIL); diff --git a/src/dal/recordset.cpp b/src/dal/recordset.cpp index 6b2211ea..20bf2f12 100644 --- a/src/dal/recordset.cpp +++ b/src/dal/recordset.cpp @@ -141,11 +141,6 @@ const std::string& RecordSet::operator()(const unsigned int row, const unsigned int col) const { - if (mHeaders.size() == 0) { - throw std::invalid_argument( - "nothing to return as the recordset is empty."); - } - if ((row >= mRows.size()) || (col >= mHeaders.size())) { std::ostringstream os; os << "(" << row << ", " << col << ") is out of range; " @@ -166,11 +161,6 @@ const std::string& RecordSet::operator()(const unsigned int row, const std::string& name) const { - if (mHeaders.size() == 0) { - throw std::invalid_argument( - "nothing to return as the recordset is empty."); - } - if (row >= mRows.size()) { std::ostringstream os; os << "row " << row << " is out of range; " diff --git a/src/dalstorage.cpp b/src/dalstorage.cpp index 6950f3d6..349666bf 100644 --- a/src/dalstorage.cpp +++ b/src/dalstorage.cpp @@ -329,82 +329,57 @@ DALStorage::getEmailList() } /** - * Return the number of same Emails in account's table. + * Tells if the email address already exists + * @return true if the email address exists. */ - -unsigned int -DALStorage::getSameEmailNumber(const std::string &email) +bool DALStorage::doesEmailAddressExist(std::string const &email) { // If not opened already open(); try { - std::string sql("select count(email) from "); - sql += ACCOUNTS_TBL_NAME; - sql += " where upper(email) = upper(\"" + email + "\");"; + std::ostringstream sql; + sql << "select count(email) from " << ACCOUNTS_TBL_NAME + << " where upper(email) = upper(\"" << email << "\");"; + dal::RecordSet const &accountInfo = mDb->execSql(sql.str()); - const dal::RecordSet& accountInfo = mDb->execSql(sql); - - // If the account is empty then we have no choice but to return false. - if (accountInfo.isEmpty()) { - return 0; - } - - std::stringstream ssStream(accountInfo(0,0)); - unsigned int iReturn = 0; + std::istringstream ssStream(accountInfo(0, 0)); + unsigned int iReturn = 1; ssStream >> iReturn; - return iReturn; - } - catch (const dal::DbSqlQueryExecFailure& e) { + return iReturn != 0; + } catch (std::exception const &e) { // TODO: throw an exception. LOG_ERROR("SQL query failure: " << e.what(), 0); } - return 0; + return true; } /** * Tells if the character's name already exists * @return true if character's name exists. */ -bool -DALStorage::doesCharacterNameExists(const std::string& name) +bool DALStorage::doesCharacterNameExist(const std::string& name) { // If not opened already open(); try { - std::string sql("select count(name) from "); - sql += CHARACTERS_TBL_NAME; - sql += " where name = \""; - sql += name; - sql += "\";"; - const dal::RecordSet& accountInfo = mDb->execSql(sql); - - // if the account is empty then - // we have no choice but to return false. - if (accountInfo.isEmpty()) { - return false; - } + std::ostringstream sql; + sql << "select count(name) from " << CHARACTERS_TBL_NAME + << " where name = \"" << name << "\";"; + dal::RecordSet const &accountInfo = mDb->execSql(sql.str()); - std::stringstream ssStream(accountInfo(0,0)); - int iReturn = -1; - ssStream >> iReturn; - if ( iReturn > 0 ) - { - return true; - } - else - { - return false; - } - } - catch (const dal::DbSqlQueryExecFailure& e) { + std::istringstream ssStream(accountInfo(0, 0)); + int iReturn = 1; + ssStream >> iReturn; + return iReturn != 0; + } catch (std::exception const &e) { // TODO: throw an exception. LOG_ERROR("SQL query failure: " << e.what(), 0); - } + } - return false; + return true; } /** diff --git a/src/dalstorage.h b/src/dalstorage.h index ec80a8cc..b5e7c03b 100644 --- a/src/dalstorage.h +++ b/src/dalstorage.h @@ -98,18 +98,16 @@ class DALStorage: public Storage getEmailList(); /** - * Return the number of same Emails in account's table. - * @return Number of same Email. + * Tells if the email address already exists. + * @return true if the email address exists. */ - unsigned int - getSameEmailNumber(const std::string &email); + bool doesEmailAddressExist(std::string const &email); /** * Tells if the character's name already exists * @return true if character's name exists. */ - bool - doesCharacterNameExists(const std::string &name); + bool doesCharacterNameExist(std::string const &name); /** * Tells the map name from the map id diff --git a/src/storage.h b/src/storage.h index d66c3e26..c4486b5c 100644 --- a/src/storage.h +++ b/src/storage.h @@ -210,18 +210,16 @@ class Storage std::list getEmailList() = 0; /** - * Return the number of same Emails in account's table. - * @return Number of same Email. + * Tells if the email address already exists. + * @return true if the email address exists. */ - virtual unsigned int - getSameEmailNumber(const std::string &email) = 0; + virtual bool doesEmailAddressExist(std::string const &email) = 0; /** * Tells if the character's name already exists * @return true if character's name exists. */ - virtual bool - doesCharacterNameExists(const std::string &name) = 0; + virtual bool doesCharacterNameExist(std::string const &name) = 0; /** * Tells the map name from the map id -- cgit v1.2.3-70-g09d2