Skip to content

Commit

Permalink
Handle input after the action passed and check for user's existence.
Browse files Browse the repository at this point in the history
  • Loading branch information
cristianfrasineanu committed Nov 28, 2016
1 parent 971c4a4 commit 4fc87f8
Show file tree
Hide file tree
Showing 14 changed files with 110 additions and 82 deletions.
3 changes: 2 additions & 1 deletion app/Bootstrap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ void main()
{
toast(string(e.what()), string("error"));

sleepAndClearBuffer(console.getDelay());
sleepAndFlushInput(console.getDelay());
clearPreviousLines(0);
}
} while (!console.shouldExit());
}
catch (const invalid_argument &e)
{
clearScreen();
toast(string(e.what()), string("error"));
}
catch (const system_error &e)
Expand Down
5 changes: 1 addition & 4 deletions app/Console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -287,10 +287,7 @@ Console::~Console()
wbkgd(stdscr, COLOR_PAIR(1));
printString("Program exited the main console.\n");

// This or use the windows Sleep.
// Maybe better Sleep.
//timeout(this->delay);
getch();
sleepAndFlushInput(this->delay);

endwin();
refresh();
Expand Down
31 changes: 19 additions & 12 deletions app/Controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// Well, just a general controller... not much to see here.
string Controller::userInputString = "@input-";
string Controller::outputString = "@output-";
string Controller::actionString = "@action-";

vector<string> Controller::errorBag = {};

Expand All @@ -22,6 +23,9 @@ void Controller::prepareView()
if (this->hasInput(copyChunk))
{
this->controllerAttributions.push_back("input");

// Pass the corresposing action to userInputs.
this->prepareAction(copyChunk);
}
if (this->hasOutput(copyChunk))
{
Expand Down Expand Up @@ -80,39 +84,41 @@ void Controller::prepareViewInput(const string &subChunk, const string &inputAli
const char backspace = 8;

unsigned char ch = 0;

noecho();
while ((ch = getch()) != carriage_return)
{
if (ch == backspace && userInput.size())
{
printw("\b \b");
refresh();
// Otherwise the previous character will still remain on the screen if not replaced with a whitespace.
printString("\b \b");
userInput.resize(userInput.size() - 1);
}
else if (ch != backspace)
{
userInput += ch;
printw("*");
refresh();
printString("*");
}
}

// Re-enable input and render the previous carriage return.
echo();
printString("\n");
}
else
{
//getline(cin, userInput);
getString(userInput);
}

printString("\n");

this->userInputs[inputAlias] = userInput;
}

void Controller::prepareAction(string &chunk)
{
this->userInputs["action"] = chunk.substr(chunk.find(Controller::actionString) + Controller::actionString.size(),
chunk.find("\n", chunk.find(Controller::actionString)) - chunk.find(Controller::actionString) - Controller::actionString.size());

chunk.erase(chunk.find(Controller::actionString),
(Controller::actionString + this->userInputs["action"]).size() + 1);
}

void Controller::pushError(string &error)
{
if (error == "")
Expand Down Expand Up @@ -145,6 +151,7 @@ Controller::Controller(char *viewName, string &viewChunk, string &ViewExtension)
string controllerName = viewName;
controllerName.erase(controllerName.find(ViewExtension), ViewExtension.size()).append("Controller");

// Enable input echoing when typing.
echo();

this->controllerName = new char[controllerName.size() + 1];
Expand Down Expand Up @@ -175,7 +182,7 @@ void Controller::chopChunkAndGetAlias(string &chunk)

this->prepareViewInput(chunk.substr(0, chunk.find(Controller::userInputString) - 1), inputAlias);

chunk.erase(0, chunk.find(inputAlias) + inputAlias.size() + 2);
chunk.erase(0, chunk.find(inputAlias) + inputAlias.size() + 1);
}

// Don't assign it multiple times, stupid.
Expand Down
5 changes: 3 additions & 2 deletions app/Controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class Controller {
private:
static string userInputString;
static string outputString;
static string actionString;

static vector<string> errorBag;

Expand All @@ -23,6 +24,8 @@ class Controller {
void justShow();
void prepareView();
void prepareViewInput(const string &, const string &);
void prepareAction(string &);
void chopChunkAndGetAlias(string &);
public:
static void pushError(string &);
static vector<string> getErrorBag();
Expand All @@ -36,8 +39,6 @@ class Controller {
vector<string> &getControllerAttributions();
char *getControllerName();

void chopChunkAndGetAlias(string &);

void operator=(const Controller &);

~Controller();
Expand Down
2 changes: 1 addition & 1 deletion app/Helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ void toLowerCase(string &input)
[](unsigned char ch) { return tolower(ch); });
}

void sleepAndClearBuffer(unsigned delay)
void sleepAndFlushInput(unsigned delay)
{
Sleep(delay);
flushinp();
Expand Down
2 changes: 1 addition & 1 deletion app/Helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ inline bool isInMap(map<FirstT, SecondT> &haystack, FirstT needle)
void printVector(vector<string> &);
void toast(string &, string &);
void toLowerCase(string &);
void sleepAndClearBuffer(unsigned);
void sleepAndFlushInput(unsigned);
void clearScreen();
void getString(string &);
void printString(const char *);
Expand Down
10 changes: 9 additions & 1 deletion app/Model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ void Model::confirmInput(const map<string, string> &payLoad)
{
this->rawInput = payLoad;

string entityName = this->parseEntityName(this->rawInput.begin()->first),
string entityName = this->parseEntityName((++this->rawInput.begin())->first),
inputAlias;

if (this->repository == NULL)
Expand All @@ -44,6 +44,14 @@ void Model::confirmInput(const map<string, string> &payLoad)

for (map<string, string>::iterator it = this->rawInput.begin(); it != this->rawInput.end(); it++)
{
// Skip truncation for action.
if (it->first == "action")
{
this->truncatedInput["action"] = it->second;

continue;
}

inputAlias = it->first;
inputAlias.erase(0, entityName.size() + 1);

Expand Down
31 changes: 30 additions & 1 deletion app/UserModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ 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!");
}

Expand All @@ -20,6 +23,8 @@ User UserModel::getAfterId(int id)
this->io.seekg((id - 1) * sizeof(User), this->io.beg);
this->io.read(reinterpret_cast<char *>(&this->user), sizeof(User));

this->io.clear();

return this->user;
}

Expand All @@ -35,9 +40,30 @@ User UserModel::getActive()
}
}

