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
@@ -0,0 +1,30 @@
<?php
use App\Models\User;
use App\Modules\Customers\Infrastructure\Persistence\Eloquent\CustomerModel;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Features\SupportLockedProperties\CannotUpdateLockedPropertyException;
use Livewire\Livewire;
uses(RefreshDatabase::class);
it('lets every role except subcontractors use the customer pages', function (string $state, int $status) {
$user = User::factory()->{$state}()->create();
$this->actingAs($user)->get(route('customers.index'))->assertStatus($status);
$this->actingAs($user)->get(route('customers.create'))->assertStatus($status);
})->with([
'admin' => ['admin', 200],
'management' => ['management', 200],
'employee' => ['employee', 200],
'intern' => ['intern', 200],
'subcontractor' => ['subcontractor', 403],
]);
it('does not let the customer id be changed in the browser', function () {
$customer = CustomerModel::factory()->create();
Livewire::actingAs(User::factory()->create())
->test('customers::form')
->set('customerId', $customer->id);
})->throws(CannotUpdateLockedPropertyException::class);
@@ -0,0 +1,32 @@
<?php
use App\Models\User;
use App\Modules\Users\Domain\Enums\Role;
use Illuminate\Auth\Notifications\ResetPassword;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Notification;
uses(RefreshDatabase::class);
it('creates an admin and emails a set-password link', function () {
Notification::fake();
$this->artisan('users:create-admin', ['name' => 'Anna Admin', 'email' => '[email protected]'])
->assertSuccessful();
$admin = User::query()->sole();
expect($admin->role)->toBe(Role::Admin)
->and($admin->email)->toBe('[email protected]');
Notification::assertSentTo($admin, ResetPassword::class);
});
it('fails for an email address that is in use', function () {
User::factory()->create(['email' => '[email protected]']);
$this->artisan('users:create-admin', ['name' => 'Anna Admin', 'email' => '[email protected]'])
->assertFailed();
expect(User::query()->count())->toBe(1);
});
+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'));
});
@@ -0,0 +1,91 @@
<?php
use App\Models\User;
use Illuminate\Auth\Notifications\ResetPassword;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Notification;
use Illuminate\Support\Facades\Password;
use Livewire\Livewire;
uses(RefreshDatabase::class);
it('emails a Finnish set-password link to a known address', function () {
Notification::fake();
$user = User::factory()->create(['email' => '[email protected]']);
$this->get(route('password.request'))->assertOk();
Livewire::test('users::forgot-password')
->set('email', '[email protected]')
->call('sendLink')
->assertHasNoErrors()
->assertSee('Jos osoitteella on käyttäjätunnus');
Notification::assertSentTo($user, ResetPassword::class, function (ResetPassword $notification) use ($user) {
$mail = $notification->toMail($user);
return $mail->subject === 'Aseta salasana'
&& str_contains($mail->actionUrl, '/reset-password/'.$notification->token);
});
});
it('renders the whole set-password email in Finnish', function () {
$user = User::factory()->create();
$html = (string) (new ResetPassword('token'))->toMail($user)->render();
expect($html)->toContain('Aseta salasana')
->toContain('kopioi alla oleva osoite')
->toContain('Kaikki oikeudet pidätetään.')
->not->toContain('All rights reserved');
});
it('shows the same message for an unknown address', function () {
Notification::fake();
Livewire::test('users::forgot-password')
->set('email', '[email protected]')
->call('sendLink')
->assertHasNoErrors()
->assertSee('Jos osoitteella on käyttäjätunnus');
Notification::assertNothingSent();
});
it('sets a new password with a valid link', function () {
$user = User::factory()->create(['email' => '[email protected]']);
$token = Password::createToken($user);
$this->get(route('password.reset', ['token' => $token, 'email' => $user->email]))
->assertOk()
->assertSee('Aseta salasana');
Livewire::test('users::reset-password', ['token' => $token])
->set('email', '[email protected]')
->set('password', 'uusi-salasana-123')
->set('password_confirmation', 'uusi-salasana-123')
->call('resetPassword')
->assertHasNoErrors()
->assertRedirect(route('login'));
expect(Hash::check('uusi-salasana-123', $user->fresh()->password))->toBeTrue();
});
it('rejects a short password and an invalid link', function () {
$user = User::factory()->create(['email' => '[email protected]']);
Livewire::test('users::reset-password', ['token' => 'väärä'])
->set('email', '[email protected]')
->set('password', 'lyhyt')
->set('password_confirmation', 'lyhyt')
->call('resetPassword')
->assertHasErrors(['password'])
->set('password', 'uusi-salasana-123')
->set('password_confirmation', 'uusi-salasana-123')
->call('resetPassword')
->assertHasErrors(['email'])
->assertSee('Linkki on vanhentunut tai virheellinen.');
expect(Hash::check('password', $user->fresh()->password))->toBeTrue();
});
@@ -0,0 +1,143 @@
<?php
use App\Models\User;
use App\Modules\Users\Domain\Enums\Role;
use Illuminate\Auth\Notifications\ResetPassword;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Notification;
use Livewire\Features\SupportLockedProperties\CannotUpdateLockedPropertyException;
use Livewire\Livewire;
uses(RefreshDatabase::class);
beforeEach(function () {
Notification::fake();
$this->admin = User::factory()->admin()->create(['name' => 'Anna Admin']);
});
it('only lets admins manage users', function (string $state, int $status) {
$user = User::factory()->{$state}()->create();
$this->actingAs($user)->get(route('users.index'))->assertStatus($status);
$this->actingAs($user)->get(route('users.create'))->assertStatus($status);
})->with([
'admin' => ['admin', 200],
'management' => ['management', 403],
'employee' => ['employee', 403],
'intern' => ['intern', 403],
'subcontractor' => ['subcontractor', 403],
]);
it('lists users with their role and status', function () {
$supervisor = User::factory()->create(['name' => 'Eero Työntekijä']);
User::factory()->intern('2027-05-31', $supervisor)->create(['name' => 'Ida Harjoittelija']);
User::factory()->inactive()->create(['name' => 'Pekka Poissa']);
$this->actingAs($this->admin)->get(route('users.index'))
->assertOk()
->assertSee('Ida Harjoittelija')
->assertSee('Harjoittelija')
->assertSee('Vastuuhenkilö: Eero Työntekijä')
->assertSee('Päättyy 31.5.2027')
->assertSee('Ei käytössä');
});
it('creates an intern and emails a set-password link', function () {
$supervisor = User::factory()->create();
Livewire::actingAs($this->admin)
->test('users::form')
->set('form.name', 'Ida Harjoittelija')
->set('form.email', '[email protected]')
->set('form.role', 'intern')
->set('form.employment_ends_on', '2027-05-31')
->set('form.supervisor_id', (string) $supervisor->id)
->call('save')
->assertHasNoErrors()
->assertRedirect(route('users.index'));
$intern = User::query()->where('email', '[email protected]')->sole();
expect($intern->role)->toBe(Role::Intern)
->and($intern->employment_ends_on?->toDateString())->toBe('2027-05-31')
->and($intern->supervisor_id)->toBe($supervisor->id);
Notification::assertSentTo($intern, ResetPassword::class);
});
it('requires an end date and a supervisor for an intern', function () {
Livewire::actingAs($this->admin)
->test('users::form')
->set('form.name', 'Ida')
->set('form.email', '[email protected]')
->set('form.role', 'intern')
->call('save')
->assertHasErrors(['form.employment_ends_on', 'form.supervisor_id'])
->assertSee('Anna harjoittelun viimeinen päivä.');
});
it('rejects an admin as an intern supervisor', function () {
Livewire::actingAs($this->admin)
->test('users::form')
->set('form.name', 'Ida')
->set('form.email', '[email protected]')
->set('form.role', 'intern')
->set('form.employment_ends_on', '2027-05-31')
->set('form.supervisor_id', (string) $this->admin->id)
->call('save')
->assertHasErrors(['form.supervisor_id']);
});
it('rejects an email address that is in use', function () {
Livewire::actingAs($this->admin)
->test('users::form')
->set('form.name', 'Toinen Anna')
->set('form.email', strtoupper($this->admin->email))
->call('save')
->assertHasErrors(['form.email'])
->assertSee('Tällä sähköpostiosoitteella on jo käyttäjä.');
});
it('deactivates a user', function () {
$user = User::factory()->create();
Livewire::actingAs($this->admin)
->test('users::form', ['user' => $user->id])
->set('form.active', false)
->call('save')
->assertHasNoErrors();
expect($user->fresh()->is_active)->toBeFalse();
});
it('does not let the last admin deactivate themselves', function () {
Livewire::actingAs($this->admin)
->test('users::form', ['user' => $this->admin->id])
->set('form.active', false)
->call('save')
->assertHasErrors(['form.role'])
->assertSee('Viimeistä aktiivista pääkäyttäjää ei voi poistaa käytöstä');
expect($this->admin->fresh()->is_active)->toBeTrue();
});
it('sends a set-password link from the edit page', function () {
$user = User::factory()->create(['email' => '[email protected]']);
Livewire::actingAs($this->admin)
->test('users::form', ['user' => $user->id])
->call('sendPasswordLink')
->assertSee('Lähetimme linkin salasanan asettamiseen osoitteeseen [email protected].');
Notification::assertSentTo($user, ResetPassword::class);
});
it('does not let the user id be changed in the browser', function () {
Livewire::actingAs($this->admin)
->test('users::form')
->set('userId', 1);
})->throws(CannotUpdateLockedPropertyException::class);
it('returns 404 for an unknown user', function () {
$this->actingAs($this->admin)->get(route('users.edit', 999))->assertNotFound();
});