4

I want the authenticated users on my site to be able to delete their own account or temporarily block/de-active. I give the necessary permissions for this. Well and good for now. However, the tricky part is that i want these as separate forms in a page different that user edit. Something like shown in the image below:enter image description here So that when the user navigates to this page say node/100 then the above is displayed. Any suggestions how to achieve this.

Regards

3
  • If user click on delete my account do we need to delete all the content created by this user? or just delete the account? Apr 11 '13 at 12:42
  • either should be fine...though i would prefer only to delete the account
    – why
    Apr 11 '13 at 17:47
  • any suggestions
    – why
    Apr 11 '13 at 17:49
4
+25

What you are describing will require a custom module that will provide the non standard functionality.

You will need to provide hooks for blocks (hook_block_info and hook_block_view) producing forms using the FAPI to produce the forms as you are showing in your mock.

Deleting an account would require using form validation to test that the username and password is correct and matches the content that you are viewing as well matches the user that is logged in. Once validated you would call the user_delete function, however be aware that this will delete all content owned by the user.

Disabling or Deactivating account will require a hook_block_info and hook_block_view as well the form using the FAPI as well a subset of the validation mentioned above, then calling the user_cancel function passing in the appropriate arguments.

The forms could be rendered using... 1. the blocks ui with conditional visibility settings. 2. at the theme level using a preprocess_page hook in template.php 3. other...

As Drupal is like a swiss army knife and this is one of many ways to solve the issue.

1
  • haha nice one "Drupal is like a swiss army knife" so i need to build a custom module...let me try..i will get back to you
    – why
    Apr 11 '13 at 17:48
3

That can be achieved with a custom module. In the example code below we assume the custom module is called disable_my_account - if your module is called something different then replace everywhere is says disable_my_account with the name of your module.

First create a hook_menu item for the disable my account page. This can call drupal_get_form directly and pass the name of our form as an argument:

function disable_my_account_menu() {
    $items['disable-my-account'] = array(
        'title' => 'Disable My Account',
        'page callback' => 'drupal_get_form',
        'page arguments' => array('disable_my_account_form'),
        'access arguments' => array('access content'),
        'type' => MENU_NORMAL_ITEM,
    );
    return $items;
}

Now we need to define what is in the "disable_my_account_form":

function disable_my_account_form($form, &$form_state) {

    $form = array();

    // put the delete part in one frameset

    $form['delete'] = array(
        '#type' => 'fieldset',
        '#title' => 'Delete Account',
        '#description' => t("Are you sure you want to PERMANENTLY DELETE your account?"),
    );
    $form['delete']['username'] = array(
        '#type' => 'textfield',
        '#title' => t('Email or username'),
        '#required' => TRUE,
    );
    $form['delete']['password'] = array(
        '#type' => 'password',
        '#title' => t('Password'),
        '#required' => TRUE,
    );
    $form['delete']['delete_submit'] = array(
        '#type' => 'submit',
        '#value' => t('DELETE MY ACCOUNT'),
    );

    // and put the disable part in a separate frameset

    $form['disable'] = array(
        '#type' => 'fieldset',
        '#title' => 'Disable Account',
    );
    $form['disable']['disable_account'] = array(
        '#type' => 'checkbox',
        '#title' => t('Temporarily deactive my account'),
    );
    $form['disable']['disable_submit'] = array(
        '#type' => 'submit',
        '#value' => t('OKAY'),
    );

    return $form;

}

Now the actual functionality to delete and disable accounts is going to sit in the form submit function:

function disable_my_account_form_submit($form, &$form_state) {

    global $user;

    switch($form_state['values']['op']) {

        case 'DELETE MY ACCOUNT':

            // check the details supply authenticate and match the user currently logged in
            if(user_authenticate($form_state['values']['username'], $form_state['values']['password']) && $form_state['values']['username']==$user->name) {

                // delete the account       
                user_delete($user->uid);
                // set a message and send the user to the homepage
                drupal_set_message('Your account has been deleted');
                drupal_goto();

            } else {
                drupal_set_message('That username and or password is not valid.');
            }

            break;

        case 'OKAY':

            // disable the account and save it
            $edit['status'] = 0;
            user_save($user, $edit);

            break;

    }

}
2
  • for password confirm you can use form type password_confirm it will create 2 inputs, and to check password user_check_password function, also use disable_my_account_form_validate to validate your form, probably you will need to check if user is administrator to not delete admin account ^_^ Apr 12 '13 at 12:33
  • I don't see the value of asking for a password twice when we are just deleting the account - it's only useful when making sure someone has typed it correctly when registering for an account. I agree it would be a good check to make sure the admin account is not being deleted...
    – Felix Eve
    Apr 14 '13 at 11:19
2

Hi I did this with some custom coding and using panels.in the following codes I assume user is logged in. Add the following code in your custom module and remember to change mymodulename with your module name.

<?php

/**
 *
 * Implements hook_menu_alter()
 * Setting permission for our final page for logged in user
 */
function mymodulename_menu_alter(&$items) {

// I have created panel page with path as user-settings
// You found me.
        $items['user-settings']['access callback'] = 'user_is_logged_in';
}

/**
 *
 * Implements hook_menu()
 * Redirect Path after user account is deactivated or deleted
 * @return
 *   $items array of menu items created programmatically
 */
function mymodulename_menu() {
  $items['user-change/%/%'] = array(
    'title' => t('Disable My Account'),
    'page callback' => '_custom_user_deleted_successfully',
    'page arguments' => array(1, 2),
    'access arguments' => array('access content'),
  );
  return $items;
}

