You are here

class FeedsUserProcessor in Feeds 8.2

Defines a user processor.

Creates Users from feed items.

Plugin annotation


@Plugin(
  id = "user",
  title = @Translation("User processor"),
  description = @Translation("Creates users from feed items.")
)

Hierarchy

Expanded class hierarchy of FeedsUserProcessor

3 string references to 'FeedsUserProcessor'
FeedsUIUserInterfaceTestCase::testEditFeedConfiguration in feeds_ui/feeds_ui.test
UI functionality tests on feeds_ui_overview(), feeds_ui_create_form(), Change plugins on feeds_ui_edit_page().
feeds_import_feeds_importer_default in feeds_import/feeds_import.feeds_importer_default.inc
Implementation of hook_feeds_importer_default().
_feeds_feeds_plugins in ./feeds.plugins.inc
Break out for feeds_feed_plugins().

File

lib/Drupal/feeds/Plugin/feeds/processor/FeedsUserProcessor.php, line 28
FeedsUserProcessor class.

Namespace

Drupal\feeds\Plugin\feeds\processor
View source
class FeedsUserProcessor extends FeedsProcessor {

  /**
   * Define entity type.
   */
  public function entityType() {
    return 'user';
  }

  /**
   * Implements parent::entityInfo().
   */
  protected function entityInfo() {
    $info = parent::entityInfo();
    $info['label plural'] = t('Users');
    return $info;
  }

  /**
   * Creates a new user account in memory and returns it.
   */
  protected function newEntity(FeedsSource $source) {
    return entity_create('user', array(
      'uid' => 0,
      'roles' => array_filter($this->config['roles']),
      'status' => $this->config['status'],
    ));
  }

  /**
   * Loads an existing user.
   */
  protected function entityLoad(FeedsSource $source, $uid) {
    $user = parent::entityLoad($source, $uid);

    // Copy the password so that we can compare it again at save.
    $user->feeds_original_pass = $user->pass;
    return $user;
  }

  /**
   * Validates a user account.
   */
  protected function entityValidate($account) {
    if (empty($account->name) || empty($account->mail) || !valid_email_address($account->mail)) {
      throw new FeedsValidationException(t('User name missing or email not valid.'));
    }
  }

  /**
   * Save a user account.
   */
  protected function entitySave($account) {
    if ($this->config['defuse_mail']) {
      $account->mail = $account->mail . '_test';
    }

    // Remove pass from $account if the password is unchanged.
    if (isset($account->feeds_original_pass) && $account->pass == $account->feeds_original_pass) {
      unset($account->pass);
    }
    $account
      ->save();
    if ($account->uid && !empty($account->openid)) {
      $authmap = array(
        'uid' => $account->uid,
        'module' => 'openid',
        'authname' => $account->openid,
      );
      if (SAVED_UPDATED != drupal_write_record('authmap', $authmap, array(
        'uid',
        'module',
      ))) {
        drupal_write_record('authmap', $authmap);
      }
    }
  }

  /**
   * Delete multiple user accounts.
   */
  protected function entityDeleteMultiple($uids) {
    user_delete_multiple($uids);
  }

  /**
   * Override parent::configDefaults().
   */
  public function configDefaults() {
    return array(
      'roles' => array(),
      'status' => 1,
      'defuse_mail' => FALSE,
    ) + parent::configDefaults();
  }

