Skip to content

Commit

Permalink
show user registration confirmation dialog in login if account not al…
Browse files Browse the repository at this point in the history
…ready confirmed
  • Loading branch information
chrxh committed Jul 9, 2023
1 parent 874c92e commit 5ddde90
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 23 deletions.
2 changes: 1 addition & 1 deletion source/Base/Resources.h
Original file line number Diff line number Diff line change
Expand Up @@ -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/";

Expand Down
5 changes: 3 additions & 2 deletions source/Gui/ActivateUserDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,16 @@ 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.");
} else {
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();
}
}
18 changes: 15 additions & 3 deletions source/Gui/LoginDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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.");
}
}
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 2 additions & 0 deletions source/Gui/LoginDialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class _LoginDialog
_LoginDialog(
BrowserWindow const& browserWindow,
CreateUserDialog const& createUserDialog,
ActivateUserDialog const& activateUserDialog,
ResetPasswordDialog const& resetPasswordDialog,
NetworkController const& networkController);
~_LoginDialog();
Expand All @@ -21,6 +22,7 @@ class _LoginDialog

BrowserWindow _browserWindow;
CreateUserDialog _createUserDialog;
ActivateUserDialog _activateUserDialog;
NetworkController _networkController;
ResetPasswordDialog _resetPasswordDialog;

Expand Down
2 changes: 1 addition & 1 deletion source/Gui/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
28 changes: 15 additions & 13 deletions source/Gui/NetworkController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool>("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<bool>("result");
if (!result) {
log(Priority::Important, "network: negative response received from server");
}
return result;
}
}

Expand Down Expand Up @@ -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 + "'");

Expand All @@ -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<LoginErrorCode>("errorCode");

return boolResult;
} catch (...) {
logNetworkError();
Expand Down
10 changes: 9 additions & 1 deletion source/Gui/NetworkController.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@
#include "UserData.h"
#include "Definitions.h"

using LoginErrorCode = int;
enum LoginErrorCode_
{
LoginErrorCode_UnconfirmedUser,
LoginErrorCode_Other
};

class _NetworkController
{
public:
Expand All @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion source/Gui/NewPasswordDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
Expand Down
19 changes: 18 additions & 1 deletion source/Server/login.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
?>

0 comments on commit 5ddde90

Please sign in to comment.