FeedsUserProcessor.inc 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <?php
  2. /**
  3. * @file
  4. * FeedsUserProcessor class.
  5. */
  6. /**
  7. * Feeds processor plugin. Create users from feed items.
  8. */
  9. class FeedsUserProcessor extends FeedsProcessor {
  10. /**
  11. * Define entity type.
  12. */
  13. public function entityType() {
  14. return 'user';
  15. }
  16. /**
  17. * Implements parent::entityInfo().
  18. */
  19. protected function entityInfo() {
  20. $info = parent::entityInfo();
  21. $info['label plural'] = t('Users');
  22. return $info;
  23. }
  24. /**
  25. * Creates a new user account in memory and returns it.
  26. */
  27. protected function newEntity(FeedsSource $source) {
  28. $account = new stdClass();
  29. $account->uid = 0;
  30. $account->roles = array_filter($this->config['roles']);
  31. $account->status = $this->config['status'];
  32. return $account;
  33. }
  34. /**
  35. * Loads an existing user.
  36. */
  37. protected function entityLoad(FeedsSource $source, $uid) {
  38. // Copy the password so that we can compare it again at save.
  39. $user = user_load($uid);
  40. $user->feeds_original_pass = $user->pass;
  41. return $user;
  42. }
  43. /**
  44. * Validates a user account.
  45. */
  46. protected function entityValidate($account) {
  47. if (empty($account->name) || empty($account->mail) || !valid_email_address($account->mail)) {
  48. throw new FeedsValidationException(t('User name missing or email not valid.'));
  49. }
  50. }
  51. /**
  52. * Save a user account.
  53. */
  54. protected function entitySave($account) {
  55. if ($this->config['defuse_mail']) {
  56. $account->mail = $account->mail . '_test';
  57. }
  58. $edit = (array) $account;
  59. // Remove pass from $edit if the password is unchanged.
  60. if (isset($account->feeds_original_pass) && $account->pass == $account->feeds_original_pass) {
  61. unset($edit['pass']);
  62. }
  63. user_save($account, $edit);
  64. if ($account->uid && !empty($account->openid)) {
  65. $authmap = array(
  66. 'uid' => $account->uid,
  67. 'module' => 'openid',
  68. 'authname' => $account->openid,
  69. );
  70. if (SAVED_UPDATED != drupal_write_record('authmap', $authmap, array('uid', 'module'))) {
  71. drupal_write_record('authmap', $authmap);
  72. }
  73. }
  74. }
  75. /**
  76. * Delete multiple user accounts.
  77. */
  78. protected function entityDeleteMultiple($uids) {
  79. foreach ($uids as $uid) {
  80. user_delete($uid);
  81. }
  82. }
  83. /**
  84. * Override parent::configDefaults().
  85. */
  86. public function configDefaults() {
  87. return array(
  88. 'roles' => array(),
  89. 'status' => 1,
  90. 'defuse_mail' => FALSE,
  91. ) + parent::configDefaults();
  92. }
  93. /**
  94. * Override parent::configForm().
  95. */
  96. public function configForm(&$form_state) {
  97. $form = parent::configForm($form_state);
  98. $form['status'] = array(
  99. '#type' => 'radios',
  100. '#title' => t('Status'),
  101. '#description' => t('Select whether users should be imported active or blocked.'),
  102. '#options' => array(0 => t('Blocked'), 1 => t('Active')),
  103. '#default_value' => $this->config['status'],
  104. );
  105. $roles = user_roles(TRUE);
  106. unset($roles[2]);
  107. if (count($roles)) {
  108. $form['roles'] = array(
  109. '#type' => 'checkboxes',
  110. '#title' => t('Additional roles'),
  111. '#description' => t('Every user is assigned the "authenticated user" role. Select additional roles here.'),
  112. '#default_value' => $this->config['roles'],
  113. '#options' => $roles,
  114. );
  115. }
  116. // @todo Implement true updating.
  117. $form['update_existing'] = array(
  118. '#type' => 'checkbox',
  119. '#title' => t('Replace existing users'),
  120. '#description' => t('If an existing user is found for an imported user, replace it. Existing users will be determined using mappings that are a "unique target".'),
  121. '#default_value' => $this->config['update_existing'],
  122. );
  123. $form['defuse_mail'] = array(
  124. '#type' => 'checkbox',
  125. '#title' => t('Defuse e-mail addresses'),
  126. '#description' => t('This appends _test to all imported e-mail addresses to ensure they cannot be used as recipients.'),
  127. '#default_value' => $this->config['defuse_mail'],
  128. );
  129. return $form;
  130. }
  131. /**
  132. * Override setTargetElement to operate on a target item that is a node.
  133. */
  134. public function setTargetElement(FeedsSource $source, $target_user, $target_element, $value) {
  135. switch ($target_element) {
  136. case 'created':
  137. $target_user->created = feeds_to_unixtime($value, REQUEST_TIME);
  138. break;
  139. case 'language':
  140. $target_user->language = strtolower($value);
  141. break;
  142. default:
  143. parent::setTargetElement($source, $target_user, $target_element, $value);
  144. break;
  145. }
  146. }
  147. /**
  148. * Return available mapping targets.
  149. */
  150. public function getMappingTargets() {
  151. $targets = parent::getMappingTargets();
  152. $targets += array(
  153. 'name' => array(
  154. 'name' => t('User name'),
  155. 'description' => t('Name of the user.'),
  156. 'optional_unique' => TRUE,
  157. ),
  158. 'mail' => array(
  159. 'name' => t('Email address'),
  160. 'description' => t('Email address of the user.'),
  161. 'optional_unique' => TRUE,
  162. ),
  163. 'created' => array(
  164. 'name' => t('Created date'),
  165. 'description' => t('The created (e. g. joined) data of the user.'),
  166. ),
  167. 'pass' => array(
  168. 'name' => t('Unencrypted Password'),
  169. 'description' => t('The unencrypted user password.'),
  170. ),
  171. 'status' => array(
  172. 'name' => t('Account status'),
  173. 'description' => t('Whether a user is active or not. 1 stands for active, 0 for blocked.'),
  174. ),
  175. 'language' => array(
  176. 'name' => t('User language'),
  177. 'description' => t('Default language for the user.'),
  178. ),
  179. );
  180. if (module_exists('openid')) {
  181. $targets['openid'] = array(
  182. 'name' => t('OpenID identifier'),
  183. '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.'),
  184. 'optional_unique' => TRUE,
  185. );
  186. }
  187. // Let other modules expose mapping targets.
  188. self::loadMappers();
  189. $entity_type = $this->entityType();
  190. $bundle = $this->entityType();
  191. drupal_alter('feeds_processor_targets', $targets, $entity_type, $bundle);
  192. return $targets;
  193. }
  194. /**
  195. * Get id of an existing feed item term if available.
  196. */
  197. protected function existingEntityId(FeedsSource $source, FeedsParserResult $result) {
  198. if ($uid = parent::existingEntityId($source, $result)) {
  199. return $uid;
  200. }
  201. // Iterate through all unique targets and try to find a user for the
  202. // target's value.
  203. foreach ($this->uniqueTargets($source, $result) as $target => $value) {
  204. switch ($target) {
  205. case 'name':
  206. $uid = db_query("SELECT uid FROM {users} WHERE name = :name", array(':name' => $value))->fetchField();
  207. break;
  208. case 'mail':
  209. $uid = db_query("SELECT uid FROM {users} WHERE mail = :mail", array(':mail' => $value))->fetchField();
  210. break;
  211. case 'openid':
  212. $uid = db_query("SELECT uid FROM {authmap} WHERE authname = :authname AND module = 'openid'", array(':authname' => $value))->fetchField();
  213. break;
  214. }
  215. if ($uid) {
  216. // Return with the first nid found.
  217. return $uid;
  218. }
  219. }
  220. return 0;
  221. }
  222. }