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;
@ -132,12 +132,37 @@ public:
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
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);
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