gruene-seele/app/Http/Requests/StoreIngredientRequest.php
Kevin Adametz 9ed47c02f3 Warenwirtschaft: OPT-9 AuthZ/CSRF-Härtung (Audit M-2, M-3, M-6, M-7, M-10)
- M-2: Produktionen buchen Bestand — StoreProductionRequest verlangt
  jetzt admin >= 7 (wie manuelle Bestandsbewegungen); create/edit/copy
  im Controller gegated, UI-Buttons für niedrigere Stufen ausgeblendet.
- M-3: Ausschuss-/Bewegungs-/Produktions-Buttons nur noch für
  Berechtigte sichtbar (keine toten Formulare mehr).
- M-6: Zustandsändernde GET-Routen umgestellt — stock-entries/copy
  jetzt POST mit CSRF; /admin/product/delete jetzt DELETE-Formulare
  (Produktliste + Rezeptur-Zeile im Produktformular).
- M-7: ProductController::imageAttribute whitelistet Spalten
  ('active', 'pos') + numerische Werte, sonst 400 — kein beliebiger
  Spalten-Write per URL mehr.
- M-10: URL-Felder (Lieferant, Verpackungsartikel, INCI-Lieferanten-
  Repeater) validieren url:http,https — keine speicherbaren
  javascript:-Links mehr.

Tests: ProductionHardeningTest +2; drei bestehende Tests an neue
Regeln angepasst (admin-Level 7 für Produktions-Formular, POST-Copy).
Suite: 231 grün.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-15 12:55:11 +00:00

71 lines
2.8 KiB
PHP

<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class StoreIngredientRequest extends FormRequest
{
public function authorize(): bool
{
return true;
}
/**
* @return array<string, array<int, mixed>>
*/
public function rules(): array
{
return [
'name' => ['required', 'string', 'max:255'],
'alt_name' => ['nullable', 'string', 'max:255'],
'inci' => ['nullable', 'string'],
'effect' => ['nullable', 'string'],
'pos' => ['nullable', 'integer', 'min:0', 'max:255'],
'default_factor' => ['nullable', 'numeric', 'min:0'],
'min_stock_alert' => ['nullable', 'numeric', 'min:0'],
'material_quality_id' => ['nullable', 'integer', 'exists:material_qualities,id'],
'tax_rate_id' => ['nullable', 'integer', 'exists:tax_rates,id'],
'delivery_time' => ['nullable', 'string', 'max:255'],
'delivery_time_days' => ['nullable', 'integer', 'min:0', 'max:65535'],
'suppliers' => ['nullable', 'array'],
'suppliers.*.id' => ['nullable', 'integer', 'exists:suppliers,id'],
'suppliers.*.url' => ['nullable', 'string', 'url:http,https', 'max:2048'],
'storage_room_id' => ['nullable', 'integer', 'exists:storage_rooms,id'],
'storage_shelf_id' => ['nullable', 'integer', 'exists:storage_shelves,id'],
'storage_slot_id' => ['nullable', 'integer', 'exists:storage_slots,id'],
'active' => ['sometimes', 'boolean'],
'exclude_from_stock' => ['sometimes', 'boolean'],
];
}
protected function prepareForValidation(): void
{
$this->merge([
'active' => $this->boolean('active'),
'exclude_from_stock' => $this->boolean('exclude_from_stock'),
'default_factor' => $this->normalizeDecimal($this->input('default_factor')),
'min_stock_alert' => $this->normalizeDecimal($this->input('min_stock_alert')),
'material_quality_id' => $this->nullIfEmpty($this->input('material_quality_id')),
'tax_rate_id' => $this->nullIfEmpty($this->input('tax_rate_id')),
'delivery_time_days' => $this->nullIfEmpty($this->input('delivery_time_days')),
'storage_room_id' => $this->nullIfEmpty($this->input('storage_room_id')),
'storage_shelf_id' => $this->nullIfEmpty($this->input('storage_shelf_id')),
'storage_slot_id' => $this->nullIfEmpty($this->input('storage_slot_id')),
]);
}
private function normalizeDecimal(mixed $value): ?string
{
if ($value === null || $value === '') {
return null;
}
return (string) reFormatNumber($value);
}
private function nullIfEmpty(mixed $value): mixed
{
return $value === '' ? null : $value;
}
}