summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--autodelete_account.info4
-rw-r--r--autodelete_account.module115
2 files changed, 119 insertions, 0 deletions
diff --git a/autodelete_account.info b/autodelete_account.info
new file mode 100644
index 0000000..f8a21e5
--- /dev/null
+++ b/autodelete_account.info
@@ -0,0 +1,4 @@
+; $Id$
+name = Autodelete Account
+description = Provides a possibility to delete the own account to regular users.
+core = 6.x
diff --git a/autodelete_account.module b/autodelete_account.module
new file mode 100644
index 0000000..9e99663
--- /dev/null
+++ b/autodelete_account.module
@@ -0,0 +1,115 @@
+<?php
+
+
+/**
+ * Implementation of hook_perm().
+ */
+function autodelete_account_perm() {
+ return array('delete own account');
+}
+
+/**
+ * Implementation of hook_menu().
+ */
+function autodelete_account_menu() {
+ $items['user/%user/custom_delete'] = array(
+ 'title' => 'Delete',
+ 'page callback' => 'drupal_get_form',
+ 'page arguments' => array('autodelete_account_user_confirm_delete', 1),
+ 'access callback' => 'user_access',
+ 'access arguments' => array('delete own account'),
+ 'type' => MENU_CALLBACK,
+ );
+
+ return $items;
+}
+/**
+ * Submit function for the 'Delete' button on the normal user edit form.
+ */
+function autodelete_account_user_edit_delete_submit($form, &$form_state) {
+ $destination = '';
+ if (isset($_REQUEST['destination'])) {
+ $destination = drupal_get_destination();
+ unset($_REQUEST['destination']);
+ }
+ // Note: We redirect from user/uid/edit to user/uid/delete to make the tabs disappear.
+ $form_state['redirect'] = array("user/". $form_state['values']['_account']->uid ."/custom_delete", $destination);
+}
+
+
+/**
+ * Form builder; confirm form for user deletion.
+ *
+ * @ingroup forms
+ * @see user_confirm_delete_submit()
+ */
+function autodelete_account_user_confirm_delete(&$form_state, $account) {
+ global $user;
+
+ if ($user->uid == $account->uid) {
+ $form['_account'] = array('#type' => 'value', '#value' => $account);
+
+ return confirm_form($form,
+ t('Are you sure you want to delete the account %name?', array('%name' => $account->name)),
+ 'user/' . $account->uid,
+ t('All submissions made by this user will be attributed to the anonymous account. This action cannot be undone.'),
+ t('Delete'), t('Cancel'));
+ }
+ else {
+ $form['delete'] = array(
+ '#type' => 'markup',
+ '#value' => t('Error trying delete this user'),
+ );
+ }
+}
+
+/**
+ * Submit function for the confirm form for user deletion.
+ */
+function autodelete_account_user_confirm_delete_submit($form, &$form_state) {
+ global $user;
+
+ drupal_set_message(t('%name has been deleted.', array('%name' => $form_state['values']['_account']->name)));
+
+ if (!isset($_REQUEST['destination'])) {
+ watchdog('user', 'Session closed for %name.', array('%name' => $user->name));
+ watchdog('user', 'Username %name has removed his own account.', array('%name' => $user->name));
+
+ //Delete User
+ user_delete($form_state['values'], $form_state['values']['_account']->uid);
+
+ // Destroy the current session:
+ session_destroy();
+ // Only variables can be passed by reference workaround.
+ $null = NULL;
+ user_module_invoke('logout', $null, $user);
+
+ // Load the anonymous user
+ $user = drupal_anonymous_user();
+
+ drupal_goto();
+ }
+}
+
+/**
+ * Implementation of hook_form_alter().
+ */
+function autodelete_account_form_alter(&$form, &$form_state, $form_id) {
+
+global $user;
+
+ //Authenticated should see the delete button
+ /* Verified current user is viewing his own account */
+ if (user_access('delete own account')) {
+ if ($form_id == 'user_profile_form' && $user->uid == arg(1)) {
+ $form['delete'] = array(
+ '#type' => 'submit',
+ '#value' => t('Delete'),
+ '#weight' => 31,
+ '#submit' => array('autodelete_account_user_edit_delete_submit'),
+ );
+ }
+ }
+
+}
+ \ No newline at end of file