Skip to content
This repository has been archived by the owner on Sep 27, 2024. It is now read-only.

Commit

Permalink
chore: move header files
Browse files Browse the repository at this point in the history
  • Loading branch information
engsr6982 committed Mar 19, 2024
1 parent f5ba3ca commit fd0927b
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 69 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,38 @@
#include "db/db.h"
#pragma once

#include "entry/Macros.h"
#include "permission/struct.h"
#include <memory>
#include <optional>
#include <string>
#include <unordered_map>
#include <vector>


namespace perm {

using string = std::string;

struct PERMISSION_CORE_API UserGroup {
std::string groupName;
std::vector<std::string> authority;
std::vector<std::string> user;
};
struct PERMISSION_CORE_API PluginPermData {
std::vector<std::string> admin;
std::vector<UserGroup> user;
std::vector<std::string> publicAuthority;
};
struct PERMISSION_CORE_API GetUserGroupStruct {
const int index;
const UserGroup data;

GetUserGroupStruct(int idx, const UserGroup& grp) : index(idx), data(grp) {}
};
struct PERMISSION_CORE_API GetUserPermissionsStruct {
std::unordered_map<string, std::vector<string>> source;
std::vector<string> authority;
};

class PERMISSION_CORE_API PermissionCore {
public:
PermissionCore(string pluginName, bool enablePublicGroups);
Expand All @@ -26,9 +49,9 @@ class PERMISSION_CORE_API PermissionCore {
//! user
bool hasUserGroup(const std::string& name);

const std::optional<perm::structs::GetUserGroupStruct> getUserGroup(const string& name);
const std::optional<GetUserGroupStruct> getUserGroup(const string& name);

const std::vector<perm::structs::UserGroup>& getAllUserGroups();
const std::vector<UserGroup>& getAllUserGroups();

bool createUserGroup(const std::string& name);

Expand All @@ -48,9 +71,9 @@ class PERMISSION_CORE_API PermissionCore {

bool removeUserToUserGroup(const std::string& name, const std::string& userid);

const std::vector<perm::structs::UserGroup> getUserGroupsOfUser(const string& userid);
const std::vector<UserGroup> getUserGroupsOfUser(const string& userid);

const std::optional<perm::structs::GetUserPermissionsStruct> getUserPermissionOfUserData(const string& userid);
const std::optional<GetUserPermissionsStruct> getUserPermissionOfUserData(const string& userid);

//! public
const std::vector<std::string>& getPublicGroupAllPermissions();
Expand All @@ -70,7 +93,7 @@ class PERMISSION_CORE_API PermissionCore {
static bool validateName(const std::string& name);

private:
std::unique_ptr<perm::structs::PluginPermData> data;
std::unique_ptr<PluginPermData> data;

bool enablePublicGroups;
string pluginName;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#pragma once
#include "entry/Macros.h"
#include <optional>
#include <string>
Expand Down
2 changes: 2 additions & 0 deletions include/include_all.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#include "PermissionCore/PermissionCore/PermissionCore.h"
#include "PermissionCore/Registers/Registers.h"
14 changes: 7 additions & 7 deletions src/DB/db.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "db/db.h"
#include "entry/Entry.h"
#include "permission/struct.h"
#include "include_all.h"
#include <ll/api/data/KeyValueDB.h>
#include <memory>
#include <nlohmann/json.hpp>
Expand Down Expand Up @@ -30,11 +30,11 @@ bool loadLevelDB() {
using string = std::string;
using json = nlohmann::json;

perm::structs::PluginPermData from_json(const json& j) {
perm::structs::PluginPermData permData;
PluginPermData from_json(const json& j) {
PluginPermData permData;
permData.admin = j["admin"].get<std::vector<std::string>>();
for (const auto& userGroup : j["user"]) {
perm::structs::UserGroup group;
UserGroup group;
group.groupName = userGroup["groupName"];
group.authority = userGroup["authority"].get<std::vector<std::string>>();
group.user = userGroup["user"].get<std::vector<std::string>>();
Expand All @@ -44,7 +44,7 @@ perm::structs::PluginPermData from_json(const json& j) {
return permData;
}

json to_json(const perm::structs::PluginPermData& permData) {
json to_json(const PluginPermData& permData) {
json data;
data["admin"] = permData.admin;
for (const auto& group : permData.user) {
Expand All @@ -58,7 +58,7 @@ json to_json(const perm::structs::PluginPermData& permData) {
return data;
}

std::optional<perm::structs::PluginPermData> getPluginData(string pluginName) {
std::optional<PluginPermData> getPluginData(string pluginName) {
auto& logger = entry::entry::getInstance().getSelf().getLogger();
try {
auto d = mKVDB->get(pluginName);
Expand All @@ -73,7 +73,7 @@ std::optional<perm::structs::PluginPermData> getPluginData(string pluginName) {
}
}

bool setPluginData(string pluginName, perm::structs::PluginPermData& data) {
bool setPluginData(string pluginName, PluginPermData& data) {
auto& logger = entry::entry::getInstance().getSelf().getLogger();
try {
auto j = to_json(data);
Expand Down
14 changes: 7 additions & 7 deletions src/DB/db.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "permission/struct.h"
#include "include_all.h"
#include <ll/api/data/KeyValueDB.h>
#include <nlohmann/json.hpp>
#include <nlohmann/json_fwd.hpp>
Expand All @@ -15,12 +15,12 @@ std::unique_ptr<ll::data::KeyValueDB>& getInstance();

bool loadLevelDB();

json to_json(const perm::structs::PluginPermData& permData);
perm::structs::PluginPermData from_json(const json& j);
json to_json(const PluginPermData& permData);
PluginPermData from_json(const json& j);

std::optional<perm::structs::PluginPermData> getPluginData(string pluginName);
bool setPluginData(string pluginName, perm::structs::PluginPermData& data);
bool initPluginData(string pluginName);
bool isPluginInit(string pluginName);
std::optional<PluginPermData> getPluginData(string pluginName);
bool setPluginData(string pluginName, PluginPermData& data);
bool initPluginData(string pluginName);
bool isPluginInit(string pluginName);

} // namespace perm::db
2 changes: 2 additions & 0 deletions src/Entry/Entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

// my
#include "db/db.h"


namespace entry {

entry::entry() = default;
Expand Down
34 changes: 0 additions & 34 deletions src/Permission/struct.h

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "permission/Permission.h"
#include "db/db.h"
#include "include_all.h"
#include <exception>
#include <fstream>
#include <iostream>
Expand All @@ -11,6 +11,7 @@
#include <string>
#include <vector>


namespace perm {

using json = nlohmann::json;
Expand All @@ -26,7 +27,7 @@ bool PermissionCore::loadPermDataFromDB() {
}
auto d = perm::db::getPluginData(pluginName);
if (d) {
data = std::unique_ptr<perm::structs::PluginPermData>(new perm::structs::PluginPermData(*d));
data = std::unique_ptr<PluginPermData>(new PluginPermData(*d));
return true;
}
return false;
Expand Down Expand Up @@ -76,24 +77,24 @@ bool PermissionCore::hasUserGroup(const string& name) {
}

// 获取组
const std::optional<perm::structs::GetUserGroupStruct> PermissionCore::getUserGroup(const string& name) {
const std::optional<GetUserGroupStruct> PermissionCore::getUserGroup(const string& name) {
auto& userGroup = data->user;
for (size_t i = 0; i < userGroup.size(); ++i) {
if (userGroup[i].groupName == name) {
// 使用构造函数创建a的实例
return perm::structs::GetUserGroupStruct(i, userGroup[i]);
return GetUserGroupStruct(i, userGroup[i]);
}
}
return std::nullopt;
}

// 获取所有组
const std::vector<perm::structs::UserGroup>& PermissionCore::getAllUserGroups() { return data->user; }
const std::vector<UserGroup>& PermissionCore::getAllUserGroups() { return data->user; }

// 创建组
bool PermissionCore::createUserGroup(const string& name) {
if (hasUserGroup(name) || !validateName(name)) return false;
perm::structs::UserGroup gp;
UserGroup gp;
gp.groupName = name;
gp.user = std::vector<string>();
gp.authority = std::vector<string>();
Expand Down Expand Up @@ -182,8 +183,8 @@ bool PermissionCore::removeUserToUserGroup(const string& name, const string& use
}

// 获取用户所在的组
const std::vector<perm::structs::UserGroup> PermissionCore::getUserGroupsOfUser(const string& userid) {
std::vector<perm::structs::UserGroup> us;
const std::vector<UserGroup> PermissionCore::getUserGroupsOfUser(const string& userid) {
std::vector<UserGroup> us;

auto& userGroup = data->user;
for (const auto& group : userGroup) {
Expand All @@ -198,9 +199,8 @@ const std::vector<perm::structs::UserGroup> PermissionCore::getUserGroupsOfUser(
}

// 获取用户权限
const std::optional<perm::structs::GetUserPermissionsStruct>
PermissionCore::getUserPermissionOfUserData(const string& userid) {
perm::structs::GetUserPermissionsStruct data;
const std::optional<GetUserPermissionsStruct> PermissionCore::getUserPermissionOfUserData(const string& userid) {
GetUserPermissionsStruct data;

auto groups = getUserGroupsOfUser(userid);
for (const auto& group : groups) {
Expand Down
3 changes: 1 addition & 2 deletions src/register/register.cpp → src/Registers/Registers.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "register/register.h"
#include "permission/Permission.h"
#include "include_all.h"
#include <algorithm>


Expand Down
2 changes: 1 addition & 1 deletion xmake.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ target("Permission") -- Change this to your plugin name.
"PERMISSION_CORE_API_EXPORT" -- export dll api
)
add_files("src/**.cpp")
add_includedirs("src")
add_includedirs("src", "include")
add_packages("levilamina")
add_shflags("/DELAYLOAD:bedrock_server.dll") -- To use symbols provided by SymbolProvider.
set_exceptions("none") -- To avoid conflicts with /EHa.
Expand Down

0 comments on commit fd0927b

Please sign in to comment.