Error: Call to a member function id() on null i

Created on 20 October 2023, 8 months ago
Updated 29 January 2024, 5 months ago

Problem/Motivation

WSOD on all site pages for anonymous users, Watchdog error:
Error: Call to a member function id() on null in Drupal\permissions_by_term\Service\AccessCheck->canUserAccessByNode() (line 79 of /web/modules/contrib/permissions_by_term/src/Service/AccessCheck.php)

Steps to reproduce

- Enable / update module
- try to open any website page

Proposed resolution

The simplest: in AccessCheck.php file add an additional check: isset($user) or !empty($user) anywhere near existing if ($user->id()

Perhaps there is a better solution distinguishing anonymous users from logged in.

🐛 Bug report
Status

Fixed

Version

3.1

Component

Code

Created by

🇸🇰Slovakia yurg

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

Merge Requests

Comments & Activities

  • Issue created by @yurg
  • Assigned to Sahana _N
  • Status changed to Needs review 8 months ago
  • 🇮🇳India sarwan

    Hi @yurg,
    I have fixed this issue "Error: Call to a member function id() on null i" and also attached patch,
    please review and verify.

  • Issue was unassigned.
  • 🇸🇰Slovakia yurg

    @sarwan_verma, @sahana _N thank you, there is one more place in the code to edit, it is at line 85 near the "(int) $user->id()":

    if ((int) $user->id() !== (int) $node->getOwnerId() && !$node->isPublished()) {
          return FALSE;
        }
    

    I'd also checked a few lines starting from the line 56:

        if (empty($uid)) {
          $uid = \Drupal::currentUser()->id();
        }
        $user = User::load($uid);
    

    How about this (from https://api.drupal.org/api/drupal/core%21lib%21Drupal.php/function/Drupa...):

    $user = \Drupal\user\Entity\User::load(\Drupal::currentUser()
      ->id());
    

    Also we may want to handle anonymous users, e.g.

    if ($uid === 0)
    // or
    if(isset($user))
    
  • Assigned to marcoliver
  • 🇩🇪Germany marcoliver Neuss, NRW, Germany
  • 🇩🇪Germany marcoliver Neuss, NRW, Germany

    Hi @yurg, I created an Issue Fork & a merge request that go a bit further than what the patch in #4 does.

    Basically we first check whether the $user variable implements AccountInterface, since all the methods we subsequently use are declared in that interface. If it is implemented, we proceed to the tests.

    Can you please check if the issue fork solves your problem?

  • 🇸🇰Slovakia yurg

    @marcoliver Thank you, have tried the patch with the latest Drupal 10.1.6 upgrade. Almost there, there is a block of code where empty $user throws an error, please see below:

    • Error: Call to a member function getRoles() on null in Drupal\permissions_by_term\Service\AccessCheck->isAccessAllowedByDatabase() (line 165 of /permissions_by_term/src/Service/AccessCheck.php).
    • Error: Call to a member function id() on null in Drupal\permissions_by_term\Service\AccessCheck->isAccessAllowedByDatabase() (line 175 of /permissions_by_term/src/Service/AccessCheck.php).

    After wrapping foreach ($user->getRoles() as $sUserRole) { to if(!empty($user)) { the issue is gone. Well, mostly: there is a lot of The 'entity:user' context is required and not present. but I suppose it comes from somewhere else.
    Thank you for all your help.

  • 🇩🇪Germany marcoliver Neuss, NRW, Germany

    Hi yurg,

    I've pushed some more changes to the issue fork. I hope the remaining two errors you experienced should now be gone as well. Can you please check again?

  • 🇸🇰Slovakia yurg

    @marcoliver
    Thank you, it looks working with the most recent Version: 3.1.30

    • marcoliver committed 95f2bfa4 on 3.1.x-dev
      Issue #3395700 by marcoliver, sarwan_verma, yurg: Error: Call to a...
  • Status changed to Fixed 5 months ago
  • 🇩🇪Germany marcoliver Neuss, NRW, Germany

    Issue fork is merged. Will be released in 3.1.31.

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

Production build 0.69.0 2024