Add login, password reset and user management (Users module)

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]>
This commit is contained in:
2026-11-22 11:03:00 +02:00
co-authored by Claude Opus 5.5
parent 73d535278d
commit 8c7cb9e3b4
76 changed files with 2978 additions and 62 deletions
+118
View File
@@ -0,0 +1,118 @@
<?php
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\RateLimiter;
use Livewire\Livewire;
uses(RefreshDatabase::class);
beforeEach(function () {
$this->travelTo(now('Europe/Helsinki')->setDate(2027, 5, 31)->setTime(12, 0));
});
it('sends guests to the login page and back where they were going', function () {
$user = User::factory()->create(['email' => '[email protected]']);
$this->get(route('customers.index'))->assertRedirect(route('login'));
$this->get(route('login'))->assertOk()->assertSee('Kirjaudu sisään');
Livewire::test('users::login')
->set('email', ' [email protected] ')
->set('password', 'password')
->call('login')
->assertHasNoErrors()
->assertRedirect(route('customers.index'));
$this->assertAuthenticatedAs($user);
});
it('rejects a wrong password', function () {
User::factory()->create(['email' => '[email protected]']);
Livewire::test('users::login')
->set('email', '[email protected]')
->set('password', 'väärä')
->call('login')
->assertHasErrors(['email'])
->assertSee('Sähköposti tai salasana on väärin.')
->assertSet('password', '');
$this->assertGuest();
});
it('does not let a deactivated user or an intern past the last day log in', function (string $state) {
$factory = User::factory();
$user = match ($state) {
'inactive' => $factory->inactive()->create(),
'intern ended yesterday' => $factory->intern('2027-05-30')->create(),
};
Livewire::test('users::login')
->set('email', $user->email)
->set('password', 'password')
->call('login')
->assertHasErrors(['email'])
->assertSee('Käyttöoikeutesi on päättynyt.');
$this->assertGuest();
})->with(['inactive', 'intern ended yesterday']);
it('lets an intern log in on the last day', function () {
$intern = User::factory()->intern('2027-05-31')->create();
Livewire::test('users::login')
->set('email', $intern->email)
->set('password', 'password')
->call('login')
->assertHasNoErrors();
$this->assertAuthenticatedAs($intern);
});
it('blocks login after five failed attempts', function () {
RateLimiter::clear('[email protected]|127.0.0.1');
User::factory()->create(['email' => '[email protected]']);
$login = Livewire::test('users::login')->set('email', '[email protected]');
foreach (range(1, 5) as $attempt) {
$login->set('password', 'väärä')->call('login');
}
$login->set('password', 'password')
->call('login')
->assertHasErrors(['email'])
->assertSee('Liian monta kirjautumisyritystä.');
$this->assertGuest();
});
it('ends the session of a user who is deactivated while signed in', function () {
$user = User::factory()->create();
$this->actingAs($user)->get(route('customers.index'))->assertOk();
$user->update(['is_active' => false]);
$this->get(route('customers.index'))
->assertRedirect(route('login'))
->assertSessionHas('status', 'Käyttöoikeutesi on päättynyt. Ota yhteyttä pääkäyttäjään.');
$this->assertGuest();
});
it('logs out', function () {
$this->actingAs(User::factory()->create())
->post(route('logout'))
->assertRedirect(route('login'));
$this->assertGuest();
});
it('sends signed-in users away from the login page', function () {
$this->actingAs(User::factory()->create())
->get(route('login'))
->assertRedirect(route('home'));
});