Merge pull request #594 from GhzGarage/main

WIP Permissions System
This commit is contained in:
Kakarot
2022-03-30 16:31:36 -06:00
committed by GitHub
6 changed files with 77 additions and 159 deletions
+1 -5
View File
@@ -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 = {}
+36 -19
View File
@@ -1,17 +1,29 @@
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 god is allowed all commands
['user'] = true -- We don't need to create an ace because builtin.everyone
}
CreateThread(function() -- Add ace to node for perm checking
for k,v in pairs(QBConfig.Server.Permissions) do
ExecuteCommand(('add_ace qbcore.%s %s allow'):format(v, v))
end
end)
-- Register & Refresh Commands
function QBCore.Commands.Add(name, help, arguments, argsrequired, callback, permission)
if type(permission) == 'string' then
permission = permission:lower()
else
permission = 'user'
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 qbcore.%s command.%s allow'):format(permission, name))
end
QBCore.Commands.List[name:lower()] = {
name = name:lower(),
permission = permission,
permission = tostring(permission:lower()),
help = help,
arguments = arguments,
argsrequired = argsrequired,
@@ -20,20 +32,24 @@ 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 = QBCore.Functions.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(QBCore.Commands.List) do
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', src, suggestions)
end
TriggerClientEvent('chat:addSuggestions', source, suggestions)
end
-- Teleport
@@ -84,10 +100,11 @@ QBCore.Commands.Add('addpermission', 'Give Player Permissions (God Only)', { { n
end
end, 'god')
QBCore.Commands.Add('removepermission', 'Remove Players Permissions (God Only)', { { name = 'id', help = 'ID of player' } }, true, function(source, args)
QBCore.Commands.Add('removepermission', 'Remove Players Permissions (God Only)', { { name = 'id', help = 'ID of player' }, { name = 'permission', help = 'Permission level' } }, true, function(source, args)
local Player = QBCore.Functions.GetPlayer(tonumber(args[1]))
local permission = tostring(args[2]):lower()
if Player then
QBCore.Functions.RemovePermission(Player.PlayerData.source)
QBCore.Functions.RemovePermission(Player.PlayerData.source, permission)
else
TriggerClientEvent('QBCore:Notify', source, Lang:t('error.not_online'), 'error')
end
@@ -113,7 +130,7 @@ QBCore.Commands.Add('closeserver', 'Close the server for people without permissi
return
end
if QBCore.Functions.HasPermission(source, 'admin') then
reason = args[1] or 'No reason specified'
local reason = args[1] or 'No reason specified'
QBCore.Config.Server.Closed = true
QBCore.Config.Server.ClosedReason = reason
for k in pairs(QBCore.Players) do
+4 -24
View File
@@ -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))
@@ -203,7 +183,7 @@ RegisterNetEvent('QBCore:CallCommand', function(command, args)
local Player = QBCore.Functions.GetPlayer(src)
if not Player then return end
local hasPerm = QBCore.Functions.HasPermission(src, QBCore.Commands.List[command].permission)
if (QBCore.Commands.List[command].permission == Player.PlayerData.job.name) or hasPerm then
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
+34 -70
View File
@@ -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,81 +257,54 @@ 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 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)
else
Player.Functions.SetPermission(permission)
end
local src = source
local license = QBCore.Functions.GetIdentifier(src, 'license')
ExecuteCommand(('add_principal identifier.%s qbcore.%s'):format(license, permission))
QBCore.Commands.Refresh(src)
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')
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 qbcore.%s'):format(license, permission))
QBCore.Commands.Refresh(src)
end
else
Player.Functions.SetPermission('user')
for k,v in pairs(QBCore.Config.Server.Permissions) do
if IsPlayerAceAllowed(src, v) then
ExecuteCommand(('remove_principal identifier.%s qbcore.%s'):format(license, v))
QBCore.Commands.Refresh(src)
end
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(src, permission) 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
function QBCore.Functions.GetPermissions(source)
local src = source
local perms = {}
for k,v in pairs (QBCore.Config.Server.Permissions) do
if IsPlayerAceAllowed(src, v) then
perms[v] = true
end
end
return perms
end
-- Opt in or out of admin reports
@@ -340,24 +312,16 @@ end
function QBCore.Functions.IsOptin(source)
local license = QBCore.Functions.GetIdentifier(source, 'license')
if not license or not QBCore.Functions.HasPermission(source, 'admin') then return false end
if QBCore.Config.Server.UseOldPermissionSystem then
return QBCore.Config.Server.PermissionList[license].optin
else
local Player = QBCore.Functions.GetPlayer(source)
return Player.PlayerData.optin
end
local Player = QBCore.Functions.GetPlayer(source)
return Player.PlayerData.optin
end
function QBCore.Functions.ToggleOptin(source)
local license = QBCore.Functions.GetIdentifier(source, 'license')
if not license or not QBCore.Functions.HasPermission(source, 'admin') then return end
if QBCore.Config.Server.UseOldPermissionSystem then
QBCore.Config.Server.PermissionList[license].optin = not QBCore.Config.Server.PermissionList[license].optin
else
local Player = QBCore.Functions.GetPlayer(source)
Player.PlayerData.optin = not Player.PlayerData.optin
Player.Functions.SetMetaData('optin', Player.PlayerData.optin)
end
local Player = QBCore.Functions.GetPlayer(source)
Player.PlayerData.optin = not Player.PlayerData.optin
Player.Functions.SetMetaData('optin', Player.PlayerData.optin)
end
-- Check if player is banned
+1 -21
View File
@@ -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()
+1 -20
View File
@@ -43,16 +43,8 @@ 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 {}
PlayerData.optin = PlayerData.optin or true
for moneytype, startamount in pairs(QBCore.Config.Money.MoneyTypes) do
PlayerData.money[moneytype] = PlayerData.money[moneytype] or startamount
end
@@ -168,17 +160,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'