Problem/Motivation

The flag count service, in its getUserFlagFlaggingCount it does not perform any access checks. This results into having inconsistent flag counts in specific use cases.

The use case that I have is:

- Users needs to view the count of their favourite content.
- Currently, by using the getUserFlagFlaggingCount method, the count of favourite content is taking into account content which the user does not have access (either is unpublished, either the specific rules of the site do not allow them to access).

I am still not sure either this is a bug or it is a feature needed for the specific project, but it looks problematic for more use cases.

Steps to reproduce

Create a flag associated with a content type. Then create a node with a user without access to unpublished content and flag it. After that, unpublish it. The count of flags made by the user will take that node into account.

$flag = \Drupal::service('flag')->getFlagById('test_flag');
$current_user = \Drupal::currentUser();
$count = \Drupal::service('flag.count')->getUserFlagFlaggingCount($flag, $current_user);

Proposed resolution

When calculating the user flagged counts, do not take into account the content that the user does not have access to.

Issue fork flag-3228503

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

omarlopesino created an issue. See original summary.

omarlopesino’s picture

Title: Flag counts are not checking user access to flagged entities » Flag counts are not checking user access in user flagging counts
omarlopesino’s picture

Status: Active » Needs review

Created a MR that check accesses to entities in order to get the count. Please review, thanks!

ivnish’s picture

Status: Needs review » Needs work

Tests failed

omarlopesino’s picture

Status: Needs work » Needs review

I've just adapted the tests to the new functionality, ready to review again.

ivnish’s picture

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

Version: 8.x-4.x-dev » 5.x-dev
ivnish’s picture

Status: Reviewed & tested by the community » Needs work

@omarlopesino I don't see in the new test the case from steps to reproduce. Could you add it?

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

deaom’s picture

Tried to reproduce this locally, was always getting the wrong count, meaning if user A flagged node, the count was 1. then user A lost access to the flagged node expecting the count to be 0, but it was always 1.
Added a test to see if it can be reproduced with test and it can be. The test should fail and it fails because when the getUserFlagFlaggingCount is first called it checks the access, which for now is allowed. Then the access is removed and the getUserFlagFlaggingCount called again, in this second call it does not check the access because the userFlagCounts gets populated and the else catches that as it's the same user and skips the access check part and just returns the count from before.

elseif (isset($this->userFlagCounts[$flag_id][$uid])) {
      return $this->userFlagCounts[$flag_id][$uid];
    }

If in the test I remove the first call to the getUserFlagFlaggingCount, then the access check does not get skipped and "it works". But that is not how this should or would work. So code needs adjusting.
Also better steps to reproduce as this seems to be some custom code that explicitly calls the getUserFlagFlaggingCount.

deaom’s picture

Status: Needs work » Needs review

As the access check needs to be checked every time, the "cached" userFlagCounts is not needed here, as the information about access is missing, so removed that part of code. Test is now passing, but not sure this is the best approach for this. Like mentioned before, better steps to reproduce are needed to be more clear on what changes are actually needed. Changing status to needs review so somebody else can also have a look.

ivnish’s picture

Issue summary: View changes
ivnish’s picture

I added a test code to steps to reproduce. I tested this MR manually, it works as expected

  • ivnish committed b56abfb3 on 5.x authored by omarlopesino
    fix: #3228503 Flag counts are not checking user access in user flagging...
ivnish’s picture

Status: Needs review » Fixed

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

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