Problem/Motivation

The UserSelection entity reference handler currently shows blocked users only for people with the administer users permission. For many user reference fields, only active users are relevant. It would be nice to allow these to be filtered for admins, similar to how the anonymous user can be excluded.

Proposed resolution

Add a configuration option Include blocked to the UserSelection. It defaults to TRUE so existing filters are unchanged.

Remaining tasks

  1. Write a merge request
  2. Review
  3. Commit

User interface changes

A new configuration option Include blocked is available when editing an Entity reference field that targets users.

API changes

None

Data model changes

Configuration for UserSelection supports a new option include_blocked that can be disabled.

Issue fork drupal-2849620

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

jhedstrom created an issue. See original summary.

jhedstrom’s picture

Status: Active » Needs review
Issue tags: +Needs tests
StatusFileSize
new1.74 KB

We'll need to update/add a test for this.

Status: Needs review » Needs work

The last submitted patch, 2: 2849620-02.patch, failed testing.

dpi’s picture

Assigned: Unassigned » dpi

Looking at the patch, it is likely implemented in the reverse. The default behaviour is to exclude blocked, so the option should probably be "Include blocked".

Tackling test failures, and there is a HTML issue with the form field not closing its <em> tag.

dpi’s picture

Assigned: dpi » Unassigned
Status: Needs work » Needs review
Issue tags: -Needs tests
  • Tests added.
  • Fixed HTML for checkbox.
  • Added schema for entity reference selection plugin settings
  • Inverted text to be "include blocked", since the current and default behaviour is to exclude blocked.

Interdiff with #2

dpi’s picture

jhedstrom’s picture

+++ b/core/modules/user/src/Plugin/EntityReferenceSelection/UserSelection.php
@@ -91,6 +91,7 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta
+      'include_blocked' => FALSE,

