When using a rule to remove a (non-OG) role from a user when that user is removed from the only group of which they are a member, the user actually gets added to the group again, so that the user appears twice in the membership list for that group.

Steps to reproduce:

- Install Drupal with the minimal profile
- Create a role called testrole
- Dowload OG, rules, and their dependencies
- Enable og_ui and rules_admin
- Add a content type called testtype and make it an OG group
- Create a rule called testrule that reacts to the "User has become a group member" event
- Add an action of "Add user role" which adds testrole to the user.
- Create a node called testnode of testtype
- Add a user called testuser
- Add testuser as a member to testnode

"People in group testnode" now shows both user number 1 and testuser are group members. On the core Drupal People page you can see that both of them have the role testrole.

- Add a rule called testrule2 that reacts to the "User has been removed from group" event
- Add a condition of "Value is empty" with parameter account:og-membership
- Add an action of "Remove user role" which removes testrole from the user
- Remove testuser from the testnode group

"People in group testnode" now shows testuser twice.

I've attached the database export from MySQL in case that is useful.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

crantok’s picture

Title: Ruoles integration bug - user added twice to same group » Rules integration bug - user added twice to same group
markpavlitski’s picture

Version: 7.x-2.7 » 7.x-2.x-dev
Status: Active » Needs review
FileSize
1.48 KB

The issue is triggered because Rules calls user_save() after the "User has become a group member" rule has been executed, and this results in og_group() being called a second time during page load.

This shouldn't be an issue, but og_get_membership() uses a static cache to keep track of which entities are in which groups and this isn't cleared after a change in group membership.

The attached patch clears the og static caches whenever a change takes place to the membership information using the existing insert/update/delete hooks.

markpavlitski’s picture

Priority: Normal » Major

Bumping to Major priority as rules integration is a very common use-case and this causes both group administration and content duplication issues (appearing duplicated in og/commons Views), especially with Drupal Commons.

markpavlitski’s picture

The previous patch was too generic when clearing the static cache, which seems to cause an issue with the UIF module.

This patch clears only the specific static cache entries.

ultimike’s picture

I'm seeing the exact same problem as the original poster, even with the patch in comment 4 applied. It seems that after the "User has been removed from group" rule fires, og_og_membership_insert() is still being called and re-adding the user membership.

-mike

hackwater’s picture

I'm trying to use Rules to add or remove a value on a user entity (the user entity of the og membership entity) when the user is added or removed from a group. Regardless of whether I use the "User has been removed from group" or "After deleting a og membership" trigger, like ultimike, even after applying the patch in #4, I'm seeing that the og membership gets restored (og membership ID is incremented).

hackwater’s picture

Status: Needs review » Needs work

I decided to try this outside of Rules: implement hook_og_membership_insert and hook_og_membership_delete in a module and update or clear the field I was targeting in my rules. Interestingly, I had the same result: I couldn't get the insert to work via the OG admin interface, but manually updating the user to add the group worked, setting my field to the correct value, and then attempting to remove the user from the group began exhibiting the same behavior: the user remained in the group, but the og_membership id got incremented (aka old membership deleted and then restored). So this behavior seems independent of the Rules module.

vadym.kononenko’s picture

I see this is related to #2148047 issue.

indigoxela’s picture

Any update on this issue?

The problem still exists in 7.x-2.10. I tried patch #4, but it doesn't solve the problem.

indigoxela’s picture

I can confirm hackwater's finding: this problem is actually completely independent from rules.

Even if using hook_og_membership_delete() in a custom module. As soon as anything does user_save (could be directly, could be via user_multiple_role_edit), then the membership gets created again immediately after deletion.

This really seems to be a major bug in the module's logic. I suspect, it has something to do with the membership field (og_group_ref), but I can't provide a solution.

indigoxela’s picture

OK, there are several bug reports addressing the base problem.

I wonder, if there's actually an issue, where possible work could be brought together.
None of these threads seems to be very active.

Anaconda777’s picture

I have same problem with 7.x-2.10.
When group node is deleted, the author will stay member of the group even it is deleted (user stays in the og_membership table).

In my case the problem comes only when group node is deleted by site administrator and a role is added or removed to the node:author using a rule.
Problem does not exists if the group node is deleted by the author, the author will not appear back to the og_membership table.

So maybe it's something related to (user save) when it's done by admin...

Tried also with the latest dev version but same problem.