You are here

protected function UserExpire::doExecute in User Expire 8

Expire user.

Parameters

\Drupal\user\UserInterface $user: The user object.

mixed $expiration: User expiration date.

File

src/Plugin/RulesAction/UserExpire.php, line 74

Class

UserExpire
Provides "Expire User" action.

Namespace

Drupal\user_expire\Plugin\RulesAction

Code

protected function doExecute(UserInterface $user, $expiration = NULL) {
  if (!empty($expiration)) {

    // If there's an expiration, save it.
    $this->database
      ->merge('user_expire')
      ->key([
      'uid' => $user
        ->id(),
    ])
      ->fields([
      'uid' => $user
        ->id(),
      'expiration' => $expiration,
    ])
      ->execute();
    $user->expiration = $expiration;
    user_expire_notify_user($user);
  }
  else {

    // If the expiration is not set, delete any value that might be set.
    if (!$user
      ->isNew()) {

      // New accounts can't have a record to delete.
      // Existing records (!is_new) might.
      // Remove user expiration times for this user.
      $deleted = $this->database
        ->delete('user_expire')
        ->condition('uid', $user
        ->id())
        ->execute();

      // Notify user that expiration time has been deleted.
      if ($deleted) {
        \Drupal::messenger()
          ->addMessage($this
          ->t("%name's expiration date has been reset.", [
          '%name' => $user
            ->getAccountName(),
        ]));
      }
    }
  }
}