Files
petriandClaude Sonnet 5.5 ef400054b0 Return stock of cancelled maintenance, throttle the PIN page's password
Cancelling a maintenance record now reverses the stock it used, through
Inventory's new ReverseMaintenanceStockUse behind the MaintenanceMaterials
port, in the same transaction as the cancellation.

The PIN page allows 5 wrong current passwords a minute per user, as the
login form does, so a signed-in session cannot be used to guess the
password.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
2026-11-29 10:10:18 +02:00

140 lines
4.2 KiB
PHP

<?php
use App\Modules\Users\Application\UseCases\LoginPins;
use App\Modules\Users\Domain\Exceptions\InvalidLoginPin;
use App\Modules\Users\Domain\Exceptions\WrongPassword;
use Illuminate\Support\Facades\RateLimiter;
use Livewire\Attributes\Locked;
use Livewire\Component;
/**
* The user's own PIN code for switching user on a machine console (spec
* 19.6). Setting or removing it asks for the user's password, which may
* be wrong 5 times a minute, as on the login page.
*/
new class extends Component
{
public string $currentPassword = '';
public string $pin = '';
public string $pinConfirmation = '';
#[Locked]
public string $message = '';
public function mount(): void
{
$this->authorize('machines.use');
}
public function save(LoginPins $pins): void
{
$this->authorize('machines.use');
$this->resetErrorBag();
if (trim($this->pin) !== trim($this->pinConfirmation)) {
$this->addError('pinConfirmation', __('users.pin.errors.confirmation'));
return;
}
if ($this->passwordThrottled()) {
return;
}
try {
$pins->set((int) auth()->id(), $this->currentPassword, $this->pin);
} catch (InvalidLoginPin) {
$this->addError('pin', __('users.pin.errors.pin'));
return;
} catch (WrongPassword) {
$this->wrongPassword();
return;
}
RateLimiter::clear($this->throttleKey());
$this->reset('currentPassword', 'pin', 'pinConfirmation');
$this->message = __('users.pin.saved');
}
public function remove(LoginPins $pins): void
{
$this->authorize('machines.use');
$this->resetErrorBag();
if ($this->passwordThrottled()) {
return;
}
try {
$pins->remove((int) auth()->id(), $this->currentPassword);
} catch (WrongPassword) {
$this->wrongPassword();
return;
}
RateLimiter::clear($this->throttleKey());
$this->reset('currentPassword', 'pin', 'pinConfirmation');
$this->message = __('users.pin.removed');
}
public function render(): mixed
{
return $this->view()->title(__('users.pin.title'));
}
private function throttleKey(): string
{
return 'pin-password|'.auth()->id();
}
private function passwordThrottled(): bool
{
if (! RateLimiter::tooManyAttempts($this->throttleKey(), 5)) {
return false;
}
$this->addError('currentPassword', __('auth.throttle', ['seconds' => RateLimiter::availableIn($this->throttleKey())]));
$this->reset('currentPassword');
return true;
}
private function wrongPassword(): void
{
RateLimiter::hit($this->throttleKey());
$this->addError('currentPassword', __('users.pin.errors.password'));
$this->reset('currentPassword');
}
};
?>
<div class="mx-auto max-w-md space-y-6 p-4 sm:p-6">
<h1 class="text-2xl font-semibold">{{ __('users.pin.title') }}</h1>
<p class="text-sm">{{ __('users.pin.intro') }}</p>
<p role="status" aria-live="polite" class="text-sm font-medium">
{{ $message !== '' ? $message : (app(LoginPins::class)->has((int) auth()->id()) ? __('users.pin.is_set') : __('users.pin.not_set')) }}
</p>
<form wire:submit="save" class="space-y-4" novalidate>
<x-password-field id="current-password" wire:model="currentPassword" :label="__('users.pin.current_password')" autocomplete="current-password" />
<x-input id="pin" wire:model="pin" type="password" inputmode="numeric" maxlength="6" :label="__('users.pin.pin')" :hint="__('users.pin.pin_hint')" autocomplete="off" />
<x-input id="pin-confirmation" wire:model="pinConfirmation" type="password" inputmode="numeric" maxlength="6" :label="__('users.pin.pin_confirmation')" autocomplete="off" />
<div class="flex flex-wrap gap-3">
<x-button submit :text="__('users.pin.save')" />
@if (app(LoginPins::class)->has((int) auth()->id()))
<x-button wire:click="remove" subtle color="black" :text="__('users.pin.remove')" />
@endif
</div>
</form>
</div>