From 8e624d8b5a13c48f2051392ecd1b0407a80e5931 Mon Sep 17 00:00:00 2001 From: Relintai Date: Mon, 1 Nov 2021 16:39:30 +0100 Subject: [PATCH] Initial rbac model setup. --- modules/rbac/rbac_controller.cpp | 6 ++- modules/rbac/rbac_controller.h | 12 ++++- modules/rbac/rbac_model.cpp | 76 ++++++++++++++++++++++++++++++++ modules/rbac/rbac_model.h | 15 +++++-- 4 files changed, 103 insertions(+), 6 deletions(-) diff --git a/modules/rbac/rbac_controller.cpp b/modules/rbac/rbac_controller.cpp index 52e9931..f30b59c 100644 --- a/modules/rbac/rbac_controller.cpp +++ b/modules/rbac/rbac_controller.cpp @@ -15,13 +15,17 @@ void RBACController::create_validators() { } void RBACController::admin_handle_request_main(Request *request) { - + } void RBACController::admin_add_section_links(Vector *links) { links->push_back(AdminSectionLinkInfo("Test", "test")); } +void RBACController::initialize() { + _permissions = RBACModel::get_singleton()->load_permissions(); +} + RBACController *RBACController::get_singleton() { return _self; } diff --git a/modules/rbac/rbac_controller.h b/modules/rbac/rbac_controller.h index b057de1..0400b18 100644 --- a/modules/rbac/rbac_controller.h +++ b/modules/rbac/rbac_controller.h @@ -1,9 +1,15 @@ #ifndef RBAC_CONTROLLER_H #define RBAC_CONTROLLER_H +#include + #include "modules/admin_panel/admin_controller.h" -#include +#include "core/containers/vector.h" +#include "core/string.h" + +#include "rbac_permission.h" +#include "rbac_rank.h" class Request; class FormValidator; @@ -18,6 +24,8 @@ public: void admin_handle_request_main(Request *request); void admin_add_section_links(Vector *links); + void initialize(); + static RBACController *get_singleton(); RBACController(); @@ -25,6 +33,8 @@ public: protected: static RBACController *_self; + + std::map > _permissions; }; #endif \ No newline at end of file diff --git a/modules/rbac/rbac_model.cpp b/modules/rbac/rbac_model.cpp index dd1bf4b..afe8481 100644 --- a/modules/rbac/rbac_model.cpp +++ b/modules/rbac/rbac_model.cpp @@ -9,6 +9,82 @@ #define RBAC_RANK_TABLE "rbac_ranks" #define RBAC_PERMISSION_TABLE "rbac_permissions" +std::map > RBACModel::load_permissions() { + std::map > ranks; + + Ref qb = DatabaseManager::get_singleton()->ddb->get_query_builder(); + + qb->select("id,name,name_internal,settings,rank_permissions")->from(RBAC_RANK_TABLE); + Ref res = qb->run(); + + while (res->next_row()) { + Ref r; + r.instance(); + + r->id = res->get_cell_int(0); + r->name = res->get_cell_str(1); + r->name_internal = res->get_cell_str(2); + r->settings = res->get_cell_str(3); + r->rank_permissions = res->get_cell_int(4); + + ranks[r->id] = r; + } + +/* + qb->reset(); + qb->select("id,name,name_internal,settings,rank_permissions")->from(RBAC_PERMISSION_TABLE); + res = qb->run(); +*/ + + return ranks; +} + +void RBACModel::save(const Ref &rank) { + save_rank(rank); + + for (int i = 0; i < rank->permissions.size(); ++i) { + Ref permission = rank->permissions[i]; + + if (permission->rank_id == 0) { + permission->rank_id = rank->id; + } + + save_permission(permission); + } +} + +void RBACModel::save_rank(const Ref &rank) { + Ref qb = DatabaseManager::get_singleton()->ddb->get_query_builder(); + + if (rank->id == 0) { + qb->insert(RBAC_RANK_TABLE, "name,name_internal,settings,rank_permissions")->values(); + qb->eval(rank->name)->eval(rank->name_internal)->eval(rank->settings)->val(rank->rank_permissions); + qb->cvalues(); + qb->select_last_insert_id(); + Ref res = qb->run(); + qb->print(); + + Ref r = rank; + + r->id = res->get_last_insert_rowid(); + } else { + qb->udpate(RBAC_RANK_TABLE)->set(); + qb->esetp("name", rank->name); + qb->esetp("name_internal", rank->name_internal); + qb->esetp("settings", rank->settings); + qb->setp("rank_permissions", rank->rank_permissions); + qb->cset(); + qb->where()->wp("id", rank->id); + qb->end_command(); + qb->run_query(); + qb->print(); + } +} + +void RBACModel::save_permission(const Ref &permission) { + +} + void RBACModel::create_table() { Ref tb = DatabaseManager::get_singleton()->ddb->get_table_builder(); diff --git a/modules/rbac/rbac_model.h b/modules/rbac/rbac_model.h index 21f8836..2a9cc48 100644 --- a/modules/rbac/rbac_model.h +++ b/modules/rbac/rbac_model.h @@ -3,13 +3,22 @@ #include "core/http/model.h" -#include -#include +#include +#include "core/containers/vector.h" +#include "core/string.h" + +#include "rbac_permission.h" +#include "rbac_rank.h" class RBACModel : public Model { RCPP_OBJECT(RBACModel, Model); public: + virtual std::map > load_permissions(); + + virtual void save(const Ref &rank); + virtual void save_rank(const Ref &rank); + virtual void save_permission(const Ref &permission); void create_table(); void drop_table(); @@ -22,8 +31,6 @@ public: protected: static RBACModel *_self; - - std::string _table_name; }; #endif \ No newline at end of file