You are here

public function User::postSave in Drupal 10

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

File

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

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