[CSM] Add function to get player privileges (#5933)

* [CSM] Add function to get player privileges + move related help functions to common

* Added @Zeno- const
This commit is contained in:
red-001 2017-06-07 08:09:06 +01:00 committed by Loïc Blot
parent 6c55874417
commit c91a8c7061
7 changed files with 56 additions and 25 deletions

@ -723,3 +723,28 @@ function core.pointed_thing_to_face_pos(placer, pointed_thing)
end end
return fine_pos return fine_pos
end end
function core.string_to_privs(str, delim)
assert(type(str) == "string")
delim = delim or ','
local privs = {}
for _, priv in pairs(string.split(str, delim)) do
privs[priv:trim()] = true
end
return privs
end
function core.privs_to_string(privs, delim)
assert(type(privs) == "table")
delim = delim or ','
local list = {}
for priv, bool in pairs(privs) do
if bool then
list[#list + 1] = priv
end
end
return table.concat(list, delim)
end
assert(core.string_to_privs("a,b").b == true)
assert(core.privs_to_string({a=true,b=true}) == "a,b")

@ -4,31 +4,6 @@
-- Authentication handler -- Authentication handler
-- --
function core.string_to_privs(str, delim)
assert(type(str) == "string")
delim = delim or ','
local privs = {}
for _, priv in pairs(string.split(str, delim)) do
privs[priv:trim()] = true
end
return privs
end
function core.privs_to_string(privs, delim)
assert(type(privs) == "table")
delim = delim or ','
local list = {}
for priv, bool in pairs(privs) do
if bool then
list[#list + 1] = priv
end
end
return table.concat(list, delim)
end
assert(core.string_to_privs("a,b").b == true)
assert(core.privs_to_string({a=true,b=true}) == "a,b")
core.auth_file_path = core.get_worldpath().."/auth.txt" core.auth_file_path = core.get_worldpath().."/auth.txt"
core.auth_table = {} core.auth_table = {}

@ -150,3 +150,9 @@ core.register_on_punchnode(function(pos, node)
return false return false
end) end)
core.register_chatcommand("privs", {
func = function(param)
return true, core.privs_to_string(minetest.get_privilege_list())
end,
})

@ -730,6 +730,13 @@ Call these functions only at load time!
* `minetest.localplayer` * `minetest.localplayer`
* Reference to the LocalPlayer object. See [`LocalPlayer`](#localplayer) class reference for methods. * Reference to the LocalPlayer object. See [`LocalPlayer`](#localplayer) class reference for methods.
### Privileges
* `minetest.get_privilege_list()`
* Returns a list of privileges the currect player has in the format `{priv1=true,...}`
* `minetest.string_to_privs(str)`: returns `{priv1=true,...}`
* `minetest.privs_to_string(privs)`: returns `"priv1,priv2,..."`
* Convert between two privilege representations
### Client Environment ### Client Environment
* `minetest.get_player_names()` * `minetest.get_player_names()`
* Returns list of player names on server * Returns list of player names on server

@ -416,6 +416,9 @@ public:
bool checkPrivilege(const std::string &priv) const bool checkPrivilege(const std::string &priv) const
{ return (m_privileges.count(priv) != 0); } { return (m_privileges.count(priv) != 0); }
const std::unordered_set<std::string> &getPrivilegeList() const
{ return m_privileges; }
bool getChatMessage(std::wstring &message); bool getChatMessage(std::wstring &message);
void typeChatMessage(const std::wstring& message); void typeChatMessage(const std::wstring& message);

@ -329,6 +329,16 @@ int ModApiClient::l_take_screenshot(lua_State *L)
return 0; return 0;
} }
int ModApiClient::l_get_privilege_list(lua_State *L)
{
const Client *client = getClient(L);
lua_newtable(L);
for (const std::string &priv : client->getPrivilegeList()) {
lua_pushboolean(L, true);
lua_setfield(L, -2, priv.c_str());
}
return 1;
}
void ModApiClient::Initialize(lua_State *L, int top) void ModApiClient::Initialize(lua_State *L, int top)
{ {
API_FCT(get_current_modname); API_FCT(get_current_modname);
@ -353,4 +363,5 @@ void ModApiClient::Initialize(lua_State *L, int top)
API_FCT(get_item_def); API_FCT(get_item_def);
API_FCT(get_node_def); API_FCT(get_node_def);
API_FCT(take_screenshot); API_FCT(take_screenshot);
API_FCT(get_privilege_list);
} }

@ -89,8 +89,12 @@ private:
// get_node_def(nodename) // get_node_def(nodename)
static int l_get_node_def(lua_State *L); static int l_get_node_def(lua_State *L);
// take_screenshot()
static int l_take_screenshot(lua_State *L); static int l_take_screenshot(lua_State *L);
// get_privilege_list()
static int l_get_privilege_list(lua_State *L);
public: public:
static void Initialize(lua_State *L, int top); static void Initialize(lua_State *L, int top);
}; };