Started porting my old RBAC code to a new module.

This commit is contained in:
Relintai 2021-10-31 21:45:12 +01:00
parent 304a2a19c9
commit 4c5c439faf
10 changed files with 268 additions and 0 deletions

12
modules/rbac/SCsub Normal file
View File

@ -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])

27
modules/rbac/detect.py Normal file
View File

@ -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

View File

@ -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;

View File

@ -0,0 +1,27 @@
#ifndef RBAC_CONTROLLER_H
#define RBAC_CONTROLLER_H
#include "core/http/controller.h"
#include <string>
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

View File

@ -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<TableBuilder> 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<TableBuilder> 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;

29
modules/rbac/rbac_model.h Normal file
View File

@ -0,0 +1,29 @@
#ifndef RBAC_MODEL_H
#define RBAC_MODEL_H
#include "core/http/model.h"
#include <string>
#include <vector>
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

View File

@ -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() {
}

View File

@ -0,0 +1,25 @@
#ifndef RBAC_PERMISSION_H
#define RBAC_PERMISSION_H
#include "core/resource.h"
#include <string>
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

View File

@ -0,0 +1,11 @@
#include "rbac_rank.h"
RBACRank::RBACRank() :
Resource() {
id = 0;
}
RBACRank::~RBACRank() {
groups.clear();
}

27
modules/rbac/rbac_rank.h Normal file
View File

@ -0,0 +1,27 @@
#ifndef RBAC_RANK_H
#define RBAC_RANK_H
#include "core/resource.h"
#include <string>
#include <vector>
#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<Ref<RBACPermission> > groups;
RBACRank();
~RBACRank();
};
#endif