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.
Comments
Comment #2
yurg commentedComment #3
sahana _n commentedComment #4
sarwan_verma commentedHi @yurg,
I have fixed this issue "Error: Call to a member function id() on null i" and also attached patch,
please review and verify.
Comment #5
sahana _n commentedComment #6
yurg commented@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()":
I'd also checked a few lines starting from the line 56:
How about this (from https://api.drupal.org/api/drupal/core%21lib%21Drupal.php/function/Drupa...):
Also we may want to handle anonymous users, e.g.
Comment #7
marcoliverComment #9
marcoliverHi @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
$uservariable implementsAccountInterface, 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?
Comment #10
yurg commented@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:
After wrapping
foreach ($user->getRoles() as $sUserRole) {toif(!empty($user)) {the issue is gone. Well, mostly: there is a lot ofThe 'entity:user' context is required and not present.but I suppose it comes from somewhere else.Thank you for all your help.
Comment #11
marcoliverHi 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?
Comment #12
yurg commented@marcoliver
Thank you, it looks working with the most recent Version: 3.1.30
Comment #14
marcoliverIssue fork is merged. Will be released in 3.1.31.