Skip to content
This repository has been archived by the owner on Jan 15, 2019. It is now read-only.

Commit

Permalink
Redesign CustomVariableExtender for new credential system
Browse files Browse the repository at this point in the history
The WHERE statements are now aggregated correctly to match
the key/values exactly.

fixes #3578
refs #3715
  • Loading branch information
lazyfrosch committed Apr 25, 2013
1 parent 1335fd3 commit 9dc7513
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 27 deletions.
4 changes: 0 additions & 4 deletions app/modules/Api/config/views/host.xml
Expand Up @@ -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>
Expand Down
8 changes: 0 additions & 8 deletions app/modules/Api/config/views/misc.xml
Expand Up @@ -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>
Expand Down Expand Up @@ -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>
Expand Down
Expand Up @@ -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));
}
Expand Down

0 comments on commit 9dc7513

Please sign in to comment.