/** * {@inheritdoc} */ public function preSave() { parent::preSave(); $entity = $this->getEntity(); if ($this->pre_hashed) { // Reset the pre_hashed value since it has now been used. $this->pre_hashed = FALSE; } elseif ($entity->isNew() || strlen(trim($this->value)) > 0 && $this->value != $entity->original->{$this->getFieldDefinition()->getName()}->value) { // Allow alternate password hashing schemes. $this->value = \Drupal::service('password')->hash(trim($this->value)); // Abort if the hashing failed and returned FALSE. if (!$this->value) { 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($this->value)) { $this->value = $entity->original->{$this->getFieldDefinition()->getName()}->value; } } // Ensure that the existing password is unset to minimise risks of it // getting serialized and stored somewhere. $this->existing = NULL; }
/** * {@inheritdoc} */ public function preSave() { parent::preSave(); $entity = $this->getEntity(); // Update the user password if it has changed. if ($entity->isNew() || $this->value && $this->value != $entity->original->{$this->getFieldDefinition()->getName()}->value) { // Allow alternate password hashing schemes. $this->value = \Drupal::service('password')->hash(trim($this->value)); // Abort if the hashing failed and returned FALSE. if (!$this->value) { 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($this->value)) { $this->value = $entity->original->{$this->getFieldDefinition()->getName()}->value; } } }