User::postSave

public User::postSave(EntityStorageInterface $storage, $update = TRUE)

Acts on a saved entity before the insert or update hook is invoked.

Used after the entity is saved, but before invoking the insert or update hook. Note that in case of translatable content entities this callback is only fired on their current translation. It is up to the developer to iterate over all translations if needed.

Parameters

\Drupal\Core\Entity\EntityStorageInterface $storage: The entity storage object.

bool $update: TRUE if the entity has been updated, or FALSE if it has been inserted.

Overrides Entity::postSave

File

core/modules/user/src/Entity/User.php, line 101

Class

User
Defines the user entity class.

Namespace

Drupal\user\Entity

Code

public function postSave(EntityStorageInterface $storage, $update = TRUE) {
  parent::postSave($storage, $update);

  if ($update) {
    $session_manager = \Drupal::service('session_manager');
    // If the password has been changed, delete all open sessions for the
    // user and recreate the current one.
    if ($this->pass->value != $this->original->pass->value) {
      $session_manager->delete($this->id());
      if ($this->id() == \Drupal::currentUser()->id()) {
        \Drupal::service('session')->migrate();
      }
    }

    // If the user was blocked, delete the user's sessions to force a logout.
    if ($this->original->status->value != $this->status->value && $this->status->value == 0) {
      $session_manager->delete($this->id());
    }

    // Send emails after we have the new user object.
    if ($this->status->value != $this->original->status->value) {
      // The user's status is changing; conditionally send notification email.
      $op = $this->status->value == 1 ? 'status_activated' : 'status_blocked';
      _user_mail_notify($op, $this);
    }
  }
}
doc_Drupal
2016-10-29 09:52:09
Comments
Leave a Comment

Please login to continue.