You are here

function _account_sync_roles in Account Sync 7.2

Same name and namespace in other branches
  1. 6 account_sync.receiver.inc \_account_sync_roles()

Find a list of roles that should be set on the given account.

If a role exists both on this site and on the sending site then sync it against the account that was sent.

Parameters

Array $all_roles: user_roles() from the sending site

Array $new_roles: roles as set on the account that was sent

Array $existing_roles: roles as they exist on the matching user account on this site

Return value

Array the list of roles to be set on the provided account.

2 calls to _account_sync_roles()
account_sync_update_user in ./account_sync.receiver.inc
XMLRPC callback to update the user account on /this/ drupal site.
_account_sync_edit_roles in ./account_sync.receiver.inc
Helper to convert the $account role format into the $edit role format.

File

./account_sync.receiver.inc, line 123
Handler for receiving and updating user account data.

Code

function _account_sync_roles($all_roles, $new_roles, $existing_roles = NULL) {

  // If not matching roles based on role name, then we can return. This implies
  // that the rids on this site are identical to those on the sending site.
  if (!variable_get('account_sync_match_roles', FALSE)) {
    return $new_roles;
  }

  // Set any roles that exist both on this site AND are enabled on the user
  // account that was sent to us.
  $roles = array();
  foreach (user_roles() as $rid => $role_name) {
    if ($rid == DRUPAL_ANONYMOUS_RID || $rid == DRUPAL_AUTHENTICATED_RID) {
      continue;
    }
    if (in_array($role_name, $new_roles, TRUE)) {
      $roles[$rid] = $role_name;
    }
  }

  // Set any roles that are currently enabled on the user on this site and
  // don't exist on the sending site.
  if (isset($existing_roles)) {
    foreach ($existing_roles as $rid => $name) {

      // Add in any roles that were already set but don't exist on the
      // foreign site.
      if (!in_array($name, $all_roles)) {
        $roles[$rid] = $name;
      }
    }
  }
  return $roles;
}