Files
Toiminnanohjaus/app/Modules/Customers/Presentation/Livewire/⚡form.blade.php
T
petriandClaude Opus 5.5 1acdaa5b19 Harden the app: block project files, add rate limits and security headers
- Root .htaccess refuses everything outside public/, because local Apache
  serves the whole project folder (.env, logs with set-password links, .git).
- Login allows 20 failed attempts a minute per IP across accounts, besides
  5 per email and IP; the forgot-password page 5 requests a minute per IP.
- A global middleware sends X-Frame-Options, frame-ancestors, nosniff,
  Referrer-Policy, Permissions-Policy and HSTS over HTTPS, and drops
  X-Powered-By.
- The weekly time sheet locks the shown week and selected day, so the
  browser cannot set them.
- Users who may not see personal identity codes can save a new code at
  most 10 times an hour, so the duplicate check cannot be used to test codes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-24 06:25:26 +02:00

183 lines
6.3 KiB
PHP

<?php
use App\Modules\Customers\Application\Contracts\CustomerQueries;
use App\Modules\Customers\Application\UseCases\RegisterCustomer;
use App\Modules\Customers\Application\UseCases\UpdateCustomer;
use App\Modules\Customers\Domain\Enums\CustomerType;
use App\Modules\Customers\Domain\Exceptions\CustomerNotFound;
use App\Modules\Customers\Presentation\Livewire\Forms\CustomerForm;
use App\Modules\Shared\Domain\Exceptions\DomainException;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\RateLimiter;
use Livewire\Attributes\Locked;
use Livewire\Component;
/**
* Create page (no route parameter) and edit page (with {customer}).
*/
new class extends Component
{
/**
* Saves with a new personal identity code per hour for users who may not
* see codes. "Already in use" tells whether a person is a customer, so
* the form must not be usable to test many codes.
*/
public const int MAX_IDENTITY_CODE_SAVES_PER_HOUR = 10;
#[Locked]
public ?int $customerId = null;
public CustomerForm $form;
/**
* The stored personal identity code, masked (e.g. ••••••-308T). The
* full code never reaches the form.
*/
#[Locked]
public ?string $storedPersonalIdentityCode = null;
public function mount(CustomerQueries $customerQueries, ?int $customer = null): void
{
$this->authorize('customers.use');
if ($customer === null) {
return;
}
$summary = $customerQueries->findSummary($customer);
abort_if($summary === null, 404);
$this->customerId = $summary->id;
$this->storedPersonalIdentityCode = $summary->personalIdentityCodeMasked;
$this->form->fillFromSummary($summary);
}
public function save(RegisterCustomer $registerCustomer, UpdateCustomer $updateCustomer): void
{
$this->authorize('customers.use');
if ($this->form->remove_personal_identity_code) {
$this->authorize('customers.view-personal-identity-code');
}
if (! $this->identityCodeAttemptAllowed()) {
$this->resetErrorBag('form.personal_identity_code');
$this->addError('form.personal_identity_code', __('customers.errors.personal_identity_code_throttled'));
return;
}
$data = $this->form->toData();
try {
if ($this->customerId === null) {
$customerId = $registerCustomer->execute($data);
session()->flash('status', __('customers.created', ['name' => $data->name]));
} else {
$updateCustomer->execute($this->customerId, $data);
$customerId = $this->customerId;
session()->flash('status', __('customers.updated', ['name' => $data->name]));
}
} catch (CustomerNotFound) {
abort(404);
} catch (DomainException $exception) {
$this->form->showDomainError($exception);
return;
}
// The customer's page, where contacts and billing units are added.
$this->redirectRoute('customers.show', $customerId, navigate: true);
}
/**
* Counts a save that carries a new personal identity code; admins and
* management, who see the codes anyway, are not limited.
*/
private function identityCodeAttemptAllowed(): bool
{
if (trim($this->form->personal_identity_code) === '' || Gate::allows('customers.view-personal-identity-code')) {
return true;
}
$key = 'identity-code-save|'.auth()->id();
if (RateLimiter::tooManyAttempts($key, self::MAX_IDENTITY_CODE_SAVES_PER_HOUR)) {
return false;
}
RateLimiter::hit($key, 3600);
return true;
}
public function render(): mixed
{
return $this->view()->title(__($this->customerId === null ? 'customers.create_title' : 'customers.edit_title'));
}
};
?>
<div class="mx-auto max-w-2xl space-y-6 p-4 sm:p-6">
<h1 class="text-2xl font-semibold">
{{ __($customerId === null ? 'customers.create_title' : 'customers.edit_title') }}
</h1>
<form wire:submit="save" class="space-y-6" novalidate>
<x-input
wire:model="form.name"
:label="__('customers.fields.name')"
autocomplete="organization"
required
/>
<x-radio.group
wire:model.live="form.type"
:label="__('customers.fields.type')"
:options="collect(CustomerType::cases())->map(fn (CustomerType $type) => ['label' => __('customers.types.'.$type->value), 'value' => $type->value])->all()"
/>
@if ($form->type === CustomerType::Company->value)
{{-- No inputmode="numeric": the iOS number pad has no hyphen for 1234567-8. --}}
<x-input
wire:model="form.business_id"
:label="__('customers.fields.business_id')"
:hint="__('customers.fields.business_id_description')"
autocomplete="off"
/>
@else
{{-- Never filled from the stored code; type="text" because the code has letters. --}}
<x-input
wire:model="form.personal_identity_code"
:label="__('customers.fields.personal_identity_code')"
:hint="$storedPersonalIdentityCode === null
? __('customers.fields.personal_identity_code_description')
: __('customers.fields.personal_identity_code_stored', ['code' => $storedPersonalIdentityCode])"
autocomplete="off"
spellcheck="false"
/>
@if ($storedPersonalIdentityCode !== null)
@can('customers.view-personal-identity-code')
<x-checkbox
wire:model="form.remove_personal_identity_code"
:label="__('customers.fields.remove_personal_identity_code')"
/>
@endcan
@endif
@endif
<div class="flex flex-wrap gap-3">
<x-button submit :text="__('customers.save')" />
<x-button
:href="$customerId === null ? route('customers.index') : route('customers.show', $customerId)"
wire:navigate
subtle
color="black"
:text="__('customers.cancel')"
/>
</div>
</form>
</div>