The user formatter has a setting "Link to the user". The link is only displayed if the account (user that the output is being generated for) has access to view the target user (the one whose name is being displayed).

Expected behaviour: Drupal check access accurately, calling all custom hooks such as hook_entity_access.
Actual behaviour: Drupal checks permission 'access user profiles'

This isn't a security issue - when the link is clicked to visit the page the correct access check is performed, so there will be access denied/403 if necessary.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

AdamPS created an issue. See original summary.

AdamPS’s picture

Title: Username template ignores entity access » Username formatter ignores entity access
AdamPS’s picture

Status: Needs review » Needs work

The last submitted patch, 3: user.username.formatter.2921114-3.patch, failed testing. View results

AdamPS’s picture

Some of the existing tests cover this. At the moment they assert the presence of a span, which is incorrect for a user with admin permission - it should be a. First patch is test-only, should fail.

AdamPS’s picture

Status: Needs work » Needs review
FileSize
3.1 KB

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.

a.milkovsky’s picture

The patch looks good and makes sense +1 for RTBC.

AdamPS’s picture

Status: Needs review » Reviewed & tested by the community

Status is RTBC based on #9

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 6: user.username.formatter.2921114-6.patch, failed testing. View results

AdamPS’s picture

AdamPS’s picture

Status: Needs review » Reviewed & tested by the community

This was just a re-roll so assume it doesn't need another review - putting status back to RTBC

a.milkovsky’s picture

The re-roll looks good.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/core/modules/user/user.module
@@ -501,7 +502,12 @@ function template_preprocess_username(&$variables) {
-  $variables['profile_access'] = \Drupal::currentUser()->hasPermission('access user profiles');
+  if ($account instanceof AccessibleInterface) {
+    $variables['profile_access'] = $account->access('view');
+  }
+  else {
+    $variables['profile_access'] = \Drupal::currentUser()->hasPermission('access user profiles');
+  }

For a more BC friendly change should this be something like (there might be neater logic)

  $variables['profile_access'] = FALSE;
  if ($account instanceof AccessibleInterface) {
    $variables['profile_access'] = $account->access('view');
  }
  $variables['profile_access'] = $variables['profile_access'] || \Drupal::currentUser()->hasPermission('access user profiles');

Also the test coverage seems quite indirect. I think we could do with some more explicit test coverage. Especially with the extra conditionality being introduced by this patch.

AdamPS’s picture

Here is a new patch with an explicit test.

Regarding BC, I propose that in this case the existing behavior is a bug so we do want to change it. The specific case that is relevant to BC is if the user has 'access user profiles' permission but does not have access. That can happen if an access hook returns forbidden.

  • Existing behavior: show a link but if the user clicks it they get 403 because they don't have access = bug
  • New behavior: don't show a link

Please can someone review the test change then set back to RTBC?

Berdir’s picture

+++ b/core/modules/user/tests/src/Functional/Views/UserFieldsAccessChangeTest.php
@@ -24,11 +24,15 @@ class UserFieldsAccessChangeTest extends UserTestBase {
 
   /**
+   * Path to the test_user_fields_access view.
+   */
+  const VIEW_PATH = 'test_user_fields_access';
+

Why is this added?

Usually we just duplicate paths in test, this just adds indirection which makes it IMHO harder to understand.

AdamPS’s picture

@a.milkovsky, @Berdir or anyone else please can you set to RTBC so we can unblock this and hopefully get it fixed in time for D8.7?

It just needs a quick review of the tests, which maybe Berdir has done already? No need to actually test because the code hasn't changed since last time, only tests have changed.

a.milkovsky’s picture

Status: Needs review » Reviewed & tested by the community

Looks good -> RTBC

larowlan’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs reroll
error: patch failed: core/modules/user/tests/src/Functional/UserAdminTest.php:71
error: core/modules/user/tests/src/Functional/UserAdminTest.php: patch does not apply
make: *** [patch87] Error 1
AdamPS’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll
FileSize
5.79 KB

@larowlan Thanks here is the reroll

Status: Needs review » Needs work

The last submitted patch, 22: user.username.formatter.2921114-22.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

AdamPS’s picture

AdamPS’s picture

Status: Needs review » Reviewed & tested by the community

This is just a reroll, the patch is the same as before, so it doesn't need reviewing again

AdamPS’s picture

I tend to think this issue does not need a change notice because it is just fixing a bug. In case people think it does I don't want to delay commit of the issue so I have written one just in case - feel free to delete/ignore it.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work

#24 includes another patch.

AdamPS’s picture

Status: Needs work » Needs review
FileSize
5.8 KB

Sorry that was the wrong patch file

AdamPS’s picture

Status: Needs review » Reviewed & tested by the community
larowlan’s picture

Adding issue credit

larowlan’s picture

Status: Reviewed & tested by the community » Fixed

Committed bd6a7df and pushed to 8.7.x. Thanks!

  • larowlan committed bd6a7df on 8.7.x
    Issue #2921114 by AdamPS, Berdir: Username formatter ignores entity...
AdamPS’s picture

Status: Reviewed & tested by the community » Fixed

Great, many thanks @larowlan

@a.milkovsky helped by reviewing 3 patches so I wonder if we could add credit for him too?

larowlan’s picture

Sure

AdamPS’s picture

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.