  /**
   * Override parent::configForm().
   */
  public function configForm(&$form_state) {
    $form = parent::configForm($form_state);
    $form['status'] = array(
      '#type' => 'radios',
      '#title' => t('Status'),
      '#description' => t('Select whether users should be imported active or blocked.'),
      '#options' => array(
        0 => t('Blocked'),
        1 => t('Active'),
      ),
      '#default_value' => $this->config['status'],
    );
    $roles = user_roles(TRUE);
    unset($roles['authenticated']);
    $options = array();
    foreach ($roles as $role) {
      $options[$role->id] = $role
        ->label();
    }
    if ($options) {
      $form['roles'] = array(
        '#type' => 'checkboxes',
        '#title' => t('Additional roles'),
        '#description' => t('Every user is assigned the "authenticated user" role. Select additional roles here.'),
        '#default_value' => $this->config['roles'],
        '#options' => $options,
      );
    }
    $form['defuse_mail'] = array(
      '#type' => 'checkbox',
      '#title' => t('Defuse e-mail addresses'),
      '#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'],
    );
    return $form;
  }

  /**
   * Override setTargetElement to operate on a target item that is a node.
   */
  public function setTargetElement(FeedsSource $source, $target_user, $target_element, $value) {
    switch ($target_element) {
      case 'created':
        $target_user->created = feeds_to_unixtime($value, REQUEST_TIME);
        break;
      case 'language':
        $target_user->language = strtolower($value);
        break;
      default:
        parent::setTargetElement($source, $target_user, $target_element, $value);
        break;
    }
  }

  /**
   * Return available mapping targets.
   */
  public function getMappingTargets() {
    $targets = parent::getMappingTargets();
    $targets += array(
      'name' => array(
        'name' => t('User name'),
        'description' => t('Name of the user.'),
        'optional_unique' => TRUE,
      ),
      'mail' => array(
        'name' => t('Email address'),
        'description' => t('Email address of the user.'),
        'optional_unique' => TRUE,
      ),
      'created' => array(
        'name' => t('Created date'),
        'description' => t('The created (e. g. joined) data of the user.'),
      ),
      'pass' => array(
        'name' => t('Unencrypted Password'),
        'description' => t('The unencrypted user password.'),
      ),
      'status' => array(
        'name' => t('Account status'),
        'description' => t('Whether a user is active or not. 1 stands for active, 0 for blocked.'),
      ),
      'language' => array(
        'name' => t('User language'),
        'description' => t('Default language for the user.'),
      ),
    );
    if (module_exists('openid')) {
      $targets['openid'] = array(
        'name' => t('OpenID identifier'),
        'description' => t('The OpenID identifier of the user. <strong>CAUTION:</strong> Use only for migration purposes, misconfiguration of the OpenID identifier can lead to severe security breaches like users gaining access to accounts other than their own.'),
        'optional_unique' => TRUE,
      );
    }

    // Let other modules expose mapping targets.
    self::loadMappers();
    $entity_type = $this
      ->entityType();
    $bundle = $this
      ->bundle();
    drupal_alter('feeds_processor_targets', $targets, $entity_type, $bundle);
    return $targets;
  }

  /**
   * Get id of an existing feed item term if available.
   */
  protected function existingEntityId(FeedsSource $source, FeedsParserResult $result) {
    if ($uid = parent::existingEntityId($source, $result)) {
      return $uid;
    }

    // Iterate through all unique targets and try to find a user for the
    // target's value.
    foreach ($this
      ->uniqueTargets($source, $result) as $target => $value) {
      switch ($target) {
        case 'name':
          $uid = db_query("SELECT uid FROM {users} WHERE name = :name", array(
            ':name' => $value,
          ))
            ->fetchField();
          break;
        case 'mail':
          $uid = db_query("SELECT uid FROM {users} WHERE mail = :mail", array(
            ':mail' => $value,
          ))
            ->fetchField();
          break;
        case 'openid':
          $uid = db_query("SELECT uid FROM {authmap} WHERE authname = :authname AND module = 'openid'", array(
            ':authname' => $value,
          ))
            ->fetchField();
          break;
      }
      if ($uid) {

        // Return with the first nid found.
        return $uid;
      }
    }
    return 0;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FeedsConfigurable::$config protected property
FeedsConfigurable::$disabled protected property CTools export enabled status of this object.
FeedsConfigurable::$export_type protected property
FeedsConfigurable::$id protected property
FeedsConfigurable::$instances public static property
FeedsConfigurable::addConfig public function Similar to setConfig but adds to existing configuration.
FeedsConfigurable::configFormSubmit public function Submission handler for configForm(). 2
FeedsConfigurable::configFormValidate public function Validation handler for configForm(). 3
FeedsConfigurable::copy public function Copy a configuration. 1
FeedsConfigurable::existing public function Determine whether this object is persistent and enabled. I. e. it is defined either in code or in the database and it is enabled. 1
FeedsConfigurable::getConfig public function Implements getConfig(). 1
FeedsConfigurable::instance public static function Instantiate a FeedsConfigurable object. 1
FeedsConfigurable::setConfig public function Set configuration.
FeedsConfigurable::__get public function Override magic method __get(). Make sure that $this->config goes through getConfig().
FeedsConfigurable::__isset public function Override magic method __isset(). This is needed due to overriding __get().
FeedsPlugin::all public static function Get all available plugins.
FeedsPlugin::byType public static function Gets all available plugins of a particular type.
FeedsPlugin::child public static function Determines whether given plugin is derived from given base plugin.
FeedsPlugin::hasSourceConfig public function Returns TRUE if $this->sourceForm() returns a form. Overrides FeedsSourceInterface::hasSourceConfig
FeedsPlugin::loadMappers public static function Loads on-behalf implementations from mappers/ directory.
FeedsPlugin::save public function Save changes to the configuration of this object. Delegate saving to parent (= Feed) which will collect information from this object by way of getConfig() and store it. Overrides FeedsConfigurable::save
FeedsPlugin::sourceDefaults public function Implements FeedsSourceInterface::sourceDefaults(). Overrides FeedsSourceInterface::sourceDefaults 1
FeedsPlugin::sourceDelete public function A source is being deleted. Overrides FeedsSourceInterface::sourceDelete 2
FeedsPlugin::sourceForm public function Callback methods, exposes source form. Overrides FeedsSourceInterface::sourceForm 3
FeedsPlugin::sourceFormValidate public function Validation handler for sourceForm. Overrides FeedsSourceInterface::sourceFormValidate 2
FeedsPlugin::sourceSave public function A source is being saved. Overrides FeedsSourceInterface::sourceSave 2
FeedsPlugin::typeOf public static function Determines the type of a plugin.
FeedsPlugin::__construct protected function Constructor. Overrides FeedsConfigurable::__construct
FeedsProcessor::bundle public function Bundle type this processor operates on.
FeedsProcessor::bundleOptions public function Provides a list of bundle options for use in select lists.
FeedsProcessor::clear public function Remove all stored results or stored results up to a certain time for a source.
FeedsProcessor::createLogMessage protected function Creates a log message for when an exception occured during import.
FeedsProcessor::entitySaveAccess protected function Access check for saving an enity. 1
FeedsProcessor::expire public function Deletes feed items older than REQUEST_TIME - $time.
FeedsProcessor::expiryQuery protected function Returns a database query used to select entities to expire. 1
FeedsProcessor::expiryTime public function Per default, don't support expiry. If processor supports expiry of imported items, return the time after which items should be removed. 1
FeedsProcessor::getHash protected function Retrieves the MD5 hash of $entity_id from the database.
FeedsProcessor::getLimit public function
FeedsProcessor::getMappings public function Get mappings.
FeedsProcessor::hash protected function Create MD5 hash of item and mappings array.
FeedsProcessor::itemCount public function Counts the number of items imported by this processor.
FeedsProcessor::loadItemInfo protected function Loads existing entity information and places it on $entity->feeds_item.
FeedsProcessor::map protected function Execute mapping on an item.
FeedsProcessor::newItemInfo protected function Adds Feeds specific information on $entity->feeds_item.
FeedsProcessor::pluginType public function Implements FeedsPlugin::pluginType(). Overrides FeedsPlugin::pluginType
FeedsProcessor::process public function Process the result of the parsing stage.
FeedsProcessor::uniqueTargets protected function Utility function that iterates over a target array and retrieves all sources that are unique.
FeedsUserProcessor::configDefaults public function Override parent::configDefaults(). Overrides FeedsProcessor::configDefaults
FeedsUserProcessor::configForm public function Override parent::configForm(). Overrides FeedsProcessor::configForm
FeedsUserProcessor::entityDeleteMultiple protected function Delete multiple user accounts. Overrides FeedsProcessor::entityDeleteMultiple
FeedsUserProcessor::entityInfo protected function Implements parent::entityInfo(). Overrides FeedsProcessor::entityInfo
FeedsUserProcessor::entityLoad protected function Loads an existing user. Overrides FeedsProcessor::entityLoad
FeedsUserProcessor::entitySave protected function Save a user account. Overrides FeedsProcessor::entitySave
FeedsUserProcessor::entityType public function Define entity type. Overrides FeedsProcessor::entityType
FeedsUserProcessor::entityValidate protected function Validates a user account. Overrides FeedsProcessor::entityValidate
FeedsUserProcessor::existingEntityId protected function Get id of an existing feed item term if available. Overrides FeedsProcessor::existingEntityId
FeedsUserProcessor::getMappingTargets public function Return available mapping targets. Overrides FeedsProcessor::getMappingTargets
FeedsUserProcessor::newEntity protected function Creates a new user account in memory and returns it. Overrides FeedsProcessor::newEntity
FeedsUserProcessor::setTargetElement public function Override setTargetElement to operate on a target item that is a node. Overrides FeedsProcessor::setTargetElement