1 user.entity.inc protected UserStorageController::preSave(EntityInterface $entity)

Overrides EntityDatabaseStorageController::preSave().

Parameters

User $entity: The user entity about to be saved.

Overrides EntityDatabaseStorageController::preSave

File

core/modules/user/user.entity.inc, line 362
Entity classes and controllers for User module.

Class

UserStorageController
Controller class for users.

Code

protected function preSave(EntityInterface $entity) {
  // Save current time as last changed time.
  $entity->changed = REQUEST_TIME;

  // Update the user password if it has changed.
  $new_pass = isset($entity->pass) && strlen(trim($entity->pass)) > 0 ? trim($entity->pass) : NULL;
  if ($entity->isNew() || !is_null($new_pass) && $new_pass !== $entity->original->pass) {
    // Allow alternate password hashing schemes.
    require_once BACKDROP_ROOT . '/' . settings_get('password_inc', 'core/includes/password.inc');
    $entity->pass = user_hash_password($new_pass);
    // Abort if the hashing failed and returned FALSE.
    if (!$entity->pass) {
      throw new EntityMalformedException('The entity does not have a password.');
    }
  }

  if (valid_email_address($entity->name)) {
    // Check to see if matching is required.
    $user_email_match = config_get('system.core', 'user_email_match');
    if ($user_email_match && ($entity->name != $entity->mail)) {
      throw new EntityMalformedException('The username and email are both addresses that do not match.');
    }
  }

  if (!empty($entity->picture_upload)) {
    $entity->picture = $entity->picture_upload;
  }
  // Delete the picture if the submission indicates that it should be deleted
  // and no replacement was submitted.
  elseif (!empty($entity->picture_delete)) {
    $entity->picture = NULL;
    file_usage_delete($entity->original->picture, 'user', 'user', $entity->uid);
    file_delete($entity->original->picture->fid);
  }

  if (!$entity->isNew()) {
    // Process picture uploads.
    if (!empty($entity->picture->fid) && (!isset($entity->original->picture->fid) || $entity->picture->fid != $entity->original->picture->fid)) {
      $picture = $entity->picture;
      // If the picture is a temporary file, move it to its final location
      // and make it permanent.
      if (!$picture->status) {
        $info = image_get_info($picture->uri);
        $picture_directory = file_default_scheme() . '://' . config_get('system.core', 'user_picture_path');

        // Prepare the pictures directory.
        file_prepare_directory($picture_directory, FILE_CREATE_DIRECTORY);
        $destination = file_stream_wrapper_uri_normalize($picture_directory . '/picture-' . $entity->uid . '-' . REQUEST_TIME . '.' . $info['extension']);

        // Move the temporary file into the final location.
        if ($picture = file_move($picture, $destination, FILE_EXISTS_RENAME)) {
          $entity->picture = $picture;
          file_usage_add($picture, 'user', 'user', $entity->uid);
        }
      }
      // Delete the previous picture if it was deleted or replaced.
      if (!empty($entity->original->picture->fid)) {
        file_usage_delete($entity->original->picture, 'user', 'user', $entity->uid);
        file_delete($entity->original->picture->fid);
      }
    }
    $entity->picture = empty($entity->picture->fid) ? NULL : $entity->picture->fid;

    // If the password is empty, that means it was not changed, so use the
    // original password.
    if (empty($entity->pass)) {
      $entity->pass = $entity->original->pass;
    }
  }

  // Prepare user roles.
  if (isset($entity->roles)) {
    $entity->roles = array_values(array_filter($entity->roles));
  }

  // Move account cancellation information into $entity->data.
  foreach (array('user_cancel_method', 'user_cancel_notify') as $key) {
    if (isset($entity->{$key})) {
      if (!$entity->data) {
        $entity->data = array();
      }
      $entity->data[$key] = $entity->{$key};
    }
  }
}