So this issue stems from the fact that when you save an OG membership although you are only saving one entity (the og_membership), you are actually influencing two entities (the other being the entity reference field on say, a user).

Firstly, a proof of concept will help: Implement hook_og_membership_insert(), add a user to a group and use code similar to this to debug:

function MY_MODULE_og_membership_insert($og_membership) {
  dsm(user_load($og_membership->etid));
}

What you should notice (or I am anyway), is that the user object that gets loaded doesn't have the new membership field value embedded. This means that if we should want to run the object though a user_save() we would unsubscribe the user from the group.

This is important because it's breaking rules integration; og_og_membership_insert() triggers a rule and it includes a reloaded user object. The problem is that the user object doesn't get that membership, so when I create a simple rule to add a role to a user on membership creation save they loose their group association but get the role.

Now my initial thought was that we should be using something like entity_load_unchanged() instead, because that goes around the cache. But it turns out (and maybe this is a Drupal core issue) that entity_load_unchanged() will still load field data from the cache_field bin. To stop that you would have to call cache_clear_all("field:user:$og_membership->etid", 'cache_field'); first.

I'm not sure what needs to change, but I think that when an og_membership is saved, the target entity needs to have it's field cache invalidated somehow, even if that's via a direct call to cache_clear_all().

CommentFileSizeAuthor
#6 og-entity-cache-support-1981406-6.patch996 bytesRoySegall
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

cgmonroe’s picture

I can confirm that this is a problem using EntityCache with CAS for authentication and probably LDAP as well. I have a site using CAS that keeps reporting that they added a user to a group via the Group UI but later the user is not there.

Here's what I finally tracked down:

Group Admin added a user to a group (or groups) via the Group / Add person UI. The list of People in the group now shows that person.

However, if you then edit the user's account settings, the user's group membership field does not show the new group(s).

This is because it is using the entitycache version of the user object which is stale.

The user then logs in via CAS. When CAS creates a user session, the code loads the user object, adds some CAS specific info to it, and saves the user.

However, CAS get the user object from the Entity Cache table with the out of date group membership field.

When the CAS saves the user object, OG does not see the new group(s) listed and assumes the user has been unregistrered from them. So, it removes the added membership(s). This is probably also happen if you are using the LDAP module since I believe it does the same load/save method to add it's information to a user.

Bottom line is that if you are using EntityCache with OG you may have group member problems if the system cache is not cleared after adding a member.

I *think* I have some code to solve this. But OG is complex and I'd like someone to review before I create a patch.

Proposed solution:

Modify the OGMembership class (og.membership.inc) so that the save() and delete() methods have the following code at the end:

// Clear the user cache if entitycache is enabled
if ( $this->entity_type == 'user' && module_exists('entitycache') ) {
  cache_clear_all( $this->etid, 'cache_entity_user' );
}

I suspect that there might be some other cases needed for some of the other entity types that entitycache supports but the user case is the major one.

bryancasler’s picture

Scyther’s picture

Have problems when adding, and removing users from group when I have Entity Cache enabled

RoySegall’s picture

@animelion Don't see how that issue is relate although this is a buggy issue and need a further look.
@cgmonroe You can suggest patches without fear of the comments(well, there could be a minor faer ;)) because not knowing where you applied the fixes are a lot more difficult then reviewing the patch.

I'll try to sum this issue as much as i can: when the entity cache module is enabled removing and adding users are not working as expected. I'll take a look on that. Can you confirm this applied on nodes or other group content?

RoySegall’s picture

I managed to reproduce and i describe exactly because #1 way to reproduce wasn't clear enough(for me):
1. Enable the entity cache module
2. Add a new user to the group
3. Go to the edit page of the user. The group is not selected in the the group membership field.

I managed to reproduce this for nodes as well - all i needed to do is to use the API function og_group which not invalidate the cache of the entity which grouped(i.e if i grouped node then the entity cache for node need to be invalidate).

RoySegall’s picture

Status: Active » Needs review
FileSize
996 bytes

Adding patch, it's worked for me. When the OG membership is saved or deleted we need to invalidate the cache.

jaxtheking’s picture

Thanks Roy, you're a God-sent saviour! I have a pretty complex setup running on Commons and I run into the exact same issue described by you in #5.

I can gladly confirm your patch in #6 has indeed solved the issue. Great work guys, thanks

amitaibu’s picture

Status: Needs review » Fixed

Committed, thanks.

Status: Fixed » Closed (fixed)

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