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.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

dcam’s picture

Category: Task » Feature request
Status: Needs work » Needs review

For future reference, the issue status must be set to Needs review in order to trigger our automatic Testbot.

This is more of a new feature though. As such, it will likely need to be bumped up to Drupal 8 since new features must be added to the current development version first. Then they can be considered for backport.

vikashsoni’s picture

FileSize
21.69 KB
6.36 KB

Thanks @StoraH
patch is working fine now we are not able to delete user with have 1 uid sharing screenshot ....

apaderno’s picture

#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.

screenshot

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.

screenshot

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

screenshot

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

apaderno’s picture

Status: Needs review » Closed (duplicate)