/**
  * Apply a restriction on the given data view
  *
  * @param   string      $restriction    The name of restriction
  * @param   Filterable  $filterable     The filterable to restrict
  *
  * @return  Filterable  The filterable
  */
 protected static function applyRestriction($restriction, Filterable $filterable)
 {
     $restrictions = Filter::matchAny();
     foreach (Manager::getInstance()->getRestrictions($restriction) as $filter) {
         $restrictions->addFilter(Filter::fromQueryString($filter));
     }
     $filterable->applyFilter($restrictions);
     return $filterable;
 }
Ejemplo n.º 2
0
 /**
  * Apply a restriction on the given data view
  *
  * @param   string      $restriction    The name of restriction
  * @param   Filterable  $view           The filterable to restrict
  *
  * @return  Filterable  The filterable
  */
 protected function applyRestriction($restriction, Filterable $view)
 {
     $restrictions = Filter::matchAny();
     $restrictions->setAllowedFilterColumns(array('host_name', 'hostgroup_name', 'service_description', 'servicegroup_name', function ($c) {
         return preg_match('/^_(?:host|service)_/', $c);
     }));
     foreach ($this->getRestrictions($restriction) as $filter) {
         try {
             $restrictions->addFilter(Filter::fromQueryString($filter));
         } catch (QueryException $e) {
             throw new ConfigurationError($this->translate('Cannot apply restriction %s using the filter %s. You can only use the following columns: %s'), $restriction, $filter, implode(', ', array('host_name', 'hostgroup_name', 'service_description', 'servicegroup_name', '_(host|service)_<customvar-name>')), $e);
         }
     }
     $view->applyFilter($restrictions);
     return $view;
 }
Ejemplo n.º 3
0
 public function handleRequest($request)
 {
     $this->setUrl($request->getUrl()->without($this->ignoreParams));
     $params = $this->url()->getParams();
     $preserve = array();
     foreach ($this->preserveParams as $key) {
         if (null !== ($value = $params->shift($key))) {
             $preserve[$key] = $value;
         }
     }
     $this->preservedParams = $preserve;
     $add = $params->shift('addFilter');
     $remove = $params->shift('removeFilter');
     $strip = $params->shift('stripFilter');
     $modify = $params->shift('modifyFilter');
     $search = null;
     if ($request->isPost()) {
         $search = $request->getPost('q');
     }
     if ($search === null) {
         $search = $params->shift('q');
     }
     $filter = $this->getFilter();
     if ($search !== null) {
         if (strpos($search, '=') !== false) {
             list($k, $v) = preg_split('/=/', $search);
             $filter = $this->mergeRootExpression($filter, trim($k), '=', ltrim($v));
         } else {
             if ($this->searchColumns === null && $this->query instanceof FilterColumns) {
                 $this->searchColumns = $this->query->getSearchColumns($search);
             }
             if (!empty($this->searchColumns)) {
                 if (!$this->resetSearchColumns($filter)) {
                     $filter = Filter::matchAll();
                 }
                 $filters = array();
                 $search = ltrim($search);
                 foreach ($this->searchColumns as $searchColumn) {
                     $filters[] = Filter::expression($searchColumn, '=', "*{$search}*");
                 }
                 $filter = $filter->andFilter(new FilterOr($filters));
             } else {
                 Notification::error(mt('monitoring', 'Cannot search here'));
                 return $this;
             }
         }
         $url = $this->url()->setQueryString($filter->toQueryString())->addParams($preserve);
         if ($modify) {
             $url->getParams()->add('modifyFilter');
         }
         $this->redirectNow($url);
     }
     if ($remove) {
         $redirect = $this->url();
         if ($filter->getById($remove)->isRootNode()) {
             $redirect->setQueryString('');
         } else {
             $filter->removeId($remove);
             $redirect->setQueryString($filter->toQueryString())->getParams()->add('modifyFilter');
         }
         $this->redirectNow($redirect->addParams($preserve));
     }
     if ($strip) {
         $redirect = $this->url();
         $subId = $strip . '-1';
         if ($filter->getId() === $strip) {
             $filter = $filter->getById($strip . '-1');
         } else {
             $filter->replaceById($strip, $filter->getById($strip . '-1'));
         }
         $redirect->setQueryString($filter->toQueryString())->getParams()->add('modifyFilter');
         $this->redirectNow($redirect->addParams($preserve));
     }
     if ($modify) {
         if ($request->isPost()) {
             if ($request->get('cancel') === 'Cancel') {
                 $this->redirectNow($this->preservedUrl()->without('modifyFilter'));
             }
             $filter = $this->applyChanges($request->getPost());
             $url = $this->url()->setQueryString($filter->toQueryString())->addParams($preserve);
             $url->getParams()->add('modifyFilter');
             $this->redirectNow($url);
         }
         $this->url()->getParams()->add('modifyFilter');
     }
     if ($add) {
         $this->addFilterToId($add);
     }
     if ($this->query !== null && $request->isGet()) {
         $this->query->applyFilter($this->getFilter());
     }
     return $this;
 }
Ejemplo n.º 4
0
 /**
  * Apply a restriction of the authenticated on the given filterable
  *
  * @param   string      $name       Name of the restriction
  * @param   Filterable  $filterable Filterable to restrict
  *
  * @return  Filterable  The filterable having the restriction applied
  */
 protected function applyRestriction($name, Filterable $filterable)
 {
     $filterable->applyFilter($this->getRestriction($name));
     return $filterable;
 }