UserFailedLoginsSensorPlugin.php in Monitoring 8
File
src/Plugin/monitoring/SensorPlugin/UserFailedLoginsSensorPlugin.php
View source
<?php
namespace Drupal\monitoring\Plugin\monitoring\SensorPlugin;
use Drupal\monitoring\Result\SensorResultInterface;
class UserFailedLoginsSensorPlugin extends WatchdogAggregatorSensorPlugin {
public function getAggregateQuery() {
$query = parent::getAggregateQuery();
$query
->addField('watchdog', 'variables');
$query
->groupBy('variables');
$query
->orderBy('records_count', 'DESC');
return $query;
}
public function runSensor(SensorResultInterface $result) {
$records_count = 0;
foreach ($this
->getAggregateQuery()
->execute() as $row) {
$records_count += $row->records_count;
$variables = unserialize($row->variables);
$result
->addStatusMessage('@user: @count', array(
'@user' => $variables['%user'],
'@count' => $row->records_count,
));
}
$result
->setValue($records_count);
}
public function resultVerbose(SensorResultInterface $result) {
$output = parent::resultVerbose($result);
$this
->verboseResultCounting($output);
return $output;
}
public function verboseResultCounting(array &$output) {
if ($this->sensorConfig
->getSetting('verbose_fields')) {
$query = $this
->getAggregateQuery();
$query_result = $query
->range(0, 20)
->execute();
$this->queryString = $query_result
->getQueryString();
$rows = $this
->buildTableRows($query_result
->fetchAll());
$results = [];
foreach ($rows as $key => $row) {
$results[$key] = [];
$variables = unserialize($row['variables']);
$results[$key]['user'] = $variables['%user'];
$results[$key]['attempts'] = $row['records_count'];
}
$output['attempts_per_user'] = array(
'#type' => 'verbose_table_result',
'#title' => t('Attempts per user'),
'#header' => $this
->buildTableHeader($results),
'#rows' => $results,
'#query' => $query_result
->getQueryString(),
'#query_args' => $query
->getArguments(),
);
}
}
}