this->io.clear();

throw exception("No active user!");
}

bool UserModel::userExists(string &username)
{
User user;

this->io.seekg(0, this->io.beg);

while (this->io.read(reinterpret_cast<char *>(&user), sizeof(User)))
{
if (user.username == username)
{
return true;
}
}

this->io.clear();

return false;
}

void UserModel::markAs(string &status, int id)
{
this->getAfterId(id);
Expand All @@ -51,6 +77,7 @@ void UserModel::markAs(string &status, int id)
void UserModel::save()
{
this->io.seekp((this->user.id - 1) * sizeof(User), this->io.beg);

this->io.write(reinterpret_cast<char *>(&this->user), sizeof(User));
}

Expand Down Expand Up @@ -81,7 +108,7 @@ void UserModel::setAttributes(map<string, string> &cleanInputs)
}

// If there's a new user, assign created_at with the current date.
if (strlen(this->user.full_name) != 0)
if (cleanInputs.find("action")->second == "signup")
{
time_t t = time(nullptr);
strftime(this->user.created_at, sizeof(this->user.created_at), "%c", localtime(&t));
Expand Down Expand Up @@ -149,6 +176,8 @@ void UserModel::setLastId()
this->io.seekg((this->fileSize / sizeof(User) - 1) * sizeof(User), this->io.beg);
this->io.read(reinterpret_cast<char *>(&this->user), sizeof(User));

this->io.clear();

this->lastId = this->user.id;
}
}
Expand Down
1 change: 1 addition & 0 deletions app/UserModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class UserModel : public ModelInterface {
User getAfterId(int);
User getActive();

bool userExists(string &);
void markAs(string &, int);
void save();
void setAttributes(map<string, string> &);
Expand Down
27 changes: 20 additions & 7 deletions app/UserRepository.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ void UserRepository::defineValidation()
{ "fullname", "^(?=.*(?:[\\s\\-])([a-zA-Z]+\\s?)+)([a-zA-Z]+)(?:[\\s\\-]).*$" },
{ "email", "^(?!.*[._]{2})[a-z0-9._]+@[a-z0-9]+(\\.[a-z]{1,3}){1,2}$" },
{ "username", "^(?=.{7,19}$)(?![^a-zA-Z0-9])(?!.*[!_\\-\\.]{2})[a-zA-Z0-9\\.\\-_]+$" },
{ "password", "^(?=.{5,16}$)(?=.*[A-Z])(?=.*[0-9])(?=.*[!\\-_@.$#])[a-zA-Z0-9!\\-_@.$#]+$" }
{ "password", "^(?=.{5,16}$)(?=.*[A-Z])(?=.*[0-9])(?=.*[!\\-_@.$#])[a-zA-Z0-9!\\-_@.$#]+$" },
{ "action", "^(?!.*\\W+)(?!.*[A-Z]+)[a-z]+$" }
};

this->ValidationErrors = {
Expand All @@ -34,7 +35,9 @@ void UserRepository::receiveCleanInput(map<string, string> &cleanInput)
{
this->model.setAttributes(cleanInput);

if (cleanInput.find("fullname") == cleanInput.end())
string action = cleanInput.find("action")->second;

if (action == "login")
{
try
{
Expand All @@ -57,14 +60,24 @@ void UserRepository::receiveCleanInput(map<string, string> &cleanInput)
Controller::pushError(string(e.what()));
}
}
else if (action == "signup")
{
// Try finding the user and validate the request.
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");

this->model.save();
}
}
else
{
// TODO: check for user uniqueness when creating an account.
toast(string("Account created successfully! Please press -c- confirm your access to the dashboard."), string("success"));
printString("\n");
Controller::pushError(string("Please provide a valid action!"));
}

this->model.save();
// TODO: for a ban action, check if the current user has the according privilegies.
}

string &UserRepository::getAlias()
Expand Down
Binary file modified database/users.store
Binary file not shown.
Loading

0 comments on commit 4fc87f8

Please sign in to comment.