diff --git a/core/core.services.yml b/core/core.services.yml index a499bb6..8aede8a 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -116,7 +116,7 @@ services: arguments: ['@config.storage', '@config.storage.schema', '@cache.config'] cron: class: Drupal\Core\Cron - arguments: ['@module_handler', '@lock', '@queue', '@state'] + arguments: ['@module_handler', '@lock', '@queue', '@state', '@current_user'] database: class: Drupal\Core\Database\Connection factory_class: Drupal\Core\Database\Database diff --git a/core/lib/Drupal/Core/Cron.php b/core/lib/Drupal/Core/Cron.php index 95942c5..6bf463e 100644 --- a/core/lib/Drupal/Core/Cron.php +++ b/core/lib/Drupal/Core/Cron.php @@ -11,6 +11,7 @@ use Drupal\Core\KeyValueStore\StateInterface; use Drupal\Core\Lock\LockBackendInterface; use Drupal\Core\Queue\QueueFactory; +use Drupal\Core\Session\AccountProxyInterface; use Drupal\Core\Session\AnonymousUserSession; /** @@ -47,6 +48,13 @@ class Cron implements CronInterface { protected $state; /** + * The current user. + * + * @var \Drupal\Core\Session\AccountProxyInterface + */ + protected $currentUser; + + /** * Constructs a cron object. * * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler @@ -57,12 +65,15 @@ class Cron implements CronInterface { * The queue service. * @param \Drupal\Core\KeyValueStore\StateInterface $state * The state service. + * @param \Drupal\Core\Session\AccountProxyInterface $current_user + * The current user. */ - public function __construct(ModuleHandlerInterface $module_handler, LockBackendInterface $lock, QueueFactory $queue_factory, StateInterface $state) { + public function __construct(ModuleHandlerInterface $module_handler, LockBackendInterface $lock, QueueFactory $queue_factory, StateInterface $state, AccountProxyInterface $current_user) { $this->moduleHandler = $module_handler; $this->lock = $lock; $this->queueFactory = $queue_factory; $this->state = $state; + $this->currentUser = $current_user; } /** @@ -78,8 +89,8 @@ public function run() { // Force the current user to anonymous to ensure consistent permissions on // cron runs. - $original_user = \Drupal::currentUser()->getAccount(); - \Drupal::currentUser()->setAccount(new AnonymousUserSession()); + $original_user = $this->currentUser->getAccount(); + $this->currentUser->setAccount(new AnonymousUserSession()); // Try to allocate enough time to run all the hook_cron implementations. drupal_set_time_limit(240); @@ -145,7 +156,7 @@ public function run() { } // Restore the user. - \Drupal::currentUser()->setAccount($original_user); + $this->currentUser->setAccount($original_user); drupal_save_session($original_session_saving); return $return;