WIP permission system

This commit is contained in:
Kakarot
2022-03-28 01:24:40 -05:00
parent 1dfdb46105
commit 3bb1023654
6 changed files with 34 additions and 143 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 = {}
+16 -18
View File
@@ -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
+3 -23
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))
+13 -56
View File
@@ -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)
+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()
-20
View File
@@ -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'