A free open source IT asset/license management system
add validation and normalization for fonts in label editor
Godfrey M4c23d22f9c93
diff --git a/app/Http/Controllers/LabelsController.php b/app/Http/Controllers/LabelsController.phpindex 486c64e16c..b871f097ba 100755--- a/app/Http/Controllers/LabelsController.php+++ b/app/Http/Controllers/LabelsController.php@@ -2,7 +2,6 @@namespace App\Http\Controllers;-use App\Http\Requests\ImportCustomLabelRequest;use App\Models\Asset;use App\Models\CustomField;use App\Models\Labels\CustomLabels\PreviewSheetLabel;@@ -105,9 +104,23 @@ class LabelsController extends Controller}- public function edit(CustomUserLabel $label)+ public function edit(CustomUserLabel $label, CustomLabelImportValidator $configValidator){- $config = $label->config_snapshot;+ $config = $label->config_snapshot ?? [];+ $config = $configValidator->normalizeFonts($config);++ try {+ $config = $configValidator->validate(+ json_encode($config)+ );+ } catch (ValidationException $e) {+ return redirect()+ ->route('settings.labels.index')+ ->with(+ 'error',+ trans('admin/labels/general.invalid_config')+ );+ }$selectedType = $label->type?? data_get($config, 'type')@@ -147,6 +160,10 @@ class LabelsController extends Controller'type' => ['required', Rule::in(['sheet', 'tape'])],'content' => ['required', 'array'],'supports' => ['required', 'array'],+ 'content.tag_font' => ['nullable', 'string', Rule::in(CustomLabelFonts::ALLOWED),],+ 'content.title_font' => ['nullable', 'string', Rule::in(CustomLabelFonts::ALLOWED),],+ 'content.field_label_font' => ['nullable', 'string', Rule::in(CustomLabelFonts::ALLOWED),],+ 'content.field_value_font' => ['nullable', 'string', Rule::in(CustomLabelFonts::ALLOWED),],];if (!$isTape) {@@ -258,7 +275,12 @@ class LabelsController extends Controller'type' => ['required', Rule::in(['sheet', 'tape'])],'content' => ['required', 'array'],'supports' => ['required', 'array'],+ 'content.tag_font' => ['nullable', 'string', Rule::in(CustomLabelFonts::ALLOWED),],+ 'content.title_font' => ['nullable', 'string', Rule::in(CustomLabelFonts::ALLOWED),],+ 'content.field_label_font' => ['nullable', 'string', Rule::in(CustomLabelFonts::ALLOWED),],+ 'content.field_value_font' => ['nullable', 'string', Rule::in(CustomLabelFonts::ALLOWED),],];+if ($type === 'sheet') {$rules += ['page' => ['required', 'array'],@@ -364,10 +386,10 @@ class LabelsController extends Controller->with('success', $customLabel->name . ' created successfully.');}- public function create(Request $request)+ public function create(Request $request, CustomLabelImportValidator $validator){if ($request->filled('custom_label_id') || $request->filled('label') || session()->has('imported_label_config')) {- return $this->createFromExisting($request);+ return $this->createFromExisting($request, $validator);}$validated = $request->validate([@@ -509,37 +531,21 @@ class LabelsController extends Controller->with('count', 0);}-- public function import(ImportCustomLabelRequest $request, CustomLabelImportValidator $importValidator)- {- try {- $config = $importValidator->validate($request->rawConfigJson());- } catch (ValidationException $e) {- $messages = collect($e->validator->errors()->all())- ->map(fn($message) => '<i class="fa-solid fa-xmark text-danger"></i> ' . $message)- ->toArray();-- return back()- ->withErrors([- 'config_snapshot' => $messages,- ])- ->withInput();- }-- return redirect()->route('settings.labels.create', [- 'label' => $config['template'] ?? null,- ])->with('imported_label_config', $config);-- }-- public function createFromExisting(Request $request)+ public function createFromExisting(Request $request, CustomLabelImportValidator $configValidator){if ($request->filled('custom_label_id')) {$customLabel = CustomUserLabel::findOrFail($request->get('custom_label_id'));- $config = $customLabel->config_snapshot;+ $config = $customLabel->config_snapshot ?? [];++ $config = $configValidator->normalizeFonts($config);++ $config = $configValidator->validate(+ json_encode($config)+ );+$config['name'] = 'Copy of ' . $customLabel->name;$previewLabel = $customLabel->type === 'tape'diff --git a/app/Services/CustomLabelImportValidator.php b/app/Services/CustomLabelImportValidator.phpindex d163304ba1..16acf035eb 100644--- a/app/Services/CustomLabelImportValidator.php+++ b/app/Services/CustomLabelImportValidator.php@@ -190,6 +190,27 @@ class CustomLabelImportValidator);}+ public function normalizeFonts(array $config): array+ {+ foreach ([+ 'tag_font',+ 'title_font',+ 'field_label_font',+ 'field_value_font',+ ] as $key) {+ $font = data_get($config, "content.{$key}");++ if (+ $font !== null+ && !in_array($font, CustomLabelFonts::ALLOWED, true)+ ) {+ data_set($config, "content.{$key}", 'freesans');+ }+ }++ return $config;+ }+protected function attributes(): array{return [diff --git a/resources/lang/en-US/admin/labels/general.php b/resources/lang/en-US/admin/labels/general.phpindex c3ef3f0030..2d36e5d99f 100644--- a/resources/lang/en-US/admin/labels/general.php+++ b/resources/lang/en-US/admin/labels/general.php@@ -84,6 +84,7 @@ return [],'create_label' => 'Create Label','import_label_config' => 'Import Label Configuration',+ 'invalid_config' => 'Invalid Label Configuration','import_method' => 'Import Method','import_json_help' => 'Select Label Json file to import.','import_paste_json' => 'Paste Label JSON Data',