You are here

public function User::postSave in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/modules/user/src/Entity/User.php \Drupal\user\Entity\User::postSave()

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 106
Contains \Drupal\user\Entity\User.

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);
    }
  }
}