Added a new UserController and added the new render methods from the core USerController.

This commit is contained in:
Relintai 2021-10-31 02:13:01 +02:00
parent 71d0366681
commit 5b31cb7df8
4 changed files with 208 additions and 3 deletions

2
HEADS
View File

@ -1 +1 @@
{"engine": {"master": "6aae520ed45117c7ad956b2c8cfbd809d1ac8e8c"}}
{"engine": {"master": "594ab744c52b4697464a49bf09f9895d97eb8723"}}

View File

@ -0,0 +1,182 @@
#include "mourne_user_controller.h"
#include "core/html/form_validator.h"
#include "core/html/html_builder.h"
#include "core/http/cookie.h"
#include "core/http/http_session.h"
#include "core/http/request.h"
#include "core/http/session_manager.h"
#include "modules/users/user_model.h"
void MourneUserController::render_login_request_default(Request *request, LoginRequestData *data) {
HTMLBuilder b;
b.w("Login");
b.br();
{
if (data->error_str.size() != 0) {
b.div()->cls("error");
b.w(data->error_str);
b.cdiv();
}
}
b.div()->cls("login");
{
//todo href path helper
b.form()->method("POST")->href("/user/login");
{
b.w("Username");
b.br();
b.input()->type("text")->name("username")->value(data->uname_val);
b.cinput();
b.br();
b.w("Password");
b.br();
b.input()->type("password")->name("password");
b.cinput();
b.br();
b.input()->type("submit")->value("Send");
b.cinput();
}
b.cform();
}
b.cdiv();
request->body += b.result;
request->compile_and_send_body();
}
void MourneUserController::render_register_request_default(Request *request, RegisterRequestData *data) {
HTMLBuilder b;
b.w("Registration");
b.br();
{
if (data->error_str.size() != 0) {
b.div()->cls("error");
b.w(data->error_str);
b.cdiv();
}
}
b.div()->cls("register");
{
//todo href path helper
b.form()->method("POST")->href("/user/register");
{
b.w("Username");
b.br();
b.input()->type("text")->name("username")->value(data->uname_val);
b.cinput();
b.br();
b.w("Email");
b.br();
b.input()->type("email")->name("email")->value(data->email_val);
b.cinput();
b.br();
b.w("Password");
b.br();
b.input()->type("password")->name("password");
b.cinput();
b.br();
b.w("Password again");
b.br();
b.input()->type("password")->name("password_check");
b.cinput();
b.br();
b.input()->type("submit")->value("Register");
b.cinput();
}
b.cform();
}
b.cdiv();
request->body += b.result;
request->compile_and_send_body();
}
void MourneUserController::render_already_logged_in_error(Request *request) {
request->body += "You are already logged in.";
request->compile_and_send_body();
}
void MourneUserController::render_settings_request(Ref<User> &user, Request *request, SettingsRequestData *data) {
HTMLBuilder b;
b.w("Settings");
b.br();
{
if (data->error_str.size() != 0) {
b.div()->cls("error");
b.w(data->error_str);
b.cdiv();
}
}
b.div()->cls("settings");
{
//todo href path helper
b.form()->method("POST")->href("/user/settings");
{
b.w("Username");
b.br();
b.input()->type("text")->name("username")->placeholder(user->name_user_input)->value(data->uname_val);
b.cinput();
b.br();
b.w("Email");
b.br();
b.input()->type("email")->name("email")->placeholder(user->email_user_input)->value(data->email_val);
b.cinput();
b.br();
b.w("Password");
b.br();
b.input()->type("password")->placeholder("*******")->name("password");
b.cinput();
b.br();
b.w("Password again");
b.br();
b.input()->type("password")->placeholder("*******")->name("password_check");
b.cinput();
b.br();
b.input()->type("submit")->value("Save");
b.cinput();
}
b.cform();
}
b.cdiv();
request->body += b.result;
request->compile_and_send_body();
}
MourneUserController::MourneUserController() :
UserController() {
}
MourneUserController::~MourneUserController() {
}

View File

@ -0,0 +1,23 @@
#ifndef MOURNE_USER_CONTROLLER_H
#define MOURNE_USER_CONTROLLER_H
#include "modules/users/user_controller.h"
#include <string>
#include "modules/users/user.h"
class Request;
class FormValidator;
class MourneUserController : public UserController {
public:
void render_login_request_default(Request *request, LoginRequestData *data);
void render_register_request_default(Request *request, RegisterRequestData *data);
void render_already_logged_in_error(Request *request);
void render_settings_request(Ref<User> &user, Request *request, SettingsRequestData *data);
MourneUserController();
~MourneUserController();
};
#endif

View File

@ -22,7 +22,7 @@
#include "backends/hash_hashlib/setup.h"
#include "modules/users/user.h"
#include "modules/users/user_controller.h"
#include "app/mourne_user_controller.h"
#include "modules/users/user_model.h"
#include "core/database/database_manager.h"
@ -56,7 +56,7 @@ int main(int argc, char **argv) {
::SessionManager *session_manager = new ::SessionManager();
//todo init these in the module automatically
UserController *user_controller = new UserController();
UserController *user_controller = new MourneUserController();
UserModel *user_model = new UserModel();
//user_manager->set_path("./users/");