From d23ba869499f7333a9bf6951438cc80b0d437f60 Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Wed, 19 Oct 2022 15:12:15 +0200 Subject: [PATCH] Add and use behavior `ExpressionInjector` --- .../Model/Behavior/ExpressionInjector.php | 53 +++++++++++++++++++ library/X509/Model/X509Certificate.php | 3 ++ 2 files changed, 56 insertions(+) create mode 100644 library/X509/Model/Behavior/ExpressionInjector.php diff --git a/library/X509/Model/Behavior/ExpressionInjector.php b/library/X509/Model/Behavior/ExpressionInjector.php new file mode 100644 index 00000000..02999d4a --- /dev/null +++ b/library/X509/Model/Behavior/ExpressionInjector.php @@ -0,0 +1,53 @@ +columns = $columns; + } + + public function setQuery(Query $query) + { + $this->query = $query; + + return $this; + } + + public function rewriteCondition(Filter\Condition $condition, $relation = null) + { + $columnName = $condition->metaData()->get('columnName'); + if (in_array($columnName, $this->columns, true)) { + $relationPath = $condition->metaData()->get('relationPath'); + if ($relationPath && $relationPath !== $this->query->getModel()->getTableAlias()) { + $subject = $this->query->getResolver()->resolveRelation($relationPath)->getTarget(); + } else { + $subject = $this->query->getModel(); + } + + $expression = clone $subject->getColumns()[$columnName]; + $expression->setColumns($this->query->getResolver()->qualifyColumns( + $this->query->getResolver()->requireAndResolveColumns( + $expression->getColumns(), + $subject + ), + $subject + )); + + $condition->setColumn($this->query->getDb()->getQueryBuilder()->buildExpression($expression)); + } + } +} diff --git a/library/X509/Model/X509Certificate.php b/library/X509/Model/X509Certificate.php index d78e92d2..aabb3e10 100644 --- a/library/X509/Model/X509Certificate.php +++ b/library/X509/Model/X509Certificate.php @@ -4,6 +4,7 @@ use Icinga\Module\X509\Model\Behavior\BoolCast; use Icinga\Module\X509\Model\Behavior\DistinguishedEncodingRules; +use Icinga\Module\X509\Model\Behavior\ExpressionInjector; use ipl\Orm\Behavior\Binary; use ipl\Orm\Behaviors; use ipl\Orm\Model; @@ -103,6 +104,8 @@ public function createBehaviors(Behaviors $behaviors) 'trusted', 'self_signed' ])); + + $behaviors->add(new ExpressionInjector('duration', 'expires')); } public function createRelations(Relations $relations)