diff --git a/app/UserModel.cpp b/app/UserModel.cpp index 1ef9785..27798dc 100644 --- a/app/UserModel.cpp +++ b/app/UserModel.cpp @@ -12,7 +12,6 @@ User UserModel::getAfterUser(string &username) } } - // Otherwise if we reach the end of the file, the eof flag will be set and write will fail. this->io.clear(); throw invalid_argument("Username not found!"); @@ -23,8 +22,6 @@ User UserModel::getAfterId(int id) this->io.seekg((id - 1) * sizeof(User), this->io.beg); this->io.read(reinterpret_cast(&this->user), sizeof(User)); - this->io.clear(); - return this->user; } @@ -40,8 +37,6 @@ User UserModel::getActive() } } - this->io.clear(); - throw exception("No active user!"); } @@ -59,8 +54,6 @@ bool UserModel::userExists(string &username) } } - this->io.clear(); - return false; } @@ -78,7 +71,13 @@ void UserModel::save() { this->io.seekp((this->user.id - 1) * sizeof(User), this->io.beg); - this->io.write(reinterpret_cast(&this->user), sizeof(User)); + // Otherwise if we reach the end of the file, the eof flag will be set and write will fail. + this->io.clear(); + + if (!this->io.write(reinterpret_cast(&this->user), sizeof(User))) + { + throw system_error(error_code(3, generic_category()), "Failed persisting data to file!"); + } } void UserModel::setAttributes(map &cleanInputs) @@ -176,8 +175,6 @@ void UserModel::setLastId() this->io.seekg((this->fileSize / sizeof(User) - 1) * sizeof(User), this->io.beg); this->io.read(reinterpret_cast(&this->user), sizeof(User)); - this->io.clear(); - this->lastId = this->user.id; } } diff --git a/app/UserRepository.cpp b/app/UserRepository.cpp index 91d2cd7..aa3cecf 100644 --- a/app/UserRepository.cpp +++ b/app/UserRepository.cpp @@ -42,13 +42,21 @@ void UserRepository::receiveCleanInput(map &cleanInput) try { User user = this->model.getAfterUser(cleanInput.find("username")->second); - if (!strcmp(user.password, cleanInput.find("password")->second.c_str()) + if (!strcmp(user.password, cleanInput.find("password")->second.c_str()) && !strcmp(user.deleted_at, "") && user.banned != true) { - this->model.markAs(string("active"), user.id); - toast(string("Login successful! Please press -c- confirm your access to the dashboard."), string("success")); - printString("\n"); + try + { + this->model.markAs(string("active"), user.id); + + toast(string("Login successful! Please press -c- confirm your access to the dashboard."), string("success")); + printString("\n"); + } + catch (const system_error &e) + { + Controller::pushError(string(e.what())); + } } else { @@ -63,14 +71,23 @@ void UserRepository::receiveCleanInput(map &cleanInput) else if (action == "signup") { // Try finding the user and validate the request. - if (this->model.userExists(cleanInput.find("username")->second)) { + if (this->model.userExists(cleanInput.find("username")->second)) + { Controller::pushError(string("The username already exists!")); } - else { - toast(string("Account created successfully! Please press -c- confirm your access to the dashboard."), string("success")); - printString("\n"); + else + { + try + { + this->model.save(); - this->model.save(); + toast(string("Account created successfully! Please press -c- confirm your access to the dashboard."), string("success")); + printString("\n"); + } + catch (const system_error &e) + { + Controller::pushError(string(e.what())); + } } } else diff --git a/database/users.store b/database/users.store index bbc8338..6eaca79 100644 Binary files a/database/users.store and b/database/users.store differ diff --git a/database/users.txt b/database/users.txt index ddc5c78..61998be 100644 --- a/database/users.txt +++ b/database/users.txt @@ -42,3 +42,14 @@ user 0 0 +5 +Frasiii Fras +frac@yahoo.com +fraaac1234 +Adr5$32 +Mon Nov 28 15:25:36 2016 + +user +0 +0 + diff --git a/views/logout.view b/views/logout.view index 9f5ec34..70aad03 100644 --- a/views/logout.view +++ b/views/logout.view @@ -4,7 +4,7 @@ _\ \ || (_| | (__|