I was playing around in a test site of Drupal last night that I'd created and I thought out of interest I wonder if it is possible to either delete or block all users including the one your currently logged in as.

On Drupal 7 I found that you can indeed delete all users including yourself. Of course this does lead to the point that if you want to login to change the site in anyway you can no longer login 'cos there is no admin users any more.

I noticed in Drupal 8 it does have some protection regarding this and prevents you from deleting your own account. However I did notice though that in Drupal 8 if you set your account to be "blocked" it quite gladly blocks your own account - also meaning that you are unable to log back in to the account, and of course if your account is the only one on the system then you can't log back in.

I've set this as only a minor issue because other than testing or by mistake I don't think many people would purposefully block or delete their own accounts, especially if they're admin of the entire site and also it could be rectified by logging into the database and fixing it that way, but it is still a bit of an issue. I've noticed other CMS's like Joomla and WordPress show a message if you try and delete or block your own account, and other than by either error, deliberate malice (like if you were a disgruntled employee of a company if you were the admin of their website you could delete all users including yourself and then leave the website with no-one being able to login) or for testing I don't know why you'd actually ever want to do this.

Comments

TheKLF99 created an issue. See original summary.

pjohans’s picture

Issue tags: +Vienna2017

in vienna working on this one

pjohans’s picture

I reproduced this one - blocked myself as administrator, and could not log back in (drush user-unblock solved it)
i guess the best (and easiest) solution is to stop ANY user from blocking themselves

pjohans’s picture

Version: 8.0.x-dev » 8.5.x-dev
Status: Active » Needs review
StatusFileSize
new1.24 KB
jholding’s picture

I'm at DrupalCon Vienna, reviewing this now

Status: Needs review » Needs work

The last submitted patch, 4: drupal-ban-all-user-2912296-4-D8.patch, failed testing. View results

joum’s picture

@jholding I'm a first time sprinter, mind if I tag along? :)

jholding’s picture

Having to jump out of this, currently deep into another issue.

Feel free @joum, I'm a first time sprinter too :)

pjohans’s picture

StatusFileSize
new1.18 KB

did a new patch - forgot to delete some dev code

pjohans’s picture

Status: Needs work » Needs review
larskhansen’s picture

Status: Needs review » Reviewed & tested by the community

Looks good to me.

gábor hojtsy’s picture

Title: Drupal Core 7 allows you to delete all users, 8 allows you to ban all users (including self!) » User can ban themselves with actions in Drupal 8
Status: Reviewed & tested by the community » Needs work

Interesting! First of all if you can edit your account, you may be able to individually ban yourself there.

I think you meant this as a mass-ban side effect? I am not sure this is the right way to solve that problem. The action may be wired into some moderation flow which explicitly intends to block the user in question. Making that output a message to the end user that they cannot block themselves (when they did not even intend to run an action like this) could be odd. So it would be nice to get some feedback if this was an "intentional feature" or not.

Other than that displaying a message may also be a problem if the action is run programmatically, because that request will emit the message but nothing will be logged.

Finally, if just looking at the code as-is, there are a few code style issues:

  1. +++ b/core/modules/user/src/Plugin/Action/BlockUser.php
    @@ -18,15 +18,23 @@ class BlockUser extends ActionBase {
        * {@inheritdoc}
    +   *
    

    Extra line should not be added.

  2. +++ b/core/modules/user/src/Plugin/Action/BlockUser.php
    @@ -18,15 +18,23 @@ class BlockUser extends ActionBase {
    +      if($account->id() === \Drupal::currentUser()->id()) {
    

    Code style requires this to have a space after "if".

  3. +++ b/core/modules/user/src/Plugin/Action/BlockUser.php
    @@ -18,15 +18,23 @@ class BlockUser extends ActionBase {
    +        drupal_set_message(t('You may not block your own account'),'warning');
    

    $this->t() is available on this class already. Also code style dictates a space after the comma.

Ps. I also updated the issue title. You are not dealing with Drupal 7 here for example.

dhruveshdtripathi’s picture

Status: Needs work » Needs review
StatusFileSize
new1.16 KB
new1022 bytes

Solved suggested code style issue in the current code here. Patch and interdiff attached.

alexpott’s picture

Actually a user can cancel their own account. It prevents deleting user 1 but any other administrator can cancel their own account. In order to do this it will send you an email where you have to click on a link for it to work. And there is a problem because not all accounts have email addresses - and in this instance cancelling does not work but that is a separate bug. I'm not sure the issue outlined in the issue summary is actually a bug at all. There are valid use cases for blocking user 1 and preventing log in as that user. It is one of the simplest ways of making your site more secure.

      case 'delete':
        // Users with 'cancel account' permission can cancel their own account.
        return AccessResult::allowedIf($account->id() == $entity->id() && $account->hasPermission('cancel account'))->cachePerPermissions()->cachePerUser();

From core/modules/user/src/UserAccessControlHandler.php

As for the disgruntled employee issue - the issue here is with the company's security policies - if they've granted someone that level access then they need to be aware of that and rescind it if they feel that that authority is likely to be abused.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

larowlan’s picture

Status: Needs review » Postponed (maintainer needs more info)
Issue tags: +Bug Smash Initiative

So is that closed works as designed on the basis of #14?

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

quietone’s picture

Status: Postponed (maintainer needs more info) » Closed (works as designed)

@TheKLF99, thanks for bringing up this issue.

I have read the issue and agree with #13.

Cheers,