public function filterLayout(
        MenuLayoutDataInterface $layout,
        $user,
        MenuPermissionsIndexDataInterface $permissionsIndex = null
    ) {
        if ($user && ! ($user instanceof UserInterface)) {
            throw new UnexpectedValueException("filterLayout expects UserInterface or boolean false");
        }

        if ($user && $user->isAdmin()) {
            return $layout;
        }

        $this->userPermissions = [];

        if (null !== $permissionsIndex) {
            $this->permissionsIndex = $permissionsIndex;
        }

        if ( ! $this->permissionsIndex) {
            throw new RuntimeException('FilterLayout requires permissions index data to be set');
        }

        // If user has all permissions, no need to walk though the tree
        if ($this->userHasAllPermissions($this->permissionsIndex->permissions())) {
            return $layout;
        }

        // Get access rights for all permissions
        foreach ($this->permissionsIndex->permissions() as $permission) {
            $this->userPermissions[ $permission] = $user && $user->can($permission);
        }

        $layout->setLayout($this->filterLayoutLayer($layout->layout()));

        return $layout;
    }