Prevent deletion of user 1

Created on 10 March 2015, over 9 years ago
Updated 18 April 2024, 5 months ago

As the title suggests I want to prevent user 1 from being deleted. At least not that easy. This can become an issue when you give site admins the permission "Administer Users". In a recent project one of our site admins accidentally did just that, deleted user 1. This patch maybe to harsh but it's a start. User 1 is a special case in Drupal and shouldn't be that easy delete I think.

Feature request
Status

Closed: duplicate

Version

7.0 ⚰️

Component
User module 

Last updated 1 day ago

Created by

🇸🇪Sweden StoraH

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, 🇮🇹

    #46149: Prevent account cancellation for uid 1 changed user_multiple_cancel_confirm() which now skips user #1.

      $accounts = user_load_multiple(array_keys(array_filter($edit['accounts'])));
      foreach ($accounts as $uid => $account) {
        // Prevent user 1 from being canceled.
        if ($uid <= 1) {
          continue;
        }
        $form['accounts'][$uid] = array(
          '#type' => 'hidden',
          '#value' => $uid,
          '#prefix' => '<li>',
          '#suffix' => check_plain($account->name) . "</li>\n",
        );
      }
      // Output a notice that user 1 cannot be canceled.
      if (isset($accounts[1])) {
        $redirect = count($accounts) == 1;
        $message = t('The user account %name cannot be cancelled.', array(
          '%name' => $accounts[1]->name,
        ));
        drupal_set_message($message, $redirect ? 'error' : 'warning');
    
        // If only user 1 was selected, redirect to the overview.
        if ($redirect) {
          drupal_goto('admin/people');
        }
      }
    

    This means that, from the user interface which seems the case described in this issue, it is not possible to delete the user #1, from /admin/people.

    I also created a new account with the administrator role, but even with that is not possible to delete the user #1, not even visiting user/1/edit, where the Cancel account button is missing.

    With other accounts, including the second administrator user, the Cancel account button is visible (and it works).

    I am closing this issue as duplicate of #46149: Prevent account cancellation for uid 1 .

  • Status changed to Closed: duplicate 5 months ago
  • 🇮🇹Italy apaderno Brescia, 🇮🇹
Production build 0.71.5 2024