/**
 *
 * Page Callback for hook_menu()
 * Redirect User to appropriate page after user deletion or diactivation with related messgae
 * @return
 *   $items array of menu items created programmatically
 */
function _custom_user_deleted_successfully($op, $uid) {
  if ($op == 'delete') {
    $success_message = 'User Account with user uid' . $uid . ' has been deleted';
  }
  elseif ($op == 'disable') {
    $success_message = 'User Account with user uid' . $uid . ' has been disabled';
  }
  unset($_SESSION['user_change']);
  return $success_message;
}

/**
 *
 * function which returns user delete form
 * @return
 *   $form array of form fields
 */
function _custom_user_delete($form, $form_state) {
  $form = array();
  $form['delete_account'] = array(
    '#type' => 'container',
  );
  $form['delete_account']['user_email'] = array(
    '#type' => 'textfield',
    '#title' => t('Email'),
    '#size' => 30,
    '#description' => t('Enter Your Email Address'),
  );
  $form['delete_account']['user_password'] = array(
    '#type' => 'password_confirm',
  );
  $form['submit_form'] = array(
    '#type' => 'submit',
    '#value' => t('Delete'),
    '#submit' => array('_custom_user_delete_submit'),
  );
  $form['#validate'] = array('_custom_user_delete_validation');
  return $form;
}

/**
 *
 * Validation function for custom user delete form
 * Validate if the email address input by the user is same as the email of curent logged in user.
 * validate the password input by the user
 */
function _custom_user_delete_validation(&$form, &$form_state) {
  global $user;
  if ($form_state['values']['user_email'] != $user->mail) {
    form_set_error('user_email', t('please enter your email address'));
  }
  $uid = _custom_user_authenticate($user, $form_state['values']['user_password']);
  if ($uid == 0 || $uid != $user->uid) {
    form_set_error('user_password', 'authentication failed');
  }
  if ($uid == 1) {
    form_set_error('user_password', 'Admin account can not be deleted');
  }
}

/**
 *
 * Submit function to delete user account
 */
function _custom_user_delete_submit($form, $form_state) {
  global $user;
  $_SESSION['user_change']['uid'] = $user->uid;
  user_delete($user->uid);
  drupal_goto('user-change/delete/' . $_SESSION['user_change']['uid']);
}

/**
 *
 * function to authenticate the password given by the logged in user
 */

function _custom_user_authenticate($account, $password) {

  require_once DRUPAL_ROOT . '/' . variable_get('password_inc', 'includes/password.inc');
  if (user_check_password($password, $account)) {
// Successful authentication.
    $uid = $account->uid;
  }
  else {
    $uid = 0;
  }
  return $uid;
}


/**
 *
 *  function which creates the custom ser disbale account
 */
function _custom_user_disable($form, $form_state) {
  $form = array();
  $form['disable_account'] = array(
    '#type' => 'container',
  );
  $form['disable_account']['email'] = array(
    '#type' => 'checkbox',
    '#title' => t('Disable My Account'),
  );
  $form['submit_form'] = array(
    '#type' => 'submit',
    '#value' => t('Okay'),
    '#submit' => array('_custom_user_disable_submit'),
  );
  return $form;
}


/**
 *
 * Submit handler to cutom user disable account form
 * Diable a logged in user account
 */
function _custom_user_disable_submit($form, $form_state) {
  global $user;
  $_SESSION['user_change']['user_disable'] = $user->uid;
  $edit['status'] = 0;
  user_save($user, $edit);
  drupal_goto('user-change/disable/' . $_SESSION['user_change']['user_disable']);
}

Download and enable panels now
Create a panel page with path as user-settings.If you want to put the path of your choice then serach for user-settings in the above code till you find comment saying "You found me",change the user-settings path with the same path you gave for your panel page.
Choose 2 column layout for panel
Now enables Php Filter module(this modulw comes with drupal core).
Back to the panel page, as you must be seeing two column panel layout on the left panel column add content using "New Custom content".
It will open a form, where in the body field use Php code as text format and add the following code.
<?php
  print drupal_render(drupal_get_form('_custom_user_delete'));
?>
similarly in the right column panel pane add the following code.
<?php
  print drupal_render(drupal_get_form('_custom_user_disable'));
?>
* remember to turn PHP Code as text format

save the panel and browse yoursite/user-settings or whatever you have changed to.

here is a screenshot of accomplishment.enter image description here

1
  • @user145078 here goes you question "I am unable to find user_settings panel page and if i try to create a new page with that i get an error that path is already in use...I understand you want to override the existing path but it is not being provided" Answer No we are not overriding the path we are creatng a page through panel and we are calling are custom form inside it programmatically you can change the user-settings path to your wish as I put on the post before.
    – arpitr
    Apr 26 '13 at 8:07
-1

You can the try the following Drupal plugin and use the permissions within drupal admin page to set it so users can select functions on if they want to delete their account.

http://drupal.org/project/user_delete

It allows you to do the following:

  1. block a user
  2. block a user and unpublish all submitted content, including nodes and comments
  3. delete a user and reassign the submitted content to the anonymous user
  4. delete a user and delete all submitted content including nodes and comments

Also have a read of the following: http://groups.drupal.org/node/252233

1
  • I have specifically tagged the question for Drupal7
    – why
    Apr 5 '13 at 5:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.