diff --git a/src/Commands/FloodUnblockCommands.php b/src/Commands/FloodUnblockCommands.php index a5d8621a5582d0393b138d0d1c6cf5d2c299ff12..46557f46e05877b80a1509431ed6b1a9231ec8bd 100644 --- a/src/Commands/FloodUnblockCommands.php +++ b/src/Commands/FloodUnblockCommands.php @@ -40,6 +40,10 @@ class FloodUnblockCommands extends DrushCommands { $events = $this->manager->getEvents(); foreach ($events as $key => $event) { $fids = $this->manager->getEventIds($key, $ip); + if (empty($fids)) { + $this->output()->writeln("No flood entries found for event {$key} and IP address {$ip}"); + continue; + } foreach ($fids as $fid) { $this->manager->floodUnblockClearEvent($fid); } @@ -57,6 +61,11 @@ class FloodUnblockCommands extends DrushCommands { $events = $this->manager->getEvents(); foreach ($events as $key => $event) { $fids = $this->manager->getEventIds($key); + if (empty($fids)) { + $label = $event['label']; + $this->output()->writeln("No flood entries found for {$label} events"); + continue; + } foreach ($fids as $fid) { $this->manager->floodUnblockClearEvent($fid); } diff --git a/src/FloodUnblockManagerDatabase.php b/src/FloodUnblockManagerDatabase.php index 7a08f0f8a2921ac28c453e55590261105042c989..035e0feb094b52aa405db454b360363fead34959 100755 --- a/src/FloodUnblockManagerDatabase.php +++ b/src/FloodUnblockManagerDatabase.php @@ -88,6 +88,20 @@ class FloodUnblockManagerDatabase extends FloodUnblockManagerBase { $this->logger = $logger_factory->get('flood_control'); } + /** + * Checks if the 'flood' table exists. + * + * @return bool + * TRUE if the table exists, FALSE otherwise. + */ + private function floodTableExists() { + if (!$this->database->schema()->tableExists('flood')) { + $this->logger->warning('The flood table does not exist.'); + return FALSE; + } + return TRUE; + } + /** * {@inheritdoc} */ @@ -99,6 +113,11 @@ class FloodUnblockManagerDatabase extends FloodUnblockManagerBase { * {@inheritdoc} */ public function floodUnblockClearEvent($fid) { + if (!$this->floodTableExists()) { + $this->messenger->addMessage($this->t('The flood table does not exist.'), 'error'); + return; + } + $txn = $this->database->startTransaction('flood_unblock_clear'); try { $query = $this->database->delete('flood') @@ -122,6 +141,10 @@ class FloodUnblockManagerDatabase extends FloodUnblockManagerBase { * {@inheritdoc} */ public function getEntries($limit = 50, $identifier = '', $header = []) { + if (!$this->floodTableExists()) { + return []; + } + $query = $this->database->select('flood', 'f') ->extend('Drupal\Core\Database\Query\TableSortExtender') ->orderByHeader($header); @@ -144,6 +167,10 @@ class FloodUnblockManagerDatabase extends FloodUnblockManagerBase { * {@inheritdoc} */ public function getEventIds($event, $identifier = NULL) { + if (!$this->floodTableExists()) { + return []; + } + $event_ids = []; $query = $this->database->select('flood', 'f'); $query->condition('event', $event); diff --git a/src/Form/FloodUnblockAdminForm.php b/src/Form/FloodUnblockAdminForm.php index 70c44a09e5d9b6b8a55ec1642fe06fbc16d27e49..b002b0405484d1fba3c8ef0ff0d1c87948050903 100755 --- a/src/Form/FloodUnblockAdminForm.php +++ b/src/Form/FloodUnblockAdminForm.php @@ -186,44 +186,54 @@ class FloodUnblockAdminForm extends FormBase { // Fetches data for the table. $entries = $this->floodUnblockManager->getEntries($limit, $identifier, $header); - // Fetches user names or location string for identifiers. - $identifiers = $this->floodUnblockManager->fetchIdentifiers(array_unique($entries['result_identifiers'])); - - foreach ($entries['results'] as $result) { - - // Gets status of identifier. - $is_blocked = $this->floodUnblockManager->isBlocked($result->identifier, $result->event); - - // Defines list of options for tableselect element. - if ($blocked && $is_blocked) { - $options[$result->fid] = [ - 'title' => ['data' => ['#title' => $this->t('Flood id @id', ['@id' => $result->fid])]], - 'identifier' => $identifiers[$result->identifier], - 'blocked' => $is_blocked ? $this->t('Blocked') : $this->t('Not blocked'), - 'event' => $this->floodUnblockManager->getEventLabel($result->event), - 'timestamp' => $this->dateFormatter->format($result->timestamp, 'short'), - 'expiration' => $this->dateFormatter->format($result->expiration, 'short'), - ]; - } - elseif (!$blocked) { - $options[$result->fid] = [ - 'title' => ['data' => ['#title' => $this->t('Flood id @id', ['@id' => $result->fid])]], - 'identifier' => $identifiers[$result->identifier], - 'blocked' => $is_blocked ? $this->t('Blocked') : $this->t('Not blocked'), - 'event' => $this->floodUnblockManager->getEventLabel($result->event), - 'timestamp' => $this->dateFormatter->format($result->timestamp, 'short'), - 'expiration' => $this->dateFormatter->format($result->expiration, 'short'), - ]; + if (!empty($entries)) { + // Fetches user names or location string for identifiers. + $identifiers = $this->floodUnblockManager->fetchIdentifiers(array_unique($entries['result_identifiers'])); + + foreach ($entries['results'] as $result) { + + // Gets status of identifier. + $is_blocked = $this->floodUnblockManager->isBlocked($result->identifier, $result->event); + + // Defines list of options for tableselect element. + if ($blocked && $is_blocked) { + $options[$result->fid] = [ + 'title' => ['data' => ['#title' => $this->t('Flood id @id', ['@id' => $result->fid])]], + 'identifier' => $identifiers[$result->identifier], + 'blocked' => $is_blocked ? $this->t('Blocked') : $this->t('Not blocked'), + 'event' => $this->floodUnblockManager->getEventLabel($result->event), + 'timestamp' => $this->dateFormatter->format($result->timestamp, 'short'), + 'expiration' => $this->dateFormatter->format($result->expiration, 'short'), + ]; + } + elseif (!$blocked) { + $options[$result->fid] = [ + 'title' => ['data' => ['#title' => $this->t('Flood id @id', ['@id' => $result->fid])]], + 'identifier' => $identifiers[$result->identifier], + 'blocked' => $is_blocked ? $this->t('Blocked') : $this->t('Not blocked'), + 'event' => $this->floodUnblockManager->getEventLabel($result->event), + 'timestamp' => $this->dateFormatter->format($result->timestamp, 'short'), + 'expiration' => $this->dateFormatter->format($result->expiration, 'short'), + ]; + } } + // Provides the tableselect element. + $form['table'] = [ + '#type' => 'tableselect', + '#header' => $header, + '#options' => $options, + '#empty' => $this->t('There are no failed logins at this time.'), + ]; } + else { + $form['table'] = [ + '#type' => 'tableselect', + '#header' => $header, + '#options' => $options, + '#empty' => $this->t("There is no table found named 'flood'."), + ]; - // Provides the tableselect element. - $form['table'] = [ - '#type' => 'tableselect', - '#header' => $header, - '#options' => $options, - '#empty' => $this->t('There are no failed logins at this time.'), - ]; + } $form['actions'] = ['#type' => 'actions'];