From 3bb10236542ab46934cf1e06ae428abed9a351d5 Mon Sep 17 00:00:00 2001 From: Kakarot <57848836+GhzGarage@users.noreply.github.com> Date: Mon, 28 Mar 2022 01:24:40 -0500 Subject: [PATCH] WIP permission system --- config.lua | 6 +--- server/commands.lua | 34 ++++++++++------------ server/events.lua | 26 ++--------------- server/functions.lua | 69 +++++++++----------------------------------- server/main.lua | 22 +------------- server/player.lua | 20 ------------- 6 files changed, 34 insertions(+), 143 deletions(-) diff --git a/config.lua b/config.lua index f18ac41..43de9d5 100644 --- a/config.lua +++ b/config.lua @@ -30,11 +30,7 @@ QBConfig.Server.WhitelistPermission = 'admin' -- Permission that's able to enter QBConfig.Server.PVP = true -- Enable or disable pvp on the server (Ability to shoot other players) QBConfig.Server.Discord = "" -- Discord invite link QBConfig.Server.CheckDuplicateLicense = true -- Check for duplicate rockstar license on join -QBConfig.Server.PermissionList = {} -- Permission list for old permission system -QBConfig.Server.UseOldPermissionSystem = false -- Use old permissions system from the database -QBConfig.Server.AllPermissions = { -- Table of permissions which have access to everything for the new permissions system - 'god', -} +QBConfig.Server.Permissions = {'god', 'admin', 'mod'} -- Add as many groups as you want here after creating them in your server.cfg QBConfig.Notify = {} diff --git a/server/commands.lua b/server/commands.lua index 7a68187..877236c 100644 --- a/server/commands.lua +++ b/server/commands.lua @@ -4,14 +4,10 @@ QBCore.Commands.List = {} -- Register & Refresh Commands function QBCore.Commands.Add(name, help, arguments, argsrequired, callback, permission) - if type(permission) == 'string' then - permission = permission:lower() - else - permission = 'user' - end - QBCore.Commands.List[name:lower()] = { + RegisterCommand(name, callback, permission) + CommandList[name:lower()] = { name = name:lower(), - permission = permission, + permission = tostring(permission:lower()), help = help, arguments = arguments, argsrequired = argsrequired, @@ -20,20 +16,22 @@ function QBCore.Commands.Add(name, help, arguments, argsrequired, callback, perm end function QBCore.Commands.Refresh(source) - local Player = QBCore.Functions.GetPlayer(source) + local src = source + local Player = GetPlayer(src) local suggestions = {} - if not Player then return end - for command, info in pairs(QBCore.Commands.List) do - local hasPerm = QBCore.Functions.HasPermission(source, QBCore.Commands.List[command].permission) - if hasPerm then - suggestions[#suggestions + 1] = { - name = '/' .. command, - help = info.help, - params = info.arguments - } + if Player then + for command, info in pairs(CommandList) do + local hasPerm = IsPlayerAceAllowed(tostring(src), info.permission) + if hasPerm then + suggestions[#suggestions + 1] = { + name = '/' .. command, + help = info.help, + params = info.arguments + } + end end + TriggerClientEvent('chat:addSuggestions', tonumber(src), suggestions) end - TriggerClientEvent('chat:addSuggestions', source, suggestions) end -- Teleport diff --git a/server/events.lua b/server/events.lua index 5a99f61..84bb425 100644 --- a/server/events.lua +++ b/server/events.lua @@ -10,28 +10,6 @@ AddEventHandler('playerDropped', function() QBCore.Players[src] = nil end) -AddEventHandler('chatMessage', function(source, _, message) - local src = source - if string.sub(message, 1, 1) ~= '/' then return end - local args = QBCore.Shared.SplitStr(message, ' ') - local command = string.gsub(args[1]:lower(), '/', '') - CancelEvent() - if not QBCore.Commands.List[command] then return end - local Player = QBCore.Functions.GetPlayer(src) - if not Player then return end - local hasPerm = QBCore.Functions.HasPermission(src, QBCore.Commands.List[command].permission) - table.remove(args, 1) - if hasPerm then - if QBCore.Commands.List[command].argsrequired and #QBCore.Commands.List[command].arguments ~= 0 and not args[#QBCore.Commands.List[command].arguments] then - TriggerClientEvent('QBCore:Notify', src, Lang:t('error.missing_args2'), 'error') - else - QBCore.Commands.List[command].callback(src, args) - end - else - TriggerClientEvent('QBCore:Notify', src, Lang:t('error.no_access'), 'error') - end -end) - -- Player Connecting local function onPlayerConnecting(name, setKickReason, deferrals) @@ -44,7 +22,9 @@ local function onPlayerConnecting(name, setKickReason, deferrals) Wait(0) if QBCore.Config.Server.Closed then - deferrals.done(QBCore.Config.Server.ClosedReason) + if not IsPlayerAceAllowed(src, 'qbadmin.join') then + deferrals.done(QBCore.Config.Server.ClosedReason) + end end deferrals.update(string.format('Hello %s. Validating Your Rockstar License', name)) diff --git a/server/functions.lua b/server/functions.lua index 2e89a2c..5802a27 100644 --- a/server/functions.lua +++ b/server/functions.lua @@ -265,76 +265,33 @@ end -- Setting & Removing Permissions function QBCore.Functions.AddPermission(source, permission) - local Player = QBCore.Functions.GetPlayer(source) - local plicense = Player.PlayerData.license - if not Player then return end - if QBCore.Config.Server.UseOldPermissionSystem then - QBCore.Config.Server.PermissionList[plicense] = { - license = plicense, - permission = permission:lower(), - } - MySQL.Async.execute('DELETE FROM permissions WHERE license = ?', { plicense }) - MySQL.Async.insert('INSERT INTO permissions (name, license, permission) VALUES (?, ?, ?)', { - GetPlayerName(source), - plicense, - permission:lower() - }) - Player.Functions.UpdatePlayerData() - TriggerClientEvent('QBCore:Client:OnPermissionUpdate', 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 - Player.Functions.SetPermission(permission) + TriggerClientEvent('QBCore:Notify', source, 'Invalid permission level', 'error') end end function QBCore.Functions.RemovePermission(source) - local Player = QBCore.Functions.GetPlayer(source) - local license = Player.PlayerData.license - if not Player then return end - if QBCore.Config.Server.UseOldPermissionSystem then - QBCore.Config.Server.PermissionList[license] = nil - MySQL.Async.execute('DELETE FROM permissions WHERE license = ?', { license }) - Player.Functions.UpdatePlayerData() - TriggerClientEvent('QBCore:Client:OnPermissionUpdate', source, 'user') - else - Player.Functions.SetPermission('user') + 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) + end end end -- Checking for Permission Level function QBCore.Functions.HasPermission(source, permission) - permission = tostring(permission:lower()) - local license = QBCore.Functions.GetIdentifier(source, 'license') - if QBCore.Config.Server.UseOldPermissionSystem then - if permission == 'user' then return true end - if not QBCore.Config.Server.PermissionList[license] or QBCore.Config.Server.PermissionList[license].license ~= license then return false end - if QBCore.Config.Server.PermissionList[license].permission ~= permission and QBCore.Config.Server.PermissionList[license].permission ~= 'god' then return false end - return true - else - local Player = QBCore.Functions.GetPlayer(source) - if next(QBCore.Config.Server.AllPermissions) then - for i = 1, #QBCore.Config.Server.AllPermissions do - if QBCore.Config.Server.AllPermissions[i] == Player.PlayerData.permission then - return true - end - end - end - return Player.PlayerData.permission == permission - end + local src = source + if IsPlayerAceAllowed(tostring(src), tostring(permission:lower())) then return true end return false end -function QBCore.Functions.GetPermission(source) - local license = QBCore.Functions.GetIdentifier(source, 'license') - if QBCore.Config.Server.UseOldPermissionSystem then - if not license or not QBCore.Config.Server.PermissionList[license] or QBCore.Config.Server.PermissionList[license].license ~= license then return 'user' end - return QBCore.Config.Server.PermissionList[license].permission - else - local Player = QBCore.Functions.GetPlayer(source) - return Player.PlayerData.permission - end -end - -- Opt in or out of admin reports function QBCore.Functions.IsOptin(source) diff --git a/server/main.lua b/server/main.lua index d6f3fad..41c0c43 100644 --- a/server/main.lua +++ b/server/main.lua @@ -10,24 +10,4 @@ end) -- To use this export in a script instead of manifest method -- Just put this line of code below at the very top of the script --- local QBCore = exports['qb-core']:GetCoreObject() - --- Get permissions on server start - -CreateThread(function() - if QBCore.Config.Server.UseOldPermissionSystem then - local result = MySQL.Sync.fetchAll('SELECT * FROM permissions', {}) - if not result then return end - for k, v in pairs(result) do - QBCore.Config.Server.PermissionList[v.license] = { - license = v.license, - permission = v.permission, - optin = true - } - end - else - for i = 1, #QBCore.Config.Server.AllPermissions do - ExecuteCommand(('add_principal group.%s group.admin'):format(QBCore.Config.Server.AllPermissions[i])) - end - end -end) +-- local QBCore = exports['qb-core']:GetCoreObject() \ No newline at end of file diff --git a/server/player.lua b/server/player.lua index 1d133be..b706e67 100644 --- a/server/player.lua +++ b/server/player.lua @@ -43,15 +43,6 @@ function QBCore.Player.CheckPlayerData(source, PlayerData) PlayerData.license = PlayerData.license or QBCore.Functions.GetIdentifier(source, 'license') PlayerData.name = GetPlayerName(source) PlayerData.cid = PlayerData.cid or 1 - if not QBCore.Config.Server.UseOldPermissionSystem then - if PlayerData.permission then ExecuteCommand(('remove_principal identifier.%s group.%s'):format(PlayerData.license, PlayerData.permission)) end - if not PlayerData.permission and IsPlayerAceAllowed(source, 'command') then -- if both aces are allowed then the person has group.admin and is a system admin - PlayerData.permission = QBCore.Config.Server.AllPermissions[1] or 'god' - end - PlayerData.permission = PlayerData.permission or 'user' - ExecuteCommand(('add_principal identifier.%s group.%s'):format(PlayerData.license, PlayerData.permission)) - PlayerData.optin = PlayerData.optin or true - end PlayerData.money = PlayerData.money or {} for moneytype, startamount in pairs(QBCore.Config.Money.MoneyTypes) do PlayerData.money[moneytype] = PlayerData.money[moneytype] or startamount @@ -168,17 +159,6 @@ function QBCore.Player.CreatePlayer(PlayerData) end end - if not QBCore.Config.Server.UseOldPermissionSystem then - function self.Functions.SetPermission(permission) - permission = permission:lower() - ExecuteCommand(('remove_principal identifier.%s group.%s'):format(self.PlayerData.license, self.PlayerData.permission)) - ExecuteCommand(('add_principal identifier.%s group.%s'):format(self.PlayerData.license, permission)) - self.PlayerData.permission = permission - self.Functions.UpdatePlayerData() - TriggerClientEvent('QBCore:Client:OnPermissionUpdate', self.PlayerData.source, permission) - end - end - function self.Functions.SetJob(job, grade) job = job:lower() grade = tostring(grade) or '0'