You are here

function account_sync_update_user in Account Sync 7.2

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

XMLRPC callback to update the user account on /this/ drupal site.

@TODO: What do we do about deleted users?

1 string reference to 'account_sync_update_user'
account_sync_xmlrpc in ./account_sync.module
Implements hook_xmlrpc().

File

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

Code

function account_sync_update_user($server_key, $username, $edit, $remote_account, $category, $roles) {
  global $_account_sync_;

  // Flag to catch recursive syncing.
  $_account_sync_ = TRUE;
  if ($message = _account_sync_sync_in_deny($server_key, $category)) {
    return $message;
  }

  // Find the matching account on our side of things
  // @TODO: What if an account is being renamed and the renamed account
  // matches an existing account on our end?
  if ($username && ($local_account = user_load_by_name($username))) {

    // Found the account, so update it.
    if (user_access('sync account', $local_account)) {
      if (array_key_exists('roles', $edit)) {
        $edit['roles'] = _account_sync_edit_roles($roles, $remote_account['roles'], $local_account->roles);
      }
      unset($edit['original']);
      $local_account = user_save($local_account, $edit, $category);
      _account_sync_update_pass($local_account->uid, $remote_account['pass']);
      $message = array(
        WATCHDOG_INFO,
        'User account @account has been updated.',
        array(
          '@account' => $local_account->name,
        ),
      );
      watchdog('account_sync', 'User account @account has been updated by a remote server.', array(
        '@account' => $local_account->name,
      ));
      module_invoke_all('account_sync_update', $remote_account);
    }
    else {
      $message = array(
        WATCHDOG_ERROR,
        'Not permitted to update account @account.',
        array(
          '@account' => $remote_account['name'],
        ),
      );
      watchdog('account_sync', 'Remote server attempted to sync @account, but it is not in a role that can be synced', array(
        '@account' => $username,
      ));
    }
  }
  else {

    // No account found, so create the user account.
    if (variable_get('account_sync_create_users', FALSE)) {
      unset($remote_account['uid']);
      $user = new stdClass();
      $user->uid = 0;
      $remote_account['roles'] = _account_sync_roles($roles, $remote_account['roles']);
      unset($edit['original']);
      $remote_account['is_new'] = TRUE;
      $local_account = user_save($user, $remote_account);
      _account_sync_update_pass($local_account->uid, $remote_account['pass']);
      $message = array(
        WATCHDOG_INFO,
        'User account @account created',
        array(
          '@account' => $local_account->name,
        ),
      );
      watchdog('account_sync', 'User account @account created by a remote server.', array(
        '@account' => $local_account->name,
      ));
      module_invoke_all('account_sync_add', $remote_account);
    }
    else {

      // No account found, and don't create new accounts on this site.
      $message = array(
        WATCHDOG_ERROR,
        "Couldn't find a valid account matching @account :(",
        array(
          '@account' => $remote_account['init'],
        ),
      );
    }
  }
  return $message;
}