New Users module with the four layers: - Domain: Role enum, UserAccount entity (intern needs end date and supervisor, login allowed until the end of the last day), email and name value objects, SupervisorRule, last-admin protection. - Application: CreateUserAccount (emails a set-password link), UpdateUserAccount, SendPasswordSetupLink, CheckLoginAllowed, query contract and Clock (business time zone Europe/Helsinki). - Infrastructure: users table gets role, is_active, employment_ends_on and supervisor_id; Eloquent repository and queries; Password broker link sender with a Finnish email; Gate users.manage. - Presentation: Livewire login (rate limited), forgot and reset password pages, logout, EnsureAccountIsUsable middleware that ends sessions of deactivated users, admin-only user list and form, and users:create-admin command. Also: - Layout gets a labelled main menu, the signed-in user and logout. - Remove the local /dev/login; guests are redirected to /login. - Customers: customers.use ability (no subcontractors), authorization in mount and save, locked customerId, 404 for a missing customer, empty-state check uses the total count. - Architecture test lets module providers register their own Presentation classes; docs and CLAUDE.md updated. Co-Authored-By: Claude Opus 5.5 <[email protected]>
119 lines
4.3 KiB
PHP
119 lines
4.3 KiB
PHP
<?php
|
|
|
|
namespace App\Modules\Users\Presentation\Livewire\Forms;
|
|
|
|
use App\Modules\Shared\Domain\Exceptions\DomainException;
|
|
use App\Modules\Users\Application\DTOs\UserAccountData;
|
|
use App\Modules\Users\Application\DTOs\UserSummary;
|
|
use App\Modules\Users\Domain\Enums\Role;
|
|
use App\Modules\Users\Domain\Exceptions\EmailAlreadyInUse;
|
|
use App\Modules\Users\Domain\Exceptions\InternRequiresEndDateAndSupervisor;
|
|
use App\Modules\Users\Domain\Exceptions\InvalidEmailAddress;
|
|
use App\Modules\Users\Domain\Exceptions\InvalidPersonName;
|
|
use App\Modules\Users\Domain\Exceptions\InvalidSupervisor;
|
|
use App\Modules\Users\Domain\Exceptions\LastAdminCannotBeRemoved;
|
|
use Illuminate\Validation\Rule;
|
|
use Livewire\Form;
|
|
|
|
/**
|
|
* User details form shared by the create and edit pages. Validates the
|
|
* shape of the input; business rules come from the Domain and are shown as
|
|
* field errors by showDomainError().
|
|
*/
|
|
class UserForm extends Form
|
|
{
|
|
public string $name = '';
|
|
|
|
public string $email = '';
|
|
|
|
public string $role = Role::Employee->value;
|
|
|
|
public bool $active = true;
|
|
|
|
public string $employment_ends_on = '';
|
|
|
|
public string $supervisor_id = '';
|
|
|
|
public function fillFromSummary(UserSummary $user): void
|
|
{
|
|
$this->name = $user->name;
|
|
$this->email = $user->email;
|
|
$this->role = $user->role->value;
|
|
$this->active = $user->active;
|
|
$this->employment_ends_on = $user->employmentEndsOn ?? '';
|
|
$this->supervisor_id = $user->supervisorId === null ? '' : (string) $user->supervisorId;
|
|
}
|
|
|
|
public function isIntern(): bool
|
|
{
|
|
return $this->role === Role::Intern->value;
|
|
}
|
|
|
|
public function toData(): UserAccountData
|
|
{
|
|
$this->validate();
|
|
|
|
return UserAccountData::fromArray([
|
|
'name' => $this->name,
|
|
'email' => $this->email,
|
|
'role' => $this->role,
|
|
'active' => $this->active,
|
|
'employment_ends_on' => $this->isIntern() ? $this->employment_ends_on : null,
|
|
'supervisor_id' => $this->isIntern() ? $this->supervisor_id : null,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Shows a broken business rule as an error on the field it concerns.
|
|
*/
|
|
public function showDomainError(DomainException $exception): void
|
|
{
|
|
[$field, $key] = match ($exception::class) {
|
|
InvalidPersonName::class => ['name', 'users.errors.invalid_name'],
|
|
InvalidEmailAddress::class => ['email', 'users.errors.invalid_email'],
|
|
EmailAlreadyInUse::class => ['email', 'users.errors.email_taken'],
|
|
InternRequiresEndDateAndSupervisor::class => ['supervisor_id', 'users.errors.supervisor_required'],
|
|
InvalidSupervisor::class => ['supervisor_id', 'users.errors.invalid_supervisor'],
|
|
LastAdminCannotBeRemoved::class => ['role', 'users.errors.last_admin'],
|
|
default => throw $exception,
|
|
};
|
|
|
|
$this->addError($field, __($key));
|
|
}
|
|
|
|
/**
|
|
* @return array<string, array<int, mixed>>
|
|
*/
|
|
protected function rules(): array
|
|
{
|
|
return [
|
|
'name' => ['required', 'string', 'max:255'],
|
|
'email' => ['required', 'string', 'email:filter', 'max:255'],
|
|
'role' => ['required', Rule::enum(Role::class)],
|
|
'active' => ['boolean'],
|
|
'employment_ends_on' => [Rule::requiredIf($this->isIntern()), 'nullable', 'date_format:Y-m-d'],
|
|
'supervisor_id' => [Rule::requiredIf($this->isIntern()), 'nullable', 'integer'],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @return array<string, string>
|
|
*/
|
|
protected function messages(): array
|
|
{
|
|
return [
|
|
'name.required' => __('users.errors.name_required'),
|
|
'name.max' => __('users.errors.invalid_name'),
|
|
'email.required' => __('users.errors.email_required'),
|
|
'email.email' => __('users.errors.invalid_email'),
|
|
'email.max' => __('users.errors.invalid_email'),
|
|
'role.required' => __('users.errors.role_required'),
|
|
'role.enum' => __('users.errors.role_required'),
|
|
'employment_ends_on.required' => __('users.errors.end_date_required'),
|
|
'employment_ends_on.date_format' => __('users.errors.end_date_invalid'),
|
|
'supervisor_id.required' => __('users.errors.supervisor_required'),
|
|
'supervisor_id.integer' => __('users.errors.invalid_supervisor'),
|
|
];
|
|
}
|
|
}
|