How to limit a user to specific OG groups based on user role? In a quite complex project page, I have several user roles (from Drupal core). Further, the page has an internal area which is built upon Organic Groups (OG). There are a lot of groups available, each group is part of a so-called "area". These areas correspond to user roles and what I want to achieve is that users of a role may only subscribe to groups of the area with the same name.

To visualize the concept, let's say I have three user roles (besides anonymous, authenticated and administrator): A, B and C.

There is a node type called "Group" which has a select field for area, the available options are A, B and C.

There are nine groups, called A1, A2, A3, B1, B2, B3, C1, C2 and C3. I don't want users to apply for a group but immediately join (subscribe) to it, but only if the group's area matches the user's role. So user Alex of role A may only see and join A1 through A3 but not any other group.

The OG group permissions only distinguishes between non-member, member and manager. There's the option to subscribe to a group without approval, which I want. But how to combine that with the Drupal user role?
Due to other restrictions in OG I couldn't overcome, I ended up using the Group module instead. In addition to other advantages for my use case, it offers some functions to easily add group memberships (these are entities) by using something like

$entity_type = 'group_membership';
$entity = entity_create($entity_type, array('gid' =&gt; $gid, 'uid' =&gt; $uid));
$wrapper = entity_metadata_wrapper($entity_type, $entity);
$wrapper-&gt;status = 'active';
$wrapper-&gt;added_on = time();
$wrapper-&gt;added_by = 1;
$wrapper-&gt;save();


Adding that into a hook_user_update(&amp;$edit, $account, $category) function, I am able to add and revoke memberships based on the updated account's roles.