-
Notifications
You must be signed in to change notification settings - Fork 3
/
AccessControl.h
119 lines (103 loc) · 3.28 KB
/
AccessControl.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#pragma once
#include <string>
#include <set>
#include <map>
#include <memory>
#include <boost/variant.hpp>
#include "Lua.h"
struct ServerRole {
std::string name;
std::string password;
bool isLoginless;
bool canExecuteArbitraryLua;
std::set<std::string> allowedCommands;
std::set<std::string> members;
public:
ServerRole() :
name("default"),
password("default"),
isLoginless(false),
canExecuteArbitraryLua(false),
members()
{}
ServerRole(std::string name, std::string password, bool canExecuteArbitraryLua) :
name(name),
password(password),
isLoginless(false),
canExecuteArbitraryLua(canExecuteArbitraryLua),
allowedCommands(),
members()
{}
ServerRole(std::string name, bool canExecuteArbitraryLua) :
name(name),
password("default"),
isLoginless(true),
canExecuteArbitraryLua(canExecuteArbitraryLua),
allowedCommands(),
members()
{}
void addAllowedCommand(std::string commandName);
void removeAllowedCommand(std::string commandName);
bool isCommandAllowed(std::string commandName);
void addMember(std::string name);
};
struct ServerCommand {
enum class CommandArgType {
BOOL = 0,
INT = 1,
FLOAT = 2,
STRING = 3
};
struct ServerCommandArg {
std::string argument;
CommandArgType type;
public:
ServerCommandArg(std::string argument, CommandArgType type) :
argument(argument),
type(type)
{}
};
typedef boost::variant<bool, int, double, std::string> ParsedArg;
typedef std::vector<std::string> ValidationErrors;
typedef boost::variant<ValidationErrors, std::vector<ParsedArg>> ArgValidationResult;
std::string commandName;
std::vector<ServerCommandArg> arguments;
LuaRef* func;
public:
ServerCommand() :
commandName(""),
arguments(std::vector<ServerCommandArg>()),
func(NULL)
{}
ServerCommand(std::string commandName, std::vector<ServerCommandArg> arguments, LuaRef func) :
commandName(commandName),
arguments(arguments),
func(new LuaRef(func))
{}
~ServerCommand() {
delete func;
}
// Returns a vector of validation errors; empty if successful
ArgValidationResult validateArguments(const std::vector<std::string>& receivedParameters);
// Returns empty vec on success, list of errors on failure
std::vector<std::string> execute(std::string playerName, std::string rolename, std::vector<std::string> receivedParameters);
template<typename Tuple, size_t ... I>
auto callFunc(Tuple t, std::index_sequence<I ...>) {
return (*func)(std::get<I>(t) ...);
}
template<typename Tuple>
auto callFunc(Tuple t) {
static constexpr auto size = std::tuple_size<Tuple>::value;
return callFunc(t, std::make_index_sequence<size>{});
}
};
struct ServerAccessControl {
std::map<std::string, std::shared_ptr<ServerRole>> roles;
std::map<std::string, std::shared_ptr<ServerCommand>> commands;
public:
ServerAccessControl() {
// Always add the default role
auto defaultRole = std::make_shared<ServerRole>();
roles[defaultRole->name] = defaultRole;
}
};