You are here

class FloodUnblockManager in Flood Unblock 8

Same name and namespace in other branches
  1. 8.2 src/FloodUnblockManager.php \Drupal\flood_unblock\FloodUnblockManager

Hierarchy

Expanded class hierarchy of FloodUnblockManager

2 files declare their use of FloodUnblockManager
FloodUnblockAdminForm.php in src/Form/FloodUnblockAdminForm.php
FloodUnblockCommands.php in src/Commands/FloodUnblockCommands.php
1 string reference to 'FloodUnblockManager'
flood_unblock.services.yml in ./flood_unblock.services.yml
flood_unblock.services.yml
1 service uses FloodUnblockManager
flood_unblock.flood_unblock_manager in ./flood_unblock.services.yml
Drupal\flood_unblock\FloodUnblockManager

File

src/FloodUnblockManager.php, line 12

Namespace

Drupal\flood_unblock
View source
class FloodUnblockManager {
  use StringTranslationTrait;

  /**
   * The Database Connection
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $database;

  /**
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * @var \Drupal\Core\Flood\FloodInterface
   */
  protected $flood;

  /**
   * @var \Drupal\Core\Config\ImmutableConfig
   */
  protected $config;

  /**
   * The messenger.
   *
   * @var \Drupal\Core\Messenger\MessengerInterface
   */
  protected $messenger;

  /**
   * FloodUnblockAdminForm constructor.
   *
   * @param \Drupal\Core\Database\Connection $database
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
   */
  public function __construct(Connection $database, FloodInterface $flood, ConfigFactoryInterface $configFactory, EntityTypeManagerInterface $entityTypeManager, MessengerInterface $messenger) {
    $this->database = $database;
    $this->flood = $flood;
    $this->entityTypeManager = $entityTypeManager;
    $this->config = $configFactory
      ->get('user.flood');
    $this->messenger = $messenger;
  }

  /**
   * Generate rows from the entries in the flood table.
   *
   * @return array
   *   Ip blocked entries in the flood table.
   */
  public function get_blocked_ip_entries() {
    $entries = [];
    if ($this->database
      ->schema()
      ->tableExists('flood')) {
      $query = $this->database
        ->select('flood', 'f');
      $query
        ->addField('f', 'identifier');
      $query
        ->addField('f', 'identifier', 'ip');
      $query
        ->addExpression('count(*)', 'count');
      $query
        ->condition('f.event', '%failed_login_ip', 'LIKE');
      $query
        ->groupBy('identifier');
      $results = $query
        ->execute();
      foreach ($results as $result) {
        if (function_exists('smart_ip_get_location')) {
          $location = smart_ip_get_location($result->ip);
          $location_string = sprintf(" (%s %s %s)", $location['city'], $location['region'], $location['country_code']);
        }
        else {
          $location_string = '';
        }
        $blocked = !$this->flood
          ->isAllowed('user.failed_login_ip', $this->config
          ->get('ip_limit'), $this->config
          ->get('ip_window'), $result->ip);
        $entries[$result->identifier] = [
          'type' => 'ip',
          'ip' => $result->ip,
          'count' => $result->count,
          'location' => $location_string,
          'blocked' => $blocked,
        ];
      }
    }
    return $entries;
  }

  /**
   * Generate rows from the entries in the flood table.
   *
   * @return array
   *   User blocked entries in the flood table.
   */
  public function get_blocked_user_entries() {
    $entries = [];
    if ($this->database
      ->schema()
      ->tableExists('flood')) {
      $query = $this->database
        ->select('flood', 'f');
      $query
        ->addField('f', 'identifier');
      $query
        ->addExpression('count(*)', 'count');
      $query
        ->condition('f.event', '%failed_login_user', 'LIKE');
      $query
        ->groupBy('identifier');
      $results = $query
        ->execute();
      foreach ($results as $result) {
        $parts = explode('-', $result->identifier);
        $result->uid = $parts[0];
        $result->ip = $parts[1] ?? NULL;
        if (function_exists('smart_ip_get_location') && $result->ip) {
          $location = smart_ip_get_location($result->ip);
          $location_string = sprintf(" (%s %s %s)", $location['city'], $location['region'], $location['country_code']);
        }
        else {
          $location_string = '';
        }
        $blocked = !$this->flood
          ->isAllowed('user.failed_login_user', $this->config
          ->get('user_limit'), $this->config
          ->get('user_window'), $result->identifier);

        /** @var \Drupal\user\Entity\User $user */
        $user = $this->entityTypeManager
          ->getStorage('user')
          ->load($result->uid);
        if (isset($user)) {
          $user_link = $user
            ->toLink($user
            ->getAccountName());
        }
        else {
          $user_link = $this
            ->t('Deleted user: @user', [
            '@user' => $result->uid,
          ]);
        }
        $entries[$result->identifier] = [
          'type' => 'user',
          'uid' => $result->uid,
          'ip' => $result->ip,
          'username' => $user_link,
          'count' => $result->count,
          'location' => $location_string,
          'blocked' => $blocked,
        ];
      }
    }
    return $entries;
  }

  /**
   * The function that clear the flood.
   */
  public function flood_unblock_clear_event($type, $identifier) {
    $txn = $this->database
      ->startTransaction('flood_unblock_clear');
    try {
      $query = $this->database
        ->delete('flood')
        ->condition('event', '%' . $type, 'LIKE');
      if (isset($identifier)) {
        $query
          ->condition('identifier', $identifier);
      }
      $success = $query
        ->execute();
      if ($success) {
        \Drupal::messenger()
          ->addMessage($this
          ->t('Flood entries cleared.'), 'status', FALSE);
      }
    } catch (\Exception $e) {

      // Something went wrong somewhere, so roll back now.
      $txn
        ->rollback();

      // Log the exception to watchdog.
      watchdog_exception('type', $e);
      \Drupal::messenger()
        ->addMessage($this
        ->t('Error: @error', [
        '@error' => (string) $e,
      ]), 'error');
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FloodUnblockManager::$config protected property
FloodUnblockManager::$database protected property The Database Connection
FloodUnblockManager::$entityTypeManager protected property
FloodUnblockManager::$flood protected property
FloodUnblockManager::$messenger protected property The messenger.
FloodUnblockManager::flood_unblock_clear_event public function The function that clear the flood.
FloodUnblockManager::get_blocked_ip_entries public function Generate rows from the entries in the flood table.
FloodUnblockManager::get_blocked_user_entries public function Generate rows from the entries in the flood table.
FloodUnblockManager::__construct public function FloodUnblockAdminForm constructor.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.