class FloodUnblockManager in Flood Unblock 8
Same name and namespace in other branches
- 8.2 src/FloodUnblockManager.php \Drupal\flood_unblock\FloodUnblockManager
Hierarchy
- class \Drupal\flood_unblock\FloodUnblockManager uses StringTranslationTrait
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'
1 service uses FloodUnblockManager
File
- src/
FloodUnblockManager.php, line 12
Namespace
Drupal\flood_unblockView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
FloodUnblockManager:: |
protected | property | ||
FloodUnblockManager:: |
protected | property | The Database Connection | |
FloodUnblockManager:: |
protected | property | ||
FloodUnblockManager:: |
protected | property | ||
FloodUnblockManager:: |
protected | property | The messenger. | |
FloodUnblockManager:: |
public | function | The function that clear the flood. | |
FloodUnblockManager:: |
public | function | Generate rows from the entries in the flood table. | |
FloodUnblockManager:: |
public | function | Generate rows from the entries in the flood table. | |
FloodUnblockManager:: |
public | function | FloodUnblockAdminForm constructor. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |