Initial rbac model setup.

This commit is contained in:
Relintai 2021-11-01 16:39:30 +01:00
parent 32d370bfa3
commit 8e624d8b5a
4 changed files with 103 additions and 6 deletions

View File

@ -15,13 +15,17 @@ void RBACController::create_validators() {
} }
void RBACController::admin_handle_request_main(Request *request) { void RBACController::admin_handle_request_main(Request *request) {
} }
void RBACController::admin_add_section_links(Vector<AdminSectionLinkInfo> *links) { void RBACController::admin_add_section_links(Vector<AdminSectionLinkInfo> *links) {
links->push_back(AdminSectionLinkInfo("Test", "test")); links->push_back(AdminSectionLinkInfo("Test", "test"));
} }
void RBACController::initialize() {
_permissions = RBACModel::get_singleton()->load_permissions();
}
RBACController *RBACController::get_singleton() { RBACController *RBACController::get_singleton() {
return _self; return _self;
} }

View File

@ -1,9 +1,15 @@
#ifndef RBAC_CONTROLLER_H #ifndef RBAC_CONTROLLER_H
#define RBAC_CONTROLLER_H #define RBAC_CONTROLLER_H
#include <map>
#include "modules/admin_panel/admin_controller.h" #include "modules/admin_panel/admin_controller.h"
#include <string> #include "core/containers/vector.h"
#include "core/string.h"
#include "rbac_permission.h"
#include "rbac_rank.h"
class Request; class Request;
class FormValidator; class FormValidator;
@ -18,6 +24,8 @@ public:
void admin_handle_request_main(Request *request); void admin_handle_request_main(Request *request);
void admin_add_section_links(Vector<AdminSectionLinkInfo> *links); void admin_add_section_links(Vector<AdminSectionLinkInfo> *links);
void initialize();
static RBACController *get_singleton(); static RBACController *get_singleton();
RBACController(); RBACController();
@ -25,6 +33,8 @@ public:
protected: protected:
static RBACController *_self; static RBACController *_self;
std::map<int, Ref<RBACRank> > _permissions;
}; };
#endif #endif

View File

@ -9,6 +9,82 @@
#define RBAC_RANK_TABLE "rbac_ranks" #define RBAC_RANK_TABLE "rbac_ranks"
#define RBAC_PERMISSION_TABLE "rbac_permissions" #define RBAC_PERMISSION_TABLE "rbac_permissions"
std::map<int, Ref<RBACRank> > RBACModel::load_permissions() {
std::map<int, Ref<RBACRank> > ranks;
Ref<QueryBuilder> qb = DatabaseManager::get_singleton()->ddb->get_query_builder();
qb->select("id,name,name_internal,settings,rank_permissions")->from(RBAC_RANK_TABLE);
Ref<QueryResult> res = qb->run();
while (res->next_row()) {
Ref<RBACRank> 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<RBACRank> &rank) {
save_rank(rank);
for (int i = 0; i < rank->permissions.size(); ++i) {
Ref<RBACPermission> permission = rank->permissions[i];
if (permission->rank_id == 0) {
permission->rank_id = rank->id;
}
save_permission(permission);
}
}
void RBACModel::save_rank(const Ref<RBACRank> &rank) {
Ref<QueryBuilder> 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<QueryResult> res = qb->run();
qb->print();
Ref<RBACRank> 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<RBACPermission> &permission) {
}
void RBACModel::create_table() { void RBACModel::create_table() {
Ref<TableBuilder> tb = DatabaseManager::get_singleton()->ddb->get_table_builder(); Ref<TableBuilder> tb = DatabaseManager::get_singleton()->ddb->get_table_builder();

View File

@ -3,13 +3,22 @@
#include "core/http/model.h" #include "core/http/model.h"
#include <string> #include <map>
#include <vector> #include "core/containers/vector.h"
#include "core/string.h"
#include "rbac_permission.h"
#include "rbac_rank.h"
class RBACModel : public Model { class RBACModel : public Model {
RCPP_OBJECT(RBACModel, Model); RCPP_OBJECT(RBACModel, Model);
public: public:
virtual std::map<int, Ref<RBACRank> > load_permissions();
virtual void save(const Ref<RBACRank> &rank);
virtual void save_rank(const Ref<RBACRank> &rank);
virtual void save_permission(const Ref<RBACPermission> &permission);
void create_table(); void create_table();
void drop_table(); void drop_table();
@ -22,8 +31,6 @@ public:
protected: protected:
static RBACModel *_self; static RBACModel *_self;
std::string _table_name;
}; };
#endif #endif