fediauth/priv_revoke.lua

58 lines
2.1 KiB
Lua
Raw Normal View History

2023-02-02 13:25:16 +00:00
-- privs to revoke until the verification code is validated
local temp_revoke_privs = {}
2023-09-27 10:57:15 +00:00
-- mark player health-related privs as "fediauth_keep" (they don't get removed while entering the fediauth code)
2023-02-02 13:25:16 +00:00
for _, name in ipairs({"fly", "noclip"}) do
local priv_def = minetest.registered_privileges[name]
if priv_def then
2023-09-27 10:57:15 +00:00
priv_def.fediauth_keep = true
2023-02-02 13:25:16 +00:00
end
end
minetest.register_on_mods_loaded(function()
2023-09-27 10:57:15 +00:00
-- collect all privs to revoke while entering the fediauth code
2023-02-02 13:25:16 +00:00
for name, priv_def in pairs(minetest.registered_privileges) do
2023-09-27 10:57:15 +00:00
if not priv_def.fediauth_keep then
2023-02-02 13:25:16 +00:00
-- not marked explicitly as "keep"
table.insert(temp_revoke_privs, name)
end
end
end)
-- moves all "temp_revoke_privs" to mod-storage
2023-09-27 10:57:15 +00:00
function fediauth.revoke_privs(playername)
2023-02-02 13:25:16 +00:00
local privs = minetest.get_player_privs(playername)
2023-09-27 10:57:15 +00:00
if fediauth.storage:get_string(playername .. "_privs") == "" then
2023-02-02 13:25:16 +00:00
local moved_privs = {}
for _, priv_name in ipairs(temp_revoke_privs) do
if privs[priv_name] then
privs[priv_name] = nil
moved_privs[priv_name] = true
end
end
2023-09-27 10:57:15 +00:00
minetest.log("action", "[fediauth] revoking privs of '" .. playername .. "' list: " .. dump(moved_privs))
2023-02-02 13:25:16 +00:00
minetest.set_player_privs(playername, privs)
2023-09-27 10:57:15 +00:00
fediauth.storage:set_string(playername .. "_privs", minetest.serialize(moved_privs))
2023-02-02 13:25:16 +00:00
end
end
-- moves all privs from mod-storage into the live privs
2023-09-27 10:57:15 +00:00
function fediauth.regrant_privs(playername)
local stored_priv_str = fediauth.storage:get_string(playername .. "_privs")
2023-02-02 13:25:16 +00:00
if stored_priv_str ~= "" then
local privs = minetest.get_player_privs(playername)
local stored_privs = minetest.deserialize(stored_priv_str)
-- merge stored privs into existing table
for priv_name in pairs(stored_privs) do
privs[priv_name] = true
end
2023-09-27 10:57:15 +00:00
minetest.log("action", "[fediauth] regranting privs of '" .. playername .. "' list: " .. dump(stored_privs))
2023-02-02 13:25:16 +00:00
minetest.set_player_privs(playername, privs)
2023-09-27 10:57:15 +00:00
fediauth.storage:set_string(playername .. "_privs", "")
2023-02-02 13:25:16 +00:00
end
end