Skip to main content
Nowo\DashboardMenuBundle\Service\MenuPermissionCheckerInterface Implement this interface to control which menu items are visible in a given context. The bundle calls canView() for every item when loading the tree. Items that return false are hidden, and any parent (including section titles) whose children all become hidden is automatically pruned.

Interface

namespace Nowo\DashboardMenuBundle\Service;

use Nowo\DashboardMenuBundle\Entity\MenuItem;

interface MenuPermissionCheckerInterface
{
    /**
     * Whether the given menu item should be visible in the current context.
     *
     * @param mixed $context Optional context (e.g. current user, request); depends on implementation
     */
    public function canView(MenuItem $item, mixed $context = null): bool;
}

Method

canView
bool
Return true to show the item, false to hide it.

Parameters

item
MenuItem
required
The menu item being evaluated. Use $item->getPermissionKey() to read the optional permission key configured per item in the dashboard, or inspect any other property.
context
mixed
default:"null"
Contextual value passed by the caller. When invoked from a Twig template via dashboard_menu_tree(), this is the $permissionContext argument (defaults to the current Request object when not supplied). When invoked from the JSON API, this is always null.

Auto-registration

Any service whose class implements MenuPermissionCheckerInterface is automatically discovered and included in the dashboard Permission checker dropdown. You do not need to add any tag in services.yaml. For this to work, the service must be visible to Symfony’s dependency injection container. The standard App\Service\ resource autodiscovery in services.yaml covers this automatically:
services.yaml
services:
  App\:
    resource: '../src/'

Setting the dashboard label

Optionally set a human-readable label for the dashboard dropdown using either approach:

Class constant

public const string DASHBOARD_LABEL = 'Symfony Security checker';

Attribute

use Nowo\DashboardMenuBundle\Attribute\PermissionCheckerLabel;

#[PermissionCheckerLabel('Symfony Security checker')]
final class MyPermissionChecker implements MenuPermissionCheckerInterface
{
    // ...
}
If neither is set, the service ID (typically the FQCN) is used as the label.

Example: Symfony security checker

namespace App\Menu;

use Nowo\DashboardMenuBundle\Attribute\PermissionCheckerLabel;
use Nowo\DashboardMenuBundle\Entity\MenuItem;
use Nowo\DashboardMenuBundle\Service\MenuPermissionCheckerInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;

#[PermissionCheckerLabel('Symfony Security checker')]
final class SecurityMenuPermissionChecker implements MenuPermissionCheckerInterface
{
    public function __construct(
        private readonly AuthorizationCheckerInterface $authorizationChecker,
    ) {
    }

    public function canView(MenuItem $item, mixed $context = null): bool
    {
        // Items with no permission key are always visible
        if ($item->getPermissionKey() === null) {
            return true;
        }

        // Check the permission key against Symfony's security layer
        return $this->authorizationChecker->isGranted($item->getPermissionKey(), $context);
    }
}

Reference implementation: AllowAllMenuPermissionChecker

The bundle ships AllowAllMenuPermissionChecker as the default when no custom checker is configured. It always returns true, meaning every item is visible.
final class AllowAllMenuPermissionChecker implements MenuPermissionCheckerInterface
{
    public const DASHBOARD_LABEL = 'form.menu_type.permission_checker.allow_all';

    public function canView(MenuItem $item, mixed $context = null): bool
    {
        return true;
    }
}
Parents and section headings with no visible children are automatically pruned from the tree — you only need to return false for the leaf items that should be hidden.

Assigning a checker to a menu

Assign your checker to a specific menu in the dashboard UI (per-menu configuration, gear icon). Menus with no checker assigned use the allow-all default. You can also control the order and labels shown in the dashboard dropdown via permission_checker_choices in nowo_dashboard_menu.yaml:
nowo_dashboard_menu.yaml
nowo_dashboard_menu:
  permission_checker_choices:
    App\Menu\SecurityMenuPermissionChecker: 'Symfony Security checker'
    App\Menu\FeatureFlagChecker: 'Feature flags'