[icinga-checkins] icinga.org: icinga-web/next: Redesign CustomVariableExtender for new credential system

git at icinga.org git at icinga.org
Fri Apr 26 11:58:04 CEST 2013


Module: icinga-web
Branch: next
Commit: 9dc7513fdefd6b64220575cb70dbcc3579a343f1
URL:    https://git.icinga.org/?p=icinga-web.git;a=commit;h=9dc7513fdefd6b64220575cb70dbcc3579a343f1

Author: Markus Frosch <markus at lazyfrosch.de>
Date:   Thu Apr 25 15:04:10 2013 +0200

Redesign CustomVariableExtender for new credential system

The WHERE statements are now aggregated correctly to match
the key/values exactly.

fixes #3578
refs #3715

---

 app/modules/Api/config/views/host.xml              |    4 --
 app/modules/Api/config/views/misc.xml              |    8 ----
 .../Extender/CustomVariableExtenderModel.class.php |   44 +++++++++++++-------
 3 files changed, 29 insertions(+), 27 deletions(-)

diff --git a/app/modules/Api/config/views/host.xml b/app/modules/Api/config/views/host.xml
index fcbeed8..20f4f7e 100644
--- a/app/modules/Api/config/views/host.xml
+++ b/app/modules/Api/config/views/host.xml
@@ -367,15 +367,11 @@
             <credential name="IcingaHostCustomVariablePair" type="CustomVariable" affects="host">
                 <parameter name="alias">h</parameter>
                 <parameter name="target">host</parameter>
-                <parameter name="joinType">left</parameter>
-                <parameter name="where">OR o.objecttype_id = 2</parameter>
             </credential>
 
             <credential name="IcingaServiceCustomVariablePair" type="CustomVariable" affects="service">
                 <parameter name="alias">s</parameter>
                 <parameter name="target">service</parameter>
-                <parameter name="joinType">left</parameter>
-                <parameter name="where">OR o.objecttype_id = 1</parameter>
             </credential>
 
         </dql>
diff --git a/app/modules/Api/config/views/misc.xml b/app/modules/Api/config/views/misc.xml
index 31096a6..51df304 100644
--- a/app/modules/Api/config/views/misc.xml
+++ b/app/modules/Api/config/views/misc.xml
@@ -146,15 +146,11 @@
             <credential name="IcingaHostCustomVariablePair" type="CustomVariable" affects="host">
                 <parameter name="alias">h</parameter>
                 <parameter name="target">host</parameter>
-                <parameter name="joinType">left</parameter>
-                <parameter name="where">OR o.objecttype_id = 2</parameter>
             </credential>
 
             <credential name="IcingaServiceCustomVariablePair" type="CustomVariable" affects="service">
                 <parameter name="alias">s</parameter>
                 <parameter name="target">service</parameter>
-                <parameter name="joinType">left</parameter>
-                <parameter name="where">OR o.objecttype_id = 1</parameter>
             </credential>
 
         </dql>
@@ -269,15 +265,11 @@
             <credential name="IcingaHostCustomVariablePair" type="CustomVariable" affects="host">
                 <parameter name="alias">h</parameter>
                 <parameter name="target">host</parameter>
-                <parameter name="joinType">left</parameter>
-                <parameter name="where">OR o.objecttype_id = 2</parameter>
             </credential>
 
             <credential name="IcingaServiceCustomVariablePair" type="CustomVariable" affects="service">
                 <parameter name="alias">s</parameter>
                 <parameter name="target">service</parameter>
-                <parameter name="joinType">left</parameter>
-                <parameter name="where">OR o.objecttype_id = 1</parameter>
             </credential>
 
         </dql>
diff --git a/app/modules/Api/models/Views/Extender/CustomVariableExtenderModel.class.php b/app/modules/Api/models/Views/Extender/CustomVariableExtenderModel.class.php
index f39a673..b510cfd 100644
--- a/app/modules/Api/models/Views/Extender/CustomVariableExtenderModel.class.php
+++ b/app/modules/Api/models/Views/Extender/CustomVariableExtenderModel.class.php
@@ -32,44 +32,58 @@ class Api_Views_Extender_CustomVariableExtenderModel extends IcingaBaseModel
     private $user;
 
     public function extend(IcingaDoctrine_Query $query,array $params) {
+        // target, host or service
         $target = $params["target"];
+        // alias for the table to join from
         $alias  = $params["alias"];
-        $joinType = isset($params["joinType"]) ? $params["joinType"] : "inner";
-        $whereAppendix = isset($params["where"]) ? $params["where"] : "";
-        $isObject = isset($params["isObject"]);
-        $objectTypeClause = $isObject ? " AND $alias.objecttype_id = " : "";
+
         $this->user = $this->getContext()->getUser()->getNsmUser();
         $aliasAbbr = "cv";
         switch($target) {
             case 'host':
                 $aliasAbbr = "h_cv";
                 $target = IcingaIPrincipalConstants::TYPE_CUSTOMVAR_HOST;
-                if($objectTypeClause != "")
-                    $objectTypeClause .= "1";
                 break;
             case 'service':
                 $aliasAbbr = "s_cv";
                 $target = IcingaIPrincipalConstants::TYPE_CUSTOMVAR_SERVICE;
-                if($objectTypeClause != "")
-                    $objectTypeClause .= "2";
                 break;
         }
         $targetVals = $this->user->getTargetValues($target,true)->toArray();
         if(empty($targetVals))
            return;
-        if($joinType == "left")
-            $query->leftJoin("$alias.customvariables ".$aliasAbbr);
-        else
-            $query->innerJoin("$alias.customvariables ".$aliasAbbr);
 
         $keymap = array(
             "cv_name" => "varname",
             "cv_value" => "varvalue"
         );
+
         $pairs = array();
-        foreach($targetVals as $cvKeyValuePair) {
-            $pairs[] = "($aliasAbbr.".$keymap[$cvKeyValuePair["tv_key"]]." LIKE '".$cvKeyValuePair["tv_val"]."'
-                        $objectTypeClause  ".$whereAppendix.")";
+
+        $CVcredentials = array();
+
+        // build correct array with the data we need
+        foreach($targetVals as $targetData) {
+            if(isset($targetData["tv_pt_id"]) and isset($targetData["tv_key"])) {
+                $tvid = $targetData["tv_pt_id"];
+                if($targetData["tv_key"] == "cv_name")
+                    $CVcredentials[$tvid]["name"] = $targetData["tv_val"];
+                else if($targetData["tv_key"] == "cv_value")
+                    $CVcredentials[$tvid]["value"] = $targetData["tv_val"];
+            }
+        }
+
+        // make a join for each CV permission
+        $query->leftJoin("$alias.customvariables ".$aliasAbbr);
+
+        // now we build the sql data
+        foreach($CVcredentials as $tvid => $cvdata) {
+            // skip incomplete sets
+            if(!isset($cvdata["name"]) || !isset($cvdata["value"]))
+                continue;
+
+
+            $pairs[] = "($aliasAbbr.varname LIKE '".$cvdata["name"]."' and $aliasAbbr.varvalue LIKE '".$cvdata["value"]."')";
         }
         $query->orWhere(join(" OR ", $pairs));
     }





More information about the icinga-checkins mailing list