Skip to content

A free open source IT asset/license management system

move validation to requests and validator class, update import method

Godfrey Ma72b3910487e
diff --git a/app/Http/Controllers/LabelsController.php b/app/Http/Controllers/LabelsController.php
index ea6e44826c..cd422295e6 100755
--- a/app/Http/Controllers/LabelsController.php
+++ b/app/Http/Controllers/LabelsController.php
@@ -2,6 +2,7 @@
namespace App\Http\Controllers;
+use App\Http\Requests\ImportCustomLabelRequest;
use App\Models\Asset;
use App\Models\AssetModel;
use App\Models\Category;
@@ -20,7 +21,7 @@ use App\Models\User;
use App\View\Label as LabelView;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
-
+use App\Services\CustomLabelImportValidator;
class LabelsController extends Controller
{
/**
@@ -462,205 +463,13 @@ class LabelsController extends Controller
}
- public function import(Request $request)
+ public function import(ImportCustomLabelRequest $request, CustomLabelImportValidator $importValidator)
{
- $request->validate([
- 'import_method' => ['required', 'in:json,text'],
- 'config_file' => ['required_if:import_method,json', 'file', 'mimes:json,txt'],
- 'config_snapshot' => ['required_if:import_method,text', 'nullable', 'string'],
- ]);
-
- $rawJson = $request->input('import_method') === 'json'
- ? file_get_contents($request->file('config_file')->getRealPath())
- : $request->input('config_snapshot');
-
- $config = json_decode($rawJson, true);
-
- if (json_last_error() !== JSON_ERROR_NONE || !is_array($config)) {
- return back()
- ->withErrors(['config_snapshot' => 'The imported label config must be valid JSON.'])
- ->withInput();
- }
-
- $configValidator = validator(
- $config,
- [
- 'unit' => ['required', 'string', 'in:mm'],
- 'template' => ['required', 'string'],
- 'type' => ['required', 'string', 'in:sheet'],
- 'name' => ['required', 'string'],
-
- /*
- |--------------------------------------------------------------------------
- | Page
- |--------------------------------------------------------------------------
- */
- 'page' => ['required', 'array'],
- 'page.width' => ['required', 'numeric'],
- 'page.height' => ['required', 'numeric'],
- 'page.margin_top' => ['required', 'numeric'],
- 'page.margin_right' => ['required', 'numeric'],
- 'page.margin_bottom' => ['required', 'numeric'],
- 'page.margin_left' => ['required', 'numeric'],
-
- /*
- |--------------------------------------------------------------------------
- | Grid
- |--------------------------------------------------------------------------
- */
- 'grid' => ['required', 'array'],
- 'grid.columns' => ['required', 'integer'],
- 'grid.rows' => ['required', 'integer'],
- 'grid.column_spacing' => ['required', 'numeric'],
- 'grid.row_spacing' => ['required', 'numeric'],
-
- /*
- |--------------------------------------------------------------------------
- | Printable Area
- |--------------------------------------------------------------------------
- */
- 'printable_area' => ['required', 'array'],
- 'printable_area.x1' => ['required', 'numeric'],
- 'printable_area.y1' => ['required', 'numeric'],
- 'printable_area.x2' => ['required', 'numeric'],
- 'printable_area.y2' => ['required', 'numeric'],
- 'printable_area.width' => ['required', 'numeric'],
- 'printable_area.height' => ['required', 'numeric'],
-
- /*
- |--------------------------------------------------------------------------
- | Label
- |--------------------------------------------------------------------------
- */
- 'label' => ['required', 'array'],
- 'label.width' => ['required', 'numeric'],
- 'label.height' => ['required', 'numeric'],
- 'label.border' => ['required', 'numeric'],
- 'label.padding_top' => ['required', 'numeric'],
- 'label.padding_right' => ['required', 'numeric'],
- 'label.padding_bottom' => ['required', 'numeric'],
- 'label.padding_left' => ['required', 'numeric'],
-
- /*
- |--------------------------------------------------------------------------
- | Content
- |--------------------------------------------------------------------------
- */
- 'content' => ['required', 'array'],
-
- 'content.barcode_size' => ['required', 'numeric'],
- 'content.barcode_margin' => ['required', 'numeric'],
-
- 'content.barcode2D_h_align' => ['required', 'string', 'in:L,C,R'],
- 'content.barcode2D_v_align' => ['required', 'string', 'in:T,C,B'],
-
- 'content.tag_alignment' => ['required', 'string', 'in:L,C,R'],
-
- 'content.barcode_2d_size' => ['required', 'numeric'],
-
- 'content.logo_max_width' => ['required', 'numeric'],
- 'content.logo_margin' => ['required', 'numeric'],
-
- 'content.logo_h_align' => ['required', 'string', 'in:L,C,R'],
- 'content.logo_v_align' => ['required', 'string', 'in:T,C,B'],
-
- 'content.tag_font_size' => ['required', 'numeric'],
- 'content.tag_offset_x' => ['required', 'numeric'],
- 'content.tag_offset_y' => ['required', 'numeric'],
-
- 'content.title_font_size' => ['required', 'numeric'],
- 'content.title_margin' => ['required', 'numeric'],
- 'content.title_offset_x' => ['required', 'numeric'],
-
- 'content.field_label_font_size' => ['required', 'numeric'],
- 'content.field_label_margin' => ['required', 'numeric'],
-
- 'content.field_value_font_size' => ['required', 'numeric'],
- 'content.field_value_margin' => ['required', 'numeric'],
-
- 'content.text_area_width' => ['nullable', 'numeric'],
- 'content.text_area_height' => ['nullable', 'numeric'],
-
- /*
- |--------------------------------------------------------------------------
- | Supports
- |--------------------------------------------------------------------------
- */
- 'supports' => ['required', 'array'],
- 'supports.asset_tag' => ['required', 'boolean'],
- 'supports.barcode_1d' => ['required', 'boolean'],
- 'supports.barcode_2d' => ['required', 'boolean'],
- 'supports.fields' => ['required', 'integer'],
- 'supports.logo' => ['required', 'boolean'],
- 'supports.title' => ['required', 'boolean'],
- ],
- [],
- [
- 'page.width' => 'page width',
- 'page.height' => 'page height',
- 'page.margin_top' => 'page top margin',
- 'page.margin_right' => 'page right margin',
- 'page.margin_bottom' => 'page bottom margin',
- 'page.margin_left' => 'page left margin',
-
- 'grid.columns' => 'grid columns',
- 'grid.rows' => 'grid rows',
- 'grid.column_spacing' => 'grid column spacing',
- 'grid.row_spacing' => 'grid row spacing',
-
- 'printable_area.x1' => 'printable area x1',
- 'printable_area.y1' => 'printable area y1',
- 'printable_area.x2' => 'printable area x2',
- 'printable_area.y2' => 'printable area y2',
- 'printable_area.width' => 'printable area width',
- 'printable_area.height' => 'printable area height',
-
- 'label.width' => 'label width',
- 'label.height' => 'label height',
- 'label.border' => 'label border',
- 'label.padding_top' => 'label top padding',
- 'label.padding_right' => 'label right padding',
- 'label.padding_bottom' => 'label bottom padding',
- 'label.padding_left' => 'label left padding',
-
- 'content.barcode_size' => 'barcode size',
- 'content.barcode_margin' => 'barcode margin',
- 'content.barcode2D_h_align' => '2D barcode horizontal alignment',
- 'content.barcode2D_v_align' => '2D barcode vertical alignment',
- 'content.tag_alignment' => 'tag alignment',
- 'content.barcode_2d_size' => '2D barcode size',
- 'content.logo_max_width' => 'logo max width',
- 'content.logo_margin' => 'logo margin',
- 'content.logo_h_align' => 'logo horizontal alignment',
- 'content.logo_v_align' => 'logo vertical alignment',
- 'content.tag_font_size' => 'tag font size',
- 'content.tag_offset_x' => 'tag horizontal offset',
- 'content.tag_offset_y' => 'tag vertical offset',
- 'content.title_font_size' => 'title font size',
- 'content.title_margin' => 'title margin',
- 'content.title_offset_x' => 'title horizontal offset',
- 'content.field_label_font_size' => 'field label font size',
- 'content.field_label_margin' => 'field label margin',
- 'content.field_value_font_size' => 'field value font size',
- 'content.field_value_margin' => 'field value margin',
- 'content.text_area_width' => 'text area width',
- 'content.text_area_height' => 'text area height',
-
- 'supports.asset_tag' => 'supports asset tag',
- 'supports.barcode_1d' => 'supports 1D barcode',
- 'supports.barcode_2d' => 'supports 2D barcode',
- 'supports.fields' => 'supports fields',
- 'supports.logo' => 'supports logo',
- 'supports.title' => 'supports title',
- ]
- );
-
- if ($configValidator->fails()) {
-
- $messages = collect($configValidator->errors()->all())
- ->map(function ($message) {
- return '<i class="fa-solid fa-xmark text-danger"></i> ' . $message;
- })
+ 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()
@@ -670,9 +479,10 @@ class LabelsController extends Controller
->withInput();
}
- return redirect()
- ->route('settings.labels.create')
- ->with('imported_label_config', $config);
+ return redirect()->route('settings.labels.create', [
+ 'label' => $config['template'] ?? null,
+ ])->with('imported_label_config', $config);
+
}
protected function previewLabelForTemplate($template)
diff --git a/app/Http/Requests/ImportCustomLabelRequest.php b/app/Http/Requests/ImportCustomLabelRequest.php
new file mode 100644
index 0000000000..38174cb474
--- /dev/null
+++ b/app/Http/Requests/ImportCustomLabelRequest.php
@@ -0,0 +1,30 @@
+<?php
+
+namespace App\Http\Requests;
+
+use Illuminate\Foundation\Http\FormRequest;
+
+class ImportCustomLabelRequest extends FormRequest
+{
+ public function authorize(): bool
+ {
+ return true;
+ }
+
+ public function rules(): array
+ {
+ return [
+ 'import_method' => ['required', 'in:json,text'],
+ 'config_file' => ['required_if:import_method,json', 'file', 'mimes:json,txt'],
+ 'config_snapshot' => ['required_if:import_method,text', 'nullable', 'string'],
+ ];
+ }
+
+ public function rawConfigJson(): ?string
+ {
+ return $this->input('import_method') === 'json'
+ ? file_get_contents($this->file('config_file')->getRealPath())
+ : $this->input('config_snapshot');
+ }
+
+}
\ No newline at end of file
diff --git a/app/Models/Labels/CustomLabels/CustomTapeLabel.php b/app/Models/Labels/CustomLabels/CustomTapeLabel.php
index a80a2f4067..ef9a74adb6 100644
--- a/app/Models/Labels/CustomLabels/CustomTapeLabel.php
+++ b/app/Models/Labels/CustomLabels/CustomTapeLabel.php
@@ -328,8 +328,12 @@ abstract class CustomTapeLabel extends Label
'tag_offset_x' => $this->getTagOffsetX(),
'tag_offset_y' => $this->getTagOffsetY(),
'field_label_font_size' => $this->getLabelSize(),
+ 'field_label_margin' => $this->getLabelMargin(),
'field_value_font_size' => $this->getFieldSize(),
'field_value_margin' => $this->getFieldMargin(),
+ 'title_font_size' => $this->getTitleSize(),
+ 'title_margin' => $this->getTitleMargin(),
+ 'title_offset_x' => $this->getTitleOffsetX(),
'tag_alignment' => $this->getTagAlignment(),
// 'field_alignment' => $this->getFieldAlignment(),
'logo_max_width' => $this->getLogoMaxWidth(),
diff --git a/app/Services/CustomLabelImportValidator.php b/app/Services/CustomLabelImportValidator.php
new file mode 100644
index 0000000000..04259488ec
--- /dev/null
+++ b/app/Services/CustomLabelImportValidator.php
@@ -0,0 +1,296 @@
+<?php
+
+namespace App\Services;
+
+class CustomLabelImportValidator
+{
+ public function validate(?string $rawJson): array
+ {
+ $config = json_decode($rawJson, true);
+
+ if (json_last_error() !== JSON_ERROR_NONE || !is_array($config)) {
+ throw ValidationException::withMessages([
+ 'config_snapshot' => 'The imported label config must be valid JSON.',
+ ]);
+ }
+
+ validator($config, $this->rules($config), [], $this->attributes())->validate();
+
+ return $config;
+ }
+
+ protected function rules(array $config): array
+ {
+ return match ($config['type'] ?? null) {
+ 'sheet' => array_merge(
+ $this->baseRules(),
+ $this->sharedContentRules(),
+ $this->sheetRules(),
+ ),
+
+ 'tape' => array_merge(
+ $this->baseRules(),
+ $this->sharedContentRules(),
+ $this->printableAreaRules(),
+ $this->tapeContentRules(),
+ ),
+
+ default => $this->baseRules(),
+ };
+ }
+
+ protected function baseRules(): array
+ {
+ return [
+ 'unit' => ['required', 'string', 'in:mm'],
+ 'template' => ['required', 'string'],
+ 'type' => ['required', 'string', 'in:sheet,tape'],
+ 'name' => ['required', 'string'],
+ 'content' => ['required', 'array'],
+ 'supports' => ['required', 'array'],
+ 'supports.asset_tag' => ['required', 'boolean'],
+ 'supports.barcode_1d' => ['required', 'boolean'],
+ 'supports.barcode_2d' => ['required', 'boolean'],
+ 'supports.fields' => ['required', 'integer'],
+ 'supports.logo' => ['required', 'boolean'],
+ 'supports.title' => ['required', 'boolean'],
+ ];
+ }
+
+ protected function printableAreaRules(): array
+ {
+ return [
+ 'printable_area' => ['required', 'array'],
+ 'printable_area.x1' => ['required', 'numeric'],
+ 'printable_area.y1' => ['required', 'numeric'],
+ 'printable_area.x2' => ['required', 'numeric'],
+ 'printable_area.y2' => ['required', 'numeric'],
+ 'printable_area.width' => ['required', 'numeric'],
+ 'printable_area.height' => ['required', 'numeric'],
+ ];
+ }
+
+ protected function sharedContentRules(): array
+ {
+ return [
+ 'content.barcode_size' => ['required', 'numeric'],
+ 'content.barcode_margin' => ['required', 'numeric'],
+
+ 'content.barcode_2d_size' => ['required', 'numeric'],
+ 'content.barcode2D_h_align' => ['required', 'string', 'in:L,C,R'],
+ 'content.barcode2D_v_align' => ['required', 'string', 'in:T,C,B'],
+
+ 'content.tag_alignment' => ['required', 'string', 'in:L,C,R'],
+
+ 'content.logo_max_width' => ['required', 'numeric'],
+ 'content.logo_margin' => ['required', 'numeric'],
+ 'content.logo_h_align' => ['required', 'string', 'in:L,C,R'],
+ 'content.logo_v_align' => ['required', 'string', 'in:T,C,B'],
+
+ 'content.tag_font_size' => ['required', 'numeric'],
+ 'content.tag_offset_x' => ['required', 'numeric'],
+ 'content.tag_offset_y' => ['required', 'numeric'],
+
+ 'content.title_font_size' => ['required', 'numeric'],
+ 'content.title_margin' => ['required', 'numeric'],
+
+ 'content.field_label_font_size' => ['required', 'numeric'],
+ 'content.field_label_margin' => ['required', 'numeric'],
+ 'content.field_value_font_size' => ['required', 'numeric'],
+ 'content.field_value_margin' => ['required', 'numeric'],
+ ];
+ }
+
+ protected function tapeContentRules(): array
+ {
+ return [
+ 'printable_area' => ['required', 'array'],
+
+ 'content.barcode1D_v_align' => ['nullable', 'string', 'in:T,C,B'],
+ 'content.barcode1D_placement' => ['nullable', 'string', 'in:full_width,text_column'],
+
+ 'content.barcode2D_placement' => ['nullable', 'string', 'in:stacked,text_column'],
+ 'content.barcode_2d_margin' => ['nullable', 'numeric'],
+
+ 'content.text_size_mod' => ['nullable', 'numeric'],
+ 'content.text_area_offset_y' => ['nullable', 'numeric'],
+
+ 'content.logo_max_height' => ['nullable', 'numeric'],
+ 'content.logo_placement' => ['nullable', 'string', 'in:text_column'],
+
+ 'content.tag_position_mode' => ['nullable', 'string', 'in:free,inline'],
+ 'content.field_alignment' => ['nullable', 'string', 'in:L,C,R'],
+
+ 'content.text_render_mode' => ['nullable', 'string', 'in:inline,block,vertical_stack'],
+ ];
+ }
+
+ protected function sheetRules(): array
+ {
+ return array_merge(
+ $this->printableAreaRules(),
+ [
+ /*
+ |--------------------------------------------------------------------------
+ | Page
+ |--------------------------------------------------------------------------
+ */
+ 'page' => ['required', 'array'],
+ 'page.width' => ['required', 'numeric'],
+ 'page.height' => ['required', 'numeric'],
+ 'page.margin_top' => ['required', 'numeric'],
+ 'page.margin_right' => ['required', 'numeric'],
+ 'page.margin_bottom' => ['required', 'numeric'],
+ 'page.margin_left' => ['required', 'numeric'],
+
+ /*
+ |--------------------------------------------------------------------------
+ | Grid
+ |--------------------------------------------------------------------------
+ */
+ 'grid' => ['required', 'array'],
+ 'grid.columns' => ['required', 'integer'],
+ 'grid.rows' => ['required', 'integer'],
+ 'grid.column_spacing' => ['required', 'numeric'],
+ 'grid.row_spacing' => ['required', 'numeric'],
+
+ /*
+ |--------------------------------------------------------------------------
+ | Label
+ |--------------------------------------------------------------------------
+ */
+ 'label' => ['required', 'array'],
+ 'label.width' => ['required', 'numeric'],
+ 'label.height' => ['required', 'numeric'],
+ 'label.border' => ['required', 'numeric'],
+ 'label.padding_top' => ['required', 'numeric'],
+ 'label.padding_right' => ['required', 'numeric'],
+ 'label.padding_bottom' => ['required', 'numeric'],
+ 'label.padding_left' => ['required', 'numeric'],
+
+ /*
+ |--------------------------------------------------------------------------
+ | Sheet-only Content
+ |--------------------------------------------------------------------------
+ */
+ 'content.title_offset_x' => ['required', 'numeric'],
+ 'content.text_area_width' => ['nullable', 'numeric'],
+ 'content.text_area_height' => ['nullable', 'numeric'],
+ ]
+ );
+ }
+
+ protected function attributes(): array
+ {
+ return [
+ /*
+ |--------------------------------------------------------------------------
+ | Page
+ |--------------------------------------------------------------------------
+ */
+ 'page.width' => 'page width',
+ 'page.height' => 'page height',
+ 'page.margin_top' => 'page top margin',
+ 'page.margin_right' => 'page right margin',
+ 'page.margin_bottom' => 'page bottom margin',
+ 'page.margin_left' => 'page left margin',
+
+ /*
+ |--------------------------------------------------------------------------
+ | Grid
+ |--------------------------------------------------------------------------
+ */
+ 'grid.columns' => 'grid columns',
+ 'grid.rows' => 'grid rows',
+ 'grid.column_spacing' => 'grid column spacing',
+ 'grid.row_spacing' => 'grid row spacing',
+
+ /*
+ |--------------------------------------------------------------------------
+ | Printable Area
+ |--------------------------------------------------------------------------
+ */
+ 'printable_area.x1' => 'printable area x1',
+ 'printable_area.y1' => 'printable area y1',
+ 'printable_area.x2' => 'printable area x2',
+ 'printable_area.y2' => 'printable area y2',
+ 'printable_area.width' => 'printable area width',
+ 'printable_area.height' => 'printable area height',
+
+ /*
+ |--------------------------------------------------------------------------
+ | Label
+ |--------------------------------------------------------------------------
+ */
+ 'label.width' => 'label width',
+ 'label.height' => 'label height',
+ 'label.border' => 'label border',
+ 'label.padding_top' => 'label top padding',
+ 'label.padding_right' => 'label right padding',
+ 'label.padding_bottom' => 'label bottom padding',
+ 'label.padding_left' => 'label left padding',
+
+ /*
+ |--------------------------------------------------------------------------
+ | Content
+ |--------------------------------------------------------------------------
+ */
+ 'content.barcode_size' => 'barcode size',
+ 'content.barcode_margin' => 'barcode margin',
+
+ 'content.barcode1D_v_align' => '1D barcode vertical alignment',
+ 'content.barcode1D_placement' => '1D barcode placement',
+
+ 'content.barcode2D_h_align' => '2D barcode horizontal alignment',
+ 'content.barcode2D_v_align' => '2D barcode vertical alignment',
+ 'content.barcode2D_placement' => '2D barcode placement',
+
+ 'content.tag_alignment' => 'tag alignment',
+ 'content.tag_position_mode' => 'tag position mode',
+
+ 'content.barcode_2d_size' => '2D barcode size',
+ 'content.barcode_2d_margin' => '2D barcode margin',
+
+ 'content.logo_max_width' => 'logo max width',
+ 'content.logo_max_height' => 'logo max height',
+ 'content.logo_margin' => 'logo margin',
+ 'content.logo_h_align' => 'logo horizontal alignment',
+ 'content.logo_v_align' => 'logo vertical alignment',
+ 'content.logo_placement' => 'logo placement',
+
+ 'content.tag_font_size' => 'tag font size',
+ 'content.tag_offset_x' => 'tag horizontal offset',
+ 'content.tag_offset_y' => 'tag vertical offset',
+
+ 'content.title_font_size' => 'title font size',
+ 'content.title_margin' => 'title margin',
+ 'content.title_offset_x' => 'title horizontal offset',
+
+ 'content.field_label_font_size' => 'field label font size',
+ 'content.field_label_margin' => 'field label margin',
+
+ 'content.field_value_font_size' => 'field value font size',
+ 'content.field_value_margin' => 'field value margin',
+
+ 'content.field_alignment' => 'field alignment',
+
+ 'content.text_size_mod' => 'text size modifier',
+ 'content.text_area_width' => 'text area width',
+ 'content.text_area_height' => 'text area height',
+ 'content.text_area_offset_y' => 'text area vertical offset',
+ 'content.text_render_mode' => 'text render mode',
+
+ /*
+ |--------------------------------------------------------------------------
+ | Supports
+ |--------------------------------------------------------------------------
+ */
+ 'supports.asset_tag' => 'supports asset tag',
+ 'supports.barcode_1d' => 'supports 1D barcode',
+ 'supports.barcode_2d' => 'supports 2D barcode',
+ 'supports.fields' => 'supports fields',
+ 'supports.logo' => 'supports logo',
+ 'supports.title' => 'supports title',
+ ];
+ }
+}
\ No newline at end of file