Files
Toiminnanohjaus/app/Modules/Users/Presentation/Livewire/⚡form.blade.php
T
petriandClaude Opus 5.5 c9a7f970ab Fix review findings in login and user management
- Load stored users without re-checking role rules, so an intern whose
  supervisor was deleted no longer gets a 500 on every request; the
  rules apply again on the next change.
- Refuse to deactivate or demote a supervisor of active interns
  (SupervisorHasInterns).
- Lock active admin rows in the last-admin check so concurrent changes
  cannot remove every admin.
- Creating a user survives mail failures: the error is reported and
  the admin is asked to resend the link (CreatedUserAccount result).
- Session-ended redirect passes the reason in the URL
  (/login?access=ended), so Livewire requests also show it.
- Same login message for a wrong password and a disabled account.
- Forgot-password sends links only to users who may sign in today
  (RequestPasswordReset), with the same neutral message.
- Use the default callout instead of the low-contrast warning variant.
- Document make:class/make:test for module files in CLAUDE.md.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-22 11:18:22 +02:00

165 lines
5.5 KiB
PHP

<?php
use App\Modules\Shared\Domain\Exceptions\DomainException;
use App\Modules\Users\Application\Contracts\UserAccountQueries;
use App\Modules\Users\Application\DTOs\UserOption;
use App\Modules\Users\Application\UseCases\CreateUserAccount;
use App\Modules\Users\Application\UseCases\SendPasswordSetupLink;
use App\Modules\Users\Application\UseCases\UpdateUserAccount;
use App\Modules\Users\Domain\Enums\Role;
use App\Modules\Users\Domain\Exceptions\UserAccountNotFound;
use App\Modules\Users\Presentation\Livewire\Forms\UserForm;
use Livewire\Attributes\Computed;
use Livewire\Attributes\Locked;
use Livewire\Component;
/**
* Create page (no route parameter) and edit page (with {user}).
*/
new class extends Component
{
#[Locked]
public ?int $userId = null;
public UserForm $form;
public string $linkStatus = '';
public function mount(UserAccountQueries $userQueries, ?int $user = null): void
{
$this->authorize('users.manage');
if ($user === null) {
return;
}
$summary = $userQueries->findSummary($user);
abort_if($summary === null, 404);
$this->userId = $summary->id;
$this->form->fillFromSummary($summary);
}
/**
* @return list<UserOption>
*/
#[Computed]
public function supervisorOptions(): array
{
return app(UserAccountQueries::class)->supervisorOptions($this->userId);
}
public function save(CreateUserAccount $createUser, UpdateUserAccount $updateUser): void
{
$this->authorize('users.manage');
$data = $this->form->toData();
try {
if ($this->userId === null) {
$created = $createUser->execute($data);
session()->flash('status', __(
$created->setupLinkSent ? 'users.created' : 'users.created_link_failed',
['name' => $data->name],
));
} else {
$updateUser->execute($this->userId, $data);
session()->flash('status', __('users.updated', ['name' => $data->name]));
}
} catch (UserAccountNotFound) {
abort(404);
} catch (DomainException $exception) {
$this->form->showDomainError($exception);
return;
}
$this->redirectRoute('users.index', navigate: true);
}
public function sendPasswordLink(SendPasswordSetupLink $sendLink): void
{
$this->authorize('users.manage');
abort_if($this->userId === null, 404);
try {
$sent = $sendLink->execute($this->userId);
} catch (UserAccountNotFound) {
abort(404);
}
$this->linkStatus = $sent
? __('users.link_sent', ['email' => $this->form->email])
: __('users.link_throttled');
}
public function render(): mixed
{
return $this->view()->title(__($this->userId === null ? 'users.create_title' : 'users.edit_title'));
}
};
?>
<div class="mx-auto max-w-2xl space-y-6 p-4 sm:p-6">
<flux:heading size="xl" level="1">
{{ __($userId === null ? 'users.create_title' : 'users.edit_title') }}
</flux:heading>
<form wire:submit="save" class="space-y-6" novalidate>
<flux:input wire:model="form.name" :label="__('users.fields.name')" autocomplete="off" required />
<flux:input wire:model="form.email" type="email" :label="__('users.fields.email')" autocomplete="off" required />
<flux:select wire:model.live="form.role" :label="__('users.fields.role')">
@foreach (Role::cases() as $role)
<flux:select.option wire:key="role-{{ $role->value }}" :value="$role->value">
{{ __('users.roles.'.$role->value) }}
</flux:select.option>
@endforeach
</flux:select>
@if ($form->isIntern())
<flux:input
wire:model="form.employment_ends_on"
type="date"
:label="__('users.fields.employment_ends_on')"
:description="__('users.fields.employment_ends_on_description')"
required
/>
<flux:select wire:model="form.supervisor_id" :label="__('users.fields.supervisor')" :placeholder="__('users.fields.supervisor_placeholder')">
@foreach ($this->supervisorOptions as $option)
<flux:select.option wire:key="supervisor-{{ $option->id }}" :value="(string) $option->id">
{{ $option->name }}
</flux:select.option>
@endforeach
</flux:select>
@endif
<flux:checkbox
wire:model="form.active"
:label="__('users.fields.active')"
:description="__('users.fields.active_description')"
/>
<div class="flex flex-wrap gap-3">
<flux:button type="submit" variant="primary">{{ __('users.save') }}</flux:button>
<flux:button :href="route('users.index')" wire:navigate variant="ghost">{{ __('users.cancel') }}</flux:button>
</div>
</form>
@if ($userId !== null)
<div class="space-y-3 border-t border-zinc-200 pt-6 dark:border-zinc-700">
<flux:button wire:click="sendPasswordLink" icon="envelope">{{ __('users.send_link') }}</flux:button>
<div role="status" aria-live="polite">
@if ($linkStatus !== '')
<flux:text>{{ $linkStatus }}</flux:text>
@endif
</div>
</div>
@endif
</div>