diff --git a/application/controllers/HostController.php b/application/controllers/HostController.php index 99e5266f..51056990 100644 --- a/application/controllers/HostController.php +++ b/application/controllers/HostController.php @@ -191,8 +191,7 @@ class HostController extends ObjectController $branch = $this->getBranch(); $hostHasBeenCreatedInBranch = $branch->isBranch() && $host->get('id'); $content = $this->content(); - $table = (new ObjectsTableService($this->db())) - ->setAuth($this->Auth()) + $table = (new ObjectsTableService($this->db(), $this->Auth())) ->setHost($host) ->setBranch($branch) ->setTitle($this->translate('Individual Service objects')) @@ -206,8 +205,7 @@ class HostController extends ObjectController $parents = IcingaTemplateRepository::instanceByObject($this->object) ->getTemplatesFor($this->object, true); foreach ($parents as $parent) { - $table = (new ObjectsTableService($this->db())) - ->setAuth($this->Auth()) + $table = (new ObjectsTableService($this->db(), $this->Auth())) ->setBranch($branch) ->setHost($parent) ->setInheritedBy($host) @@ -273,8 +271,7 @@ class HostController extends ObjectController $this->addTitle($this->translate('Services on %s'), $host->getObjectName()); $content = $this->content(); - $table = (new ObjectsTableService($db)) - ->setAuth($this->Auth()) + $table = (new ObjectsTableService($db, $this->Auth())) ->setHost($host) ->setBranch($branch) ->setReadonly() @@ -289,7 +286,7 @@ class HostController extends ObjectController $parents = IcingaTemplateRepository::instanceByObject($this->object) ->getTemplatesFor($this->object, true); foreach ($parents as $parent) { - $table = (new ObjectsTableService($db)) + $table = (new ObjectsTableService($db, $this->Auth())) ->setReadonly() ->setBranch($branch) ->setHost($parent) diff --git a/application/forms/IcingaServiceForm.php b/application/forms/IcingaServiceForm.php index 41157ccd..5744d8d3 100644 --- a/application/forms/IcingaServiceForm.php +++ b/application/forms/IcingaServiceForm.php @@ -725,8 +725,7 @@ class IcingaServiceForm extends DirectorObjectForm protected function enumHosts() { $db = $this->db->getDbAdapter(); - $table = new ObjectsTableHost($this->db); - $table->setAuth($this->getAuth()); + $table = new ObjectsTableHost($this->db, $this->getAuth()); if ($this->branch && $this->branch->isBranch()) { $table->setBranchUuid($this->branch->getUuid()); } diff --git a/doc/82-Changelog.md b/doc/82-Changelog.md index 86968dfd..07941817 100644 --- a/doc/82-Changelog.md +++ b/doc/82-Changelog.md @@ -64,6 +64,7 @@ This version hasn't been released yet ### Internals * FIX: group membership is no longer resolved when not needed (#2048) +* FIX: require Auth object for all object tables (#2808) * FEATURE: support PHP 8.2 (#2777, #2792) ### Fixed issues diff --git a/library/Director/Data/Exporter.php b/library/Director/Data/Exporter.php index 06122a3a..1a3cfcb7 100644 --- a/library/Director/Data/Exporter.php +++ b/library/Director/Data/Exporter.php @@ -4,6 +4,7 @@ namespace Icinga\Module\Director\Data; use gipfl\Json\JsonString; use gipfl\ZfDb\Adapter\Adapter; +use Icinga\Authentication\Auth; use Icinga\Module\Director\Data\Db\DbDataFormatter; use Icinga\Module\Director\Data\Db\DbObject; use Icinga\Module\Director\Data\Db\DbObjectWithSettings; @@ -185,7 +186,7 @@ class Exporter public function serviceLoader() { if ($this->serviceLoader === null) { - $this->serviceLoader = new HostServiceLoader($this->connection); + $this->serviceLoader = new HostServiceLoader($this->connection, Auth::getInstance()); $this->serviceLoader->resolveObjects($this->resolveObjects); } diff --git a/library/Director/Data/HostServiceLoader.php b/library/Director/Data/HostServiceLoader.php index 4cc4b961..c8bd8b9e 100644 --- a/library/Director/Data/HostServiceLoader.php +++ b/library/Director/Data/HostServiceLoader.php @@ -4,6 +4,7 @@ namespace Icinga\Module\Director\Data; use gipfl\IcingaWeb2\Table\QueryBasedTable; use gipfl\ZfDb\Select; +use Icinga\Authentication\Auth; use Icinga\Data\SimpleQuery; use Icinga\Module\Director\Db; use Icinga\Module\Director\Db\AppliedServiceSetLoader; @@ -26,21 +27,26 @@ class HostServiceLoader /** @var \Zend_Db_Adapter_Abstract */ protected $db; + /** @var Auth */ + protected $auth; + /** @var bool */ protected $resolveHostServices = false; /** @var bool */ protected $resolveObjects = false; - public function __construct(Db $connection) + public function __construct(Db $connection, Auth $auth) { $this->connection = $connection; $this->db = $connection->getDbAdapter(); + $this->auth = $auth; } public function fetchServicesForHost(IcingaHost $host) { - $table = (new ObjectsTableService($this->connection))->setHost($host); + $table = (new ObjectsTableService($this->connection, $this->auth)) + ->setHost($host); $services = $this->fetchServicesForTable($table); if ($this->resolveHostServices) { foreach ($this->fetchAllServicesForHost($host) as $service) { @@ -69,7 +75,7 @@ class HostServiceLoader /** @var IcingaHost[] $parents */ $parents = IcingaTemplateRepository::instanceByObject($host)->getTemplatesFor($host, true); foreach ($parents as $parent) { - $table = (new ObjectsTableService($this->connection)) + $table = (new ObjectsTableService($this->connection, $this->auth)) ->setHost($parent) ->setInheritedBy($host); foreach ($this->fetchServicesForTable($table) as $service) { diff --git a/library/Director/Web/Controller/ObjectsController.php b/library/Director/Web/Controller/ObjectsController.php index a9a47425..c45ef021 100644 --- a/library/Director/Web/Controller/ObjectsController.php +++ b/library/Director/Web/Controller/ObjectsController.php @@ -144,8 +144,7 @@ abstract class ObjectsController extends ActionController */ protected function getTable() { - $table = ObjectsTable::create($this->getType(), $this->db()) - ->setAuth($this->getAuth()) + $table = ObjectsTable::create($this->getType(), $this->db(), $this->getAuth()) ->setBranchUuid($this->getBranchUuid()) ->setBaseObjectUrl($this->getBaseObjectUrl()); diff --git a/library/Director/Web/Controller/TemplateController.php b/library/Director/Web/Controller/TemplateController.php index 3572c3cc..b0f045f3 100644 --- a/library/Director/Web/Controller/TemplateController.php +++ b/library/Director/Web/Controller/TemplateController.php @@ -43,8 +43,7 @@ abstract class TemplateController extends CompatController $template->getObjectName() )->addBackToUsageLink($template); - ObjectsTable::create($this->getType(), $this->db()) - ->setAuth($this->Auth()) + ObjectsTable::create($this->getType(), $this->db(), $this->Auth()) ->setBranch($this->getBranch()) ->setBaseObjectUrl($this->getBaseObjectUrl()) ->filterTemplate($template, $this->getInheritance()) diff --git a/library/Director/Web/Table/ObjectsTable.php b/library/Director/Web/Table/ObjectsTable.php index 7ed0bbb6..4ad11661 100644 --- a/library/Director/Web/Table/ObjectsTable.php +++ b/library/Director/Web/Table/ObjectsTable.php @@ -51,12 +51,18 @@ class ObjectsTable extends ZfQueryBasedTable /** @var Auth */ private $auth; + public function __construct($db, Auth $auth) + { + $this->auth = $auth; + parent::__construct($db); + } + /** * @param $type * @param Db $db * @return static */ - public static function create($type, Db $db) + public static function create($type, Db $db, Auth $auth) { $class = __NAMESPACE__ . '\\ObjectsTable' . ucfirst($type); if (! class_exists($class)) { @@ -64,7 +70,7 @@ class ObjectsTable extends ZfQueryBasedTable } /** @var static $table */ - $table = new $class($db); + $table = new $class($db, $auth); $table->type = $type; return $table; } @@ -85,20 +91,6 @@ class ObjectsTable extends ZfQueryBasedTable return $this; } - /** - * @return Auth - */ - public function getAuth() - { - return $this->auth; - } - - public function setAuth(Auth $auth) - { - $this->auth = $auth; - return $this; - } - public function filterObjectType($type) { $this->filterObjectType = $type; @@ -234,11 +226,10 @@ class ObjectsTable extends ZfQueryBasedTable { /** @var Db $db */ $db = $this->connection(); - $auth = $this->getAuth(); return [ - new HostgroupRestriction($db, $auth), - new FilterByNameRestriction($db, $auth, $this->getDummyObject()->getShortTableName()) + new HostgroupRestriction($db, $this->auth), + new FilterByNameRestriction($db, $this->auth, $this->getDummyObject()->getShortTableName()) ]; } diff --git a/library/Director/Web/Table/ServiceTemplateUsageTable.php b/library/Director/Web/Table/ServiceTemplateUsageTable.php index 5829d305..c2806f68 100644 --- a/library/Director/Web/Table/ServiceTemplateUsageTable.php +++ b/library/Director/Web/Table/ServiceTemplateUsageTable.php @@ -25,8 +25,7 @@ class ServiceTemplateUsageTable extends TemplateUsageTable $templateType, $connection ), - 'objects' => ObjectsTable::create($templateType, $connection) - ->setAuth($auth) + 'objects' => ObjectsTable::create($templateType, $connection, $this->auth) ->setBranchUuid($this->branchUuid), 'applyrules' => ApplyRulesTable::create($templateType, $connection) ->setBranchUuid($this->branchUuid), diff --git a/library/Director/Web/Table/TemplateUsageTable.php b/library/Director/Web/Table/TemplateUsageTable.php index 1c5172ad..d74ba262 100644 --- a/library/Director/Web/Table/TemplateUsageTable.php +++ b/library/Director/Web/Table/TemplateUsageTable.php @@ -18,6 +18,9 @@ class TemplateUsageTable extends Table use TableWithBranchSupport; + /** @var Auth */ + protected $auth; + protected $defaultAttributes = ['class' => 'pivot']; protected $objectType; @@ -40,14 +43,14 @@ class TemplateUsageTable extends Table * * @throws ProgrammingError */ - public static function forTemplate(IcingaObject $template, Branch $branch = null) + public static function forTemplate(IcingaObject $template, Auth $auth, Branch $branch = null) { $type = ucfirst($template->getShortTableName()); $class = __NAMESPACE__ . "\\{$type}TemplateUsageTable"; if (class_exists($class)) { return new $class($template, $branch); } else { - return new static($template, $branch); + return new static($template, $auth, $branch); } } @@ -61,8 +64,9 @@ class TemplateUsageTable extends Table ]; } - protected function __construct(IcingaObject $template, Branch $branch = null) + protected function __construct(IcingaObject $template, Auth $auth, Branch $branch = null) { + $this->auth = $auth; if ($template->get('object_type') !== 'template') { throw new ProgrammingError( @@ -154,8 +158,7 @@ class TemplateUsageTable extends Table $templateType, $connection ), - 'objects' => ObjectsTable::create($templateType, $connection) - ->setAuth(Auth::getInstance()) + 'objects' => ObjectsTable::create($templateType, $connection, $this->auth) ->setBranchUuid($this->branchUuid) ]; }