Problem/Motivation

I ran into this when I tried to use the "Send newsletter issue" action from an ECA model. The action does an access check:

  public function access($node, ?AccountInterface $account = NULL, $return_as_object = FALSE) {
    if ($node->hasField('simplenews_issue') && $node->simplenews_issue->target_id != NULL) {
      return AccessResult::allowedIfHasPermission($account, 'administer newsletters')
        ->orIf(AccessResult::allowedIfHasPermission($account, 'send newsletter'));
    }
    return AccessResult::neutral();
  }

But I got this this error:

TypeError: Drupal\Core\Access\AccessResult::allowedIfHasPermission(): Argument #1 ($account) must be of type Drupal\Core\Session\AccountInterface, null given ...

Steps to reproduce

Just a simple ECA model:
Event: Update content entity, Content: Any Simplenews-enabled node
Action: User, switch current account (account must have permission to administer or send newsletters)
Action: Send newsletter issue, entity: entity (entity is the content entity referenced in the event)

Proposed resolution

The ECA maintainer says the access function needs to provide for the case when the $account variable isn't available, so I added this:

if (!$account) {
  $account = \Drupal::currentUser();
}

It looks as if the same code is used in StopIssue.php so will include that in a merge request.

Remaining tasks

Merge request pending.

User interface changes

None.

API changes

None.

Data model changes

None.

Issue fork simplenews-3568037

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

rclemings created an issue. See original summary.

rclemings’s picture

Issue summary: View changes

rclemings’s picture

Assigned: rclemings » Unassigned
rclemings’s picture

Status: Active » Needs review

adamps made their first commit to this issue’s fork.

  • adamps committed 03dd75fc on 4.x authored by rclemings
    fix: #3568037 Null $account in SendIssue.php and StopIssue.php
    
    By:...
adamps’s picture

Status: Needs review » Fixed

Thanks

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.