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"];
     $this->user = $this->getContext()->getUser()->getNsmUser();
     $aliasAbbr = "cv";
     $impl = ++Api_Views_Extender_CustomVariableExtenderModel::$impl;
     switch ($target) {
         case 'host':
             $aliasAbbr = "h_cv_{$impl}";
             $target = IcingaIPrincipalConstants::TYPE_CUSTOMVAR_HOST;
             break;
         case 'service':
             $aliasAbbr = "s_cv_{$impl}";
             $target = IcingaIPrincipalConstants::TYPE_CUSTOMVAR_SERVICE;
             break;
     }
     $targetVals = $this->user->getTargetValues($target, true)->toArray();
     if (empty($targetVals)) {
         return;
     }
     $keymap = array("cv_name" => "varname", "cv_value" => "varvalue");
     $pairs = array();
     $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"] . "')";
     }
     if ($target == IcingaIPrincipalConstants::TYPE_CUSTOMVAR_SERVICE) {
         $pairs[] = $params["alias"] . '.service_object_id IS NULL';
     }
     $query->orWhere(join(" OR ", $pairs));
 }
 private function getCredentialValues($target)
 {
     if (!$this->user->hasTarget($target, true)) {
         return array();
     }
     if ($target != IcingaIPrincipalConstants::TYPE_CONTACTGROUP) {
         return $this->user->getTargetValues($target, true)->toArray();
     }
     $targetValue = new NsmTargetValue();
     $targetValue->tv_key = 'contactname';
     $targetValue->tv_val = $this->user->user_name;
     return array($targetValue);
 }