mirror of
https://github.com/Relintai/rcpp_framework.git
synced 2024-11-14 04:57:21 +01:00
Even more work on from validation.
This commit is contained in:
parent
0abb42bd61
commit
3bc7bd98c6
@ -357,8 +357,96 @@ FormEmailFieldEntry::FormEmailFieldEntry() {
|
|||||||
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 *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) {
|
void FormField::add_entry(FormFieldEntry *field) {
|
||||||
fields.push_back(field);
|
fields.push_back(field);
|
||||||
}
|
}
|
||||||
@ -406,6 +494,15 @@ void FormValidator::add_field(FormField *field) {
|
|||||||
fields.push_back(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() {
|
FormValidator::FormValidator() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
#ifndef FORM_H
|
#ifndef FORM_H
|
||||||
#define FORM_H
|
#define FORM_H
|
||||||
|
|
||||||
|
#include <ctype.h>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <ctype.h>
|
|
||||||
|
|
||||||
class Request;
|
class Request;
|
||||||
|
|
||||||
@ -23,7 +23,7 @@ public:
|
|||||||
FormExistsFieldEntry();
|
FormExistsFieldEntry();
|
||||||
~FormExistsFieldEntry();
|
~FormExistsFieldEntry();
|
||||||
|
|
||||||
std::string not_exists_error;
|
std::string not_exists_error;
|
||||||
};
|
};
|
||||||
|
|
||||||
class FormIntFieldEntry : public FormFieldEntry {
|
class FormIntFieldEntry : public FormFieldEntry {
|
||||||
@ -33,7 +33,7 @@ public:
|
|||||||
FormIntFieldEntry();
|
FormIntFieldEntry();
|
||||||
~FormIntFieldEntry();
|
~FormIntFieldEntry();
|
||||||
|
|
||||||
std::string not_int_error;
|
std::string not_int_error;
|
||||||
};
|
};
|
||||||
|
|
||||||
class FormFloatFieldEntry : public FormFieldEntry {
|
class FormFloatFieldEntry : public FormFieldEntry {
|
||||||
@ -43,7 +43,7 @@ public:
|
|||||||
FormFloatFieldEntry();
|
FormFloatFieldEntry();
|
||||||
~FormFloatFieldEntry();
|
~FormFloatFieldEntry();
|
||||||
|
|
||||||
std::string not_float_error;
|
std::string not_float_error;
|
||||||
};
|
};
|
||||||
|
|
||||||
class FormAlphaFieldEntry : public FormFieldEntry {
|
class FormAlphaFieldEntry : public FormFieldEntry {
|
||||||
@ -53,7 +53,7 @@ public:
|
|||||||
FormAlphaFieldEntry();
|
FormAlphaFieldEntry();
|
||||||
~FormAlphaFieldEntry();
|
~FormAlphaFieldEntry();
|
||||||
|
|
||||||
std::string not_alpha_error;
|
std::string not_alpha_error;
|
||||||
};
|
};
|
||||||
|
|
||||||
class FormAlphaNumericFieldEntry : public FormFieldEntry {
|
class FormAlphaNumericFieldEntry : public FormFieldEntry {
|
||||||
@ -63,7 +63,7 @@ public:
|
|||||||
FormAlphaNumericFieldEntry();
|
FormAlphaNumericFieldEntry();
|
||||||
~FormAlphaNumericFieldEntry();
|
~FormAlphaNumericFieldEntry();
|
||||||
|
|
||||||
std::string not_alpha_numeric_error;
|
std::string not_alpha_numeric_error;
|
||||||
};
|
};
|
||||||
|
|
||||||
class FormNeedsLowercaseCharacterFieldEntry : public FormFieldEntry {
|
class FormNeedsLowercaseCharacterFieldEntry : public FormFieldEntry {
|
||||||
@ -73,7 +73,7 @@ public:
|
|||||||
FormNeedsLowercaseCharacterFieldEntry();
|
FormNeedsLowercaseCharacterFieldEntry();
|
||||||
~FormNeedsLowercaseCharacterFieldEntry();
|
~FormNeedsLowercaseCharacterFieldEntry();
|
||||||
|
|
||||||
std::string does_not_have_lowercase_error;
|
std::string does_not_have_lowercase_error;
|
||||||
};
|
};
|
||||||
|
|
||||||
class FormNeedsUppercaseCharacterFieldEntry : public FormFieldEntry {
|
class FormNeedsUppercaseCharacterFieldEntry : public FormFieldEntry {
|
||||||
@ -83,7 +83,7 @@ public:
|
|||||||
FormNeedsUppercaseCharacterFieldEntry();
|
FormNeedsUppercaseCharacterFieldEntry();
|
||||||
~FormNeedsUppercaseCharacterFieldEntry();
|
~FormNeedsUppercaseCharacterFieldEntry();
|
||||||
|
|
||||||
std::string does_not_have_uppercase_error;
|
std::string does_not_have_uppercase_error;
|
||||||
};
|
};
|
||||||
|
|
||||||
class FormNeedsOtherCharacterFieldEntry : public FormFieldEntry {
|
class FormNeedsOtherCharacterFieldEntry : public FormFieldEntry {
|
||||||
@ -93,7 +93,7 @@ public:
|
|||||||
FormNeedsOtherCharacterFieldEntry();
|
FormNeedsOtherCharacterFieldEntry();
|
||||||
~FormNeedsOtherCharacterFieldEntry();
|
~FormNeedsOtherCharacterFieldEntry();
|
||||||
|
|
||||||
std::string does_not_have_other_error;
|
std::string does_not_have_other_error;
|
||||||
};
|
};
|
||||||
|
|
||||||
class FormMinimumLengthFieldEntry : public FormFieldEntry {
|
class FormMinimumLengthFieldEntry : public FormFieldEntry {
|
||||||
@ -103,10 +103,10 @@ public:
|
|||||||
FormMinimumLengthFieldEntry();
|
FormMinimumLengthFieldEntry();
|
||||||
~FormMinimumLengthFieldEntry();
|
~FormMinimumLengthFieldEntry();
|
||||||
|
|
||||||
int min_length;
|
int min_length;
|
||||||
|
|
||||||
std::string does_not_have_min_length_errorf;
|
std::string does_not_have_min_length_errorf;
|
||||||
std::string does_not_have_min_length_errors;
|
std::string does_not_have_min_length_errors;
|
||||||
};
|
};
|
||||||
|
|
||||||
class FormMaximumLengthFieldEntry : public FormFieldEntry {
|
class FormMaximumLengthFieldEntry : public FormFieldEntry {
|
||||||
@ -116,10 +116,10 @@ public:
|
|||||||
FormMaximumLengthFieldEntry();
|
FormMaximumLengthFieldEntry();
|
||||||
~FormMaximumLengthFieldEntry();
|
~FormMaximumLengthFieldEntry();
|
||||||
|
|
||||||
int max_length;
|
int max_length;
|
||||||
|
|
||||||
std::string does_not_have_max_length_errorf;
|
std::string does_not_have_max_length_errorf;
|
||||||
std::string does_not_have_max_length_errors;
|
std::string does_not_have_max_length_errors;
|
||||||
};
|
};
|
||||||
|
|
||||||
class FormEmailFieldEntry : public FormFieldEntry {
|
class FormEmailFieldEntry : public FormFieldEntry {
|
||||||
@ -129,7 +129,19 @@ public:
|
|||||||
FormEmailFieldEntry();
|
FormEmailFieldEntry();
|
||||||
~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
|
//FormField
|
||||||
@ -138,6 +150,19 @@ class FormField {
|
|||||||
public:
|
public:
|
||||||
std::string name;
|
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);
|
void add_entry(FormFieldEntry *field);
|
||||||
|
|
||||||
bool validate(Request *request, std::vector<std::string> *errors);
|
bool validate(Request *request, std::vector<std::string> *errors);
|
||||||
@ -148,11 +173,14 @@ public:
|
|||||||
std::vector<FormFieldEntry *> fields;
|
std::vector<FormFieldEntry *> fields;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
//FormValidator
|
||||||
|
|
||||||
class FormValidator {
|
class FormValidator {
|
||||||
public:
|
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);
|
void add_field(FormField *field);
|
||||||
|
FormField *new_field(const std::string &name);
|
||||||
|
|
||||||
FormValidator();
|
FormValidator();
|
||||||
virtual ~FormValidator();
|
virtual ~FormValidator();
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
#include "user.h"
|
#include "user.h"
|
||||||
|
|
||||||
#include "core/hash/sha256.h"
|
#include "core/hash/sha256.h"
|
||||||
|
#include "core/html/form_validator.h"
|
||||||
#include "core/html/html_builder.h"
|
#include "core/html/html_builder.h"
|
||||||
#include "core/http/cookie.h"
|
#include "core/http/cookie.h"
|
||||||
#include "core/http/http_session.h"
|
#include "core/http/http_session.h"
|
||||||
@ -387,6 +388,32 @@ void User::handle_delete_request(Request *request) {
|
|||||||
request->compile_and_send_body();
|
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() :
|
User::User() :
|
||||||
Object() {
|
Object() {
|
||||||
|
|
||||||
@ -394,6 +421,11 @@ User::User() :
|
|||||||
rank = 0;
|
rank = 0;
|
||||||
banned = false;
|
banned = false;
|
||||||
locked = false;
|
locked = false;
|
||||||
|
|
||||||
|
_login_validator = nullptr;
|
||||||
|
_registration_validator = nullptr;
|
||||||
|
|
||||||
|
create_validators();
|
||||||
}
|
}
|
||||||
|
|
||||||
User::~User() {
|
User::~User() {
|
||||||
|
@ -7,6 +7,7 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
class Request;
|
class Request;
|
||||||
|
class FormValidator;
|
||||||
|
|
||||||
class User : public Object {
|
class User : public Object {
|
||||||
RCPP_OBJECT(User, Object);
|
RCPP_OBJECT(User, Object);
|
||||||
@ -47,11 +48,16 @@ public:
|
|||||||
virtual void handle_logout_request(Request *request);
|
virtual void handle_logout_request(Request *request);
|
||||||
virtual void handle_delete_request(Request *request);
|
virtual void handle_delete_request(Request *request);
|
||||||
|
|
||||||
|
virtual void create_validators();
|
||||||
|
|
||||||
void register_sessions();
|
void register_sessions();
|
||||||
void unregister_sessions();
|
void unregister_sessions();
|
||||||
|
|
||||||
User();
|
User();
|
||||||
~User();
|
~User();
|
||||||
|
|
||||||
|
FormValidator *_login_validator;
|
||||||
|
FormValidator *_registration_validator;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
Loading…
Reference in New Issue
Block a user