From 5ddde90f820277f62d28d6922dc35fc52ba4d89d Mon Sep 17 00:00:00 2001 From: Christian Heinemann Date: Sun, 9 Jul 2023 19:41:16 +0200 Subject: [PATCH] show user registration confirmation dialog in login if account not already confirmed --- source/Base/Resources.h | 2 +- source/Gui/ActivateUserDialog.cpp | 5 +++-- source/Gui/LoginDialog.cpp | 18 +++++++++++++++--- source/Gui/LoginDialog.h | 2 ++ source/Gui/MainWindow.cpp | 2 +- source/Gui/NetworkController.cpp | 28 +++++++++++++++------------- source/Gui/NetworkController.h | 10 +++++++++- source/Gui/NewPasswordDialog.cpp | 3 ++- source/Server/login.php | 19 ++++++++++++++++++- 9 files changed, 66 insertions(+), 23 deletions(-) diff --git a/source/Base/Resources.h b/source/Base/Resources.h index b84be6a40..076b70df4 100644 --- a/source/Base/Resources.h +++ b/source/Base/Resources.h @@ -2,7 +2,7 @@ namespace Const { - std::string const ProgramVersion = "4.0.0.beta.21"; + std::string const ProgramVersion = "4.0.0.beta.22"; std::string const BasePath = "resources/"; diff --git a/source/Gui/ActivateUserDialog.cpp b/source/Gui/ActivateUserDialog.cpp index b24d37a2e..b81fc0903 100644 --- a/source/Gui/ActivateUserDialog.cpp +++ b/source/Gui/ActivateUserDialog.cpp @@ -62,7 +62,8 @@ void _ActivateUserDialog::onActivateUser() { auto result = _networkController->activateUser(_userName, _password, _confirmationCode); if (result) { - result |= _networkController->login(_userName, _password); + LoginErrorCode errorCode; + result |= _networkController->login(errorCode, _userName, _password); } if (!result) { MessageDialog::getInstance().show("Error", "An error occurred on the server. Your entered code may be incorrect.\nPlease try to register again."); @@ -70,7 +71,7 @@ void _ActivateUserDialog::onActivateUser() MessageDialog::getInstance().show( "Information", "The user '" + _userName - + "' has been successfully created.\nYou are logged in and are now able to upload your own simulations\nor rate others by likes."); + + "' has been successfully created.\nYou are logged in and are now able to upload your own simulations\nor upvote others by likes."); _browserWindow->onRefresh(); } } diff --git a/source/Gui/LoginDialog.cpp b/source/Gui/LoginDialog.cpp index 9304aa108..8d7d608f8 100644 --- a/source/Gui/LoginDialog.cpp +++ b/source/Gui/LoginDialog.cpp @@ -9,14 +9,17 @@ #include "CreateUserDialog.h" #include "BrowserWindow.h" #include "ResetPasswordDialog.h" +#include "ActivateUserDialog.h" _LoginDialog::_LoginDialog( BrowserWindow const& browserWindow, CreateUserDialog const& createUserDialog, + ActivateUserDialog const& activateUserDialog, ResetPasswordDialog const& resetPasswordDialog, NetworkController const& networkController) : _browserWindow(browserWindow) , _createUserDialog(createUserDialog) + , _activateUserDialog(activateUserDialog) , _networkController(networkController) , _resetPasswordDialog(resetPasswordDialog) @@ -27,7 +30,8 @@ _LoginDialog::_LoginDialog( _userName = settings.getStringState("dialogs.login.user name", ""); _password = settings.getStringState("dialogs.login.password", ""); if (!_userName.empty()) { - if (!_networkController->login(_userName, _password)) { + LoginErrorCode errorCode; + if (!_networkController->login(errorCode, _userName, _password)) { MessageDialog::getInstance().show("Error", "Login failed."); } } @@ -126,8 +130,16 @@ void _LoginDialog::show() void _LoginDialog::onLogin() { - if (!_networkController->login(_userName, _password)) { - MessageDialog::getInstance().show("Error", "Login failed."); + LoginErrorCode errorCode; + if (!_networkController->login(errorCode, _userName, _password)) { + switch (errorCode) { + case LoginErrorCode_UnconfirmedUser: { + _activateUserDialog->show(_userName, _password); + } break; + default: { + MessageDialog::getInstance().show("Error", "Login failed."); + } break; + } return; } _browserWindow->onRefresh(); diff --git a/source/Gui/LoginDialog.h b/source/Gui/LoginDialog.h index deb30f265..9e3588f00 100644 --- a/source/Gui/LoginDialog.h +++ b/source/Gui/LoginDialog.h @@ -8,6 +8,7 @@ class _LoginDialog _LoginDialog( BrowserWindow const& browserWindow, CreateUserDialog const& createUserDialog, + ActivateUserDialog const& activateUserDialog, ResetPasswordDialog const& resetPasswordDialog, NetworkController const& networkController); ~_LoginDialog(); @@ -21,6 +22,7 @@ class _LoginDialog BrowserWindow _browserWindow; CreateUserDialog _createUserDialog; + ActivateUserDialog _activateUserDialog; NetworkController _networkController; ResetPasswordDialog _resetPasswordDialog; diff --git a/source/Gui/MainWindow.cpp b/source/Gui/MainWindow.cpp index aaeed8e41..3f0ddd1c8 100644 --- a/source/Gui/MainWindow.cpp +++ b/source/Gui/MainWindow.cpp @@ -156,7 +156,7 @@ _MainWindow::_MainWindow(SimulationController const& simController, SimpleLogger _createUserDialog = std::make_shared<_CreateUserDialog>(_activateUserDialog, _networkController); _newPasswordDialog = std::make_shared<_NewPasswordDialog>(_browserWindow, _networkController); _resetPasswordDialog = std::make_shared<_ResetPasswordDialog>(_newPasswordDialog, _networkController); - _loginDialog = std::make_shared<_LoginDialog>(_browserWindow, _createUserDialog, _resetPasswordDialog, _networkController); + _loginDialog = std::make_shared<_LoginDialog>(_browserWindow, _createUserDialog, _activateUserDialog, _resetPasswordDialog, _networkController); _uploadSimulationDialog = std::make_shared<_UploadSimulationDialog>(_browserWindow, _simController, _networkController, _viewport); _deleteUserDialog = std::make_shared<_DeleteUserDialog>(_browserWindow, _networkController); _networkSettingsDialog = std::make_shared<_NetworkSettingsDialog>(_browserWindow, _networkController); diff --git a/source/Gui/NetworkController.cpp b/source/Gui/NetworkController.cpp index bbff8c84d..52acd83ab 100644 --- a/source/Gui/NetworkController.cpp +++ b/source/Gui/NetworkController.cpp @@ -47,19 +47,14 @@ namespace bool parseBoolResult(std::string const& serverResponse) { - try { - std::stringstream stream(serverResponse); - boost::property_tree::ptree tree; - boost::property_tree::read_json(stream, tree); - auto result = tree.get("result"); - if (!result) { - log(Priority::Important, "network: negative response received from server"); - } - return result; - } catch (...) { - logNetworkError(); - return false; + std::stringstream stream(serverResponse); + boost::property_tree::ptree tree; + boost::property_tree::read_json(stream, tree); + auto result = tree.get("result"); + if (!result) { + log(Priority::Important, "network: negative response received from server"); } + return result; } } @@ -147,7 +142,7 @@ bool _NetworkController::activateUser(std::string const& userName, std::string c } } -bool _NetworkController::login(std::string const& userName, std::string const& password) +bool _NetworkController::login(LoginErrorCode& errorCode, std::string const& userName, std::string const& password) { log(Priority::Important, "network: login user '" + userName + "'"); @@ -166,6 +161,13 @@ bool _NetworkController::login(std::string const& userName, std::string const& p _loggedInUserName = userName; _password = password; } + + errorCode = false; + std::stringstream stream(result->body); + boost::property_tree::ptree tree; + boost::property_tree::read_json(stream, tree); + errorCode = tree.get("errorCode"); + return boolResult; } catch (...) { logNetworkError(); diff --git a/source/Gui/NetworkController.h b/source/Gui/NetworkController.h index 4c627e995..4d269260e 100644 --- a/source/Gui/NetworkController.h +++ b/source/Gui/NetworkController.h @@ -6,6 +6,13 @@ #include "UserData.h" #include "Definitions.h" +using LoginErrorCode = int; +enum LoginErrorCode_ +{ + LoginErrorCode_UnconfirmedUser, + LoginErrorCode_Other +}; + class _NetworkController { public: @@ -21,7 +28,8 @@ class _NetworkController bool createUser(std::string const& userName, std::string const& password, std::string const& email); bool activateUser(std::string const& userName, std::string const& password, std::string const& confirmationCode); - bool login(std::string const& userName, std::string const& password); + + bool login(LoginErrorCode& errorCode, std::string const& userName, std::string const& password); bool logout(); bool deleteUser(); bool resetPassword(std::string const& userName, std::string const& email); diff --git a/source/Gui/NewPasswordDialog.cpp b/source/Gui/NewPasswordDialog.cpp index 21df8929e..7abc7f611 100644 --- a/source/Gui/NewPasswordDialog.cpp +++ b/source/Gui/NewPasswordDialog.cpp @@ -68,7 +68,8 @@ void _NewPasswordDialog::onNewPassword() { auto result = _networkController->setNewPassword(_userName, _newPassword, _confirmationCode); if (result) { - result |= _networkController->login(_userName, _newPassword); + LoginErrorCode errorCode; + result |= _networkController->login(errorCode, _userName, _newPassword); } if (!result) { MessageDialog::getInstance().show("Error", "An error occurred on the server. Your entered code may be incorrect.\nPlease try to reset the password again."); diff --git a/source/Server/login.php b/source/Server/login.php index c743c9630..59b3162d9 100644 --- a/source/Server/login.php +++ b/source/Server/login.php @@ -12,7 +12,24 @@ $success = $db->query("UPDATE user SET FLAGS=1, TIMESTAMP=CURRENT_TIMESTAMP WHERE NAME='".addslashes($userName)."'"); } - echo json_encode(["result"=>$success]); + $errorCode = 1; + + if ($response = $db->query( + "SELECT + u.ACTIVATION_CODE as activationCode + FROM user u + WHERE u.NAME='".addslashes($userName)."'")) { + if ($obj = $response->fetch_object()) { + if ($obj->activationCode != "") { + $errorCode = 0; + } + } + } + + echo json_encode([ + "result" => $success, + "errorCode" => $errorCode + ]); $db->commit(); $db->close(); ?>