@@ -163,7 +171,7 @@ protected function buildEntityQuery($match = NULL, $match_operator = 'CONTAINS')
-    if (!$this->currentUser->hasPermission('administer users')) {
+    if (!$this->currentUser->hasPermission('administer users') && empty($handler_settings['include_blocked'])) {

This would change the current behavior, allowing non-admin users to access blocked users if I'm understanding the logic. In the previous logic from #2, it still forced non-admin users to reference only active users. I'm fine with this either way, but it might be harder to get committed with changing behavior...

jhedstrom’s picture

Actually, it wouldn't change any existing fields...it would make fields configurable in such a way that non-admins could reference blocked users, which is also the attempt of #2756179: Separate ability to reference blocked users from 'administer users' permission., so we could perhaps consolidate issues.

jhedstrom’s picture

Arg, this is confusing :) The patch in #7 will still always should blocked users for admins, yes? The initial intent was to allow fields to be configured such that even admins cannot reference blocked accounts...

dpi’s picture

The initial intent was to allow fields to be configured such that even admins cannot reference blocked accounts...

Reading your patch, it seemed you wanted to retain the existing behaviour somewhat, because the >hasPermission('administer users') snippet was still present.

even admins cannot reference blocked

If this patch is a binary yes/no you can/t reference blocked users then this would be a behaviour change.

The current behaviour could be retained by having the radio preference:

  • Allow users with administer users to reference blocked users. (default)
  • Allow anyone to reference blocked users.
  • Reference blocked users is prohibited.
dpi’s picture

This issue is confusing

dpi’s picture

I see the intent of the original patch was to make it so blocked users would always be excluded, regardless of permission. But then this does not cover situations where you want non-admin to reference blocked.

dpi’s picture

admin = user with `administer permissions`

All these multiple binary/negating conditions are confusing, these are the options I see are available.

Option A

Proposed by this issue.

  • UI: options are (boolean, checkbox, 2-states)
    • FALSE: Admin can reference blocked users (default)
    • TRUE: Admin cannot reference blocked users.
  • Effect:
    • admin can sometimes reference blocked users
    • non-admin can never reference blocked users

Option B

  • UI: options are (radio, 3-states)
    • 0: Only admin can reference blocked (default)
    • -1: Nobody can reference blocked
    • 1: Anybody can reference blocked
  • Effect:
    • Sometimes no-one can reference blocked users.
    • Sometimes only admins can reference blocked users.
    • Sometimes everyone can reference blocked users.

Option C

Proposed by #6

  • UI: options are (boolean, checkbox, 2-states)
    • FALSE: Non-admin cannot reference blocked users (default)
    • TRUE: Non-admin can reference blocked users
  • Effect:
    • Admin can always view blocked users.
    • Non-admin can sometimes reference blocked users.

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

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

sukanya.ramakrishnan’s picture

Submitting a patch based on option A as proposed by this issue.

Patch did not apply so cudnt get an interdiff

1. Test path has changed, so made changes in the new file. Added one more assertion based on the use cases.
2. Ported patch to 8.5.

jhedstrom’s picture

I think the options where we expose access to blocked users to non-admins is unnecessary, and would be a big change since non-admins cannot see those users anyway (it's the user equivalent of unpublished content), so would still prefer the original option of just making it configurable to remove the blocked users from the autocomplete for admins.

kevin.dutra’s picture

Status: Needs review » Needs work

Hmm, I think the included test is giving a false positive (i.e. it's not actually testing what it needs to) because $handler_settings was refactored to $configuration at some point and so this patch no longer works the way it's supposed to.

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.

kevin.dutra’s picture

Assigned: Unassigned » kevin.dutra
kevin.dutra’s picture

Assigned: kevin.dutra » Unassigned
Status: Needs work » Needs review
StatusFileSize
new4.3 KB
new5.54 KB

I've made a number of modifications to the last patch:

  1. Rerolled to work with 8.6
  2. Removed redundant test cases.
  3. Fixed new test case, which was not hitting any assertions due to missing arguments in $referenceable_tests.
  4. Added #states to the "Include anonymous" field to make it clear that if you exclude blocked users, you won't get the anonymous user either.
  5. Changed default setting for include_blocked to TRUE, as that is how it functions prior to this feature and we don't want unexpected behavior to be introduced.
  6. For a slight performance improvement, the include_blocked check is now performed before checking for the administer users permission, which is more expensive.

Status: Needs review » Needs work
kevin.dutra’s picture

Status: Needs work » Needs review

Looks like some random testbot issue caused those failures, so retry.

idebr’s picture

Status: Needs review » Needs work

The referenced users are validated in \Drupal\user\Plugin\EntityReferenceSelection\UserSelection::validateReferenceableNewEntities(). This method still validates the user is active based on the permission 'administer users':

if (!$this->currentUser->hasPermission('administer users')) {
  $entities = array_filter($entities, function ($user) {
    /** @var \Drupal\user\UserInterface $user */
    return $user->isActive();
  });
}

This means this patch needs at least two changes:

  1. The validation method \Drupal\user\Plugin\EntityReferenceSelection\UserSelection::validateReferenceableNewEntities() needs to be updated to allow for the configuration option 'include blocked'.
  2. The test coverage needs to be extended so it actually saves a reference to a blocked user through a form, so it passes all the relevant form validations.

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.

dalin’s picture

This is also related to #2836245: Unable to reference blocked user on node save in that users that don't have the "Administer users" permission are unable to submit the edit form for nodes created by a now blocked user. Editing a node should not require you to alter the history about who created it.

IMO user reference fields should default to allowing all users, unless overridden with the configuration in the attached patch.

dalin’s picture

dalin’s picture

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.

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.

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

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

stBorchert made their first commit to this issue’s fork.

blanca.esqueda’s picture

Status: Needs work » Closed (outdated)

The patch don't apply to Core 10.3.1
The patch in comment #20 is already included in Core 10: https://api.drupal.org/api/drupal/core%21modules%21user%21src%21Plugin%2...

steven spasbo’s picture

Rerolled patch for 10.3.2

kopeboy’s picture

Version: 11.x-dev » 10.3.x-dev
Status: Closed (outdated) » Needs work
StatusFileSize
new31.36 KB

The fix had not been included in core yet!

I applied the rerolled patch in #39, which still applies cleanly even to Drupal 10.3.6, and it worked fine, it just needs some clarification in the field help text imo..

When creating Entity reference fields on Users, this becomes the default:

entity reference user selection exclude blocked - drupal issue 2849620 with patch 39

You would assume from the description that the checked option will make users without the 'administer users' permission able to select blocked users. The opposite is true: the checked option, ie. by default, leaves core behaving like currently (you can't select blocked users unless you have the 'admin users' perm), but if unchecked it hides the blocked users on that field even to roles with the 'aminister users' permission.

So the feature is consistent with the original request, but we can improve the intuitiveness by reverting the label & description:

[ ] Exclude blocked users (unchecked by default - also, leave the "Include the anonymous user." as independent of this option)
Even if this option is not set, only users with the administer users permission may (view and) reference blocked users.

Anyway, I'm not an expert developer so I can't say if technically correct. For example, this contrib module behaviour might be relevant: reference_blocked_users adds a permission to allow referencing blocked users even without the 'administer users' permission, even in the "Authored by" base field. The module worked fine but started producing warning & error after applying patch #20, only after excluding blocked users from the new option (unchecking the current "include" option).

quietone’s picture

Version: 10.3.x-dev » 11.x-dev

Changes are made on on 11.x (our main development branch) first, and are then back ported as needed according to our policies.

claudiu.cristea’s picture

Please move this to a MR in order to be properly reviewed

reszli’s picture

StatusFileSize
new4.27 KB

the current patch has incorrect comparison:
the status = 1 condition needs to be added if the include_blocked is false AND the user has no administer users permission
if the include_blocked is true OR the user has administer users permission, then it should not be added
so I re-rolled it for 10.x

11.x already has an "include_blocked" setting on the selection handler, but it defaults to FALSE
that approach works for entity reference fields, where you can configure it,
but it does not work for the "Name (autocomplete)" views exposed filter
so IMHO the MR for 11.x should only contain a change for the default value to TRUE
but first we need to check why it was introduced with default FALSE

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.

fjgarlin made their first commit to this issue’s fork.

fjgarlin changed the visibility of the branch main to hidden.

fjgarlin changed the visibility of the branch 11.x to hidden.

fjgarlin changed the visibility of the branch 2849620-optionally-include-blocked-users to hidden.

fjgarlin changed the visibility of the branch 2849620-include-blocked to hidden.

fjgarlin changed the visibility of the branch 2849620-include-blocked to active.

fjgarlin’s picture

Status: Needs work » Needs review

I created https://git.drupalcode.org/project/drupal/-/merge_requests/14549 from the patch in #43.

There were some changes pushed but never made into an MR that are different from the patch https://git.drupalcode.org/issue/drupal-2849620/-/compare/main...2849620...

Setting to needs review to see if the MR is all we need to move forwards with this.

drumm’s picture

Issue tags: +affects drupal.org

As an admin on Drupal.org, adding people for issue credit offering blocked spam accounts is disorienting.

dcam’s picture

Status: Needs review » Needs work

There are test failures that must be resolved.

idebr’s picture

Title: Add filter option to UserSelection to exclude blocked users » Allow blocked users to filtered from UserSelection for users with 'administer users' permission
Issue summary: View changes
Status: Needs work » Needs review
  1. The issue summary now reflects the change only affects users with the 'administer users' permission
  2. Fixed test failures
solideogloria’s picture

Title: Allow blocked users to filtered from UserSelection for users with 'administer users' permission » Allow blocked users to be filtered from UserSelection for users with 'administer users' permission
smustgrave’s picture

Status: Needs review » Needs work

Seems like it already got a review from @joachim