Example #1
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;
 }
Example #2
0
 /**
  * {@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();
 }
Example #3
0
  /**
   * Switch to another user to run an update if necessary.
   *
   * @param \Drupal\scheduled_updates\ScheduledUpdateInterface $update
   * @param \Drupal\Core\Entity\ContentEntityInterface $entity_to_update
   */
  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:
        // Running the update as the update runner means there is no need to switch
        return;
      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:
        $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;
    }

  }