You are here

function _user_resource_update in Services 7.3

Same name and namespace in other branches
  1. 6.3 resources/user_resource.inc \_user_resource_update()

Update an existing user.

This function uses drupal_form_submit() and as such expects all input to match the submitting form in question.

Parameters

$uid: Unique identifier for this user

$account: Fields to modify for this user.

Return value

The modified user object.

1 string reference to '_user_resource_update'
_user_resource_definition in resources/user_resource.inc

File

resources/user_resource.inc, line 401

Code

function _user_resource_update($uid, $account) {

  // Adds backwards compatability with regression fixed in #1083242
  $account = _services_arg_value($account, 'data');
  $account['uid'] = $uid;
  $account_loaded = user_load($uid);

  // Load the required includes for saving profile information
  // with drupal_form_submit().
  module_load_include('inc', 'user', 'user.pages');

  // If a profile category was passed in, use it. Otherwise default
  // to 'account' (for saving core user data.)
  $category = 'account';
  if (isset($account['category'])) {
    $category = $account['category'];
    unset($account['category']);
  }

  // Prepare values for the user profile image.
  if (array_key_exists('picture_upload', $account)) {
    if (is_array($account['picture_upload'])) {

      // Check if it's an array and convert to object.
      $account['picture_upload'] = (object) $account['picture_upload'];
    }
    elseif (is_int($account['picture_upload'])) {

      // Check if it's an integer and get the file object.
      $file_validate = file_validate_is_image($file = file_load($account['picture_upload']));
      if (empty($file_validate)) {
        $account['picture_upload'] = $file;
      }
    }
    elseif (is_string($account['picture_upload'])) {

      // Check if it's an string and get the file object.
      $file_validate = file_validate_is_image($file = file_load((int) $account['picture_upload']));
      if (empty($file_validate)) {
        $account['picture_upload'] = $file;
      }
    }
  }

  // Drop any passed in values into the $account var. Anything
  // unused by the form just gets ignored. We handle roles and
  // password separately.
  foreach ($account as $key => $value) {
    if ($key != 'pass' && $key != 'roles') {
      $form_state['values'][$key] = $value;
    }
  }

  // Prepare values of roles. Check user's permission before allowing changes to roles.
  if (!isset($account['roles']) || !user_access('administer users')) {
    $account['roles'] = $account_loaded->roles;
  }
  foreach ($account['roles'] as $key => $value) {
    if (!empty($value)) {
      $form_state['values']['roles'][$key] = $key;
    }
  }
  unset($form_state['values']['roles'][2]);

  // Prepare values for password.
  if (isset($account['pass'])) {

    // For legacy usage, passwords come in as a single string. To match the
    // actual form state value keys used by Drupal, we also can collect two
    // passwords via an array.
    if (is_array($account['pass'])) {
      $form_state['values']['pass'] = $account['pass'];
    }
    else {
      $form_state['values']['pass']['pass1'] = $account['pass'];
      $form_state['values']['pass']['pass2'] = $account['pass'];
    }
  }

  // If user is changing name, make sure they have permission.
  if (isset($account['name']) && $account['name'] != $account_loaded->name && !(user_access('change own username') || user_access('administer users'))) {
    return services_error(t('You are not allowed to change your username.'), 406);
  }
  $form_state['values']['op'] = variable_get('services_user_save_button_resource_update', t('Save'));
  $form_state['programmed_bypass_access_check'] = FALSE;
  $ret = drupal_form_submit('user_profile_form', $form_state, $account_loaded, $category);

  // Error if needed.
  if ($errors = form_get_errors()) {
    return services_error(implode(" ", $errors), 406, array(
      'form_errors' => $errors,
    ));
  }
  else {
    $account = (object) $account;
    services_remove_user_data($account);
    $account = (array) $account;
    _user_resource_update_services_user($uid, time());
    return $account;
  }
}