From a528a0b1272eb13dbc77449e1e5b70d8276a54d5 Mon Sep 17 00:00:00 2001 From: Kakarot Date: Tue, 29 Mar 2022 22:12:00 -0500 Subject: [PATCH] More updates --- server/commands.lua | 28 ++++++++++++++++++++++------ server/functions.lua | 44 +++++++++++++++++++++++++++----------------- 2 files changed, 49 insertions(+), 23 deletions(-) diff --git a/server/commands.lua b/server/commands.lua index 376c9a9..c32d5fd 100644 --- a/server/commands.lua +++ b/server/commands.lua @@ -1,11 +1,21 @@ QBCore.Commands = {} QBCore.Commands.List = {} +QBCore.Commands.IgnoreList = { -- Ignore old perm levels while keeping backwards compatibility + ['god'] = true, -- We don't need to create an ace because group.admin allows all commands + ['admin'] = true, -- We don't need to create an ace because group.admin allows all commands + ['user'] = true, -- We don't need to create an ace because builtin.everyone +} -- Register & Refresh Commands function QBCore.Commands.Add(name, help, arguments, argsrequired, callback, permission) - if not permission then permission = 'user' end - RegisterCommand(name, callback, permission) + local restricted = true -- Default to restricted for all commands + if not permission then permission = 'user' end -- some commands don't pass permission level + if permission == 'user' then restricted = false end -- allow all users to use command + RegisterCommand(name, callback, restricted) -- Register command within fivem + if not QBCore.Commands.IgnoreList[permission] then -- only create aces for extra perm levels + ExecuteCommand(('add_ace group.%s command.%s allow'):format(permission, name)) + end QBCore.Commands.List[name:lower()] = { name = name:lower(), permission = tostring(permission:lower()), @@ -18,20 +28,22 @@ end function QBCore.Commands.Refresh(source) local src = source - local Player = GetPlayer(src) + local Player = QBCore.Functions.GetPlayer(src) local suggestions = {} if Player then for command, info in pairs(QBCore.Commands.List) do - local hasPerm = IsPlayerAceAllowed(tostring(src), info.permission) + local hasPerm = IsPlayerAceAllowed(tostring(src), 'command.'..command) if hasPerm then suggestions[#suggestions + 1] = { name = '/' .. command, help = info.help, params = info.arguments } + else + TriggerClientEvent('chat:removeSuggestion', src, '/'..command) end end - TriggerClientEvent('chat:addSuggestions', tonumber(src), suggestions) + TriggerClientEvent('chat:addSuggestions', src, suggestions) end end @@ -77,7 +89,11 @@ QBCore.Commands.Add('addpermission', 'Give Player Permissions (God Only)', { { n local Player = QBCore.Functions.GetPlayer(tonumber(args[1])) local permission = tostring(args[2]):lower() if Player then - QBCore.Functions.AddPermission(Player.PlayerData.source, permission) + if QBCore.Config.Server.Permissions[permission] then + QBCore.Functions.AddPermission(Player.PlayerData.source, permission) + else + TriggerClientEvent('QBCore:Notify', source, 'Invalid permission level', 'error') + end else TriggerClientEvent('QBCore:Notify', source, Lang:t('error.not_online'), 'error') end diff --git a/server/functions.lua b/server/functions.lua index fcad125..b9e30db 100644 --- a/server/functions.lua +++ b/server/functions.lua @@ -14,7 +14,6 @@ function QBCore.Functions.GetCoords(entity) end function QBCore.Functions.GetIdentifier(source, idtype) - idtype = idtype or QBConfig.IdentifierType local identifiers = GetPlayerIdentifiers(source) for _, identifier in pairs(identifiers) do if string.find(identifier, idtype) then @@ -258,28 +257,37 @@ end function QBCore.Functions.IsWhitelisted(source) if not QBCore.Config.Server.Whitelist then return true end - if QBCore.Functions.HasPermission(source, QBConfig.Server.WhitelistPermission) then return true end + if QBCore.Functions.HasPermission(source, QBCore.Config.Server.WhitelistPermission) then return true end return false end -- Setting & Removing Permissions function QBCore.Functions.AddPermission(source, permission) - local license = QBCore.Functions.GetIdentifier(source, 'license') - if QBConfig.Server.Permissions[permission] then - ExecuteCommand(('add_principal identifier.%s group.%s'):format(license, permission)) - QBCore.Commands.Refresh(source) - else - TriggerClientEvent('QBCore:Notify', source, 'Invalid permission level', 'error') + local src = source + local license = QBCore.Functions.GetIdentifier(src, 'license') + for k,v in pairs(QBCore.Config.Server.Permissions) do + if permission == v then + ExecuteCommand(('add_principal identifier.%s group.%s'):format(license, permission)) + QBCore.Commands.Refresh(src) + end end end -function QBCore.Functions.RemovePermission(source) - local license = QBCore.Functions.GetIdentifier(source, 'license') - for k,v in pairs(QBConfig.Server.Permissions) do - if IsPlayerAceAllowed(source, v) then - ExecuteCommand(('remove_principal identifier.%s group.%s'):format(license, v)) - QBCore.Commands.Refresh(source) +function QBCore.Functions.RemovePermission(source, permission) + local src = source + local license = QBCore.Functions.GetIdentifier(src, 'license') + if permission then + if IsPlayerAceAllowed(src, permission) then + ExecuteCommand(('remove_principal identifier.%s group.%s'):format(license, permission)) + QBCore.Commands.Refresh(src) + end + else + for k,v in pairs(QBCore.Config.Server.Permissions) do + if IsPlayerAceAllowed(src, v) then + ExecuteCommand(('remove_principal identifier.%s group.%s'):format(license, v)) + QBCore.Commands.Refresh(src) + end end end end @@ -288,14 +296,16 @@ end function QBCore.Functions.HasPermission(source, permission) local src = source - if IsPlayerAceAllowed(tostring(src), tostring(permission:lower())) then return true end + local group = ('group.%s'):format(permission) + if IsPlayerAceAllowed(tostring(src), group) then return true end return false end function QBCore.Functions.GetPermission(source) local src = source - for k,v in pairs (QBConfig.Server.Permissions) do - if IsPlayerAceAllowed(src, 'group.'..v) then + for k,v in pairs (QBCore.Config.Server.Permissions) do + local group = ('group.%s'):format(v) + if IsPlayerAceAllowed(src, group) then return v end end