diff options
Diffstat (limited to 'modules/user')
-rw-r--r-- | modules/user/user.api.php | 42 | ||||
-rw-r--r-- | modules/user/user.module | 56 |
2 files changed, 65 insertions, 33 deletions
diff --git a/modules/user/user.api.php b/modules/user/user.api.php index 6f818688d..07f28611c 100644 --- a/modules/user/user.api.php +++ b/modules/user/user.api.php @@ -32,11 +32,29 @@ function hook_user_load($users) { } /** + * Respond to user deletion. + * + * This hook is invoked from user_delete_multiple() after the account has been + * removed from the user tables in the database, and before + * field_attach_delete() is called. + * + * @param $account + * The account that is being deleted. + * + * @see user_delete_multiple() + */ +function hook_user_delete($account) { + db_delete('mytable') + ->condition('uid', $account->uid) + ->execute(); +} + +/** * Act on user account cancellations. * * The user account is being canceled. Depending on the account cancellation - * method, the module should either do nothing, unpublish content, anonymize - * content, or delete content and data belonging to the canceled user account. + * method, the module should either do nothing, unpublish content, or anonymize + * content. * * Expensive operations should be added to the global batch with batch_set(). * @@ -83,26 +101,6 @@ function hook_user_cancel($edit, $account, $method) { ->condition('uid', $account->uid) ->execute(); break; - - case 'user_cancel_delete': - // Delete nodes (current revisions). - $nodes = db_select('node', 'n') - ->fields('n', array('nid')) - ->condition('uid', $account->uid) - ->execute() - ->fetchCol(); - foreach ($nodes as $nid) { - node_delete($nid); - } - // Delete old revisions. - db_delete('node_revision') - ->condition('uid', $account->uid) - ->execute(); - // Clean history. - db_delete('history') - ->condition('uid', $account->uid) - ->execute(); - break; } } diff --git a/modules/user/user.module b/modules/user/user.module index 0c17eaa45..510c9c261 100644 --- a/modules/user/user.module +++ b/modules/user/user.module @@ -2144,8 +2144,11 @@ function user_cancel($edit, $uid, $method) { ); batch_set($batch); - // Allow modules to add further sets to this batch. - module_invoke_all('user_cancel', $edit, $account, $method); + // Modules use hook_user_delete() to respond to deletion. + if ($method != 'user_cancel_delete') { + // Allow modules to add further sets to this batch. + module_invoke_all('user_cancel', $edit, $account, $method); + } // Finish the batch and actually cancel the account. $batch = array( @@ -2193,15 +2196,7 @@ function _user_cancel($edit, $account, $method) { if (!empty($edit['user_cancel_notify'])) { _user_mail_notify('status_canceled', $account); } - db_delete('users') - ->condition('uid', $account->uid) - ->execute(); - db_delete('users_roles') - ->condition('uid', $account->uid) - ->execute(); - db_delete('authmap') - ->condition('uid', $account->uid) - ->execute(); + user_delete($account->uid); drupal_set_message(t('%name has been deleted.', array('%name' => $account->name))); watchdog('user', 'Deleted user: %name %email.', array('%name' => $account->name, '%email' => '<' . $account->mail . '>'), WATCHDOG_NOTICE); break; @@ -2221,6 +2216,45 @@ function _user_cancel($edit, $account, $method) { } /** + * Delete a user. + * + * @param $uid + * A user ID. + */ +function user_delete($uid) { + user_delete_multiple(array($uid)); +} + +/** + * Delete multiple user accounts. + * + * @param $uids + * An array of user IDs. + */ +function user_delete_multiple(array $uids) { + if (!empty($uids)) { + $accounts = user_load_multiple($uids, array()); + + foreach ($accounts as $uid => $account) { + module_invoke_all('user_delete', $account); + field_attach_delete('user', $account); + } + + db_delete('users') + ->condition('uid', $uids, 'IN') + ->execute(); + db_delete('users_roles') + ->condition('uid', $uids, 'IN') + ->execute(); + db_delete('authmap') + ->condition('uid', $uids, 'IN') + ->execute(); + + entity_get_controller('user')->resetCache(); + } +} + +/** * Page callback wrapper for user_view(). */ function user_view_page($uid) { |