You are here

class FloodUnblockManager in Flood control 2.0.x

Provides Flood Unblock actions.

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_control.services.yml in ./flood_control.services.yml
flood_control.services.yml
1 service uses FloodUnblockManager
flood_control.flood_unblock_manager in ./flood_control.services.yml
Drupal\flood_control\FloodUnblockManager

File

src/FloodUnblockManager.php, line 15

Namespace

Drupal\flood_control
View source
class FloodUnblockManager implements FloodUnblockManagerInterface {
  use StringTranslationTrait;

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

  /**
   * The Entity Type Manager Interface.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

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

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

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

  /**
   * FloodUnblockAdminForm constructor.
   *
   * @param \Drupal\Core\Database\Connection $database
   *   The database connection.
   * @param \Drupal\Core\Flood\FloodInterface $flood
   *   The flood interface.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
   *   The Config Factory Interface.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
   *   The Entity Type Manager Interface.
   * @param \Drupal\Core\Messenger\MessengerInterface $messenger
   *   The Messenger Interface.
   */
  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;
  }

  /**
   * {@inheritdoc}
   */
  public function fetchIdentifiers($results) {
    $identifiers = [];
    foreach ($results as $result) {

      // Sets ip as default value and adds to identifiers array.
      $identifiers[$result] = $result;

      // Sets location as value and adds to identifiers array.
      if (function_exists('smart_ip_get_location')) {
        $location = smart_ip_get_location($result);
        $location_string = sprintf(" (%s %s %s)", $location['city'], $location['region'], $location['country_code']);
        $identifiers[$result] = "{$location_string} ({$result})";
      }

      // Sets link to user as value and adds to identifiers array.
      $parts = explode('-', $result);
      if (isset($parts[0]) && isset($parts[1])) {
        $uid = $parts[0];

        /** @var \Drupal\user\Entity\User $user */
        $user = $this->entityTypeManager
          ->getStorage('user')
          ->load($uid);
        if (isset($user)) {
          $user_link = $user
            ->toLink($user
            ->getAccountName());
        }
        else {
          $user_link = $this
            ->t('Deleted user: @user', [
            '@user' => $uid,
          ]);
        }
        $identifiers[$result] = $user_link;
      }
    }
    return $identifiers;
  }

  /**
   * {@inheritdoc}
   */
  public function floodUnblockClearEvent($fid) {
    $txn = $this->database
      ->startTransaction('flood_unblock_clear');
    try {
      $query = $this->database
        ->delete('flood')
        ->condition('fid', $fid);
      $success = $query
        ->execute();
      if ($success) {
        $this->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);
      $this->messenger
        ->addMessage($this
        ->t('Error: @error', [
        '@error' => (string) $e,
      ]), 'error');
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getEvents() {
    return [
      'user.failed_login_ip' => [
        'type' => 'ip',
        'label' => $this
          ->t('User failed login IP'),
      ],
      'user.failed_login_user' => [
        'type' => 'user',
        'label' => $this
          ->t('User failed login user'),
      ],
      'user.http_login' => [
        'type' => 'user',
        'label' => $this
          ->t('User failed http login'),
      ],
      'user.password_request_ip' => [
        'type' => 'user',
        'label' => $this
          ->t('User failed password request IP'),
      ],
      'user.password_request_user' => [
        'type' => 'user',
        'label' => $this
          ->t('User failed password request user'),
      ],
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function getEventLabel($event) {
    $event_mapping = $this
      ->getEvents();
    if (array_key_exists($event, $event_mapping)) {
      return $event_mapping[$event]['label'];
    }
    return ucfirst(str_replace([
      '.',
      '_',
    ], ' ', $event));
  }

  /**
   * {@inheritdoc}
   */
  public function getEventType($event) {
    $event_mapping = $this
      ->getEvents();
    if (array_key_exists($event, $event_mapping)) {
      return $event_mapping[$event]['type'];
    }
    $parts = explode('.', $event);
    return $parts[0];
  }

  /**
   * {@inheritdoc}
   */
  public function isBlocked($identifier, $event) {
    $type = $this
      ->getEventType($event);
    switch ($type) {
      case 'user':
        return !$this->flood
          ->isAllowed($event, $this->config
          ->get('user_limit'), $this->config
          ->get('user_window'), $identifier);
      case 'ip':
        return !$this->flood
          ->isAllowed($event, $this->config
          ->get('ip_limit'), $this->config
          ->get('ip_window'), $identifier);
    }
    return FALSE;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FloodUnblockManager::$config protected property The Immutable Config.
FloodUnblockManager::$database protected property The Database Connection.
FloodUnblockManager::$entityTypeManager protected property The Entity Type Manager Interface.
FloodUnblockManager::$flood protected property The Flood Interface.
FloodUnblockManager::$messenger protected property The messenger.
FloodUnblockManager::fetchIdentifiers public function Gets the user link or location string for an identifier. Overrides FloodUnblockManagerInterface::fetchIdentifiers
FloodUnblockManager::floodUnblockClearEvent public function Removes rows from flood table. Overrides FloodUnblockManagerInterface::floodUnblockClearEvent
FloodUnblockManager::getEventLabel public function Gets the label of an event. Overrides FloodUnblockManagerInterface::getEventLabel
FloodUnblockManager::getEvents public function Gets metadata about events. Overrides FloodUnblockManagerInterface::getEvents
FloodUnblockManager::getEventType public function Gets the type of an event. Overrides FloodUnblockManagerInterface::getEventType
FloodUnblockManager::isBlocked public function Provides identifier's flood status. Overrides FloodUnblockManagerInterface::isBlocked
FloodUnblockManager::__construct public function FloodUnblockAdminForm constructor.
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
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.