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.

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

yurg created an issue. See original summary.

yurg’s picture

Issue summary: View changes
sahana _n’s picture

Assigned: Unassigned » sahana _n
sarwan_verma’s picture

Status: Active » Needs review
StatusFileSize
new482 bytes

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.

sahana _n’s picture

Assigned: sahana _n » Unassigned
yurg’s picture

@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))
marcoliver’s picture

Assigned: Unassigned » marcoliver

marcoliver’s picture

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?

yurg’s picture

@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.

marcoliver’s picture

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?

yurg’s picture

@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...
marcoliver’s picture

Status: Needs review » Fixed

Issue fork is merged. Will be released in 3.1.31.

Status: Fixed » Closed (fixed)

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