From 4c5c439faf803a073e56c6805009cc4da2d9901e Mon Sep 17 00:00:00 2001 From: Relintai Date: Sun, 31 Oct 2021 21:45:12 +0100 Subject: [PATCH] Started porting my old RBAC code to a new module. --- modules/rbac/SCsub | 12 +++++++ modules/rbac/detect.py | 27 +++++++++++++++ modules/rbac/rbac_controller.cpp | 37 ++++++++++++++++++++ modules/rbac/rbac_controller.h | 27 +++++++++++++++ modules/rbac/rbac_model.cpp | 58 ++++++++++++++++++++++++++++++++ modules/rbac/rbac_model.h | 29 ++++++++++++++++ modules/rbac/rbac_permission.cpp | 15 +++++++++ modules/rbac/rbac_permission.h | 25 ++++++++++++++ modules/rbac/rbac_rank.cpp | 11 ++++++ modules/rbac/rbac_rank.h | 27 +++++++++++++++ 10 files changed, 268 insertions(+) create mode 100644 modules/rbac/SCsub create mode 100644 modules/rbac/detect.py create mode 100644 modules/rbac/rbac_controller.cpp create mode 100644 modules/rbac/rbac_controller.h create mode 100644 modules/rbac/rbac_model.cpp create mode 100644 modules/rbac/rbac_model.h create mode 100644 modules/rbac/rbac_permission.cpp create mode 100644 modules/rbac/rbac_permission.h create mode 100644 modules/rbac/rbac_rank.cpp create mode 100644 modules/rbac/rbac_rank.h diff --git a/modules/rbac/SCsub b/modules/rbac/SCsub new file mode 100644 index 0000000..33e2978 --- /dev/null +++ b/modules/rbac/SCsub @@ -0,0 +1,12 @@ +#!/usr/bin/env python + +Import("env_mod") +Import("env") + +env_mod.core_sources = [] + +env_mod.add_source_files(env_mod.core_sources, "*.cpp") + +# Build it all as a library +lib = env_mod.add_library("users", env_mod.core_sources) +env.Prepend(LIBS=[lib]) diff --git a/modules/rbac/detect.py b/modules/rbac/detect.py new file mode 100644 index 0000000..d35fcab --- /dev/null +++ b/modules/rbac/detect.py @@ -0,0 +1,27 @@ +import os +import platform +import sys + + +def is_active(): + return True + + +def get_name(): + return "users" + + +def can_build(): + return True + + +def get_opts(): + return [] + +def get_flags(): + + return [] + + +def configure(env): + pass diff --git a/modules/rbac/rbac_controller.cpp b/modules/rbac/rbac_controller.cpp new file mode 100644 index 0000000..84f6ba0 --- /dev/null +++ b/modules/rbac/rbac_controller.cpp @@ -0,0 +1,37 @@ +#include "rbac_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 "rbac_model.h" + +void RBACController::handle_request_main(Request *request) { +} + +void RBACController::create_validators() { +} + +RBACController *RBACController::get_singleton() { + return _self; +} + +RBACController::RBACController() : + Controller() { + + if (_self) { + printf("RBACController::RBACController(): Error! self is not null!/n"); + } + + _self = this; +} + +RBACController::~RBACController() { + if (_self == this) { + _self = nullptr; + } +} + +RBACController *RBACController::_self = nullptr; diff --git a/modules/rbac/rbac_controller.h b/modules/rbac/rbac_controller.h new file mode 100644 index 0000000..b6a5656 --- /dev/null +++ b/modules/rbac/rbac_controller.h @@ -0,0 +1,27 @@ +#ifndef RBAC_CONTROLLER_H +#define RBAC_CONTROLLER_H + +#include "core/http/controller.h" + +#include + +class Request; +class FormValidator; + +class RBACController : public Controller { + RCPP_OBJECT(RBACController, Controller); + +public: + void handle_request_main(Request *request); + void create_validators(); + + static RBACController *get_singleton(); + + RBACController(); + ~RBACController(); + +protected: + static RBACController *_self; +}; + +#endif \ No newline at end of file diff --git a/modules/rbac/rbac_model.cpp b/modules/rbac/rbac_model.cpp new file mode 100644 index 0000000..da4f58c --- /dev/null +++ b/modules/rbac/rbac_model.cpp @@ -0,0 +1,58 @@ +#include "rbac_model.h" + +#include "core/database/database.h" +#include "core/database/database_manager.h" +#include "core/database/query_builder.h" +#include "core/database/query_result.h" +#include "core/database/table_builder.h" + +void RBACModel::create_table() { + Ref tb = DatabaseManager::get_singleton()->ddb->get_table_builder(); + + tb->create_table(_table_name); + tb->integer("id")->auto_increment()->next_row(); + tb->varchar("username", 60)->not_null()->next_row(); + tb->varchar("email", 100)->not_null()->next_row(); + tb->integer("rank")->not_null()->next_row(); + tb->varchar("pre_salt", 100)->next_row(); + tb->varchar("post_salt", 100)->next_row(); + tb->varchar("password_hash", 100)->next_row(); + tb->integer("banned")->next_row(); + tb->varchar("password_reset_token", 100)->next_row(); + tb->integer("locked")->next_row(); + tb->primary_key("id"); + tb->ccreate_table(); + tb->run_query(); + //tb->print(); +} +void RBACModel::drop_table() { + Ref tb = DatabaseManager::get_singleton()->ddb->get_table_builder(); + + tb->drop_table_if_exists(_table_name)->run_query(); +} +void RBACModel::migrate() { + drop_table(); + create_table(); +} + +RBACModel *RBACModel::get_singleton() { + return _self; +} + +RBACModel::RBACModel() : + Model() { + + if (_self) { + printf("RBACModel::RBACModel(): Error! self is not null!/n"); + } + + _self = this; +} + +RBACModel::~RBACModel() { + if (_self == this) { + _self = nullptr; + } +} + +RBACModel *RBACModel::_self = nullptr; diff --git a/modules/rbac/rbac_model.h b/modules/rbac/rbac_model.h new file mode 100644 index 0000000..21f8836 --- /dev/null +++ b/modules/rbac/rbac_model.h @@ -0,0 +1,29 @@ +#ifndef RBAC_MODEL_H +#define RBAC_MODEL_H + +#include "core/http/model.h" + +#include +#include + +class RBACModel : public Model { + RCPP_OBJECT(RBACModel, Model); + +public: + + void create_table(); + void drop_table(); + void migrate(); + + static RBACModel *get_singleton(); + + RBACModel(); + ~RBACModel(); + +protected: + static RBACModel *_self; + + std::string _table_name; +}; + +#endif \ No newline at end of file diff --git a/modules/rbac/rbac_permission.cpp b/modules/rbac/rbac_permission.cpp new file mode 100644 index 0000000..7f335d3 --- /dev/null +++ b/modules/rbac/rbac_permission.cpp @@ -0,0 +1,15 @@ +#include "rbac_permission.h" + +RBACPermission::RBACPermission() : + Resource() { + + id = 0; + rank_id = 0; + permission_id = 0; + revoke = false; + sort_order = 0; + permissions = 0; +} + +RBACPermission::~RBACPermission() { +} diff --git a/modules/rbac/rbac_permission.h b/modules/rbac/rbac_permission.h new file mode 100644 index 0000000..39d222d --- /dev/null +++ b/modules/rbac/rbac_permission.h @@ -0,0 +1,25 @@ +#ifndef RBAC_PERMISSION_H +#define RBAC_PERMISSION_H + +#include "core/resource.h" + +#include + +class RBACPermission : public Resource { + RCPP_OBJECT(RBACPermission, Resource); + +public: + int id; + int rank_id; + int permission_id; + std::string name; + std::string url; + bool revoke; + int sort_order; + int permissions; + + RBACPermission(); + ~RBACPermission(); +}; + +#endif \ No newline at end of file diff --git a/modules/rbac/rbac_rank.cpp b/modules/rbac/rbac_rank.cpp new file mode 100644 index 0000000..67ce5c9 --- /dev/null +++ b/modules/rbac/rbac_rank.cpp @@ -0,0 +1,11 @@ +#include "rbac_rank.h" + +RBACRank::RBACRank() : + Resource() { + + id = 0; +} + +RBACRank::~RBACRank() { + groups.clear(); +} diff --git a/modules/rbac/rbac_rank.h b/modules/rbac/rbac_rank.h new file mode 100644 index 0000000..75839c2 --- /dev/null +++ b/modules/rbac/rbac_rank.h @@ -0,0 +1,27 @@ +#ifndef RBAC_RANK_H +#define RBAC_RANK_H + +#include "core/resource.h" + +#include +#include + +#include "rbac_permission.h" + +class RBACRank : public Resource { + RCPP_OBJECT(RBACRank, Resource); + +public: + int id; + + std::string name; + std::string name_internal; + std::string settings; + + std::vector > groups; + + RBACRank(); + ~RBACRank(); +}; + +#endif \ No newline at end of file