ENH - configure radio job permissions

This commit is contained in:
Dominik9906
2026-08-26 23:41:00 +02:00
parent d199097e43
commit 1bf44dd917
12 changed files with 196 additions and 7 deletions
@@ -18,6 +18,7 @@ import type { AdminConfiguratorDescribe } from '@/utils/adminConfiguratorDescrip
export type AdminConfigEditorLabels = {
addField: string
addJob: string
addRow: string
configuredSecret: string
convertToList: string
@@ -27,6 +28,7 @@ export type AdminConfigEditorLabels = {
emptyTable: string
entry: string
general: string
jobPlaceholder: string
keyPlaceholder: string
list: string
remove: string
@@ -149,6 +151,11 @@ const canExtendTable = computed(
!vectorType.value &&
(!tableStructure.value || tableStructure.value.mutableKeys === true),
)
const isJobTable = computed(
() =>
props.path === 'Radio.DisplayName.AllowedJobs' ||
/^Radio\.LockedChannels\[\d+\]\.jobs$/.test(props.path),
)
const usesFixedTableLayout = computed(
() =>
Boolean(tableStructure.value) && tableStructure.value?.mutableKeys !== true,
@@ -371,6 +378,19 @@ function updateOptionalString(event: Event): void {
}
}
function updateNewObjectKey(event: Event): void {
const target = event.target
if (!(target instanceof HTMLInputElement)) return
const value = isJobTable.value
? target.value
.toLowerCase()
.replace(/[^a-z0-9_-]/g, '')
.slice(0, 64)
: target.value
newObjectKey.value = value
target.value = value
}
function toggleOptionalString(event: Event): void {
const target = event.target
if (!(target instanceof HTMLInputElement)) return
@@ -1015,11 +1035,15 @@ function mapEntryStructure(
@submit.prevent="addTableField"
>
<input
v-model="newObjectKey"
:value="newObjectKey"
type="text"
:disabled="disabled"
:placeholder="labels.keyPlaceholder"
:aria-label="labels.keyPlaceholder"
:placeholder="
isJobTable ? labels.jobPlaceholder : labels.keyPlaceholder
"
:aria-label="isJobTable ? labels.jobPlaceholder : labels.keyPlaceholder"
autocomplete="off"
@input="updateNewObjectKey"
/>
<select
v-if="!tableStructure"
@@ -1039,7 +1063,7 @@ function mapEntryStructure(
{{ structureTypeLabel(tableStructure.template) }}
</span>
<button type="submit" :disabled="disabled || !canAddTableField">
<Plus :size="13" />{{ labels.addField }}
<Plus :size="13" />{{ isJobTable ? labels.addJob : labels.addField }}
</button>
</form>
</div>
+2
View File
@@ -400,6 +400,7 @@ function configuratorDescription(
const configuratorEditorLabels = computed<AdminConfigEditorLabels>(() => ({
addField: t('configurator.table.addField'),
addJob: t('configurator.table.addJob'),
addRow: t('configurator.table.addRow'),
configuredSecret: t('configurator.secretConfigured'),
convertToList: t('configurator.table.convertToList'),
@@ -409,6 +410,7 @@ const configuratorEditorLabels = computed<AdminConfigEditorLabels>(() => ({
emptyTable: t('configurator.table.emptyTable'),
entry: t('configurator.table.entry'),
general: configuratorLocaleText('configurator.table.general', 'General'),
jobPlaceholder: t('configurator.table.jobPlaceholder'),
keyPlaceholder: t('configurator.table.keyPlaceholder'),
list: t('configurator.table.list'),
remove: t('configurator.table.remove'),
@@ -0,0 +1,85 @@
import { readFileSync } from 'node:fs'
import { describe, expect, it } from 'vitest'
const resourceSource = (path: string): string =>
readFileSync(new URL(`../../sky_phone/${path}`, import.meta.url), 'utf8')
const configSource = resourceSource('config/config.lua')
const configuratorSource = resourceSource(
'source/server/phone_configurator.lua',
)
const radioSource = resourceSource('source/server/radio.lua')
const editorSource = readFileSync(
new URL('./components/AdminConfigValueEditor.vue', import.meta.url),
'utf8',
)
describe('radio configurator access contract', () => {
it('lets the server allow everyone or enforce configured job grades', () => {
expect(configSource).toMatch(
/DisplayName\s*=\s*\{[\s\S]*?AllowEveryone\s*=\s*false,[\s\S]*?AllowedJobs\s*=\s*\{/,
)
const start = radioSource.indexOf(
'local function can_set_display_name(source)',
)
const end = radioSource.indexOf(
'\nlocal function normalize_display_name',
start,
)
const permissionSource = radioSource.slice(start, end)
expect(permissionSource).toContain('if config.AllowEveryone == true then')
expect(permissionSource.indexOf('config.AllowEveryone')).toBeLessThan(
permissionSource.indexOf('Bridge.Framework.GetJob(source)'),
)
expect(permissionSource).toContain('config.AllowedJobs[job.name]')
expect(permissionSource).toContain('(tonumber(job.grade) or 0)')
})
it('keeps both radio job tables mutable and validates their value types', () => {
expect(configuratorSource).toContain(
'path == "Radio.DisplayName.AllowedJobs"',
)
expect(configuratorSource).toContain(
'path:match("^Radio%.LockedChannels%.%d+%.jobs$")',
)
expect(configuratorSource).toContain(
'path == "Companies.Definitions" or radio_job_entry_default(path) ~= nil',
)
expect(configuratorSource).toMatch(
/entryDefault = radio_job_default,[\s\S]*?mutableKeys = true,[\s\S]*?valueType = type\(radio_job_default\)/,
)
expect(configuratorSource).toContain('not key:match("^[a-z0-9_-]+$")')
})
it('shows a compact job-name input for both radio job tables', () => {
expect(editorSource).toContain(
"props.path === 'Radio.DisplayName.AllowedJobs'",
)
expect(editorSource).toContain(
String.raw`/^Radio\.LockedChannels\[\d+\]\.jobs$/`,
)
expect(editorSource).toContain(
'isJobTable ? labels.jobPlaceholder : labels.keyPlaceholder',
)
expect(editorSource).toContain(
'isJobTable ? labels.addJob : labels.addField',
)
expect(editorSource).toContain('function updateNewObjectKey(event: Event)')
expect(editorSource).toContain('.toLowerCase()')
expect(editorSource).toContain(".replace(/[^a-z0-9_-]/g, '')")
expect(editorSource).toContain('.slice(0, 64)')
for (const locale of ['en', 'de', 'es']) {
const localeSource = resourceSource(`config/locales/${locale}.lua`)
expect(localeSource).toContain(
`Locales["${locale}"].Nui.AdminPanel.configurator.table.addJob`,
)
expect(localeSource).toContain(
`Locales["${locale}"].Nui.AdminPanel.configurator.table.jobPlaceholder`,
)
}
})
})
+2
View File
@@ -965,10 +965,12 @@ const adminPanelFallbackLocales = {
},
addRow: 'Add row',
addField: 'Add field',
addJob: 'Add job',
remove: 'Remove',
emptyList: 'No rows yet. Add the first row with plus.',
emptyTable: 'No fields yet. Add the first key below.',
keyPlaceholder: 'New key',
jobPlaceholder: 'Job name',
convertToList: 'Use list',
convertToMap: 'Use typed key table',
convertToTable: 'Use key table',
@@ -111,4 +111,36 @@ describe('admin configurator defaults', () => {
false,
)
})
it('uses the configured access value when adding radio jobs', () => {
const channelJobs: AdminConfiguratorStructure = {
entryDefault: true,
fields: {},
kind: 'table',
mutableKeys: true,
template: { kind: 'value', valueType: 'boolean' },
}
const displayNameJobs: AdminConfiguratorStructure = {
entryDefault: 0,
fields: {},
kind: 'table',
mutableKeys: true,
template: { kind: 'value', valueType: 'number' },
}
expect(
createMutableTableEntry(
channelJobs,
'Radio.LockedChannels[1].jobs',
'mechanic',
),
).toBe(true)
expect(
createMutableTableEntry(
displayNameJobs,
'Radio.DisplayName.AllowedJobs',
'mechanic',
),
).toBe(0)
})
})
+2 -1
View File
@@ -216,8 +216,9 @@ Config.Radio = {
AutoRejoin = false,
DisplayName = {
Enabled = true,
AllowEveryone = false, -- true allows every job; false uses AllowedJobs and its minimum grades
MaxLength = 32,
AllowedJobs = { -- Job name = minimum grade. Unlisted jobs cannot set a radio display name.
AllowedJobs = { -- Job name = minimum grade. Used when AllowEveryone is false.
police = 0,
sheriff = 0,
fib = 0,
+2
View File
@@ -2141,6 +2141,8 @@ Locales["de"] = {
},
}
Locales["de"].Nui.AdminPanel.configurator.table.addJob = "Job hinzufügen"
Locales["de"].Nui.AdminPanel.configurator.table.jobPlaceholder = "Jobname"
Locales["de"].Nui.AdminPanel.configurator.table.subtabs = {
Dictionaries = "Animationsdateien",
Clips = "Clips",
+2
View File
@@ -2141,6 +2141,8 @@ Locales["en"] = {
},
}
Locales["en"].Nui.AdminPanel.configurator.table.addJob = "Add job"
Locales["en"].Nui.AdminPanel.configurator.table.jobPlaceholder = "Job name"
Locales["en"].Nui.AdminPanel.configurator.table.subtabs = {
Dictionaries = "Dictionaries",
Clips = "Clips",
+2
View File
@@ -2141,6 +2141,8 @@ Locales["es"] = {
},
}
Locales["es"].Nui.AdminPanel.configurator.table.addJob = "Añadir trabajo"
Locales["es"].Nui.AdminPanel.configurator.table.jobPlaceholder = "Nombre del trabajo"
Locales["es"].Nui.AdminPanel.configurator.table.subtabs = {
Dictionaries = "Diccionarios",
Clips = "Los clips",
+31 -1
View File
@@ -274,12 +274,22 @@ local function upgrade_legacy_map(defaults, saved)
return { __skyType = "map", entries = entries }
end
local function radio_job_entry_default(path)
if path == "Radio.DisplayName.AllowedJobs" then
return 0
end
if type(path) == "string" and path:match("^Radio%.LockedChannels%.%d+%.jobs$") then
return true
end
return nil
end
local function merge_values(defaults, saved, path, excluded_paths)
path = path or ""
if type(defaults) ~= "table" or type(saved) ~= "table" then
return copy_value(saved)
end
if path == "Companies.Definitions" then
if path == "Companies.Definitions" or radio_job_entry_default(path) ~= nil then
return copy_value(saved)
end
if defaults.__skyType == "map" and not saved.__skyType then
@@ -492,6 +502,16 @@ local function empty_structure(scope, path)
if scope ~= "config" then
return nil
end
local radio_job_default = radio_job_entry_default(path)
if radio_job_default ~= nil then
return {
entryDefault = radio_job_default,
fields = {},
kind = "table",
mutableKeys = true,
template = { kind = "value", valueType = type(radio_job_default) },
}
end
if path == "Garage.VehicleImages.ModelNames" then
return {
entries = {},
@@ -712,6 +732,16 @@ local function build_structure(value, scope, path)
for key, child in pairs(value) do
fields[key] = build_structure(child, scope, path .. "." .. tostring(key))
end
local radio_job_default = scope == "config" and radio_job_entry_default(path) or nil
if radio_job_default ~= nil then
return {
entryDefault = radio_job_default,
fields = fields,
kind = "table",
mutableKeys = true,
template = { kind = "value", valueType = type(radio_job_default) },
}
end
return {
fields = fields,
kind = "table",
+6
View File
@@ -50,8 +50,14 @@ local function can_set_display_name(source)
if type(config) ~= "table" or not config.Enabled then
return false
end
if config.AllowEveryone == true then
return true
end
local job = Bridge.Framework.GetJob(source)
if type(job) ~= "table" or type(job.name) ~= "string" then
return false
end
local minimum_grade = type(config.AllowedJobs) == "table" and tonumber(config.AllowedJobs[job.name]) or nil
return minimum_grade ~= nil and (tonumber(job.grade) or 0) >= minimum_grade
end
+2 -1
View File
@@ -201,8 +201,9 @@ Config.Radio = {
AutoRejoin = false,
DisplayName = {
Enabled = true,
AllowEveryone = false, -- true allows every job; false uses AllowedJobs and its minimum grades
MaxLength = 32,
AllowedJobs = { -- Job name = minimum grade. Unlisted jobs cannot set a radio display name.
AllowedJobs = { -- Job name = minimum grade. Used when AllowEveryone is false.
police = 0,
sheriff = 0,
fib = 0,