? tests
? translations
Index: simplenews.admin.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/simplenews/simplenews.admin.inc,v
retrieving revision 1.56
diff -u -p -r1.56 simplenews.admin.inc
--- simplenews.admin.inc	15 Mar 2009 23:58:44 -0000	1.56
+++ simplenews.admin.inc	16 Mar 2009 13:59:23 -0000
@@ -916,6 +916,15 @@ function simplenews_admin_settings_mail(
     '#default_value' => variable_get('simplenews_throttle', 20),
     '#description' => t('Sets the numbers of newsletters sent per cron run. Failure to send will also be counted.') . $description_extra,
   );
+  $cron_user = array('default' => t('Default user'), 'author' => t('Newsletter author'));
+  $form['simplenews_mail_backend']['simplenews_cron_user'] = array(
+    '#type' => 'radios',
+    '#title' => t('Cron user'),
+    '#options' => $cron_user,
+    '#default_value' => variable_get('simplenews_cron_user', 'default'),
+    '#description' => t("The user who's permission is used to render the newsletter emails. Use only when you use access control modules and the newsletter is (party) not visible to anonymous users.") .'<br />'.
+    t('When using cron the <em>default user</em> is anonymous when not using cron the <em>default user</em> is one who sends the newsletter. '),
+  );
   $form['simplenews_mail_backend']['simplenews_spool_expire'] = array(
     '#type' => 'select',
     '#title' => t('Mail spool expiration'),
Index: simplenews.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/simplenews/simplenews.module,v
retrieving revision 1.206
diff -u -p -r1.206 simplenews.module
--- simplenews.module	14 Mar 2009 02:01:38 -0000	1.206
+++ simplenews.module	16 Mar 2009 13:59:27 -0000
@@ -1625,6 +1625,11 @@ function simplenews_mail_mail($nid, $vid
   $account->mail = $mail;
   $subscription = simplenews_get_subscription($account);
   $params['context']['account'] = $subscription;
+  if (variable_get('simplenews_cron_user', 'default') != 'default') {
+    // We send the newsletter the using permissions of an other user.
+    // Store the current user to be able to switch back later.
+    simplenews_switch_user();
+  }
 
   // Get node data for the mail
   $node = node_load($nid, $vid);
@@ -1633,6 +1638,11 @@ function simplenews_mail_mail($nid, $vid
     $params['context']['newsletter'] = taxonomy_get_term($node->simplenews['tid']);
     $params['context']['node'] = $node;
 
+    if (variable_get('simplenews_cron_user', 'default') != 'default') {
+      // Switch user to the node author to use the correct permissions.
+      // After building the newsletter we switch back to the original user.
+      simplenews_switch_user($node->nid, $node->vid);
+    }
     // Send mail
     if (module_exists('mimemail')) {
       // If mimemail module is installed ALL emails are send via this module.
@@ -1655,6 +1665,11 @@ function simplenews_mail_mail($nid, $vid
       $message = drupal_mail('simplenews', $key, $subscription->mail, $subscription->language, $params, $params['from']['address'], TRUE);
     }
 
+    if (variable_get('simplenews_cron_user', 'default') != 'default') {
+      // Switch back to the original user.
+      simplenews_switch_user();
+    }
+
     // Log sent message.
     if (variable_get('simplenews_debug', FALSE)) {
       if (module_exists('mimemail')) {
@@ -2193,6 +2208,38 @@ function simplenews_help($path, $arg) {
 }
 
 /**
+ * Switch from original user to mail submision user and back.
+ *
+ * Note: taken from mailhandler.module.
+ *
+ * Note: You first need to run simplenews_switch_user without
+ * argument to store the current user. Call simplenews_switch_user
+ * without argument to set the user back to the original user.
+ *
+ * @param $uid The user ID to switch to
+ *
+ */
+function simplenews_switch_user($uid = NULL) {
+  global $user;
+  static $orig_user = array();
+
+  if (isset($uid)) {
+    session_save_session(FALSE);
+    $user = user_load(array('uid' => $uid));
+  }
+  // retrieve the initial user, can be called multiple times
+  else if (count($orig_user)) {
+    $user = array_shift($orig_user);
+    session_save_session(TRUE);
+    array_unshift($orig_user, $user);
+  }
+  // store the initial user
+  else {
+    $orig_user[] = $user;
+  }
+}
+
+/**
  * Flatten a nested array
  */
 function _simplenews_flatten_array($array) {
