diff --git a/configuration.php b/configuration.php index 0e8cc30a..073686dc 100644 --- a/configuration.php +++ b/configuration.php @@ -26,6 +26,20 @@ $this->provideRestriction( ) ); +$this->provideRestriction( + 'director/service/apply/filter-by-name', + $this->translate( + 'Filter available service apply rules' + ) +); + +$this->provideRestriction( + 'director/notification/apply/filter-by-name', + $this->translate( + 'Filter available notification apply rules' + ) +); + $this->provideSearchUrl($this->translate('Host configs'), 'director/hosts?limit=10', 60); /* diff --git a/library/Director/Web/Controller/ObjectsController.php b/library/Director/Web/Controller/ObjectsController.php index edbdc027..86ed081c 100644 --- a/library/Director/Web/Controller/ObjectsController.php +++ b/library/Director/Web/Controller/ObjectsController.php @@ -10,6 +10,7 @@ use Icinga\Module\Director\Forms\IcingaMultiEditForm; use Icinga\Module\Director\Objects\IcingaObject; use Icinga\Module\Director\Web\ActionBar\ObjectsActionBar; use Icinga\Module\Director\Web\ActionBar\TemplateActionBar; +use Icinga\Module\Director\Web\Table\ApplyRulesTable; use Icinga\Module\Director\Web\Table\ObjectSetTable; use Icinga\Module\Director\Web\Table\ObjectsTable; use Icinga\Module\Director\Web\Table\ServiceApplyRulesTable; @@ -113,7 +114,10 @@ abstract class ObjectsController extends ActionController $this ->assertPermission('director/admin') ->addObjectsTabs() - ->addTitle($this->translate('All your Service Apply Rules')); + ->addTitle( + $this->translate('All your %s Apply Rules'), + $this->translate(ucfirst($this->getType())) + ); $this->actions()/*->add( $this->getBackToDashboardLink() )*/->add( @@ -129,7 +133,9 @@ abstract class ObjectsController extends ActionController ) ); - ServiceApplyRulesTable::show($this, $this->db()); + $table = new ApplyRulesTable($this->db()); + $table->setType($this->getType()); + $table->renderTo($this); } public function setsAction() diff --git a/library/Director/Web/Table/ApplyRulesTable.php b/library/Director/Web/Table/ApplyRulesTable.php index 6eb453d9..5aef53ea 100644 --- a/library/Director/Web/Table/ApplyRulesTable.php +++ b/library/Director/Web/Table/ApplyRulesTable.php @@ -2,15 +2,18 @@ namespace Icinga\Module\Director\Web\Table; +use Icinga\Authentication\Auth; use Icinga\Data\Filter\Filter; use Icinga\Exception\IcingaException; use Icinga\Module\Director\Db\IcingaObjectFilterHelper; use Icinga\Module\Director\IcingaConfig\AssignRenderer; use Icinga\Module\Director\Objects\IcingaObject; +use ipl\Db\Zf1\FilterRenderer; use ipl\Html\Icon; use ipl\Html\Link; use ipl\Web\Table\ZfQueryBasedTable; use ipl\Web\Url; +use Zend_Db_Select as ZfSelect; class ApplyRulesTable extends ZfQueryBasedTable { @@ -110,6 +113,23 @@ class ApplyRulesTable extends ZfQueryBasedTable return $links; } + protected function applyRestrictions(ZfSelect $query) + { + $auth = Auth::getInstance(); + $type = $this->type; + $restrictions = $auth->getRestrictions("director/$type/applyrule/filter-by-name"); + if (empty($restrictions)) { + return $query; + } + + $filter = Filter::matchAny(); + foreach ($restrictions as $restriction) { + $filter->addFilter(Filter::where('o.object_name', $restriction)); + } + + return FilterRenderer::applyToQuery($filter, $query); + } + public function prepareQuery() { $type = $this->type; @@ -129,6 +149,6 @@ class ApplyRulesTable extends ZfQueryBasedTable $query->where('service_set_id IS NULL'); } - return $query; + return $this->applyRestrictions($query); } } diff --git a/library/Director/Web/Table/ServiceApplyRulesTable.php b/library/Director/Web/Table/ServiceApplyRulesTable.php deleted file mode 100644 index 9ef66402..00000000 --- a/library/Director/Web/Table/ServiceApplyRulesTable.php +++ /dev/null @@ -1,105 +0,0 @@ - $row->id, - ]); - - return static::tr([ - Table::td(Link::create($row->service, $url)), - Table::td($this->renderApplyFilter($row->assign_filter)), - Table::td($this->createActionLinks($row))->setSeparator(' ') - ]); - } - - protected function renderApplyFilter($assignFilter) - { - try { - $string = AssignRenderer::forFilter( - Filter::fromQueryString($assignFilter) - )->renderAssign(); - // Do not prefix it - $string = preg_replace('/^assign where /', '', $string); - } catch (IcingaException $e) { - // ignore errors in filter rendering - $string = 'Error in Filter rendering: ' . $e->getMessage(); - } - - return $string; - } - - public function createActionLinks($row) - { - $links = []; - $links[] = Link::create( - Icon::create('sitemap'), - 'director/servicetemplate/applytargets', - ['id' => $row->id], - ['title' => $this->translate('Show affected Hosts')] - ); - - $links[] = Link::create( - Icon::create('edit'), - 'director/service/edit', - ['id' => $row->id], - ['title' => $this->translate('Modify this Apply Rule')] - ); - - $links[] = Link::create( - Icon::create('doc-text'), - 'director/service/render', - ['id' => $row->id], - ['title' => $this->translate('Apply Rule rendering preview')] - ); - - $links[] = Link::create( - Icon::create('history'), - 'director/service/history', - ['id' => $row->id], - ['title' => $this->translate('Apply rule history')] - ); - - return $links; - } - - public function prepareQuery() - { - $columns = [ - 'id' => 's.id', - 'service' => 's.object_name', - 'assign_filter' => 's.assign_filter', - ]; - $query = $this->db()->select()->from( - ['s' => 'icinga_service'], - $columns - )->where( - "object_type = 'apply'" - )->where('service_set_id IS NULL')->order('s.object_name'); - - return $query; - } -} diff --git a/library/Director/Web/Table/TemplatesTable.php b/library/Director/Web/Table/TemplatesTable.php index 7b31f8bf..7816b0a3 100644 --- a/library/Director/Web/Table/TemplatesTable.php +++ b/library/Director/Web/Table/TemplatesTable.php @@ -2,14 +2,18 @@ namespace Icinga\Module\Director\Web\Table; +use Icinga\Authentication\Auth; +use Icinga\Data\Filter\Filter; use Icinga\Module\Director\Db; use Icinga\Module\Director\Db\IcingaObjectFilterHelper; use Icinga\Module\Director\Objects\IcingaObject; +use ipl\Db\Zf1\FilterRenderer; use ipl\Html\Html; use ipl\Html\Icon; use ipl\Html\Link; use ipl\Web\Table\ZfQueryBasedTable; use ipl\Web\Url; +use Zend_Db_Select as ZfSelect; class TemplatesTable extends ZfQueryBasedTable { @@ -72,6 +76,23 @@ class TemplatesTable extends ZfQueryBasedTable return $this; } + protected function applyRestrictions(ZfSelect $query) + { + $auth = Auth::getInstance(); + $type = $this->type; + $restrictions = $auth->getRestrictions("director/$type/template/filter-by-name"); + if (empty($restrictions)) { + return $query; + } + + $filter = Filter::matchAny(); + foreach ($restrictions as $restriction) { + $filter->addFilter(Filter::where('o.object_name', $restriction)); + } + + return FilterRenderer::applyToQuery($filter, $query); + } + protected function prepareQuery() { $type = $this->getType(); @@ -90,6 +111,6 @@ class TemplatesTable extends ZfQueryBasedTable "o.object_type = 'template'" )->order('o.object_name'); - return $query; + return $this->applyRestrictions($query); } }