Call to a member function hasPermission() on null in Drupal\comment\CommentManager->forbiddenMessage()

Created on 30 October 2023, about 1 year ago
Updated 17 January 2024, 12 months ago

Problem/Motivation

Unregistered visitor cannot access a page with a comment field.
Extract from the log:

Error: Call to a member function hasPermission() on null in Drupal\comment\CommentManager->forbiddenMessage() (regel 160 van /var/www/pi3.softpol.net/web/core/modules/comment/src/CommentManager.php)

#0 /var/www/pi3.softpol.net/web/core/modules/comment/src/CommentLinkBuilder.php(183): Drupal\comment\CommentManager->forbiddenMessage()
#1 /var/www/pi3.softpol.net/web/core/modules/comment/comment.module(194): Drupal\comment\CommentLinkBuilder->buildCommentedEntityLinks()
#2 /var/www/pi3.softpol.net/web/core/lib/Drupal/Core/Extension/ModuleHandler.php(562): comment_node_links_alter()
#3 /var/www/pi3.softpol.net/web/core/modules/node/src/NodeViewBuilder.php(108): Drupal\Core\Extension\ModuleHandler->alter()
#4 [internal function]: Drupal\node\NodeViewBuilder::renderLinks()
#5 /var/www/pi3.softpol.net/web/core/lib/Drupal/Core/Security/DoTrustedCallbackTrait.php(101): call_user_func_array()
#6 /var/www/pi3.softpol.net/web/core/lib/Drupal/Core/Render/Renderer.php(788): Drupal\Core\Render\Renderer->doTrustedCallback()
#7 /var/www/pi3.softpol.net/web/core/lib/Drupal/Core/Render/Renderer.php(353): Drupal\Core\Render\Renderer->doCallback()
#8 /var/www/pi3.softpol.net/web/core/lib/Drupal/Core/Render/Renderer.php(446): Drupal\Core\Render\Renderer->doRender()
#9 /var/www/pi3.softpol.net/web/core/lib/Drupal/Core/Render/Renderer.php(204): Drupal\Core\Render\Renderer->doRender()
#10 /var/www/pi3.softpol.net/web/core/lib/Drupal/Core/Template/TwigExtension.php(479): Drupal\Core\Render\Renderer->render()
#11 /var/www/pi3.softpol.net/web/sites/default/files/php/twig/653ffe80db77f_node.html.twig_ERuujdzytaTVlvQolY_fwVf-U/bbPxIQaIHLASDPHq5LL4FWU_HNTl3JFi7_cUQkF7lV4.php(136): Drupal\Core\Template\TwigExtension->escapeFilter()
#12 /var/www/pi3.softpol.net/vendor/twig/twig/src/Template.php(405): __TwigTemplate_0f5dd53f7bef8ec1f9ebc01a90d4a824->doDisplay()
#13 /var/www/pi3.softpol.net/vendor/twig/twig/src/Template.php(378): Twig\Template->displayWithErrorHandling()
#14 /var/www/pi3.softpol.net/vendor/twig/twig/src/Template.php(390): Twig\Template->display()
#15 /var/www/pi3.softpol.net/web/core/themes/engines/twig/twig.engine(55): Twig\Template->render()
#16 /var/www/pi3.softpol.net/web/core/lib/Drupal/Core/Theme/ThemeManager.php(384): twig_render_template()
#17 /var/www/pi3.softpol.net/web/core/lib/Drupal/Core/Render/Renderer.php(433): Drupal\Core\Theme\ThemeManager->render()
#18 /var/www/pi3.softpol.net/web/core/lib/Drupal/Core/Render/Renderer.php(204): Drupal\Core\Render\Renderer->doRender()
#19 /var/www/pi3.softpol.net/web/core/lib/Drupal/Core/Render/MainContent/HtmlRenderer.php(242): Drupal\Core\Render\Renderer->render()
#20 /var/www/pi3.softpol.net/web/core/lib/Drupal/Core/Render/Renderer.php(580): Drupal\Core\Render\MainContent\HtmlRenderer->Drupal\Core\Render\MainContent\{closure}()
#21 /var/www/pi3.softpol.net/web/core/lib/Drupal/Core/Render/MainContent/HtmlRenderer.php(243): Drupal\Core\Render\Renderer->executeInRenderContext()
#22 /var/www/pi3.softpol.net/web/core/lib/Drupal/Core/Render/MainContent/HtmlRenderer.php(132): Drupal\Core\Render\MainContent\HtmlRenderer->prepare()
#23 /var/www/pi3.softpol.net/web/core/lib/Drupal/Core/EventSubscriber/MainContentViewSubscriber.php(90): Drupal\Core\Render\MainContent\HtmlRenderer->renderResponse()
#24 [internal function]: Drupal\Core\EventSubscriber\MainContentViewSubscriber->onViewRenderArray()
#25 /var/www/pi3.softpol.net/web/core/lib/Drupal/Component/EventDispatcher/ContainerAwareEventDispatcher.php(142): call_user_func()
#26 /var/www/pi3.softpol.net/vendor/symfony/http-kernel/HttpKernel.php(174): Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch()
#27 /var/www/pi3.softpol.net/vendor/symfony/http-kernel/HttpKernel.php(81): Symfony\Component\HttpKernel\HttpKernel->handleRaw()
#28 /var/www/pi3.softpol.net/web/core/lib/Drupal/Core/StackMiddleware/Session.php(58): Symfony\Component\HttpKernel\HttpKernel->handle()
#29 /var/www/pi3.softpol.net/web/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php(48): Drupal\Core\StackMiddleware\Session->handle()
#30 /var/www/pi3.softpol.net/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(191): Drupal\Core\StackMiddleware\KernelPreHandle->handle()
#31 /var/www/pi3.softpol.net/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(128): Drupal\page_cache\StackMiddleware\PageCache->fetch()
#32 /var/www/pi3.softpol.net/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(82): Drupal\page_cache\StackMiddleware\PageCache->lookup()
#33 /var/www/pi3.softpol.net/web/core/modules/ban/src/BanMiddleware.php(50): Drupal\page_cache\StackMiddleware\PageCache->handle()
#34 /var/www/pi3.softpol.net/web/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php(48): Drupal\ban\BanMiddleware->handle()
#35 /var/www/pi3.softpol.net/web/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php(51): Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle()
#36 /var/www/pi3.softpol.net/vendor/stack/builder/src/Stack/StackedHttpKernel.php(23): Drupal\Core\StackMiddleware\NegotiationMiddleware->handle()
#37 /var/www/pi3.softpol.net/web/core/lib/Drupal/Core/DrupalKernel.php(718): Stack\StackedHttpKernel->handle()
#38 /var/www/pi3.softpol.net/web/index.php(19): Drupal\Core\DrupalKernel->handle()
#39 {main}

