1

(Drupal 7.52 CiviCRM 4.6.24)

I am trying to make sense of a work-around for keeping Drupal roles in sync with a smart group.

The Smart Group takes into account:

  • Contact Type in Organisation
  • Contact SubType
  • Membership Type of SubType is "Full Member"
  • Membership Type is New, Current, Grace
  • Contact with a (custom) Relationship Type of "Associate of:"

When the smart group is created it will only permit Mailing List rather than Access Control (by design, I understand)

So; I want this Smart Group to have permissions for Drupal under a role "Associate"

How do I do this? I understand that CiviGroup Roles Sync does not work with Smart Groups.

The reason I set it up like this is that when organisation memberships lapse or are revoked; I want the Associates to lose access with automation.

2

You could in a custom module, implement hook_user_login()

Then you could make an API call to get the contact id for the user...

Then you can do an API lookup to see if the contact is in the group (In this example I have a smart group with id 6. Then if the contact is in the group, and the user account doesn't have the role, add it....if the contact is not in the group and has the role, then remove the role. Easy peasy

function YOURMODULE_user_login(&$edit, $account){
  civicrm_initialize();

  $result = civicrm_api3('UFMatch', 'get', [
    'sequential' => 1,
    'uf_id'      => $account->uid,
  ]);

  if ($result['count']) {
    $contact_id = $result['values'][0]['contact_id'];
  }

  // replace 2 with the rid of the role Associate
  $rid = 2;
  if (!empty($contact_id)) {
    $contact_result = civicrm_api3('Contact', 'get', [
      'sequential' => 1,
      'group'      => 6, // replace with your smart group, group id
      'id'         => $contact_id,
    ]);

    if ($contact_result['count']) {
      if (!user_has_role($rid, $account)) {
        $account->roles[$rid] = 'Associate';
        user_save($account);
      }
    }
    else {
      if (user_has_role($rid, $account)) {
        unset($account->roles[$rid]);
        user_save($account);
      }
    }
  }
}
  • 1
    I don't know why that Group Roles Sync module doesn't think it can work with smart groups, that module could just use some help.... – jackrabbithanna Jul 16 '17 at 3:39
0

Dealing with related contacts can be done by setting the Memberships up as 'inherited memberships' based on a Relationship, in your case 'Associate is' (ie from the Org name). Then when the Org lapses so do the inherited Memberships, and therefore you can use the Member Sync approach to ensuring the Associates have the relevant Drupal Role.

  • I understand this is not the approach you took, and therefore may not be an acceptable answer, but I think it is helpful to others who might face the same scenario and who can use Inherited Memberships. – petednz - fuzion Mar 15 '17 at 18:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.