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]>
This commit is contained in:
@@ -12,6 +12,7 @@ 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 App\Modules\Users\Domain\Exceptions\SupervisorHasInterns;
|
||||
use Illuminate\Validation\Rule;
|
||||
use Livewire\Form;
|
||||
|
||||
@@ -75,6 +76,7 @@ class UserForm extends Form
|
||||
InternRequiresEndDateAndSupervisor::class => ['supervisor_id', 'users.errors.supervisor_required'],
|
||||
InvalidSupervisor::class => ['supervisor_id', 'users.errors.invalid_supervisor'],
|
||||
LastAdminCannotBeRemoved::class => ['role', 'users.errors.last_admin'],
|
||||
SupervisorHasInterns::class => ['role', 'users.errors.supervisor_has_interns'],
|
||||
default => throw $exception,
|
||||
};
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
use App\Modules\Users\Application\Contracts\PasswordLinkSender;
|
||||
use App\Modules\Users\Application\UseCases\RequestPasswordReset;
|
||||
use App\Modules\Users\Domain\ValueObjects\EmailAddress;
|
||||
use Livewire\Component;
|
||||
|
||||
@@ -11,17 +11,19 @@ new class extends Component
|
||||
public bool $linkRequested = false;
|
||||
|
||||
/**
|
||||
* Shows the same message whether or not the address has an account, so
|
||||
* the page cannot be used to find out which addresses are in use.
|
||||
* Shows the same message whether or not the address has a usable
|
||||
* account, so the page cannot be used to find out which addresses exist.
|
||||
*/
|
||||
public function sendLink(PasswordLinkSender $passwordLinks): void
|
||||
public function sendLink(RequestPasswordReset $requestPasswordReset): void
|
||||
{
|
||||
$this->email = trim($this->email);
|
||||
|
||||
$this->validate(
|
||||
['email' => ['required', 'string', 'email:filter', 'max:255']],
|
||||
['email.required' => __('auth.email_required'), 'email.email' => __('auth.email_invalid'), 'email.max' => __('auth.email_invalid')],
|
||||
);
|
||||
|
||||
$passwordLinks->sendSetupLink(new EmailAddress($this->email));
|
||||
$requestPasswordReset->execute(new EmailAddress($this->email));
|
||||
|
||||
$this->linkRequested = true;
|
||||
}
|
||||
|
||||
@@ -58,8 +58,11 @@ new class extends Component
|
||||
|
||||
try {
|
||||
if ($this->userId === null) {
|
||||
$createUser->execute($data);
|
||||
session()->flash('status', __('users.created', ['name' => $data->name]));
|
||||
$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]));
|
||||
|
||||
@@ -17,6 +17,18 @@ new class extends Component
|
||||
|
||||
public bool $remember = false;
|
||||
|
||||
/**
|
||||
* Set when EnsureAccountIsUsable ended the session. Passed in the URL,
|
||||
* not flashed: a Livewire request that gets redirected here would
|
||||
* consume a flash message before the browser shows the page.
|
||||
*/
|
||||
public bool $accessEnded = false;
|
||||
|
||||
public function mount(): void
|
||||
{
|
||||
$this->accessEnded = request()->query('access') === 'ended';
|
||||
}
|
||||
|
||||
public function login(CheckLoginAllowed $checkLoginAllowed): void
|
||||
{
|
||||
$this->email = trim($this->email);
|
||||
@@ -39,15 +51,9 @@ new class extends Component
|
||||
]);
|
||||
}
|
||||
|
||||
$accountDisabled = false;
|
||||
|
||||
$signedIn = Auth::attemptWhen(
|
||||
['email' => $email, 'password' => $this->password],
|
||||
function ($user) use ($checkLoginAllowed, &$accountDisabled): bool {
|
||||
$accountDisabled = ! $checkLoginAllowed->execute($user->getAuthIdentifier());
|
||||
|
||||
return ! $accountDisabled;
|
||||
},
|
||||
fn ($user): bool => $checkLoginAllowed->execute($user->getAuthIdentifier()),
|
||||
$this->remember,
|
||||
);
|
||||
|
||||
@@ -55,9 +61,9 @@ new class extends Component
|
||||
RateLimiter::hit($throttleKey);
|
||||
$this->reset('password');
|
||||
|
||||
throw ValidationException::withMessages([
|
||||
'email' => __($accountDisabled ? 'auth.account_disabled' : 'auth.failed'),
|
||||
]);
|
||||
// One message for a wrong password and a disabled account, so the
|
||||
// form never confirms that a disabled account's password is right.
|
||||
throw ValidationException::withMessages(['email' => __('auth.failed')]);
|
||||
}
|
||||
|
||||
RateLimiter::clear($throttleKey);
|
||||
@@ -76,7 +82,10 @@ new class extends Component
|
||||
<div class="mx-auto max-w-md space-y-6 p-4 sm:p-6 sm:pt-16">
|
||||
<flux:heading size="xl" level="1">{{ __('auth.login_title') }}</flux:heading>
|
||||
|
||||
@if (session('status'))
|
||||
@if ($accessEnded)
|
||||
{{-- Not variant="warning": its yellow heading is only 2.8:1 on the light background. --}}
|
||||
<flux:callout icon="exclamation-triangle" role="status" :heading="__('auth.account_disabled')" />
|
||||
@elseif (session('status'))
|
||||
<flux:callout variant="success" icon="information-circle" role="status" :heading="session('status')" />
|
||||
@endif
|
||||
|
||||
|
||||
Reference in New Issue
Block a user