user_import_delete.module 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php
  2. /**
  3. * @file
  4. * Delete users who are not included in an import file.
  5. */
  6. /**
  7. * Implements hook_cron().
  8. */
  9. function user_import_delete_cron() {
  10. user_import_delete_cancel_accounts();
  11. }
  12. function user_import_delete_preprocess_user_import_list(&$variables) {
  13. $accounts_not_created = db_query('SELECT u.uid FROM {user_import_delete} u WHERE delete_ready = 1')->rowCount();
  14. if (!empty($accounts_not_created)) {
  15. drupal_set_message(t('@delete_count accounts scheduled to be cancelled.', array('@delete_count' => $accounts_not_created)), 'status');
  16. }
  17. }
  18. /**
  19. * Implements hook_form_alter().
  20. *
  21. */
  22. function user_import_delete_form_alter(&$form, &$form_state, $form_id) {
  23. if ($form_id == 'user_import_edit') {
  24. array_unshift($form['import']['#submit'], 'user_import_delete_import_submit');
  25. }
  26. }
  27. /**
  28. * Implementation of hook_user_import_form_fieldsets().
  29. * Add options to User Import settings form.
  30. */
  31. function user_import_delete_user_import_form_fieldset($import, $collapsed) {
  32. $form = array();
  33. user_import_delete_user_import_edit_account_cancel($form, $import, $collapsed);
  34. return $form;
  35. }
  36. /**
  37. * Create a list of all existing users.
  38. */
  39. function user_import_delete_import_submit($form, &$form_state) {
  40. global $user;
  41. if (empty($form_state['values']['user_import_delete'])) {
  42. return;
  43. }
  44. $import_id = $form_state['values']['import_id'];
  45. $method = $form_state['values']['user_import_delete'];
  46. // Can't find a way to copy all users into table and set import_id and cancellation_method in one query,
  47. // so have to do it in three steps.
  48. // Delete any records without an import_id.
  49. $deleted_count = db_delete('user_import_delete')
  50. ->condition('import_id', 0)
  51. ->execute();
  52. // Add all users, except user 1.
  53. $query = db_select('users', 'u');
  54. $query->addField('u','uid', 'uid');
  55. $query->condition('uid', 1, '>');
  56. $query->condition('uid', $user->uid, '!=');
  57. db_insert('user_import_delete')
  58. ->from($query)
  59. ->execute();
  60. // Add import_id to all users.
  61. $updated_count = db_update('user_import_delete')
  62. ->fields(array(
  63. 'import_id' => $import_id,
  64. 'cancellation_method' => $method,
  65. ))
  66. ->condition('import_id', 0)
  67. ->execute();
  68. }
  69. /**
  70. * Implementation of hook_user_import_after_save().
  71. *
  72. * Delete imported users from log of original users,
  73. * so we have a list of which users where not in the import.
  74. *
  75. **/
  76. function user_import_delete_user_import_pre_save($settings, $account, $fields, $errors, $update_setting_per_module) {
  77. $method = $settings['user_import_delete'];
  78. // If no cancellation method is set, don't add accounts to list to be processed.
  79. if (empty($method)) {
  80. return;
  81. }
  82. // Check if accounts are to be updated as well as created.
  83. $update_setting = _user_import_update_user_check($settings['update_user']);
  84. // @todo document this logic.
  85. if (isset($account['uid'])) {
  86. db_delete('user_import_delete')
  87. ->condition('import_id', $settings['import_id'])
  88. ->condition('uid', $account['uid'])
  89. ->execute();
  90. }
  91. elseif (!$update_setting) { // If account is not being updated check there's a match between existing account and import.
  92. $email = $fields['user']['email'][0];
  93. $uid = db_query('SELECT u.uid FROM {users} u WHERE mail = :mail', array(':mail' => $email))->fetchField();
  94. if (!empty($uid)) {
  95. db_delete('user_import_delete')
  96. ->condition('import_id', $settings['import_id'])
  97. ->condition('uid', $uid)
  98. ->execute();
  99. }
  100. }
  101. }
  102. /**
  103. * Implementation of hook_user_import_imported().
  104. * Process import once it's completed.
  105. *
  106. **/
  107. function user_import_delete_user_import_imported($import_id, $settings) {
  108. $method = $settings['user_import_delete'];
  109. if (!empty($method)) {
  110. user_import_delete_set_delete_flag($import_id);
  111. user_import_delete_cancel_accounts();
  112. }
  113. }
  114. /**
  115. * Flag users as ready for deletion.
  116. *
  117. **/
  118. function user_import_delete_set_delete_flag($import_id) {
  119. // Prevent cancelling Anonymous user.
  120. db_delete('user_import_delete')
  121. ->condition('uid', 0)
  122. ->execute();
  123. // Prevent cancelling user 1 without confirmation.
  124. db_delete('user_import_delete')
  125. ->condition('uid', 1)
  126. ->execute();
  127. // Set users as ready to be deleted.
  128. $updated_count = db_update('user_import_delete')
  129. ->fields(array(
  130. 'delete_ready' => 1,
  131. ))
  132. ->condition('import_id', $import_id)
  133. ->execute();
  134. }
  135. /**
  136. * Delete users flagged as ready for deletion.
  137. *
  138. **/
  139. function user_import_delete_cancel_accounts() {
  140. $accounts_not_created = db_query('SELECT u.uid, u.cancellation_method FROM {user_import_delete} u WHERE delete_ready = 1 ORDER BY import_id');
  141. foreach ($accounts_not_created as $account_info) {
  142. $account = user_load($account_info->uid);
  143. if (!empty($account) && !empty($account_info->cancellation_method)) {
  144. // Set not to notify users that their account has been cancelled.
  145. $edit['user_cancel_notify'] = '';
  146. if ($account_info->cancellation_method != 'user_cancel_delete') {
  147. // Allow modules to add further sets to this operation.
  148. module_invoke_all('user_cancel', $edit, $account, $account_info->cancellation_method);
  149. }
  150. _user_cancel($edit, $account, $account_info->cancellation_method);
  151. db_delete('user_import_delete')
  152. ->condition('uid', $account->uid)
  153. ->execute();
  154. }
  155. }
  156. return;
  157. }
  158. /**
  159. * Options for the User Import settings form.
  160. */
  161. function user_import_delete_user_import_edit_account_cancel(&$form, $import, $collapsed) {
  162. $options = array(
  163. 'user_cancel_block' => t('Disable the account and keep its content'),
  164. 'user_cancel_block_unpublish' => t('Disable the account and unpublish its content'),
  165. 'user_cancel_reassign' => t('Delete the account and make its content belong to the !anonymous-name user', array('!anonymous-name' => variable_get('anonymous', t('Anonymous')))),
  166. 'user_cancel_delete' => t('Delete the account and its content'),
  167. );
  168. $form['optional']['user_import_delete'] = array(
  169. '#type' => 'select',
  170. '#title' => t('Existing Accounts Not In Import'),
  171. '#description' => t('How to deal with existing accounts that are not in this import.'),
  172. '#default_value' => isset($import['options']['user_import_delete']) ? $import['options']['user_import_delete'] : '',
  173. '#empty_option' => t("Leave active"),
  174. '#options' => $options,
  175. );
  176. }