protected function UserStorageController::preSave

Overrides Drupal\Core\Entity\DatabaseStorageController::preSave().

Overrides DatabaseStorageController::preSave

File

drupal/core/modules/user/lib/Drupal/user/UserStorageController.php, line 74
Definition of Drupal\user\UserStorageController.

Class

UserStorageController
Controller class for users.

Namespace

Drupal\user

Code

protected function preSave(EntityInterface $entity) {

  // Update the user password if it has changed.
  if ($entity
    ->isNew() || !empty($entity->pass) && $entity->pass != $entity->original->pass) {

    // Allow alternate password hashing schemes.
    $entity->pass = drupal_container()
      ->get('password')
      ->hash(trim($entity->pass));

    // Abort if the hashing failed and returned FALSE.
    if (!$entity->pass) {
      throw new EntityMalformedException('The entity does not have a password.');
    }
  }
  if (!$entity
    ->isNew()) {

    // 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_filter($entity->roles);
  }

  // Store account cancellation information.
  foreach (array(
    'user_cancel_method',
    'user_cancel_notify',
  ) as $key) {
    if (isset($entity->{$key})) {
      drupal_container()
        ->get('user.data')
        ->set('user', $entity
        ->id(), substr($key, 5), $entity->{$key});
    }
  }
}