Group the main menu into role-aware popover groups

Replace the flat link row with disclosure groups (Työt, Oma kirjaus,
Asiakkaat ja myynti, Hallinto, Ylläpito) built by App\View\MainMenu from
Gate abilities, and add menu links for new job, quick sales list and
workflow templates.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
2026-11-24 19:14:11 +02:00
co-authored by Claude Sonnet 5.5
parent 18598cc08e
commit 48030e7689
10 changed files with 283 additions and 64 deletions
+1
View File
@@ -24,6 +24,7 @@ Read `docs/architecture.md` (Finnish) before adding or changing domain code. `ap
- Modules talk only through another module's Application contracts or events, never its Infrastructure or Eloquent models. Exceptions: `App\Models\User` (Laravel's auth model, owned by `Users`) is used through `auth()`/Gate, and `Users\Domain\Enums\Role` may be used in any module's Gate rules.
- **Authorization:** define abilities with `Gate::define` in the owning module's provider (`users.manage`, `customers.use`, `catalog.manage`, `jobs.view`, `jobs.view-job`, `jobs.manage`, `jobs.archive`, `jobs.set-billing-status`, `jobs.manage-tasks`, `jobs.update-task-status`, `time.use`, `time.book`, `time.approve`, `time.approve-others`, `time.cancel-approval`, `jobs.unlock`, `catalog.view-prices`, `expenses.use`, `expenses.record`, `expenses.record-for-others`, `expenses.change`, `expenses.see-amount`, `sales.use`, `sales.view-all`, `sales.view-sale`, `sales.credit`, `sales.close-day`, `sales.reopen-day`, `billing.manage`, and in `Users` `money.prices`, `money.expenses`, `money.quotes`, `money.billing` for the personal money permissions of specification 2.3). Protect routes with `can:` and also call `$this->authorize()` in each Livewire page's `mount()` and in every action. Mark record ids on Livewire pages `#[Locked]`.
- **Login** is the `Users` module: `Auth::attemptWhen` with `CheckLoginAllowed` (active, intern's last day not passed), and the `EnsureAccountIsUsable` web middleware ends sessions of users who are no longer allowed in. Nobody handles passwords: new users and "forgot password" get the same emailed set-password link, and when an admin creates a user or asks for a new link, the link is also shown to the admin to copy. Admin links use the `admin_links` broker (own table, valid 72 h); forgot-password links use the default broker (60 min); the reset page accepts both, and using or issuing a link ends the user's earlier links. `AuthenticateSession` ends a user's other sessions when the password changes. Passwords need 12+ characters with mixed case, a number and a symbol (`Password::defaults` in `UsersServiceProvider`). Login allows 5 failed attempts a minute per email + IP and 20 per IP; the forgot-password page 5 requests a minute per IP.
- **Main menu**: the layout renders the groups of `App\View\MainMenu` (Työt, Oma kirjaus, Asiakkaat ja myynti, Hallinto, Ylläpito) as disclosure popovers (`resources/views/components/nav-group.blade.php`). A new page is added to `MainMenu::definition()` with the Gate ability of its route; groups with no allowed links disappear.
- **Holidays** are Shared: `Shared\Application\UseCases\HolidayCalendar` gives the Finnish public holidays (`FinnishHolidays`, calculated) minus those the admin took out of use plus company days off (`holiday_overrides`), edited on `shared::holidays` (`/settings/holidays`, gate `settings.manage`).
- **Company details** (name, business ID, address, phone, email) are kept by `Shared\Application\UseCases\CompanyDetailsStore` in the settings (`shared.company_*`) and edited by the admin on `shared::company` (`/settings/company`, gate `settings.manage`). `BusinessId` (Y-tunnus with check digit) is a Shared value object.
- **PDFs** use `barryvdh/laravel-dompdf` with a plain Blade view (no Tailwind; DejaVu Sans), streamed inline: see the quick sale receipt `Sales\Presentation\Http\Controllers\ShowReceipt` and `Sales/Presentation/Views/receipt.blade.php` (view namespace `sales-views`).
+110
View File
@@ -0,0 +1,110 @@
<?php
declare(strict_types=1);
namespace App\View;
use App\Models\User;
use Illuminate\Support\Facades\Gate;
/**
* The main menu: task-oriented groups of links, filtered by what the user may use.
* A new page is added here with the Gate ability that guards its route.
*/
final class MainMenu
{
/**
* @return list<array{key: string, label: string, current: bool, items: list<array{route: string, label: string, current: bool}>}>
*/
public function groups(?User $user): array
{
if ($user === null) {
return [];
}
$gate = Gate::forUser($user);
$groups = [];
foreach ($this->definition() as $key => $group) {
$items = [];
foreach ($group['items'] as $item) {
if ($gate->denies($item['ability'])) {
continue;
}
$items[] = [
'route' => $item['route'],
'label' => __($item['label']),
'current' => request()->routeIs(...$item['pattern']),
];
}
if ($items === []) {
continue;
}
$groups[] = [
'key' => $key,
'label' => __($group['label']),
'current' => in_array(true, array_column($items, 'current'), true),
'items' => $items,
];
}
return $groups;
}
/**
* @return array<string, array{label: string, items: list<array{route: string, pattern: list<string>, label: string, ability: string}>}>
*/
private function definition(): array
{
return [
'work' => [
'label' => 'layout.group_work',
'items' => [
['route' => 'jobs.index', 'pattern' => ['jobs.index', 'jobs.show', 'jobs.edit', 'jobs.tasks.*'], 'label' => 'layout.jobs', 'ability' => 'jobs.view'],
['route' => 'jobs.create', 'pattern' => ['jobs.create'], 'label' => 'layout.new_job', 'ability' => 'jobs.manage'],
['route' => 'jobs.quick', 'pattern' => ['jobs.quick'], 'label' => 'layout.quick_intake', 'ability' => 'jobs.manage'],
['route' => 'jobs.board', 'pattern' => ['jobs.board'], 'label' => 'layout.board', 'ability' => 'jobs.view'],
['route' => 'jobs.calendar', 'pattern' => ['jobs.calendar'], 'label' => 'layout.calendar', 'ability' => 'jobs.view'],
],
],
'own' => [
'label' => 'layout.group_own',
'items' => [
['route' => 'time.week', 'pattern' => ['time.week', 'time.task'], 'label' => 'layout.time', 'ability' => 'time.use'],
['route' => 'expenses.mine', 'pattern' => ['expenses.*'], 'label' => 'layout.expenses', 'ability' => 'expenses.use'],
],
],
'sales' => [
'label' => 'layout.group_sales',
'items' => [
['route' => 'customers.index', 'pattern' => ['customers.*'], 'label' => 'layout.customers', 'ability' => 'customers.use'],
['route' => 'sales.sell', 'pattern' => ['sales.sell'], 'label' => 'layout.quick_sale', 'ability' => 'sales.use'],
['route' => 'sales.index', 'pattern' => ['sales.index', 'sales.receipt'], 'label' => 'layout.sales_list', 'ability' => 'sales.use'],
['route' => 'catalog.price-list', 'pattern' => ['catalog.price-list'], 'label' => 'layout.prices', 'ability' => 'catalog.view-prices'],
],
],
'admin' => [
'label' => 'layout.group_admin',
'items' => [
['route' => 'time.approvals', 'pattern' => ['time.approvals'], 'label' => 'layout.approvals', 'ability' => 'time.approve-others'],
['route' => 'billing.index', 'pattern' => ['billing.*'], 'label' => 'layout.billing', 'ability' => 'billing.manage'],
],
],
'maintenance' => [
'label' => 'layout.group_maintenance',
'items' => [
['route' => 'catalog.index', 'pattern' => ['catalog.index', 'catalog.service-units.*', 'catalog.services.*', 'catalog.work-types.*'], 'label' => 'layout.catalog', 'ability' => 'catalog.manage'],
['route' => 'catalog.workflow-templates', 'pattern' => ['catalog.workflow-templates*'], 'label' => 'layout.workflow_templates', 'ability' => 'catalog.manage'],
['route' => 'catalog.prices', 'pattern' => ['catalog.prices', 'catalog.products.*', 'catalog.vat-rates.*'], 'label' => 'layout.price_list_admin', 'ability' => 'catalog.manage'],
['route' => 'users.index', 'pattern' => ['users.*'], 'label' => 'layout.users', 'ability' => 'users.manage'],
['route' => 'settings.company', 'pattern' => ['settings.company'], 'label' => 'layout.company', 'ability' => 'settings.manage'],
['route' => 'settings.holidays', 'pattern' => ['settings.holidays'], 'label' => 'layout.holidays', 'ability' => 'settings.manage'],
],
],
];
}
}
+11 -1
View File
@@ -9,7 +9,7 @@ return [
'board' => 'Kanban',
'calendar' => 'Kalenteri',
'time' => 'Tuntikirjaus',
'approvals' => 'Hyväksyntä',
'approvals' => 'Tuntien hyväksyntä',
'expenses' => 'Kulut',
'quick_sale' => 'Pikamyynti',
'billing' => 'Laskutus',
@@ -23,4 +23,14 @@ return [
'logout' => 'Kirjaudu ulos',
'theme_toggle' => 'Tumma teema',
'cancel' => 'Peruuta',
'group_work' => 'Työt',
'group_own' => 'Oma kirjaus',
'group_sales' => 'Asiakkaat ja myynti',
'group_admin' => 'Hallinto',
'group_maintenance' => 'Ylläpito',
'new_job' => 'Uusi työ',
'sales_list' => 'Pikamyynnit ja kassapäivät',
'workflow_templates' => 'Työnkulkupohjat',
'price_list_admin' => 'Hinnaston ylläpito',
'current_section' => '(nykyinen osio)',
];
+4
View File
@@ -27,3 +27,7 @@
--color-primary-700: #3730a3;
--color-primary-800: #312e81;
}
[x-cloak] {
display: none !important;
}
@@ -0,0 +1,59 @@
@props(['group'])
{{--
A main menu group: a disclosure button that opens a panel of links (WAI-ARIA disclosure navigation, not role="menu").
Escape and a click outside close it, and so does moving focus out of the group. The current section is
marked with an underline and hidden text, not colour alone, and the current link has aria-current="page".
--}}
<div
class="relative"
x-data="{ open: false }"
x-on:nav-group-opened.window="if ($event.detail !== '{{ $group['key'] }}') open = false"
x-on:keydown.escape="if (open) { open = false; $refs.trigger.focus() }"
x-on:click.outside="open = false"
x-on:focusout="if (open && ! $el.contains($event.relatedTarget)) open = false"
>
<button
type="button"
x-ref="trigger"
x-on:click="open = ! open; if (open) $dispatch('nav-group-opened', '{{ $group['key'] }}')"
x-bind:aria-expanded="open.toString()"
aria-expanded="false"
aria-controls="nav-group-{{ $group['key'] }}"
@class([
'flex items-center gap-1 rounded-md px-3 py-2 text-sm font-medium focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-blue-700',
'text-zinc-900 underline decoration-2 underline-offset-8 dark:text-white' => $group['current'],
'text-zinc-600 hover:bg-zinc-100 hover:text-zinc-900 dark:text-zinc-300 dark:hover:bg-zinc-700 dark:hover:text-white' => ! $group['current'],
])
>
{{ $group['label'] }}
@if ($group['current'])
<span class="sr-only">{{ __('layout.current_section') }}</span>
@endif
<x-icon name="chevron-down" class="size-4 transition-transform" x-bind:class="{ 'rotate-180': open }" aria-hidden="true" />
</button>
<ul
id="nav-group-{{ $group['key'] }}"
x-show="open"
x-cloak
class="z-40 mt-1 min-w-56 rounded-md border border-zinc-200 bg-white p-1 shadow-lg sm:absolute sm:start-0 dark:border-zinc-600 dark:bg-zinc-800"
>
@foreach ($group['items'] as $item)
<li>
<a
href="{{ route($item['route']) }}"
wire:navigate
@if ($item['current']) aria-current="page" @endif
@class([
'block rounded-md px-3 py-2 text-sm font-medium focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-blue-700',
'bg-zinc-100 text-zinc-900 underline decoration-2 underline-offset-4 dark:bg-zinc-700 dark:text-white' => $item['current'],
'text-zinc-700 hover:bg-zinc-100 hover:text-zinc-900 dark:text-zinc-200 dark:hover:bg-zinc-700 dark:hover:text-white' => ! $item['current'],
])
>
{{ $item['label'] }}
</a>
</li>
@endforeach
</ul>
</div>
+3 -47
View File
@@ -55,53 +55,9 @@
</a>
<nav aria-label="{{ __('layout.main_menu') }}" class="flex flex-1 flex-wrap items-center gap-1">
@include('partials.nav-item', ['route' => 'jobs.index', 'pattern' => ['jobs.index', 'jobs.show', 'jobs.create', 'jobs.edit'], 'label' => __('layout.jobs')])
@include('partials.nav-item', ['route' => 'jobs.board', 'pattern' => 'jobs.board', 'label' => __('layout.board')])
@include('partials.nav-item', ['route' => 'jobs.calendar', 'pattern' => 'jobs.calendar', 'label' => __('layout.calendar')])
@include('partials.nav-item', ['route' => 'time.week', 'pattern' => ['time.week', 'time.task'], 'label' => __('layout.time')])
@include('partials.nav-item', ['route' => 'expenses.mine', 'pattern' => 'expenses.*', 'label' => __('layout.expenses')])
@can('sales.use')
@include('partials.nav-item', ['route' => 'sales.sell', 'pattern' => 'sales.*', 'label' => __('layout.quick_sale')])
@endcan
@can('jobs.manage')
@include('partials.nav-item', ['route' => 'jobs.quick', 'pattern' => 'jobs.quick', 'label' => __('layout.quick_intake')])
@endcan
@can('time.approve-others')
@include('partials.nav-item', ['route' => 'time.approvals', 'pattern' => 'time.approvals', 'label' => __('layout.approvals')])
@endcan
@can('billing.manage')
@include('partials.nav-item', ['route' => 'billing.index', 'pattern' => 'billing.*', 'label' => __('layout.billing')])
@endcan
@can('customers.use')
@include('partials.nav-item', ['route' => 'customers.index', 'pattern' => 'customers.*', 'label' => __('layout.customers')])
@endcan
@can('catalog.manage')
@include('partials.nav-item', ['route' => 'catalog.index', 'pattern' => ['catalog.index', 'catalog.service-units.*', 'catalog.services.*', 'catalog.work-types.*'], 'label' => __('layout.catalog')])
@include('partials.nav-item', ['route' => 'catalog.prices', 'pattern' => ['catalog.prices', 'catalog.products.*', 'catalog.vat-rates.*', 'catalog.price-list'], 'label' => __('layout.prices')])
@elsecan('catalog.view-prices')
@include('partials.nav-item', ['route' => 'catalog.price-list', 'pattern' => 'catalog.price-list', 'label' => __('layout.prices')])
@endcan
@can('users.manage')
@include('partials.nav-item', ['route' => 'users.index', 'pattern' => 'users.*', 'label' => __('layout.users')])
@endcan
@can('settings.manage')
@include('partials.nav-item', ['route' => 'settings.company', 'pattern' => 'settings.company', 'label' => __('layout.company')])
@include('partials.nav-item', ['route' => 'settings.holidays', 'pattern' => 'settings.holidays', 'label' => __('layout.holidays')])
@endcan
@foreach (app(\App\View\MainMenu::class)->groups(auth()->user()) as $group)
<x-nav-group :group="$group" />
@endforeach
</nav>
<div class="flex flex-wrap items-center gap-3">
@@ -1,15 +0,0 @@
{{-- Main menu link. The current page is marked with aria-current and an underline, not colour alone. --}}
@php($current = request()->routeIs(...(array) $pattern))
<a
href="{{ route($route) }}"
wire:navigate
@if ($current) aria-current="page" @endif
@class([
'rounded-md px-3 py-2 text-sm font-medium focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-blue-700',
'text-zinc-900 underline decoration-2 underline-offset-8 dark:text-white' => $current,
'text-zinc-600 hover:bg-zinc-100 hover:text-zinc-900 dark:text-zinc-300 dark:hover:bg-zinc-700 dark:hover:text-white' => ! $current,
])
>
{{ $label }}
</a>
+38
View File
@@ -0,0 +1,38 @@
<?php
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
uses(RefreshDatabase::class);
beforeEach(function () {
$this->actingAs(User::factory()->admin()->create());
});
it('opens a menu group and closes it with Escape, returning focus to its button', function () {
visit(route('home'))->on()->desktop()
->assertAttribute('#nav-group-work', 'style', 'display: none;')
->click('Työt')
->assertAttribute('button[aria-controls="nav-group-work"]', 'aria-expanded', 'true')
->assertSee('Pikavastaanotto')
->keys('button[aria-controls="nav-group-work"]', 'Escape')
->assertAttribute('button[aria-controls="nav-group-work"]', 'aria-expanded', 'false')
->assertScript('document.activeElement.getAttribute("aria-controls")', 'nav-group-work');
});
it('has no accessibility issues with a menu group open', function (string $device, string $colorScheme) {
$page = visit(route('users.index'));
$page = $device === 'mobile' ? $page->on()->mobile() : $page->on()->desktop();
$page = $colorScheme === 'dark' ? $page->inDarkMode() : $page->inLightMode();
$page->click('button[aria-controls="nav-group-maintenance"]')
->assertSee('Yritystiedot')
->assertNoJavaScriptErrors()
->assertNoAccessibilityIssues(level: 2);
})->with([
'desktop, light' => ['desktop', 'light'],
'desktop, dark' => ['desktop', 'dark'],
'mobile, light' => ['mobile', 'light'],
'mobile, dark' => ['mobile', 'dark'],
]);
+3 -1
View File
@@ -44,10 +44,12 @@ it('keeps the chosen theme when moving between pages with wire:navigate', functi
->click(THEME_TOGGLE)
->assertScript('document.documentElement.classList.contains("dark")', true);
$page->click('Käyttäjät')
$page->click('[aria-controls="nav-group-maintenance"]')
->click('Käyttäjät')
->assertPathIs('/users')
->assertScript('document.documentElement.classList.contains("dark")', true)
->assertAttribute(THEME_TOGGLE, 'aria-pressed', 'true')
->click('[aria-controls="nav-group-sales"]')
->click('Asiakkaat')
->assertPathIs('/customers')
->assertScript('document.documentElement.classList.contains("dark")', true)
+54
View File
@@ -0,0 +1,54 @@
<?php
use App\Models\User;
use App\View\MainMenu;
use Illuminate\Foundation\Testing\RefreshDatabase;
uses(RefreshDatabase::class);
function menuGroupKeys(User $user): array
{
return array_column(app(MainMenu::class)->groups($user), 'key');
}
function menuLabels(User $user): array
{
return collect(app(MainMenu::class)->groups($user))->flatMap(fn ($group) => array_column($group['items'], 'label'))->all();
}
it('gives a subcontractor only work and own entries without creating jobs', function () {
$user = User::factory()->subcontractor()->create();
expect(menuGroupKeys($user))->toBe(['work', 'own'])
->and(menuLabels($user))->not->toContain('Uusi työ', 'Pikavastaanotto');
});
it('gives an employee work, own entries and customers and sales but no management groups', function () {
$user = User::factory()->create();
expect(menuGroupKeys($user))->toBe(['work', 'own', 'sales'])
->and(menuLabels($user))->toContain('Uusi työ', 'Pikamyynti', 'Hinnasto');
});
it('gives management the admin and maintenance groups but not user or company settings', function () {
$user = User::factory()->management()->create();
expect(menuGroupKeys($user))->toBe(['work', 'own', 'sales', 'admin', 'maintenance'])
->and(menuLabels($user))->toContain('Laskutus', 'Pohjatiedot', 'Työnkulkupohjat')
->and(menuLabels($user))->not->toContain('Käyttäjät', 'Yritystiedot');
});
it('gives an admin everything', function () {
$user = User::factory()->admin()->create();
expect(menuLabels($user))->toContain('Käyttäjät', 'Yritystiedot', 'Arkipyhät', 'Hinnaston ylläpito');
});
it('marks the group of the current page as current', function () {
$this->actingAs(User::factory()->admin()->create());
$this->get(route('users.index'))
->assertOk()
->assertSee('nykyinen osio')
->assertSee('aria-current="page"', false);
});