Steps to reproduce

There are 2 conditions for this error:
- unregistered user (no problem for the admin user)
- page with a comment field (no problem if the field is removed)

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

πŸ’¬ Support request
Status

Fixed

Version

9.5

Component
CommentΒ  β†’

Last updated about 13 hours ago

Created by

πŸ‡§πŸ‡ͺBelgium softpol

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @softpol
  • Status changed to Postponed: needs info about 1 year ago
  • πŸ‡¦πŸ‡ΊAustralia larowlan πŸ‡¦πŸ‡ΊπŸ.au GMT+10

    Can we get steps to reproduce here, starting from install Drupal

    Thanks

  • πŸ‡§πŸ‡ͺBelgium softpol

    Main parameters of the website:

    • Drupal-versie: 9.5.11
    • Webserver: Apache/2.4.56 (Debian)
    • Database versie: 10.5.21-MariaDB-0+deb11u1
    • PHP versie: 8.1.21

    Steps to produce the problem:
    1. create a content type
    2. Add a number of fields (e.g. of type "entity reference", text, file) to the content type

    [so far, so good: all pages of that content type can be visited by an unregistered user]

    3. add a field of type "comments" [not sure if this is the correct english field type name, as I translated it from Dutch - the machine name is field_blog_comments]

    [an unregistered vistor now gets a blank screen with the message:

    The website encountered an unexpected error. Please try again later.
    Error: Call to a member function hasPermission() on null in Drupal\comment\CommentManager->forbiddenMessage() (line 160 of core/modules/comment/src/CommentManager.php).

    ]

  • πŸ‡§πŸ‡ͺBelgium softpol

    No…

    I created the sandbox https://master-gahutiy2psj6zbxspb1qabbkeov4rt0c.tugboatqa.com.
    Then added some taxonomy terms (Auteur, Jaargang and Categorie) and a content type "Jaarboekartikel" with 6 fields (Auteur, Jaargang, Categorie, Nabeschouwing, PDF and Reactie).
    Then created /node/1 with that content type. Uploaded a PDF file "Onbeschikbaar.pdf".
    Logged out and accessed the page as unregistered user. This succeeds on this sandbox, but on my production site, I get a

    "The website encountered an unexpected error. Please try again later.
    Error: Call to a member function hasPermission() on null in Drupal\comment\CommentManager->forbiddenMessage() (line 160 of core/modules/comment/src/CommentManager.php)."

  • πŸ‡§πŸ‡ͺBelgium softpol

    How to reproduce:
    1. Allow anonymous visitor to "see comments"
    2. Create a blog with "open for comments"
    3. Add (and approve) a comment
    4. Anonymous visitor tries to read the Blog => gets a white screen, with an error message (see above) at the top.

    The error does not show up when:
    - anonymous visitor is disallowed to "see comments"; or
    - blog has no comments.

  • πŸ‡¦πŸ‡ΊAustralia larowlan πŸ‡¦πŸ‡ΊπŸ.au GMT+10

    I can't reproduce this with a fresh install.

    Looking at that code path, it would indicate you're missing the 'Authenticated user' role - i.e. a role with ID 'authenticated'

    Is it possible you've deleted this role or it has been deleted as part of a configuration dependency issue?

  • πŸ‡§πŸ‡ͺBelgium softpol

    I only have 2 system roles: (admin and anonymous) and 1 user-defined role "bestuurslid" (= board member).
    I certainly have not intentionally removed the "authenticated user" role, but some Googling learned me that it may happen by removing a module.

    But so far, Google could not tell me how to restore a system role (apart from digging directly in the database?). This problem is lingering around for quite some time here, so recent backups will not be very helpful…
    My test server has the same problem. It was loaded with a production image with the backup_migrate module.

    BTW: there is also another problem that may be related to the above: I cannot assign a role to an existing account. I get a message "Illegal choice authenticated in Rollen element.". Assigning a role when creating an account does work.

  • πŸ‡§πŸ‡ͺBelgium gorkagr

    Hi!

    For restoring your missing role, you can do either:
    - create it via the UI and set the machine name "authenticated"
    or
    - if you have a second drupal site, get the file from config/sync/user.role.authenticated.yml, copy it into the faulty site, open it with the editor to double check first you dont have any permission you should not, and then run with drush a config import (drush cim) so it is restored.

    Most likely, if you dont have that role, the problems might come from there

  • πŸ‡§πŸ‡ͺBelgium gorkagr

    Taking a look at the code lines of your error:

          $this->authenticatedCanPostComments = $this->entityTypeManager
            ->getStorage('user_role')
            ->load(RoleInterface::AUTHENTICATED_ID)
            ->hasPermission('post comments');
    

    the code is trying to load the 'authenticated' user. If you are missing it from your system, then it will be null and hasPermission will fail obviously.

    So try to restore the user as I have mentioned before :)

  • πŸ‡§πŸ‡ͺBelgium softpol

    I recreated the 'Authenticated user' role on my testserver (via the UI) and the problem indeed looks to be gone. WIll tomorrow do the same on the production server.
    Many thanks for helping me with this strange problem.

  • Status changed to Fixed 12 months ago
  • πŸ‡§πŸ‡ͺBelgium gorkagr

    As it seems to be fixed, we can set the issue as fixed I believe :)

  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024