From 48030e768910a8fb04ac1b418b766613c6166278 Mon Sep 17 00:00:00 2001 From: Petri Saarinen Date: Tue, 24 Nov 2026 19:14:11 +0200 Subject: [PATCH] Group the main menu into role-aware popover groups MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- CLAUDE.md | 1 + app/View/MainMenu.php | 110 ++++++++++++++++++ lang/fi/layout.php | 12 +- resources/css/app.css | 4 + .../views/components/nav-group.blade.php | 59 ++++++++++ resources/views/layouts/app.blade.php | 50 +------- resources/views/partials/nav-item.blade.php | 15 --- tests/Browser/MainMenuTest.php | 38 ++++++ tests/Browser/ThemeToggleTest.php | 4 +- tests/Feature/MainMenuTest.php | 54 +++++++++ 10 files changed, 283 insertions(+), 64 deletions(-) create mode 100644 app/View/MainMenu.php create mode 100644 resources/views/components/nav-group.blade.php delete mode 100644 resources/views/partials/nav-item.blade.php create mode 100644 tests/Browser/MainMenuTest.php create mode 100644 tests/Feature/MainMenuTest.php diff --git a/CLAUDE.md b/CLAUDE.md index 7313320..9175419 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -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`). diff --git a/app/View/MainMenu.php b/app/View/MainMenu.php new file mode 100644 index 0000000..ac38e4f --- /dev/null +++ b/app/View/MainMenu.php @@ -0,0 +1,110 @@ +}> + */ + 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, 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'], + ], + ], + ]; + } +} diff --git a/lang/fi/layout.php b/lang/fi/layout.php index abf1eb5..6c40b53 100644 --- a/lang/fi/layout.php +++ b/lang/fi/layout.php @@ -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)', ]; diff --git a/resources/css/app.css b/resources/css/app.css index ce7fc15..cf1dbe4 100644 --- a/resources/css/app.css +++ b/resources/css/app.css @@ -27,3 +27,7 @@ --color-primary-700: #3730a3; --color-primary-800: #312e81; } + +[x-cloak] { + display: none !important; +} diff --git a/resources/views/components/nav-group.blade.php b/resources/views/components/nav-group.blade.php new file mode 100644 index 0000000..8b1bb24 --- /dev/null +++ b/resources/views/components/nav-group.blade.php @@ -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". +--}} +
+ + + +
diff --git a/resources/views/layouts/app.blade.php b/resources/views/layouts/app.blade.php index d6a2401..38bf1b2 100644 --- a/resources/views/layouts/app.blade.php +++ b/resources/views/layouts/app.blade.php @@ -55,53 +55,9 @@
diff --git a/resources/views/partials/nav-item.blade.php b/resources/views/partials/nav-item.blade.php deleted file mode 100644 index 2882fe4..0000000 --- a/resources/views/partials/nav-item.blade.php +++ /dev/null @@ -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)) - - $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 }} - diff --git a/tests/Browser/MainMenuTest.php b/tests/Browser/MainMenuTest.php new file mode 100644 index 0000000..2940da3 --- /dev/null +++ b/tests/Browser/MainMenuTest.php @@ -0,0 +1,38 @@ +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'], +]); diff --git a/tests/Browser/ThemeToggleTest.php b/tests/Browser/ThemeToggleTest.php index 9ea2a59..aeb7f18 100644 --- a/tests/Browser/ThemeToggleTest.php +++ b/tests/Browser/ThemeToggleTest.php @@ -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) diff --git a/tests/Feature/MainMenuTest.php b/tests/Feature/MainMenuTest.php new file mode 100644 index 0000000..ce6b42c --- /dev/null +++ b/tests/Feature/MainMenuTest.php @@ -0,0 +1,54 @@ +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); +});