Problem/Motivation

user_cancel_reassign deletes the account (see core's _user_cancel()), which already triggers hook_user_predelete() and flag_user_predelete(). The explicit call in flag_user_cancel() for that method was therefore running userFlagRemoval() twice per cancellation

Steps to reproduce

  1. Create flaggings with a user account
  2. Cancel the user account using any of the methods supplied by Drupal Core
  3. Notice the user flaggings are removed through \Drupal\flag\Hook\FlagHooks::userPredelete

Remaining tasks

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

User interface changes

None

API changes

None

Data model changes

None

CommentFileSizeAuthor
#4 2489992-4.patch454 bytescasey
#2 2489992-2.patch374 bytesmichaellenahan

Issue fork flag-2489992

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

michaellenahan’s picture

Issue summary: View changes

I agree ... cancelling the user account is not the same as deleting it.

michaellenahan’s picture

Status: Active » Needs review
StatusFileSize
new374 bytes

Here's an initial, very rudimentary patch.

A more complete patch should take into account the $method argument.

For the various possibilities of the $method argument, and their meanings, see: https://api.drupal.org/api/drupal/modules%21user%21user.pages.inc/functi...

See https://api.drupal.org/api/drupal/modules%21comment%21comment.module/fun... for an example that uses a switch statement to handle the various methods differently.

joachim’s picture

Title: We should not delete flag on account cancelation » We should not delete flaggings on account cancelation
Version: 7.x-3.x-dev » 8.x-4.x-dev
Status: Needs review » Postponed (maintainer needs more info)

> "the module should either do nothing, unpublish content, or anonymize content" ... it should not be deleting user data.

Taking these options one by one:

- do nothing

That leaves flag counts that look incorrect.

- unpublish content

Can't do that, there is no concept of a flagging being unpublished

- anonymize content

We don't have a session ID we can set on the flaggings, and anon flagging might not be enabled.

casey’s picture

StatusFileSize
new454 bytes
eelkeblok’s picture

Status: Postponed (maintainer needs more info) » Needs review

I am seeing this functionality actually causing issues on cancelling a user that has a lot of flaggings. I think blanket deleting all flaggings whenever hook_user_cancel is called is not the right approach;

  • Not all cancellation methods actually remove the account, blocking the account is a valid outcome as well
  • When a cancellation method actually ends up deleting the account, the implementation of hook_user_predelete() should actually take care of the deleting. That may also end up being less work, because by then a lot of content created by the user may have been deleted as well, and thereby also any associated flaggings, through the implementation of hook_entity_predelete() (which is likely to have happened in a batch, for properly implemented cancellation methods).

This tells me there is actually no need for a hook_user_cancel() implementation in Flag, and it is in fact only causing problems.

Of course, please tell me if I'm missing something. Setting back to "Needs review" to raise the awareness again.

Edit: actually, the idea that a lot of flaggings would have been deleted by deleting the user's content is probably invalid, as flaggings mostly would apply to content created by others.

eelkeblok’s picture

BTW, if it is determined deleting flaggings is the right approach in some cases (maybe when the user is deleted but the content is re-assigned, so there will not actually be many content-related unflaggings) , it would be a good idea to do so by adding to the cancellation batch (it is not a very well documented feature that when hook_user_cancel() is called, there is actually a batch standing by which can be added to by calling batch_set() with an array that contains some extra operations). We may still get in trouble when trying to delete the flaggings at the very end when the user is deleted and there is still a large number left, at that point.

eelkeblok’s picture

Over four years onward, I needed a more nuanced implementation, here's a stab at it. This only deletes flaggings when re-assigning content to the anonymous user (deleting a user will already call the existing pre-delete hook). Also, it checks the number of flaggings to remove to a threshold, and will create a queue to delete the flaggings in batches when it exceeds the threshold. The threshold itself is obviously up for debate, maybe the current 20 is a bit too convervative.

ivnish’s picture

Status: Needs review » Needs work
eelkeblok’s picture

Status: Needs work » Needs review

Can you elaborate on the reason this needs work? AFAICT the merge request is mergeable.

ivnish’s picture

Sorry, I took your message #8 as meaning that the MR needs some work.

ivnish’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests

Also, needs tests

eelkeblok’s picture

Ah, no, it was meant to say I adjusted the MR. Fair enough to get some tests.

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

deaom’s picture

Version: 8.x-4.x-dev » 5.x-dev
Status: Needs work » Needs review

I'm not really sure what tests to add here, but did add one for admin UI and one for service call. Just to test the user and flags actually get removed. Could not cherry pick the commit from 8.x. as there were some changes in FlagService in the 5.x branch, hopefully resolved conflicts correctly. Needs review.

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

idebr changed the visibility of the branch 2489992-we-should-not to hidden.

idebr’s picture

Status: Needs review » Needs work
Issue tags: +Needs issue summary update
  1. The issue summary states the module should do nothing, but it queues flags for removal instead. The issue summary should be updated to reflect the suggested code change
  2. The code path for the queue worker is not tested, since flag removal in the tests does not meet the threshold. The tests should be updated with a user cancellation that meets the threshold so the queue worker is put under test.
  3. The queue worker itself can be converted from an annotation to attributes, since Flag requires drupal/core ^10.3. See https://www.drupal.org/project/drupal/issues/3420988
  4. The queue factory should be injected by its \Drupal\Core\Queue\QueueFactoryInterface

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

benstallings’s picture

Status: Needs work » Needs review
idebr’s picture

Status: Needs review » Needs work

#19 still applies

idebr changed the visibility of the branch account-flag-removal to hidden.

idebr’s picture

Title: We should not delete flaggings on account cancelation » Do not delete flaggings on account cancelation
Category: Bug report » Task
Issue summary: View changes
Issue tags: -Needs tests, -Needs issue summary update
Related issues: +#3633556: Use a queue when deleting a large set of flaggings

The merge request currently has code changes for two issues:

  1. Removes the deletion of flags on user cancel
  2. Deletes large amounts of flaggings in a queue

In order to make the issue easier to review, let's split up these issues. The queue implementation is now split into #3633556: Use a queue when deleting a large set of flaggings

idebr’s picture

Status: Needs work » Needs review
anybody’s picture

Status: Needs review » Reviewed & tested by the community

Nice work. We even have a test!

ivnish’s picture

Version: 5.x-dev » 5.2.x-dev

  • ivnish committed 3b8e0c00 on 5.2.x authored by idebr
    task: #2489992 Do not delete flaggings on account cancelation
    
    By:...
ivnish’s picture

Status: Reviewed & tested by the community » Fixed

Thanks! Merged!

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.