Implement the rbac rank editor.

This commit is contained in:
Relintai 2021-11-01 23:48:25 +01:00
parent 7fecc3e9ff
commit a330d0f51a
7 changed files with 156 additions and 17 deletions

View File

@ -54,6 +54,12 @@ HTMLTag *HTMLTag::method(const String &val) {
return this;
}
HTMLTag *HTMLTag::action(const String &val) {
attrib("action", val);
return this;
}
HTMLTag *HTMLTag::type(const String &val) {
attrib("type", val);

View File

@ -20,6 +20,7 @@ public:
HTMLTag *content(const String &val);
HTMLTag *value(const String &val);
HTMLTag *method(const String &val);
HTMLTag *action(const String &val);
HTMLTag *type(const String &val);
HTMLTag *placeholder(const String &val);
HTMLTag *rel(const String &val);

View File

@ -544,19 +544,19 @@ void String::append_str(const std::string &str) {
_data[_size] = '\0';
}
float String::to_float() {
float String::to_float() const {
return atof(c_str());
}
double String::to_double() {
double String::to_double() const {
return atof(c_str());
}
int String::to_int() {
int String::to_int() const {
return atoi(c_str());
}
uint32_t String::to_uint() {
uint32_t String::to_uint() const {
return static_cast<uint32_t>(atoll(c_str()));
}

View File

@ -62,10 +62,10 @@ public:
void append_str(const String &other);
void append_str(const std::string &str);
float to_float();
double to_double();
int to_int();
uint32_t to_uint();
float to_float() const;
double to_double() const;
int to_int() const;
uint32_t to_uint() const;
std::string to_string() const;
void print() const;

View File

@ -1,5 +1,7 @@
#include "rbac_controller.h"
#include "core/error_macros.h"
#include "core/html/form_validator.h"
#include "core/html/html_builder.h"
#include "core/http/cookie.h"
@ -20,15 +22,129 @@ void RBACController::admin_handle_request_main(Request *request) {
if (seg == "") {
admin_render_rank_list(request);
return;
} else if (seg == "new_rank") {
request->push_path();
admin_handle_new_rank(request);
} else if (seg == "edit_rank") {
request->push_path();
admin_handle_edit_rank(request);
}
}
void RBACController::admin_handle_new_rank(Request *request) {
if (request->get_method() == HTTP_METHOD_POST) {
Ref<RBACRank> rank;
rank.instance();
rank->name = request->get_parameter("name");
rank->name_internal = request->get_parameter("name_internal");
rank->settings = request->get_parameter("settings");
rank->rank_permissions = request->get_parameter("rank_permissions").to_int();
RBACModel::get_singleton()->save_rank(rank);
_permissions[rank->id] = rank;
request->send_redirect(request->get_url_root_parent() + "edit_rank/" + String::num(rank->id));
return;
}
RBACAdminRankViewData data;
render_rank_view(request, &data);
}
void RBACController::admin_handle_edit_rank(Request *request) {
String seg = request->get_current_path_segment();
//check whether it's numeric
//if (!seg.is)
int id = seg.to_int();
if (id == 0) {
RLOG_MSG("RBACController::admin_handle_edit_rank: id == 0!\n");
request->send_redirect(request->get_url_root_parent());
return;
}
Ref<RBACRank> rank = _permissions[id];
if (!rank.is_valid()) {
RLOG_MSG("RBACController::admin_handle_edit_rank: !rank.is_valid()\n");
request->send_redirect(request->get_url_root_parent());
return;
}
RBACAdminRankViewData data;
data.rank = rank;
if (request->get_method() == HTTP_METHOD_POST) {
rank->name = request->get_parameter("name");
rank->name_internal = request->get_parameter("name_internal");
rank->settings = request->get_parameter("settings");
rank->rank_permissions = request->get_parameter("rank_permissions").to_int();
RBACModel::get_singleton()->save_rank(rank);
data.messages.push_back("Save Success!");
}
render_rank_view(request, &data);
}
void RBACController::render_rank_view(Request *request, RBACAdminRankViewData *data) {
int id = 0;
String name = "";
String name_internal = "";
String settings = "";
int rank_permissions = 0;
if (data->rank.is_valid()) {
id = data->rank->id;
name = data->rank->name;
name_internal = data->rank->name_internal;
settings = data->rank->settings;
rank_permissions = data->rank->rank_permissions;
}
HTMLBuilder b;
b.h4()->f()->a()->href(request->get_url_root_parent())->f()->w("<- Back")->ca()->ch4();
b.h4()->f()->w("RBAC Editor")->ch4();
b.div()->cls("messages");
for (int i = 0; i < data->messages.size(); ++i) {
b.w(data->messages[i])->br();
}
b.cdiv();
b.form()->method("POST")->action(request->get_url_root() + String::num(id));
{
//b.input()->type("hidden")->name("id")->value(String::num(id))->f()->cinput();
b.w("Name:")->br();
b.input()->type("text")->name("name")->value(name)->f()->br();
b.w("Name (Internal):")->br();
b.input()->type("text")->name("name_internal")->value(name_internal)->f()->cinput()->br();
b.w("Custom Settings:")->br();
b.input()->type("text")->name("settings")->value(settings)->f()->cinput()->br();
//todo rank_permissions (checkboxes + register api)
b.input()->type("submit")->value("Save");
}
b.cform();
request->body += b.result;
}
void RBACController::admin_render_rank_list(Request *request) {
HTMLBuilder b;
b.h3()->f()->w("RBAC Editor")->ch3();
b.h4()->f()->a()->href(request->get_url_root_parent())->f()->w("<- Back")->ca()->ch4();
b.h4()->f()->w("RBAC Editor")->ch4();
for (std::map<int, Ref<RBACRank> >::iterator p = _permissions.begin(); p != _permissions.end(); p++) {
Ref<RBACRank> r = p->second;
@ -40,16 +156,19 @@ void RBACController::admin_render_rank_list(Request *request) {
b.div()->cls("row");
{
b.a()->href(request->get_url_root("edit_permissions/") + String::num(r->id));
b.w("Id: ")->w(String::num(r->id))->w(", Name: ")->w(r->name)->w(", Name Internal: ")->w(r->name_internal);
b.w("[ Id ]: ")->wn(r->id)->w(", [ Name ]: ")->w(r->name)->w(", [ Name Internal ]: ")->w(r->name_internal);
b.ca();
b.w(" - ");
b.a()->href(request->get_url_root("edit_rank/") + String::num(r->id));
b.w("Edit Names");
b.w("[ Edit Names ]");
b.ca();
}
b.cdiv();
}
b.br();
b.a()->href(request->get_url_root("new_rank"));
b.w("New Rank");
b.ca();
@ -58,10 +177,8 @@ void RBACController::admin_render_rank_list(Request *request) {
}
void RBACController::admin_render_rank_editor(Request *request) {
}
String RBACController::admin_get_section_name() {
return "Role Based Access Control";
}

View File

@ -16,7 +16,7 @@ class FormValidator;
class RBACController : public AdminController {
RCPP_OBJECT(RBACController, AdminController);
public:
void handle_request_main(Request *request);
void create_validators();
@ -25,6 +25,21 @@ public:
String admin_get_section_name();
void admin_add_section_links(Vector<AdminSectionLinkInfo> *links);
struct RBACAdminRankViewData {
Ref<RBACRank> rank;
Vector<String> messages;
int id = 0;
String name = "";
String name_internal = "";
String settings = "";
int rank_permissions = 0;
};
void admin_handle_new_rank(Request *request);
void admin_handle_edit_rank(Request *request);
void render_rank_view(Request *request, RBACAdminRankViewData *data);
void admin_render_rank_list(Request *request);
void admin_render_rank_editor(Request *request);

View File

@ -62,7 +62,7 @@ void RBACModel::save_rank(const Ref<RBACRank> &rank) {
qb->cvalues();
qb->select_last_insert_id();
Ref<QueryResult> res = qb->run();
qb->print();
//qb->print();
Ref<RBACRank> r = rank;
@ -77,7 +77,7 @@ void RBACModel::save_rank(const Ref<RBACRank> &rank) {
qb->where()->wp("id", rank->id);
qb->end_command();
qb->run_query();
qb->print();
//qb->print();
}
}