You are here

protected function BaseUpdateRunner::switchUser in Scheduled Updates 8

Switch to another user to run an update if necessary.

Parameters

\Drupal\scheduled_updates\ScheduledUpdateInterface $update:

\Drupal\Core\Entity\ContentEntityInterface $entity_to_update:

1 call to BaseUpdateRunner::switchUser()
BaseUpdateRunner::runUpdate in src/Plugin/BaseUpdateRunner.php
Run an individual update from the queue.

File

src/Plugin/BaseUpdateRunner.php, line 637
Contains \Drupal\scheduled_updates\Plugin\BaseUpdateRunner.

Class

BaseUpdateRunner

Namespace

Drupal\scheduled_updates\Plugin

Code

protected function switchUser(ScheduledUpdateInterface $update, ContentEntityInterface $entity_to_update) {
  $update_user = $this->configuration['update_user'];
  $switch_to_user = NULL;
  switch ($update_user) {
    case $this::USER_UPDATE_RUNNER:
      if ($this->isRunByCron) {

        // Running the update as the update runner using cron means we need to switch to user #1.
        $switch_to_user = User::load(1);
      }
      else {

        // Running the update as the update runner means there is no need to switch
        return;
      }
      break;
    case $this::USER_OWNER:
      $switch_to_user = $this
        ->getEntityOwner($entity_to_update);
      break;
    case $this::USER_REVISION_OWNER:
      $switch_to_user = $this
        ->getRevisionOwner($entity_to_update);
      break;
    case $this::USER_UPDATE_OWNER:
      if ($this->isRunByCron) {

        // Running the update as the update runner using cron means we need to switch to user #1.
        $switch_to_user = User::load(1);
      }
      else {
        $switch_to_user = $update
          ->getOwner();
      }
      break;
  }
  if ($switch_to_user) {

    // @todo Throw an error because we should have a user.
    $this->accountSwitcher
      ->switchTo($switch_to_user);
    $this->isUserSwitched = TRUE;
  }
}