function User::postSave

Same name in other branches
  1. 9 core/modules/user/src/Entity/User.php \Drupal\user\Entity\User::postSave()
  2. 8.9.x core/modules/user/src/Entity/User.php \Drupal\user\Entity\User::postSave()
  3. 11.x core/modules/user/src/Entity/User.php \Drupal\user\Entity\User::postSave()

Overrides ContentEntityBase::postSave

File

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

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();
            }
            $flood_config = \Drupal::config('user.flood');
            $flood_service = \Drupal::flood();
            $identifier = $this->id();
            if ($flood_config->get('uid_only')) {
                // Clear flood events based on the uid only if configured.
                $flood_service->clear('user.failed_login_user', $identifier);
            }
            elseif ($flood_service instanceof PrefixFloodInterface) {
                $flood_service->clearByPrefix('user.failed_login_user', $identifier);
            }
        }
        // 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);
        }
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.