Even more work on from validation.

This commit is contained in:
Relintai 2021-08-20 18:00:50 +02:00
parent 0abb42bd61
commit 3bc7bd98c6
4 changed files with 180 additions and 17 deletions

View File

@ -357,8 +357,96 @@ FormEmailFieldEntry::FormEmailFieldEntry() {
FormEmailFieldEntry::~FormEmailFieldEntry() {
}
//FormNeedToMatchOtherFieldEntry
bool FormNeedToMatchOtherFieldEntry::validate(Request *request, const std::string &field_name, const std::string &data, std::vector<std::string> *errors) {
if (data != request->get_parameter(other_field)) {
if (errors) {
errors->push_back(does_not_match_error + field_name + ".");
}
return false;
}
return true;
}
FormNeedToMatchOtherFieldEntry::FormNeedToMatchOtherFieldEntry() {
does_not_match_error = "Field does not match ";
}
FormNeedToMatchOtherFieldEntry::~FormNeedToMatchOtherFieldEntry() {
}
//FormField
FormField *FormField::need_to_exist() {
add_entry(new FormExistsFieldEntry());
return this;
}
FormField *FormField::need_to_be_int() {
add_entry(new FormIntFieldEntry());
return this;
}
FormField *FormField::need_to_be_float() {
add_entry(new FormFloatFieldEntry());
return this;
}
FormField *FormField::need_to_be_alpha() {
add_entry(new FormAlphaFieldEntry());
return this;
}
FormField *FormField::need_to_be_alpha_numeric() {
add_entry(new FormAlphaNumericFieldEntry());
return this;
}
FormField *FormField::need_to_have_lowercase_character() {
add_entry(new FormNeedsLowercaseCharacterFieldEntry());
return this;
}
FormField *FormField::need_to_have_uppercase_character() {
add_entry(new FormNeedsUppercaseCharacterFieldEntry());
return this;
}
FormField *FormField::need_to_have_other_character() {
add_entry(new FormNeedsOtherCharacterFieldEntry());
return this;
}
FormField *FormField::need_minimum_length(const int min_length) {
FormMinimumLengthFieldEntry *f = new FormMinimumLengthFieldEntry();
f->min_length = min_length;
add_entry(f);
return this;
}
FormField *FormField::need_maximum_length(const int max_length) {
FormMaximumLengthFieldEntry *f = new FormMaximumLengthFieldEntry();
f->max_length =max_length;
add_entry(f);
return this;
}
FormField *FormField::need_to_be_email() {
add_entry(new FormEmailFieldEntry());
return this;
}
FormField *FormField::need_to_match(const std::string &other) {
FormNeedToMatchOtherFieldEntry *f = new FormNeedToMatchOtherFieldEntry();
f->other_field = other;
add_entry(f);
return this;
}
void FormField::add_entry(FormFieldEntry *field) {
fields.push_back(field);
}
@ -406,6 +494,15 @@ void FormValidator::add_field(FormField *field) {
fields.push_back(field);
}
FormField *FormValidator::new_field(const std::string &name) {
FormField *f = new FormField();
f->name = name;
fields.push_back(f);
return f;
}
FormValidator::FormValidator() {
}

View File

@ -1,10 +1,10 @@
#ifndef FORM_H
#define FORM_H
#include <ctype.h>
#include <map>
#include <string>
#include <vector>
#include <ctype.h>
class Request;
@ -23,7 +23,7 @@ public:
FormExistsFieldEntry();
~FormExistsFieldEntry();
std::string not_exists_error;
std::string not_exists_error;
};
class FormIntFieldEntry : public FormFieldEntry {
@ -33,7 +33,7 @@ public:
FormIntFieldEntry();
~FormIntFieldEntry();
std::string not_int_error;
std::string not_int_error;
};
class FormFloatFieldEntry : public FormFieldEntry {
@ -43,7 +43,7 @@ public:
FormFloatFieldEntry();
~FormFloatFieldEntry();
std::string not_float_error;
std::string not_float_error;
};
class FormAlphaFieldEntry : public FormFieldEntry {
@ -53,7 +53,7 @@ public:
FormAlphaFieldEntry();
~FormAlphaFieldEntry();
std::string not_alpha_error;
std::string not_alpha_error;
};
class FormAlphaNumericFieldEntry : public FormFieldEntry {
@ -63,7 +63,7 @@ public:
FormAlphaNumericFieldEntry();
~FormAlphaNumericFieldEntry();
std::string not_alpha_numeric_error;
std::string not_alpha_numeric_error;
};
class FormNeedsLowercaseCharacterFieldEntry : public FormFieldEntry {
@ -73,7 +73,7 @@ public:
FormNeedsLowercaseCharacterFieldEntry();
~FormNeedsLowercaseCharacterFieldEntry();
std::string does_not_have_lowercase_error;
std::string does_not_have_lowercase_error;
};
class FormNeedsUppercaseCharacterFieldEntry : public FormFieldEntry {
@ -83,7 +83,7 @@ public:
FormNeedsUppercaseCharacterFieldEntry();
~FormNeedsUppercaseCharacterFieldEntry();
std::string does_not_have_uppercase_error;
std::string does_not_have_uppercase_error;
};
class FormNeedsOtherCharacterFieldEntry : public FormFieldEntry {
@ -93,7 +93,7 @@ public:
FormNeedsOtherCharacterFieldEntry();
~FormNeedsOtherCharacterFieldEntry();
std::string does_not_have_other_error;
std::string does_not_have_other_error;
};
class FormMinimumLengthFieldEntry : public FormFieldEntry {
@ -103,10 +103,10 @@ public:
FormMinimumLengthFieldEntry();
~FormMinimumLengthFieldEntry();
int min_length;
int min_length;
std::string does_not_have_min_length_errorf;
std::string does_not_have_min_length_errors;
std::string does_not_have_min_length_errorf;
std::string does_not_have_min_length_errors;
};
class FormMaximumLengthFieldEntry : public FormFieldEntry {
@ -116,10 +116,10 @@ public:
FormMaximumLengthFieldEntry();
~FormMaximumLengthFieldEntry();
int max_length;
int max_length;
std::string does_not_have_max_length_errorf;
std::string does_not_have_max_length_errors;
std::string does_not_have_max_length_errorf;
std::string does_not_have_max_length_errors;
};
class FormEmailFieldEntry : public FormFieldEntry {
@ -129,7 +129,19 @@ public:
FormEmailFieldEntry();
~FormEmailFieldEntry();
std::string email_format_error;
std::string email_format_error;
};
class FormNeedToMatchOtherFieldEntry : public FormFieldEntry {
public:
virtual bool validate(Request *request, const std::string &field_name, const std::string &data, std::vector<std::string> *errors);
FormNeedToMatchOtherFieldEntry();
~FormNeedToMatchOtherFieldEntry();
std::string other_field;
std::string does_not_match_error;
};
//FormField
@ -138,6 +150,19 @@ class FormField {
public:
std::string name;
FormField *need_to_exist();
FormField *need_to_be_int();
FormField *need_to_be_float();
FormField *need_to_be_alpha();
FormField *need_to_be_alpha_numeric();
FormField *need_to_have_lowercase_character();
FormField *need_to_have_uppercase_character();
FormField *need_to_have_other_character();
FormField *need_minimum_length(const int min_length);
FormField *need_maximum_length(const int max_length);
FormField *need_to_be_email();
FormField *need_to_match(const std::string &other);
void add_entry(FormFieldEntry *field);
bool validate(Request *request, std::vector<std::string> *errors);
@ -148,11 +173,14 @@ public:
std::vector<FormFieldEntry *> fields;
};
//FormValidator
class FormValidator {
public:
bool validate(Request *request, std::vector<std::string> *errors = nullptr);
bool validate(Request *request, std::vector<std::string> *errors = nullptr);
void add_field(FormField *field);
FormField *new_field(const std::string &name);
FormValidator();
virtual ~FormValidator();

View File

@ -1,6 +1,7 @@
#include "user.h"
#include "core/hash/sha256.h"
#include "core/html/form_validator.h"
#include "core/html/html_builder.h"
#include "core/http/cookie.h"
#include "core/http/http_session.h"
@ -387,6 +388,32 @@ void User::handle_delete_request(Request *request) {
request->compile_and_send_body();
}
void User::create_validators() {
//Login
_login_validator = new FormValidator();
_login_validator->new_field("username")->need_to_exist()->need_to_be_alpha_numeric()->need_minimum_length(5)->need_maximum_length(20);
FormField *pw = _login_validator->new_field("password");
pw->need_to_exist();
pw->need_to_have_lowercase_character()->need_to_have_uppercase_character();
pw->need_minimum_length(5);
//Registration
_registration_validator = new FormValidator();
_registration_validator->new_field("username")->need_to_exist()->need_to_be_alpha_numeric()->need_minimum_length(5)->need_maximum_length(20);
_registration_validator->new_field("email")->need_to_exist()->need_to_be_email();
pw = _registration_validator->new_field("password");
pw->need_to_exist();
pw->need_to_have_lowercase_character()->need_to_have_uppercase_character();
pw->need_minimum_length(5);
_registration_validator->new_field("password_check")->need_to_match("password");
_registration_validator->new_field("email")->need_to_exist()->need_to_be_email();
}
User::User() :
Object() {
@ -394,6 +421,11 @@ User::User() :
rank = 0;
banned = false;
locked = false;
_login_validator = nullptr;
_registration_validator = nullptr;
create_validators();
}
User::~User() {

View File

@ -7,6 +7,7 @@
#include <vector>
class Request;
class FormValidator;
class User : public Object {
RCPP_OBJECT(User, Object);
@ -47,11 +48,16 @@ public:
virtual void handle_logout_request(Request *request);
virtual void handle_delete_request(Request *request);
virtual void create_validators();
void register_sessions();
void unregister_sessions();
User();
~User();
FormValidator *_login_validator;
FormValidator *_registration_validator;
};
#endif