Answers.Body: <p>You could in a custom module, implement hook_user_login()</p>
  
  <p>Then you could make an API call to get the contact id for the user...</p>
  
  <p>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</p>
  
  <pre><code>function YOURMODULE_user_login(&amp;$edit, $account){
    civicrm_initialize();
  
    $result = civicrm_api3('UFMatch', 'get', [
      'sequential' =&gt; 1,
      'uf_id'      =&gt; $account-&gt;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' =&gt; 1,
        'group'      =&gt; 6, // replace with your smart group, group id
        'id'         =&gt; $contact_id,
      ]);
  
      if ($contact_result['count']) {
        if (!user_has_role($rid, $account)) {
          $account-&gt;roles[$rid] = 'Associate';
          user_save($account);
        }
      }
      else {
        if (user_has_role($rid, $account)) {
          unset($account-&gt;roles[$rid]);
          user_save($account);
        }
      }
    }
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-07-16T03:39:09.987Z
Answers.Text: 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....
Answers.UserDisplayName: jackrabbithanna
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-07-16T03:32:20.620Z
Answers.LastActivityDate: 2017-07-16T03:37:31.963Z
Answers.LastEditDate: 2017-07-16T03:37:31.963Z
Answers.UserDisplayName: jackrabbithanna
Answers.Body: <p>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.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-03-15T18:00:45.110Z
Answers.Text: 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.
Answers.UserDisplayName: petednz - fuzion
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-03-15T17:59:35.500Z
Answers.LastActivityDate: 2017-03-15T17:59:35.500Z
Answers.UserDisplayName: petednz - fuzion
Body: <p>(Drupal 7.52 CiviCRM 4.6.24)</p>
  
  <p>I am trying to make sense of a work-around for keeping Drupal roles in sync with a smart group.</p>
  
  <p>The Smart Group takes into account:</p>
  
  <ul>
  <li>Contact Type in Organisation</li>
  <li>Contact SubType </li>
  <li>Membership Type of SubType is "Full Member"</li>
  <li>Membership Type is New, Current, Grace</li>
  <li>Contact with a (custom) Relationship Type of "Associate of:"</li>
  </ul>
  
  <p>When the smart group is created it will only permit Mailing List rather than Access Control (by design, I understand)</p>
  
  <p>So; I want this Smart Group to have permissions for Drupal under a role "Associate"</p>
  
  <p>How do I do this? I understand that CiviGroup Roles Sync does not work with Smart Groups.</p>
  
  <p>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. </p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-03-16T19:42:45.370Z
Comments.Text: Seems related to http://civicrm.stackexchange.com/questions/14862/civigroup-roles-sync-workaround-for-smart-groups. Possibly useful information here?
Comments.UserDisplayName: Eli Lisseck
Tags: <drupal><smartgroups><4.6>
Title: Syncing Drupal Roles from CiviCRM Smart Groups
ThreadText: # Syncing Drupal Roles from CiviCRM Smart Groups
  
  (Asked by: username_0 on 2017-03-15)
  
  (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.
  
  **Tags:** drupal, smartgroups, 4.6
  
  > **Comments:**
  > **username_1**: Seems related to http://civicrm.stackexchange.com/questions/14862/civigroup-roles-sync-workaround-for-smart-groups. Possibly useful information here?  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_2 on 2017-07-16. Score: 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);
        }
      }
    }
  }
  
  > **Comments:**
  > **username_2**: 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....  
  
  ---
  
  ### Answer by username_3 on 2017-03-15. Score: 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.
  
  > **Comments:**
  > **username_3**: 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.  
  
  ---
  
  