From 41ea72a74ee7f0c0243aa907552acce6d5f9f737 Mon Sep 17 00:00:00 2001
From: mikran <mikran@1004738.no-reply.drupal.org>
Date: Thu, 18 Jun 2015 13:32:54 -0700
Subject: [PATCH] Issue #2092895 by mikran, MegaChriz, twistor: Block users not
 included in feed

---
 includes/FeedsSource.inc        |  2 ++
 plugins/FeedsProcessor.inc      | 10 ++++++++
 plugins/FeedsUserProcessor.inc  | 42 ++++++++++++++++++++++++++++++++-
 tests/feeds/users2.csv          |  5 ++++
 tests/feeds_processor_user.test | 15 ++++++++++++
 5 files changed, 73 insertions(+), 1 deletion(-)
 create mode 100644 tests/feeds/users2.csv

diff --git a/includes/FeedsSource.inc b/includes/FeedsSource.inc
index da77ff41..1fc5209e 100644
--- a/includes/FeedsSource.inc
+++ b/includes/FeedsSource.inc
@@ -88,6 +88,7 @@ class FeedsState {
   public $updated;
   public $deleted;
   public $unpublished;
+  public $blocked;
   public $skipped;
   public $failed;
 
@@ -106,6 +107,7 @@ class FeedsState {
     $this->updated =
     $this->deleted =
     $this->unpublished =
+    $this->blocked =
     $this->skipped =
     $this->failed = 0;
   }
diff --git a/plugins/FeedsProcessor.inc b/plugins/FeedsProcessor.inc
index ab0df881..04e69d71 100644
--- a/plugins/FeedsProcessor.inc
+++ b/plugins/FeedsProcessor.inc
@@ -314,6 +314,16 @@ abstract class FeedsProcessor extends FeedsPlugin {
         ),
       );
     }
+    if ($state->blocked) {
+      $messages[] = array(
+        'message' => format_plural(
+          $state->blocked,
+          'Blocked @number @entity.',
+          'Blocked @number @entities.',
+          array('@number' => $state->blocked) + $tokens
+        ),
+      );
+    }
     if ($state->deleted) {
       $messages[] = array(
        'message' => format_plural(
diff --git a/plugins/FeedsUserProcessor.inc b/plugins/FeedsUserProcessor.inc
index 02607f2c..778c72be 100644
--- a/plugins/FeedsUserProcessor.inc
+++ b/plugins/FeedsUserProcessor.inc
@@ -2,13 +2,21 @@
 
 /**
  * @file
- * FeedsUserProcessor class.
+ * Contains FeedsUserProcessor.
  */
 
+/**
+ * Option to block users not found in the feed.
+ *
+ * @var string
+ */
+define('FEEDS_BLOCK_NON_EXISTENT', 'block');
+
 /**
  * Feeds processor plugin. Create users from feed items.
  */
 class FeedsUserProcessor extends FeedsProcessor {
+
   /**
    * Define entity type.
    */
@@ -132,6 +140,7 @@ class FeedsUserProcessor extends FeedsProcessor {
       '#description' => t('This appends _test to all imported e-mail addresses to ensure they cannot be used as recipients.'),
       '#default_value' => $this->config['defuse_mail'],
     );
+    $form['update_non_existent']['#options'][FEEDS_BLOCK_NON_EXISTENT] = t('Block non-existent users');
     return $form;
   }
 
@@ -227,4 +236,35 @@ class FeedsUserProcessor extends FeedsProcessor {
     }
     return 0;
   }
+
+  /**
+   * Overrides FeedsProcessor::clean().
+   *
+   * Block users instead of deleting them.
+   *
+   * @param FeedsState $state
+   *   The FeedsState object for the given stage.
+   */
+  protected function clean(FeedsState $state) {
+    // Delegate to parent if not blocking or option not set.
+    if (!isset($this->config['update_non_existent']) || $this->config['update_non_existent'] !== FEEDS_BLOCK_NON_EXISTENT) {
+      return parent::clean($state);
+    }
+
+    if (!empty($state->removeList)) {
+      // @see user_user_operations_block().
+      // The following foreach is copied from above function but with an added
+      // counter to count blocked users.
+      foreach (user_load_multiple($state->removeList) as $account) {
+        $this->loadItemInfo($account);
+        $account->feeds_item->hash = $this->config['update_non_existent'];
+        // For efficiency manually save the original account before applying any
+        // changes.
+        $account->original = clone $account;
+        user_save($account, array('status' => 0));
+        $state->blocked++;
+      }
+    }
+  }
+
 }
diff --git a/tests/feeds/users2.csv b/tests/feeds/users2.csv
new file mode 100644
index 00000000..ed10078a
--- /dev/null
+++ b/tests/feeds/users2.csv
@@ -0,0 +1,5 @@
+name,mail,since,password
+Morticia,morticia@example.com,1244347500,mort
+Gomez,gomez@example.com,1228572000,gome
+Wednesday,wednesdayexample.com,1228347137,wedn
+Pugsley,pugsley@example,1228260225,pugs
diff --git a/tests/feeds_processor_user.test b/tests/feeds_processor_user.test
index 73f8d06d..4bc82d51 100644
--- a/tests/feeds_processor_user.test
+++ b/tests/feeds_processor_user.test
@@ -118,5 +118,20 @@ class FeedsCSVtoUsersTest extends FeedsWebTestCase {
     $this->assertTrue($account, 'Imported user account loaded.');
     $account->pass_raw = 'fest';
     $this->drupalLogin($account);
+
+    // Login as admin.
+    $this->drupalLogin($this->admin_user);
+
+    // Import modified CSV file, one (valid) user is missing.
+    $this->setSettings('user_import', 'FeedsUserProcessor', array('update_existing' => 2, 'update_non_existent' => 'block'));
+    $this->importFile('user_import', $this->absolutePath() . '/tests/feeds/users2.csv');
+    $this->assertText('Blocked 1 user');
+    $this->assertText('Failed importing 2 user');
+
+    // Import the original CSV file again.
+    $this->importFile('user_import', $this->absolutePath() . '/tests/feeds/users.csv');
+    $this->assertText('Updated 1 user');
+    $this->assertText('Failed importing 2 user');
   }
+
 }
-- 
GitLab