Skip to content

Commit

Permalink
Misc auth improvements (refs #69, #75)
Browse files Browse the repository at this point in the history
* renamed methods to camelCase
* renamed methods to be more precise
* auth.isPlayerAllowed now returns boolean values
  • Loading branch information
Timo Smit committed Jan 21, 2017
1 parent 004ebea commit 9012d54
Show file tree
Hide file tree
Showing 22 changed files with 46 additions and 50 deletions.
12 changes: 4 additions & 8 deletions luamods/wolfadmin/auth/acl.lua
Expand Up @@ -48,14 +48,10 @@ function acl.clearCache()
data = {}
end

function acl.isallowed(clientId, permission)
function acl.isPlayerAllowed(clientId, permission)
local level = acl.getlevel(clientId)

if data[level] ~= nil and tables.contains(data[level], permission) then
return 1
end

return 0
return data[level] ~= nil and tables.contains(data[level], permission)
end

function acl.getLevels()
Expand Down Expand Up @@ -118,13 +114,13 @@ function acl.removeLevelRoles(levelId)
data[levelId] = {}
end

function acl.getlevel(clientId)
function acl.getPlayerLevel(clientId)
local player = db.getplayer(players.getGUID(clientId))

return player["level_id"]
end

function acl.getlevelname(levelId)
function acl.getLevelName(levelId)
local level = db.getLevel(levelId)

return level["name"]
Expand Down
4 changes: 2 additions & 2 deletions luamods/wolfadmin/auth/auth.lua
Expand Up @@ -112,7 +112,7 @@ auth.PERM_IMMUNE = "immune"
-- as this module serves as a wrapper/super class, we load the selected database
-- system in this function. might have to think of a better way to implement
-- this, but it will suffice.
function auth.oninit()
function auth.onGameInit()
if settings.get("g_standalone") == 1 then
srv = require (wolfa_getLuaPath()..".auth.acl")
else
Expand All @@ -125,6 +125,6 @@ function auth.oninit()

setmetatable(auth, {__index = srv})
end
events.handle("onGameInit", auth.oninit)
events.handle("onGameInit", auth.onGameInit)

return auth
6 changes: 3 additions & 3 deletions luamods/wolfadmin/auth/shrubbot.lua
Expand Up @@ -111,11 +111,11 @@ local flags = {
[auth.PERM_IMMUNE] = "!",
}

function shrubbot.isallowed(clientId, permission)
return et.G_shrubbot_permission(clientId, flags[permission])
function shrubbot.isPlayerAllowed(clientId, permission)
return et.G_shrubbot_permission(clientId, flags[permission]) == 1
end

function shrubbot.getlevel(clientId)
function shrubbot.getPlayerLevel(clientId)
return et.G_shrubbot_level(clientId)
end

Expand Down
4 changes: 2 additions & 2 deletions luamods/wolfadmin/commands/admin/admintest.lua
Expand Up @@ -24,8 +24,8 @@ local players = require (wolfa_getLuaPath()..".players.players")
local settings = require (wolfa_getLuaPath()..".util.settings")

function commandAdminTest(clientId, cmdArguments)
local level = auth.getlevel(clientId)
local levelName = auth.getlevelname(level)
local level = auth.getPlayerLevel(clientId)
local levelName = auth.getLevelName(level)

et.trap_SendConsoleCommand(et.EXEC_APPEND, "cchat -1 \"^dadmintest: ^7"..players.getName(clientId).." ^9is a level "..level.." user (^7"..levelName.."^9).\";")

Expand Down
6 changes: 3 additions & 3 deletions luamods/wolfadmin/commands/admin/ban.lua
Expand Up @@ -55,17 +55,17 @@ function commandBan(clientId, cmdArguments)
duration = util.getTimeFromString(cmdArguments[2])
elseif cmdArguments[2] then
reason = table.concat(cmdArguments, " ", 2)
elseif auth.isallowed(clientId, "8") ~= 1 then
elseif not auth.isPlayerAllowed(clientId, "8") then
et.trap_SendConsoleCommand(et.EXEC_APPEND, "csay "..clientId.." \"^dban usage: "..commands.getadmin("ban")["syntax"].."\";")

return true
end

if auth.isallowed(cmdClient, "!") == 1 then
if auth.isPlayerAllowed(cmdClient, "!") then
et.trap_SendConsoleCommand(et.EXEC_APPEND, "csay "..clientId.." \"^dban: ^7"..et.gentity_get(cmdClient, "pers.netname").." ^9is immune to this command.\";")

return true
elseif auth.getlevel(cmdClient) > auth.getlevel(clientId) then
elseif auth.getPlayerLevel(cmdClient) > auth.getPlayerLevel(clientId) then
et.trap_SendConsoleCommand(et.EXEC_APPEND, "csay "..clientId.." \"^dban: ^9sorry, but your intended victim has a higher admin level than you do.\";")

return true
Expand Down
4 changes: 2 additions & 2 deletions luamods/wolfadmin/commands/admin/gib.lua
Expand Up @@ -44,11 +44,11 @@ function commandGib(clientId, cmdArguments)
return true
end

if auth.isallowed(cmdClient, "!") == 1 then
if auth.isPlayerAllowed(cmdClient, "!") then
et.trap_SendConsoleCommand(et.EXEC_APPEND, "csay "..clientId.." \"^dgib: ^7"..et.gentity_get(cmdClient, "pers.netname").." ^9is immune to this command.\";")

return true
elseif auth.getlevel(cmdClient) > auth.getlevel(clientId) then
elseif auth.getPlayerLevel(cmdClient) > auth.getPlayerLevel(clientId) then
et.trap_SendConsoleCommand(et.EXEC_APPEND, "csay "..clientId.." \"^dgib: ^9sorry, but your intended victim has a higher admin level than you do.\";")

return true
Expand Down
2 changes: 1 addition & 1 deletion luamods/wolfadmin/commands/admin/help.lua
Expand Up @@ -26,7 +26,7 @@ function commandHelp(clientId, cmdArguments)
local availableCommands = {}

for command, data in pairs(cmds) do
if data["function"] and data["flag"] and auth.isallowed(clientId, data["flag"]) == 1 and (not data["hidden"] or (type(data["hidden"]) == "function" and not data["hidden"]())) then
if data["function"] and data["flag"] and auth.isPlayerAllowed(clientId, data["flag"]) and (not data["hidden"] or (type(data["hidden"]) == "function" and not data["hidden"]())) then
table.insert(availableCommands, command)
end
end
Expand Down
2 changes: 1 addition & 1 deletion luamods/wolfadmin/commands/admin/incognito.lua
Expand Up @@ -40,7 +40,7 @@ function commandIncognito(clientId, cmdArguments)
-- et.G_Print(string.format("%s %s %d %s\n", adminName, adminGUID, adminLevel, adminFlags))

if players.getGUID(clientId) == adminGUID then
if auth.isallowed(clientId, "@") ~= 1 then
if not auth.isPlayerAllowed(clientId, "@") then
adminFlags = adminFlags.."+@"

et.trap_SendConsoleCommand(et.EXEC_APPEND, "cchat "..clientId.." \"^dincognito: ^9you are now playing incognito.\";")
Expand Down
4 changes: 2 additions & 2 deletions luamods/wolfadmin/commands/admin/kick.lua
Expand Up @@ -45,11 +45,11 @@ function commandKick(clientId, cmdArguments)
return true
end

if auth.isallowed(cmdClient, "!") == 1 then
if auth.isPlayerAllowed(cmdClient, "!") then
et.trap_SendConsoleCommand(et.EXEC_APPEND, "csay "..clientId.." \"^dkick: ^7"..et.gentity_get(cmdClient, "pers.netname").." ^9is immune to this command.\";")

return true
elseif auth.getlevel(cmdClient) > auth.getlevel(clientId) then
elseif auth.getPlayerLevel(cmdClient) > auth.getPlayerLevel(clientId) then
et.trap_SendConsoleCommand(et.EXEC_APPEND, "csay "..clientId.." \"^dkick: ^9sorry, but your intended victim has a higher admin level than you do.\";")

return true
Expand Down
4 changes: 2 additions & 2 deletions luamods/wolfadmin/commands/admin/listaliases.lua
Expand Up @@ -52,11 +52,11 @@ function commandListAliases(clientId, cmdArguments)
return true
end

if auth.isallowed(cmdClient, "!") == 1 then
if auth.isPlayerAllowed(cmdClient, "!") then
et.trap_SendConsoleCommand(et.EXEC_APPEND, "csay "..clientId.." \"^dlistaliases: ^7"..et.gentity_get(cmdClient, "pers.netname").." ^9is immune to this command.\";")

return true
elseif auth.getlevel(cmdClient) > auth.getlevel(clientId) then
elseif auth.getPlayerLevel(cmdClient) > auth.getPlayerLevel(clientId) then
et.trap_SendConsoleCommand(et.EXEC_APPEND, "csay "..clientId.." \"^dlistaliases: ^9sorry, but your intended victim has a higher admin level than you do.\";")

return true
Expand Down
4 changes: 2 additions & 2 deletions luamods/wolfadmin/commands/admin/listplayers.lua
Expand Up @@ -47,8 +47,8 @@ function commandListPlayers(clientId, cmdArguments)
guidStub = players.getGUID(player):sub(-8)
end

local level = auth.getlevel(player)
local levelName = auth.getlevelname(level)
local level = auth.getPlayerLevel(player)
local levelName = auth.getLevelName(level)

local teamColor, teamCode

Expand Down
6 changes: 3 additions & 3 deletions luamods/wolfadmin/commands/admin/mute.lua
Expand Up @@ -58,7 +58,7 @@ function commandMute(clientId, cmdArguments)
duration = util.getTimeFromString(cmdArguments[2])
elseif cmdArguments[2] then
reason = table.concat(cmdArguments, " ", 2)
elseif auth.isallowed(clientId, "8") ~= 1 then
elseif not auth.isPlayerAllowed(clientId, "8") then
et.trap_SendConsoleCommand(et.EXEC_APPEND, "csay "..clientId.." \"^dmute usage: "..commands.getadmin("mute")["syntax"].."\";")

return true
Expand All @@ -68,11 +68,11 @@ function commandMute(clientId, cmdArguments)
et.trap_SendConsoleCommand(et.EXEC_APPEND, "csay "..clientId.." \"^dmute: ^7"..et.gentity_get(cmdClient, "pers.netname").." ^9is already muted.\";")

return true
elseif auth.isallowed(cmdClient, "!") == 1 then
elseif auth.isPlayerAllowed(cmdClient, "!") then
et.trap_SendConsoleCommand(et.EXEC_APPEND, "csay "..clientId.." \"^dmute: ^7"..et.gentity_get(cmdClient, "pers.netname").." ^9is immune to this command.\";")

return true
elseif auth.getlevel(cmdClient) > auth.getlevel(clientId) then
elseif auth.getPlayerLevel(cmdClient) > auth.getPlayerLevel(clientId) then
et.trap_SendConsoleCommand(et.EXEC_APPEND, "csay "..clientId.." \"^dmute: ^9sorry, but your intended victim has a higher admin level than you do.\";")

return true
Expand Down
4 changes: 2 additions & 2 deletions luamods/wolfadmin/commands/admin/plock.lua
Expand Up @@ -48,11 +48,11 @@ function commandPlayerLock(clientId, cmdArguments)
et.trap_SendConsoleCommand(et.EXEC_APPEND, "csay "..clientId.." \"^dplock: ^7"..et.gentity_get(cmdClient, "pers.netname").." ^9is already locked to a team.\";")

return true
elseif auth.isallowed(cmdClient, "!") == 1 then
elseif auth.isPlayerAllowed(cmdClient, "!") then
et.trap_SendConsoleCommand(et.EXEC_APPEND, "csay "..clientId.." \"^dplock: ^7"..et.gentity_get(cmdClient, "pers.netname").." ^9is immune to this command.\";")

return true
elseif auth.getlevel(cmdClient) > auth.getlevel(clientId) then
elseif auth.getPlayerLevel(cmdClient) > auth.getPlayerLevel(clientId) then
et.trap_SendConsoleCommand(et.EXEC_APPEND, "csay "..clientId.." \"^dplock: ^9sorry, but your intended victim has a higher admin level than you do.\";")

return true
Expand Down
4 changes: 2 additions & 2 deletions luamods/wolfadmin/commands/admin/put.lua
Expand Up @@ -42,11 +42,11 @@ function commandPlayerLock(clientId, cmdArguments)
return true
end

if auth.isallowed(cmdClient, "!") == 1 then
if auth.isPlayerAllowed(cmdClient, "!") then
et.trap_SendConsoleCommand(et.EXEC_APPEND, "csay "..clientId.." \"^dput: ^7"..et.gentity_get(cmdClient, "pers.netname").." ^9is immune to this command.\";")

return true
elseif auth.getlevel(cmdClient) > auth.getlevel(cmdClient) then
elseif auth.getPlayerLevel(cmdClient) > auth.getPlayerLevel(cmdClient) then
et.trap_SendConsoleCommand(et.EXEC_APPEND, "csay "..clientId.." \"^dput: ^9sorry, but your intended victim has a higher admin level than you do.\";")

return true
Expand Down
2 changes: 1 addition & 1 deletion luamods/wolfadmin/commands/admin/setlevel.lua
Expand Up @@ -65,7 +65,7 @@ function commandSetLevel(clientId, cmdArguments)
return true
end

if auth.getlevel(cmdClient) > auth.getlevel(clientId) then
if auth.getPlayerLevel(cmdClient) > auth.getPlayerLevel(clientId) then
et.trap_SendConsoleCommand(et.EXEC_APPEND, "csay "..clientId.." \"^dsetlevel: ^9sorry, but your intended victim has a higher admin level than you do.\";")

return true
Expand Down
4 changes: 2 additions & 2 deletions luamods/wolfadmin/commands/admin/slap.lua
Expand Up @@ -44,11 +44,11 @@ function commandSlap(clientId, cmdArguments)
return true
end

if auth.isallowed(cmdClient, "!") == 1 then
if auth.isPlayerAllowed(cmdClient, "!") then
et.trap_SendConsoleCommand(et.EXEC_APPEND, "csay "..clientId.." \"^dslap: ^7"..et.gentity_get(cmdClient, "pers.netname").." ^9is immune to this command.\";")

return true
elseif auth.getlevel(cmdClient) > auth.getlevel(clientId) then
elseif auth.getPlayerLevel(cmdClient) > auth.getPlayerLevel(clientId) then
et.trap_SendConsoleCommand(et.EXEC_APPEND, "csay "..clientId.." \"^dslap: ^9sorry, but your intended victim has a higher admin level than you do.\";")

return true
Expand Down
6 changes: 3 additions & 3 deletions luamods/wolfadmin/commands/admin/vmute.lua
Expand Up @@ -56,7 +56,7 @@ function commandVoiceMute(clientId, cmdArguments)
duration = util.getTimeFromString(cmdArguments[2])
elseif cmdArguments[2] then
reason = table.concat(cmdArguments, " ", 2)
elseif auth.isallowed(clientId, "8") ~= 1 then
elseif not auth.isPlayerAllowed(clientId, "8") then
et.trap_SendConsoleCommand(et.EXEC_APPEND, "csay "..clientId.." \"^dvmute usage: "..commands.getadmin("vmute")["syntax"].."\";")

return true
Expand All @@ -66,11 +66,11 @@ function commandVoiceMute(clientId, cmdArguments)
et.trap_SendConsoleCommand(et.EXEC_APPEND, "csay "..clientId.." \"^dvmute: ^7"..et.gentity_get(cmdClient, "pers.netname").." ^9is already muted.\";")

return true
elseif auth.isallowed(cmdClient, "!") == 1 then
elseif auth.isPlayerAllowed(cmdClient, "!") then
et.trap_SendConsoleCommand(et.EXEC_APPEND, "csay "..clientId.." \"^dvmute: ^7"..et.gentity_get(cmdClient, "pers.netname").." ^9is immune to this command.\";")

return true
elseif auth.getlevel(cmdClient) > auth.getlevel(clientId) then
elseif auth.getPlayerLevel(cmdClient) > auth.getPlayerLevel(clientId) then
et.trap_SendConsoleCommand(et.EXEC_APPEND, "csay "..clientId.." \"^dvmute: ^9sorry, but your intended victim has a higher admin level than you do.\";")

return true
Expand Down
2 changes: 1 addition & 1 deletion luamods/wolfadmin/commands/admin/warn.lua
Expand Up @@ -72,7 +72,7 @@ function commandWarn(clientId, cmdArguments)
return true
end

if auth.getlevel(cmdClient) > auth.getlevel(clientId) then
if auth.getPlayerLevel(cmdClient) > auth.getPlayerLevel(clientId) then
et.trap_SendConsoleCommand(et.EXEC_APPEND, "csay "..clientId.." \"^dwarn: ^9sorry, but your intended victim has a higher admin level than you do.\";")

return true
Expand Down
2 changes: 1 addition & 1 deletion luamods/wolfadmin/commands/client/ac.lua
Expand Up @@ -33,7 +33,7 @@ function commandAdminChat(clientId, cmdArguments)
end

for playerId = 0, et.trap_Cvar_Get("sv_maxclients") - 1 do
if players.isConnected(playerId) and auth.isallowed(playerId, "~") == 1 then
if players.isConnected(playerId) and auth.isPlayerAllowed(playerId, "~") then
table.insert(recipients, playerId)
end
end
Expand Down
8 changes: 4 additions & 4 deletions luamods/wolfadmin/commands/commands.lua
Expand Up @@ -207,7 +207,7 @@ function commands.onClientCommand(clientId, cmdText)
-- mod-specific or custom commands loading
-- syntax: command arg1 arg2 ... argN
if clientcmds[wolfCmd] and clientcmds[wolfCmd]["function"] and clientcmds[wolfCmd]["flag"] then
if clientcmds[wolfCmd]["flag"] == "" or auth.isallowed(clientId, clientcmds[wolfCmd]["flag"]) == 1 then
if clientcmds[wolfCmd]["flag"] == "" or auth.isPlayerAllowed(clientId, clientcmds[wolfCmd]["flag"]) then
for i = 1, et.trap_Argc() - 1 do
cmdArguments[i] = et.trap_Argv(i)
end
Expand Down Expand Up @@ -247,7 +247,7 @@ function commands.onClientCommand(clientId, cmdText)
clientCmd = string.lower(clientCmd)

if clientcmds[clientCmd] and clientcmds[clientCmd]["function"] and clientcmds[clientCmd]["chat"] then
if clientcmds[clientCmd]["flag"] == "" or auth.isallowed(clientId, clientcmds[clientCmd]["flag"]) == 1 then
if clientcmds[clientCmd]["flag"] == "" or auth.isPlayerAllowed(clientId, clientcmds[clientCmd]["flag"]) then
return clientcmds[clientCmd]["function"](clientId, cmdArguments) and 1 or 0
end
end
Expand Down Expand Up @@ -288,8 +288,8 @@ function commands.onClientCommand(clientId, cmdText)
shrubCmd = string.lower(shrubCmd)

if admincmds[shrubCmd] and admincmds[shrubCmd]["function"] and admincmds[shrubCmd]["flag"] then
if wolfCmd == "say" or (((wolfCmd == "say_team" and et.gentity_get(cmdClient, "sess.sessionTeam") ~= et.TEAM_SPECTATORS) or wolfCmd == "say_buddy") and auth.isallowed(clientId, auth.PERM_TEAMCMDS) == 1) or (wolfCmd == "!"..shrubCmd and auth.isallowed(clientId, auth.PERM_SILENTCMDS) == 1) then
if admincmds[shrubCmd]["flag"] ~= "" and auth.isallowed(clientId, admincmds[shrubCmd]["flag"]) == 1 then
if wolfCmd == "say" or (((wolfCmd == "say_team" and et.gentity_get(cmdClient, "sess.sessionTeam") ~= et.TEAM_SPECTATORS) or wolfCmd == "say_buddy") and auth.isPlayerAllowed(clientId, auth.PERM_TEAMCMDS)) or (wolfCmd == "!"..shrubCmd and auth.isPlayerAllowed(clientId, auth.PERM_SILENTCMDS)) then
if admincmds[shrubCmd]["flag"] ~= "" and auth.isPlayerAllowed(clientId, admincmds[shrubCmd]["flag"]) then
local isFinished = admincmds[shrubCmd]["function"](clientId, cmdArguments)

if not admincmds[shrubCmd]["hidden"] then
Expand Down
2 changes: 1 addition & 1 deletion luamods/wolfadmin/game/voting.lua
Expand Up @@ -89,7 +89,7 @@ events.handle("onGameStateChange", voting.ongamestatechange)
function voting.oncallvote(clientId, type, args)
if et.gentity_get(clientId, "sess.sessionTeam") == constants.TEAM_SPECTATORS or args[1] == "?" then
return 0
elseif voting.isrestricted(type) and auth.isallowed(clientId, PERM_NOVOTELIMIT) ~= 1 then
elseif voting.isrestricted(type) and not auth.isPlayerAllowed(clientId, PERM_NOVOTELIMIT) then
et.trap_SendConsoleCommand(et.EXEC_APPEND, "csay "..clientId.." \"callvote: you are not allowed to call this type of vote.\";")
et.trap_SendServerCommand(clientId, "cp \"You are not allowed to call this type of vote.")

Expand Down
4 changes: 2 additions & 2 deletions luamods/wolfadmin/players/greetings.lua
Expand Up @@ -31,9 +31,9 @@ local userGreetings = {}
local levelGreetings = {}

function greetings.get(clientId)
local lvl = auth.getlevel(clientId)
local lvl = auth.getPlayerLevel(clientId)

if auth.isallowed(clientId, auth.PERM_INCOGNITO) ~= 1 then
if not auth.isPlayerAllowed(clientId, auth.PERM_INCOGNITO) then
if userGreetings[players.getGUID(clientId)] ~= nil then
return userGreetings[players.getGUID(clientId)]
elseif levelGreetings[lvl] ~= nil then
Expand Down

0 comments on commit 9012d54

Please sign in to comment.