User Cancel | "Default case" gets executed for other options

Created on 21 January 2015, over 9 years ago
Updated 2 May 2024, 4 months ago

As we know at the time of "Cancel user accounts", we have four options in drupal core that are following.

1. Disable the account and keep its content.
2. Disable the account and unpublish its content.
3. Delete the account and make its content belong to the Anonymous user.
4. Delete the account and its content.

In core user module (user.module) the function name "_user_cancel" has four cases in switchcase and one is "Default", the default is creating a problem. The issue is explained in below example.

Issue:
If some one want add more options(might be anything) apart from four like "Delete the account and make its content belong to the Super administrator." or any thing else. The "default:" case of switchcase will execute. It is stopper to add more options.

If we extend the code(hook_user_cancel) and will add one more option, it comes under "default" case in the switch case, as a result unwanted message appearing on the newly created option as the default option is being executed.

Proposed changes, I want to add a case before "user_cancel_delete".

case 'user_cancel_reassign_to_superadmin':

I had created a module(usercancel_contentassigntoadmin) to add one more option for user cancel.

Project (usercancel_contentassigntoadmin): https://www.drupal.org/sandbox/jaipal/2408333

I am creating a patch to add a case in existing switchcase.

File name: user.module
Function name: _user_cancel

Modified Code in existing function:
function _user_cancel($edit, $account, $method) {
global $user;

switch ($method) {
case 'user_cancel_block':
case 'user_cancel_block_unpublish':
default:
// Send account blocked notification if option was checked.
if (!empty($edit['user_cancel_notify'])) {
_user_mail_notify('status_blocked', $account);
}
user_save($account, array('status' => 0));
drupal_set_message(t('%name has been disabled.', array('%name' => $account->name)));
watchdog('user', 'Blocked user: %name %email.', array('%name' => $account->name, '%email' => '<' . $account->mail . '>'), WATCHDOG_NOTICE);
break;

case 'user_cancel_reassign':
case 'user_cancel_reassign_to_superadmin':
case 'user_cancel_delete':
// Send account canceled notification if option was checked.
if (!empty($edit['user_cancel_notify'])) {
_user_mail_notify('status_canceled', $account);
}
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;
}

// After cancelling account, ensure that user is logged out. We can't destroy
// their session though, as we might have information in it, and we can't
// regenerate it because batch API uses the session ID, we will regenerate it
// in _user_cancel_session_regenerate().
if ($account->uid == $user->uid) {
$user = drupal_anonymous_user();
}

// Clear the cache for anonymous users.
cache_clear_all();
}

💬 Support request
Status

Fixed

Version

7.0 ⚰️

Component
User module 

Last updated 1 day ago

Created by

🇮🇳India jaipal

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇮🇹Italy apaderno Brescia, 🇮🇹

    This issue seems more a support request for code the OP wanted to write, for which the OP seems convinced that the Drupal code may cause problems.

    If we extend the code(hook_user_cancel) and will add one more option, it comes under "default" case in the switch case, as a result unwanted message appearing on the newly created option as the default option is being executed.

    The hook to add more cancellation methods is hook_user_cancel_methods_alter(). The example code shows how to add a custom cancellation method.

      // Add a custom zero-out method.
      $methods['mymodule_zero_out'] = array(
        'title' => t('Delete the account and remove all content.'),
        'description' => t('All your content will be replaced by empty strings.'),
        // access should be used for administrative methods only.
        'access' => user_access('access zero-out account cancellation method'),
      );

    The hook description makes also clear that adding a custom method is the purpose of the hook.

    By implementing this hook, modules are able to add, customize, or remove account cancellation methods

  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024