Change FilterMatch to FilterEqual class for single object filter on object name.

Correspondingly the DbConnection::renderFilter() (as DbQuery::renderFilter() is deprecated and will be removed, hence no modifications here)
is also modified to render accordingly.
This correctly selects the host, service or contact in case the object name contains wild card characters like "\*".
This commit is contained in:
raviks789 2022-05-03 10:37:17 +02:00 committed by Johannes Meyer
parent 3edb5c3c94
commit 8898ed85ab
5 changed files with 13 additions and 8 deletions

View File

@ -6,6 +6,8 @@ namespace Icinga\Data\Db;
use DateTime;
use DateTimeZone;
use Exception;
use Icinga\Data\Filter\FilterMatch;
use Icinga\Data\Filter\FilterMatchNot;
use Icinga\Data\Inspectable;
use Icinga\Data\Inspection;
use PDO;
@ -552,13 +554,13 @@ class DbConnection implements Selectable, Extensible, Updatable, Reducible, Insp
throw new ProgrammingError(
'Unable to render array expressions with operators other than equal or not equal'
);
} elseif ($sign === '=' && strpos($value, '*') !== false) {
} elseif ($filter instanceof FilterMatch && strpos($value, '*') !== false) {
if ($value === '*') {
return $column . ' IS NOT NULL';
}
return $column . ' LIKE ' . $this->dbAdapter->quote(preg_replace('~\*~', '%', $value));
} elseif ($sign === '!=' && strpos($value, '*') !== false) {
} elseif ($filter instanceof FilterMatchNot && strpos($value, '*') !== false) {
if ($value === '*') {
return $column . ' IS NULL';
}

View File

@ -3,6 +3,7 @@
namespace Icinga\Module\Monitoring\Controllers;
use Icinga\Data\Filter\FilterEqual;
use Icinga\Module\Monitoring\Backend;
use Icinga\Module\Monitoring\Controller;
use Icinga\Security\SecurityException;
@ -63,7 +64,7 @@ class ShowController extends Controller
'contact_notify_host_downtime',
));
$this->applyRestriction('monitoring/filter/objects', $query);
$query->where('contact_name', $contactName);
$query->whereEx(new FilterEqual('contact_name', '=', $contactName));
$contact = $query->getQuery()->fetchRow();
if ($contact) {

View File

@ -3,11 +3,11 @@
namespace Icinga\Module\Monitoring\DataView;
use Icinga\Data\Filter\FilterExpression;
use IteratorAggregate;
use Icinga\Application\Hook;
use Icinga\Data\ConnectionInterface;
use Icinga\Data\Filter\Filter;
use Icinga\Data\Filter\FilterMatch;
use Icinga\Data\FilterColumns;
use Icinga\Data\PivotTable;
use Icinga\Data\QueryInterface;
@ -456,7 +456,7 @@ abstract class DataView implements QueryInterface, SortRules, FilterColumns, Ite
*/
public function validateFilterColumns(Filter $filter)
{
if ($filter instanceof FilterMatch) {
if ($filter instanceof FilterExpression) {
if (! $this->isValidFilterTarget($filter->getColumn())) {
throw new QueryException(
mt('monitoring', 'The filter column "%s" is not allowed here.'),

View File

@ -3,6 +3,7 @@
namespace Icinga\Module\Monitoring\Object;
use Icinga\Data\Filter\FilterEqual;
use InvalidArgumentException;
use Icinga\Module\Monitoring\Backend\MonitoringBackend;
@ -142,7 +143,7 @@ class Host extends MonitoredObject
'instance_name'
);
return $this->backend->select()->from('hoststatus', $columns)
->where('host_name', $this->host);
->whereEx(new FilterEqual('host_name', '=', $this->host));
}
/**

View File

@ -3,6 +3,7 @@
namespace Icinga\Module\Monitoring\Object;
use Icinga\Data\Filter\FilterEqual;
use InvalidArgumentException;
use Icinga\Module\Monitoring\Backend\MonitoringBackend;
@ -171,8 +172,8 @@ class Service extends MonitoredObject
'service_state',
'service_state_type'
))
->where('host_name', $this->host->getName())
->where('service_description', $this->service);
->whereEx(new FilterEqual('host_name', '=', $this->host->getName()))
->whereEx(new FilterEqual('service_description', '=', $this->service));
}
/**