Added acl command for server console (refs #62, #69)

This commit is contained in:
Timo Smit 2017-01-20 19:53:55 +01:00
parent a2808f3130
commit 702ed2748d
3 changed files with 309 additions and 9 deletions

View file

@ -15,14 +15,11 @@
-- You should have received a copy of the GNU General Public License
-- along with this program. If not, see <http://www.gnu.org/licenses/>.
local auth = require (wolfa_getLuaPath()..".auth.auth")
local db = require (wolfa_getLuaPath()..".db.db")
local players = require (wolfa_getLuaPath()..".players.players")
local events = require (wolfa_getLuaPath()..".util.events")
local files = require (wolfa_getLuaPath()..".util.files")
local tables = require (wolfa_getLuaPath()..".util.tables")
local acl = {}
@ -34,20 +31,21 @@ function acl.readPermissions()
-- should probably cache current players' permissions as well, then
-- read in new players' permissions as they join the server
local levels = db.getLevelsWithIds()
for _, level in ipairs(levels) do
data[level["id"]] = {}
end
local roles = db.getLevelRoles()
for _, role in ipairs(roles) do
if not data[role["level_id"]] then
data[role["level_id"]] = {}
end
table.insert(data[role["level_id"]], role["role"])
end
end
events.handle("onGameInit", acl.readPermissions)
function acl.clearCache()
-- clear cache whenever database is updated, or do this manually
data = {}
end
function acl.isallowed(clientId, permission)
@ -60,6 +58,66 @@ function acl.isallowed(clientId, permission)
return 0
end
function acl.getLevels()
return db.getLevels()
end
function acl.isLevel(levelId)
return (db.getLevel(levelId) ~= nil)
end
function acl.addLevel(levelId, name)
db.addLevel(levelId, name)
data[levelId] = {}
end
function acl.removeLevel(levelId)
db.removeLevel(levelId)
data[levelId] = nil
end
function acl.reLevel(levelId, newLevelId)
db.reLevel(levelId, newLevelId)
end
function acl.getLevelRoles(levelId)
return data[levelId]
end
function acl.isLevelAllowed(levelId, role)
return tables.contains(data[levelId], role)
end
function acl.addLevelRole(levelId, role)
db.addLevelRole(levelId, role)
table.insert(data[levelId], role)
end
function acl.removeLevelRole(levelId, role)
db.removeLevelRole(levelId, role)
for i, levelRole in ipairs(data[levelId]) do
if levelRole == role then
table.remove(data[levelId], i)
end
end
end
function acl.copyLevelRoles(levelId, newLevelId)
db.copyLevelRoles(levelId, newLevelId)
data[newLevelId] = tables.copy(data[levelId])
end
function acl.removeLevelRoles(levelId)
db.removeLevelRoles(levelId)
data[levelId] = {}
end
function acl.getlevel(clientId)
local player = db.getplayer(players.getGUID(clientId))

View file

@ -0,0 +1,202 @@
-- WolfAdmin module for Wolfenstein: Enemy Territory servers.
-- Copyright (C) 2015-2017 Timo 'Timothy' Smit
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- at your option any later version.
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU General Public License for more details.
-- You should have received a copy of the GNU General Public License
-- along with this program. If not, see <http://www.gnu.org/licenses/>.
local acl = require (wolfa_getLuaPath()..".auth.acl")
local commands = require (wolfa_getLuaPath()..".commands.commands")
function commandAclListLevels(cmdArguments)
for _, level in ipairs(acl.getLevels()) do
et.G_Print(string.format("%5d %30s %6d players", level["id"], level["name"], level["players"]).."\n")
end
end
function commandAclAddLevel(cmdArguments)
local levelId = tonumber(cmdArguments[2])
local name = cmdArguments[3]
if not levelId then
et.G_Print("usage: acl addlevel [id] [name]\n")
return true
elseif acl.isLevel(levelId) then
et.G_Print("error: level "..levelId.." already exists\n")
return true
end
acl.addLevel(levelId, name)
et.G_Print("added level "..levelId.." ("..name..")\n")
end
function commandAclRemoveLevel(cmdArguments)
local levelId = tonumber(cmdArguments[2])
if not levelId or not acl.isLevel(levelId) then
et.G_Print("usage: acl removelevel [id]\n")
return true
end
acl.removeLevelRoles(levelId)
acl.removeLevel(levelId)
et.G_Print("removed level "..levelId.."\n")
end
function commandAclReLevel(cmdArguments)
local levelId = tonumber(cmdArguments[2])
local newLevelId = tonumber(cmdArguments[3])
if not levelId or not acl.isLevel(levelId) or not newLevelId or not acl.isLevel(newLevelId) then
et.G_Print("usage: acl relevel [id] [newid]\n")
return true
end
acl.reLevel(levelId, newLevelId)
et.G_Print("releveled all players with "..levelId.." to "..newLevelId.."\n")
end
function commandAclListLevelRoles(cmdArguments)
local levelId = tonumber(cmdArguments[2])
if not levelId or not acl.isLevel(levelId) then
et.G_Print("usage: acl listroles [id]\n")
return true
end
et.G_Print("roles for level "..levelId..":\n")
for _, role in ipairs(acl.getLevelRoles(levelId)) do
et.G_Print(role.."\n")
end
end
function commandAclIsAllowed(cmdArguments)
local levelId = tonumber(cmdArguments[2])
local role = cmdArguments[3]
if not levelId or not acl.isLevel(levelId) or not role then
et.G_Print("usage: acl isallowed [id] [role]\n")
return true
end
local isAllowed = acl.isLevelAllowed(levelId, role)
et.G_Print("level "..levelId.." "..(isAllowed and "HAS" or "HAS NOT").." "..role.."\n")
end
function commandAclAddLevelRole(cmdArguments)
local levelId = tonumber(cmdArguments[2])
local role = cmdArguments[3]
if not levelId or not acl.isLevel(levelId) or not role then
et.G_Print("usage: acl addrole [id] [role]\n")
return true
end
local isAllowed = acl.isLevelAllowed(levelId, role)
if isAllowed then
et.G_Print("error: level "..levelId.." already has '"..role.."'\n")
return true
end
acl.addLevelRole(levelId, role)
et.G_Print("added role "..role.." to level "..levelId.."\n")
end
function commandAclRemoveLevelRole(cmdArguments)
local levelId = tonumber(cmdArguments[2])
local role = cmdArguments[3]
if not levelId or not acl.isLevel(levelId) or not role then
et.G_Print("usage: acl removerole [id] [role]\n")
return true
end
local isAllowed = acl.isLevelAllowed(levelId, role)
if not isAllowed then
et.G_Print("error: level "..levelId.." does not have '"..role.."'\n")
return true
end
acl.removeLevelRole(levelId, role)
et.G_Print("removed role "..role.." from level "..levelId.."\n")
end
function commandAclCopyLevelRoles(cmdArguments)
local levelId = tonumber(cmdArguments[2])
local newLevelId = tonumber(cmdArguments[3])
if not levelId or not acl.isLevel(levelId) or not newLevelId or not acl.isLevel(newLevelId) then
et.G_Print("usage: acl copyroles [id] [newid]\n")
return true
end
if #acl.getLevelRoles(newLevelId) ~= 0 then
et.G_Print("error: level "..newLevelId.." already has roles\n")
return true
end
acl.copyLevelRoles(levelId, newLevelId)
et.G_Print("copied roles from "..levelId.." to "..newLevelId.."\n")
end
function commandAcl(clientId, cmdArguments)
local cmd = cmdArguments[1]
if cmd == "listlevels" then
return commandAclListLevels(cmdArguments)
elseif cmd == "addlevel" then
return commandAclAddLevel(cmdArguments)
elseif cmd == "removelevel" then
return commandAclRemoveLevel(cmdArguments)
elseif cmd == "relevel" then
return commandAclReLevel(cmdArguments)
elseif cmd == "listroles" then
return commandAclListLevelRoles(cmdArguments)
elseif cmd == "isallowed" then
return commandAclIsAllowed(cmdArguments)
elseif cmd == "addrole" then
return commandAclAddLevelRole(cmdArguments)
elseif cmd == "removerole" then
return commandAclRemoveLevelRole(cmdArguments)
elseif cmd == "copyroles" then
return commandAclCopyLevelRoles(cmdArguments)
else
error("usage: acl [listlevels|addlevel|removelevel|relevel|listroles|isallowed|addrole|removerole|copyroles]")
end
return true
end
commands.addserver("acl", commandAcl)

View file

@ -65,7 +65,15 @@ function sqlite3.updateLevel(id, name)
cur = assert(con:execute("UPDATE `level` SET `name`='"..util.escape(name).."' WHERE `id`='"..tonumber(id).."'"))
end
function sqlite3.getLevels()
function sqlite3.removeLevel(id)
cur = assert(con:execute("DELETE FROM `level` WHERE `id`="..tonumber(id)..""))
end
function sqlite3.reLevel(id, newId)
cur = assert(con:execute("UPDATE `player` SET `level_id`="..tonumber(newId).." WHERE `level_id`="..tonumber(id)..""))
end
function sqlite3.getLevelsWithIds()
cur = assert(con:execute("SELECT * FROM `level`"))
local levels = {}
@ -81,6 +89,22 @@ function sqlite3.getLevels()
return levels
end
function sqlite3.getLevels()
cur = assert(con:execute("SELECT `l`.*, COUNT(`p`.`id`) AS `players` FROM `level` AS `l` LEFT JOIN `player` AS `p` ON `l`.`id`=`p`.`level_id` GROUP BY `l`.`id`"))
local levels = {}
local row = cur:fetch({}, "a")
while row do
table.insert(levels, tables.copy(row))
row = cur:fetch(row, "a")
end
cur:close()
return levels
end
function sqlite3.getLevel(id)
cur = assert(con:execute("SELECT * FROM `level` WHERE `id`='"..tonumber(id).."'"))
@ -107,6 +131,22 @@ function sqlite3.getLevelRoles()
return roles
end
function sqlite3.addLevelRole(levelId, role)
cur = assert(con:execute("INSERT INTO `level_role` (`level_id`, `role`) VALUES ("..tonumber(levelId)..", '"..util.escape(role).."')"))
end
function sqlite3.removeLevelRole(levelId, role)
cur = assert(con:execute("DELETE FROM `level_role` WHERE `level_id`="..tonumber(levelId).." AND role='"..util.escape(role).."'"))
end
function sqlite3.copyLevelRoles(levelId, newLevelId)
cur = assert(con:execute("INSERT INTO `level_role` (`level_id`, `role`) SELECT '"..tonumber(newLevelId).."' AS `level_id`, `role` FROM `level_role` WHERE `level_id`="..tonumber(levelId)))
end
function sqlite3.removeLevelRoles(levelId)
cur = assert(con:execute("DELETE FROM `level_role` WHERE `level_id`="..tonumber(levelId)..""))
end
-- aliases
function sqlite3.addalias(playerid, alias, lastused)
cur = assert(con:execute("INSERT INTO `alias` (`player_id`, `alias`, `cleanalias`, `lastused`, `used`) VALUES ("..tonumber(playerid)..", '"..util.escape(alias).."', '"..util.escape(util.removeColors(alias)).."', "..tonumber(lastused)..", 1)"))