/**
  * If the user has been switch to run an update switch the user back.
  */
 protected function switchUserBack()
 {
     if ($this->isUserSwitched) {
         $this->accountSwitcher->switchBack();
         $this->isUserSwitched = FALSE;
     }
 }
Beispiel #2
0
 /**
  * {@inheritdoc}
  */
 public function run()
 {
     // Allow execution to continue even if the request gets cancelled.
     @ignore_user_abort(TRUE);
     // Force the current user to anonymous to ensure consistent permissions on
     // cron runs.
     $this->accountSwitcher->switchTo(new AnonymousUserSession());
     // Try to allocate enough time to run all the hook_cron implementations.
     drupal_set_time_limit(240);
     $return = FALSE;
     // Try to acquire cron lock.
     if (!$this->lock->acquire('cron', 900.0)) {
         // Cron is still running normally.
         $this->logger->warning('Attempting to re-run cron while it is already running.');
     } else {
         $this->invokeCronHandlers();
         $this->setCronLastTime();
         // Release cron lock.
         $this->lock->release('cron');
         // Return TRUE so other functions can check if it did run successfully
         $return = TRUE;
     }
     // Process cron queues.
     $this->processQueues();
     // Restore the user.
     $this->accountSwitcher->switchBack();
     return $return;
 }
 /**
  * {@inheritdoc}
  */
 public function sendTest(NodeInterface $node, array $test_addresses)
 {
     // Force the current user to anonymous to ensure consistent permissions.
     $this->accountSwitcher->switchTo(new AnonymousUserSession());
     // Send the test newsletter to the test address(es) specified in the node.
     // Build array of test email addresses.
     // Send newsletter to test addresses.
     // Emails are send direct, not using the spool.
     $recipients = array('anonymous' => array(), 'user' => array());
     foreach ($test_addresses as $mail) {
         $mail = trim($mail);
         if (!empty($mail)) {
             $subscriber = simplenews_subscriber_load_by_mail($mail);
             if (!$subscriber) {
                 // @todo: Find a cleaner way to do this.
                 $subscriber = Subscriber::create(array());
                 $subscriber->setUserId(0);
                 $subscriber->setMail($mail);
                 $subscriber->setLangcode(\Drupal::languageManager()->getCurrentLanguage());
             }
             if ($subscriber->getUserId()) {
                 $account = $subscriber->uid->entity;
                 $recipients['user'][] = $account->getUserName() . ' <' . $mail . '>';
             } else {
                 $recipients['anonymous'][] = $mail;
             }
             $mail = new MailEntity($node, $subscriber, \Drupal::service('simplenews.mail_cache'));
             $mail->setKey('test');
             $this->sendMail($mail);
         }
     }
     if (count($recipients['user'])) {
         $recipients_txt = implode(', ', $recipients['user']);
         drupal_set_message(t('Test newsletter sent to user %recipient.', array('%recipient' => $recipients_txt)));
     }
     if (count($recipients['anonymous'])) {
         $recipients_txt = implode(', ', $recipients['anonymous']);
         drupal_set_message(t('Test newsletter sent to anonymous %recipient.', array('%recipient' => $recipients_txt)));
     }
     $this->accountSwitcher->switchBack();
 }