API page: http://api.drupal.org/api/drupal/modules%21user%21user.module/function/u...

I am trying to write a module that programmatically cancels user accounts. I do not want to use the native Cancel function on the User Profile form because there are some processes needed to be carried out before the user account is canceled.

My code is something like this

custom_user_cancellation_form($form, &$form_state){
// sets up form for cancellation button
    $form['cancel'] = array(
        '#type' => 'submit',
        '#value' =>'Cancel my account',
    );

    return $form;
}

custom_user_cancellation_form_submit($form, &$form_state){
    // perform all sorts of specific functionality
    // ........

   // finally, cancel the user account
   global $user;
   $method = 'user_cancel_block_unpublish';
   $uid = $user->uid;
   user_cancel(array(), $uid, $method);
}

When I run this, the user status is set to zero (user account is blocked) but I also get a warning 'No Active Batch'. I am not sure what this means, but I notice that the documentation for User_Cancel suggests that batch_process() needs to be invoked after a user_cancel. However, the documentation also states that Form API does this automatically.

Also, I looked in the Batch table in my database and I notice it now has an entry

+-----+---------------------------------------------+------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| bid | token                                       | timestamp  | batch                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            |
+-----+---------------------------------------------+------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|   9 | Y1I-KRHHAM6nkRA-enMrMMaXLyxVj79qoC1kkw8eK_A | 1342735357 | a:13:{s:4:"sets";a:2:{i:0;a:13:{s:7:"sandbox";a:0:{}s:7:"results";a:0:{}s:7:"success";b:1;s:5:"start";d:1342735358.8854160308837890625;s:7:"elapsed";i:0;s:5:"title";s:18:"Cancelling account";s:12:"init_message";s:24:"Initializing.<br/>&nbsp;";s:16:"progress_message";s:29:"Completed @current of @total.";s:13:"error_message";s:22:"An error has occurred.";s:3:"css";a:0:{}s:5:"total";i:0;s:5:"count";i:0;s:5:"queue";a:2:{s:4:"name";s:16:"drupal_batch:9:0";s:5:"class";s:10:"BatchQueue";}}i:1;a:13:{s:7:"sandbox";a:0:{}s:7:"results";a:0:{}s:7:"success";b:1;s:5:"start";d:1342735358.886889934539794921875;s:7:"elapsed";i:0;s:5:"title";s:23:"Cancelling user account";s:12:"init_message";s:24:"Initializing.<br/>&nbsp;";s:16:"progress_message";s:29:"Completed @current of @total.";s:13:"error_message";s:22:"An error has occurred.";s:3:"css";a:0:{}s:5:"total";i:1;s:5:"count";i:0;s:5:"queue";a:2:{s:4:"name";s:16:"drupal_batch:9:1";s:5:"class";s:10:"BatchQueue";}}}s:16:"has_form_submits";b:0;s:10:"form_state";a:3:{s:10:"programmed";b:0;s:7:"rebuild";b:0;s:8:"redirect";a:1:{i:0;s:26:"http://test.mysite.com";}}s:11:"progressive";b:1;s:11:"current_set";i:1;s:3:"url";s:5:"batch";s:11:"url_options";a:0:{}s:10:"source_url";s:6:"node/1";s:8:"redirect";N;s:5:"theme";s:15:"bartik_subtheme";s:17:"redirect_callback";s:11:"drupal_goto";s:2:"id";s:1:"9";s:13:"error_message";s:75:"Please continue to <a href="/batch?id=9&amp;op=finished">the error page</a>";} |
+-----+---------------------------------------------+------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

I tested canceling a few user accounts and after a while it appeared my database got corrupted. I am not sure if these events are related but I am hesitant to move into production with this issue. Does anyone know how to properly use the user_cancel function?

Comments

swarad07’s picture

Was not able to recreate.

I followed the steps you suggested, created a form with a button, in the submit called user_cancel without batch_process(). It works just fine, the user was blocked and I got the following messages on the screen.

The update has been performed.
username has been disabled.

Only difference was I used a static uid, instead of taking it from global $user. Can you try with different uids ?

Cheers,
Swarad

Jooblay.net’s picture

I am not totally sure about rules integration on this, but can you create a rule or action to achieve what you want. We use an action to block account based off dates.

devad’s picture

Title: How to programmatically cancel a user account » Request for new cancel_account method

Hi all

It would be nice to have "Delete the account and make its content belong to other user." method.

Currently, there is only "Delete the account and make its content belong to the Anonymous user." method.

Apply:

It would be usefull in situations when some user create multiple accounts and post content to them. If administrator wants to merge all these accounts into one (one by one) the above method would be helpfull.

It would be also nice to make this new method optionaly available for authenticated users as well so that they can merge their accounts themselfes (they should know login details for account they merge into of course).

It is a common situation that user forgets login details and create additional accounts, post content too, and later they would like to merge all its accounts into one.

If anyone knows if some contrib module has such method included already. ??

Status: Active » Closed (outdated)

Automatically closed because Drupal 7 security and bugfix support has ended as of 5 January 2025. If the issue verifiably applies to later versions, please reopen with details and update the version.