feat!: ace permission system

This commit is contained in:
BerkieBb
2022-02-24 21:58:49 +01:00
parent 1412d02db1
commit 31888996ab
13 changed files with 195 additions and 72 deletions
+1 -3
View File
@@ -24,10 +24,8 @@ function QBCore.Commands.Refresh(source)
local suggestions = {}
if not Player then return end
for command, info in pairs(QBCore.Commands.List) do
local isGod = QBCore.Functions.HasPermission(source, 'god')
local hasPerm = QBCore.Functions.HasPermission(source, QBCore.Commands.List[command].permission)
local isPrincipal = IsPlayerAceAllowed(source, 'command')
if isGod or hasPerm or isPrincipal then
if hasPerm then
suggestions[#suggestions + 1] = {
name = '/' .. command,
help = info.help,
+1 -1
View File
@@ -42,4 +42,4 @@ end
function QBCore.ShowSuccess(resource, msg)
print('\x1b[32m['..resource..':LOG]\x1b[0m '..msg)
end
end
+3 -7
View File
@@ -19,11 +19,9 @@ AddEventHandler('chatMessage', function(source, _, message)
if not QBCore.Commands.List[command] then return end
local Player = QBCore.Functions.GetPlayer(src)
if not Player then return end
local isGod = QBCore.Functions.HasPermission(src, 'god')
local hasPerm = QBCore.Functions.HasPermission(src, QBCore.Commands.List[command].permission)
local isPrincipal = IsPlayerAceAllowed(src, 'command')
table.remove(args, 1)
if isGod or hasPerm or isPrincipal 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
@@ -190,10 +188,8 @@ RegisterNetEvent('QBCore:CallCommand', function(command, args)
if not QBCore.Commands.List[command] then return end
local Player = QBCore.Functions.GetPlayer(src)
if not Player then return end
local isGod = QBCore.Functions.HasPermission(src, 'god')
local hasPerm = QBCore.Functions.HasPermission(src, QBCore.Commands.List[command].permission)
local isPrincipal = IsPlayerAceAllowed(src, 'command')
if (QBCore.Commands.List[command].permission == Player.PlayerData.job.name) or isGod or hasPerm or isPrincipal then
if (QBCore.Commands.List[command].permission == Player.PlayerData.job.name) or 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
@@ -258,4 +254,4 @@ QBCore.Functions.CreateCallback('QBCore:HasItem', function(source, cb, items, am
end
end
cb(retval)
end)
end)
+2 -2
View File
@@ -146,9 +146,9 @@ exports('AddGangs', AddGangs)
local function GetCoreVersion(InvokingResource)
local resourceVersion = GetResourceMetadata(GetCurrentResourceName(), 'version')
if InvokingResource and InvokingResource ~= '' then
print(("%s called qbcore version check: %s"):format(InvokingResource, resourceVersion))
print(("%s called qbcore version check: %s"):format(InvokingResource or 'Unknown Resource', resourceVersion))
end
return resourceVersion
end
QBCore.Functions.GetCoreVersion = GetCoreVersion
exports('GetCoreVersion', GetCoreVersion)
exports('GetCoreVersion', GetCoreVersion)
+57 -24
View File
@@ -275,28 +275,36 @@ function QBCore.Functions.AddPermission(source, permission)
local Player = QBCore.Functions.GetPlayer(source)
local plicense = Player.PlayerData.license
if not Player then return end
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)
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
end
function QBCore.Functions.RemovePermission(source)
local Player = QBCore.Functions.GetPlayer(source)
local license = Player.PlayerData.license
if not Player then return end
QBCore.Config.Server.PermissionList[license] = nil
MySQL.Async.execute('DELETE FROM permissions WHERE license = ?', { license })
Player.Functions.UpdatePlayerData()
TriggerClientEvent('QBCore:Client:OnPermissionUpdate', source, 'user')
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')
end
end
-- Checking for Permission Level
@@ -304,20 +312,34 @@ end
function QBCore.Functions.HasPermission(source, permission)
permission = tostring(permission:lower())
local license = QBCore.Functions.GetIdentifier(source, 'license')
if permission == 'user' then
return true
else
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 or 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] == permission then
return true
end
end
end
return Player.PlayerData.permission == permission
end
return false
end
function QBCore.Functions.GetPermission(source)
local license = QBCore.Functions.GetIdentifier(source, 'license')
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
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
@@ -325,13 +347,24 @@ 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
return QBCore.Config.Server.PermissionList[license].optin
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
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
QBCore.Config.Server.PermissionList[license].optin = not QBCore.Config.Server.PermissionList[license].optin
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
end
-- Check if player is banned
@@ -364,4 +397,4 @@ function QBCore.Functions.IsLicenseInUse(license)
end
end
return false
end
end
+15 -9
View File
@@ -15,13 +15,19 @@ end)
-- Get permissions on server start
CreateThread(function()
local result = MySQL.Sync.fetchAll('SELECT * FROM permissions', {})
if not result[1] then return end
for k, v in pairs(result) do
QBCore.Config.Server.PermissionList[v.license] = {
license = v.license,
permission = v.permission,
optin = true,
}
if QBCore.Config.Server.UseOldPermissionSystem then
local result = MySQL.Sync.fetchAll('SELECT * FROM permissions', {})
if not result[1] 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)
end)
+21 -3
View File
@@ -43,6 +43,12 @@ 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
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
@@ -152,11 +158,22 @@ function QBCore.Player.CreatePlayer(PlayerData)
function self.Functions.UpdatePlayerData(dontUpdateChat)
TriggerClientEvent('QBCore:Player:SetPlayerData', self.PlayerData.source, self.PlayerData)
if dontUpdateChat == nil then
if not dontUpdateChat then
QBCore.Commands.Refresh(self.PlayerData.source)
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'
@@ -215,13 +232,14 @@ function QBCore.Player.CreatePlayer(PlayerData)
end
function self.Functions.SetMetaData(meta, val)
meta = meta:lower()
if not meta then return end
meta = meta:lower()
self.PlayerData.metadata[meta] = val
self.Functions.UpdatePlayerData()
end
function self.Functions.AddJobReputation(amount)
if not amount then return end
amount = tonumber(amount)
self.PlayerData.metadata['jobrep'][self.PlayerData.job.name] = self.PlayerData.metadata['jobrep'][self.PlayerData.job.name] + amount
self.Functions.UpdatePlayerData()
@@ -657,4 +675,4 @@ function QBCore.Player.CreateSerialNumber()
return SerialNumber
end
PaycheckInterval() -- This starts the paycheck system
PaycheckInterval() -- This starts the paycheck system