ObjectsTable: require Auth

fixes #2808
This commit is contained in:
Thomas Gelf 2023-10-11 12:24:36 +02:00
parent 6178265a12
commit 48db90c7df
10 changed files with 38 additions and 43 deletions

View File

@ -191,8 +191,7 @@ class HostController extends ObjectController
$branch = $this->getBranch(); $branch = $this->getBranch();
$hostHasBeenCreatedInBranch = $branch->isBranch() && $host->get('id'); $hostHasBeenCreatedInBranch = $branch->isBranch() && $host->get('id');
$content = $this->content(); $content = $this->content();
$table = (new ObjectsTableService($this->db())) $table = (new ObjectsTableService($this->db(), $this->Auth()))
->setAuth($this->Auth())
->setHost($host) ->setHost($host)
->setBranch($branch) ->setBranch($branch)
->setTitle($this->translate('Individual Service objects')) ->setTitle($this->translate('Individual Service objects'))
@ -206,8 +205,7 @@ class HostController extends ObjectController
$parents = IcingaTemplateRepository::instanceByObject($this->object) $parents = IcingaTemplateRepository::instanceByObject($this->object)
->getTemplatesFor($this->object, true); ->getTemplatesFor($this->object, true);
foreach ($parents as $parent) { foreach ($parents as $parent) {
$table = (new ObjectsTableService($this->db())) $table = (new ObjectsTableService($this->db(), $this->Auth()))
->setAuth($this->Auth())
->setBranch($branch) ->setBranch($branch)
->setHost($parent) ->setHost($parent)
->setInheritedBy($host) ->setInheritedBy($host)
@ -273,8 +271,7 @@ class HostController extends ObjectController
$this->addTitle($this->translate('Services on %s'), $host->getObjectName()); $this->addTitle($this->translate('Services on %s'), $host->getObjectName());
$content = $this->content(); $content = $this->content();
$table = (new ObjectsTableService($db)) $table = (new ObjectsTableService($db, $this->Auth()))
->setAuth($this->Auth())
->setHost($host) ->setHost($host)
->setBranch($branch) ->setBranch($branch)
->setReadonly() ->setReadonly()
@ -289,7 +286,7 @@ class HostController extends ObjectController
$parents = IcingaTemplateRepository::instanceByObject($this->object) $parents = IcingaTemplateRepository::instanceByObject($this->object)
->getTemplatesFor($this->object, true); ->getTemplatesFor($this->object, true);
foreach ($parents as $parent) { foreach ($parents as $parent) {
$table = (new ObjectsTableService($db)) $table = (new ObjectsTableService($db, $this->Auth()))
->setReadonly() ->setReadonly()
->setBranch($branch) ->setBranch($branch)
->setHost($parent) ->setHost($parent)

View File

@ -725,8 +725,7 @@ class IcingaServiceForm extends DirectorObjectForm
protected function enumHosts() protected function enumHosts()
{ {
$db = $this->db->getDbAdapter(); $db = $this->db->getDbAdapter();
$table = new ObjectsTableHost($this->db); $table = new ObjectsTableHost($this->db, $this->getAuth());
$table->setAuth($this->getAuth());
if ($this->branch && $this->branch->isBranch()) { if ($this->branch && $this->branch->isBranch()) {
$table->setBranchUuid($this->branch->getUuid()); $table->setBranchUuid($this->branch->getUuid());
} }

View File

@ -64,6 +64,7 @@ This version hasn't been released yet
### Internals ### Internals
* FIX: group membership is no longer resolved when not needed (#2048) * 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) * FEATURE: support PHP 8.2 (#2777, #2792)
### Fixed issues ### Fixed issues

View File

@ -4,6 +4,7 @@ namespace Icinga\Module\Director\Data;
use gipfl\Json\JsonString; use gipfl\Json\JsonString;
use gipfl\ZfDb\Adapter\Adapter; use gipfl\ZfDb\Adapter\Adapter;
use Icinga\Authentication\Auth;
use Icinga\Module\Director\Data\Db\DbDataFormatter; use Icinga\Module\Director\Data\Db\DbDataFormatter;
use Icinga\Module\Director\Data\Db\DbObject; use Icinga\Module\Director\Data\Db\DbObject;
use Icinga\Module\Director\Data\Db\DbObjectWithSettings; use Icinga\Module\Director\Data\Db\DbObjectWithSettings;
@ -185,7 +186,7 @@ class Exporter
public function serviceLoader() public function serviceLoader()
{ {
if ($this->serviceLoader === null) { if ($this->serviceLoader === null) {
$this->serviceLoader = new HostServiceLoader($this->connection); $this->serviceLoader = new HostServiceLoader($this->connection, Auth::getInstance());
$this->serviceLoader->resolveObjects($this->resolveObjects); $this->serviceLoader->resolveObjects($this->resolveObjects);
} }

View File

@ -4,6 +4,7 @@ namespace Icinga\Module\Director\Data;
use gipfl\IcingaWeb2\Table\QueryBasedTable; use gipfl\IcingaWeb2\Table\QueryBasedTable;
use gipfl\ZfDb\Select; use gipfl\ZfDb\Select;
use Icinga\Authentication\Auth;
use Icinga\Data\SimpleQuery; use Icinga\Data\SimpleQuery;
use Icinga\Module\Director\Db; use Icinga\Module\Director\Db;
use Icinga\Module\Director\Db\AppliedServiceSetLoader; use Icinga\Module\Director\Db\AppliedServiceSetLoader;
@ -26,21 +27,26 @@ class HostServiceLoader
/** @var \Zend_Db_Adapter_Abstract */ /** @var \Zend_Db_Adapter_Abstract */
protected $db; protected $db;
/** @var Auth */
protected $auth;
/** @var bool */ /** @var bool */
protected $resolveHostServices = false; protected $resolveHostServices = false;
/** @var bool */ /** @var bool */
protected $resolveObjects = false; protected $resolveObjects = false;
public function __construct(Db $connection) public function __construct(Db $connection, Auth $auth)
{ {
$this->connection = $connection; $this->connection = $connection;
$this->db = $connection->getDbAdapter(); $this->db = $connection->getDbAdapter();
$this->auth = $auth;
} }
public function fetchServicesForHost(IcingaHost $host) 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); $services = $this->fetchServicesForTable($table);
if ($this->resolveHostServices) { if ($this->resolveHostServices) {
foreach ($this->fetchAllServicesForHost($host) as $service) { foreach ($this->fetchAllServicesForHost($host) as $service) {
@ -69,7 +75,7 @@ class HostServiceLoader
/** @var IcingaHost[] $parents */ /** @var IcingaHost[] $parents */
$parents = IcingaTemplateRepository::instanceByObject($host)->getTemplatesFor($host, true); $parents = IcingaTemplateRepository::instanceByObject($host)->getTemplatesFor($host, true);
foreach ($parents as $parent) { foreach ($parents as $parent) {
$table = (new ObjectsTableService($this->connection)) $table = (new ObjectsTableService($this->connection, $this->auth))
->setHost($parent) ->setHost($parent)
->setInheritedBy($host); ->setInheritedBy($host);
foreach ($this->fetchServicesForTable($table) as $service) { foreach ($this->fetchServicesForTable($table) as $service) {

View File

@ -144,8 +144,7 @@ abstract class ObjectsController extends ActionController
*/ */
protected function getTable() protected function getTable()
{ {
$table = ObjectsTable::create($this->getType(), $this->db()) $table = ObjectsTable::create($this->getType(), $this->db(), $this->getAuth())
->setAuth($this->getAuth())
->setBranchUuid($this->getBranchUuid()) ->setBranchUuid($this->getBranchUuid())
->setBaseObjectUrl($this->getBaseObjectUrl()); ->setBaseObjectUrl($this->getBaseObjectUrl());

View File

@ -43,8 +43,7 @@ abstract class TemplateController extends CompatController
$template->getObjectName() $template->getObjectName()
)->addBackToUsageLink($template); )->addBackToUsageLink($template);
ObjectsTable::create($this->getType(), $this->db()) ObjectsTable::create($this->getType(), $this->db(), $this->Auth())
->setAuth($this->Auth())
->setBranch($this->getBranch()) ->setBranch($this->getBranch())
->setBaseObjectUrl($this->getBaseObjectUrl()) ->setBaseObjectUrl($this->getBaseObjectUrl())
->filterTemplate($template, $this->getInheritance()) ->filterTemplate($template, $this->getInheritance())

View File

@ -51,12 +51,18 @@ class ObjectsTable extends ZfQueryBasedTable
/** @var Auth */ /** @var Auth */
private $auth; private $auth;
public function __construct($db, Auth $auth)
{
$this->auth = $auth;
parent::__construct($db);
}
/** /**
* @param $type * @param $type
* @param Db $db * @param Db $db
* @return static * @return static
*/ */
public static function create($type, Db $db) public static function create($type, Db $db, Auth $auth)
{ {
$class = __NAMESPACE__ . '\\ObjectsTable' . ucfirst($type); $class = __NAMESPACE__ . '\\ObjectsTable' . ucfirst($type);
if (! class_exists($class)) { if (! class_exists($class)) {
@ -64,7 +70,7 @@ class ObjectsTable extends ZfQueryBasedTable
} }
/** @var static $table */ /** @var static $table */
$table = new $class($db); $table = new $class($db, $auth);
$table->type = $type; $table->type = $type;
return $table; return $table;
} }
@ -85,20 +91,6 @@ class ObjectsTable extends ZfQueryBasedTable
return $this; return $this;
} }
/**
* @return Auth
*/
public function getAuth()
{
return $this->auth;
}
public function setAuth(Auth $auth)
{
$this->auth = $auth;
return $this;
}
public function filterObjectType($type) public function filterObjectType($type)
{ {
$this->filterObjectType = $type; $this->filterObjectType = $type;
@ -234,11 +226,10 @@ class ObjectsTable extends ZfQueryBasedTable
{ {
/** @var Db $db */ /** @var Db $db */
$db = $this->connection(); $db = $this->connection();
$auth = $this->getAuth();
return [ return [
new HostgroupRestriction($db, $auth), new HostgroupRestriction($db, $this->auth),
new FilterByNameRestriction($db, $auth, $this->getDummyObject()->getShortTableName()) new FilterByNameRestriction($db, $this->auth, $this->getDummyObject()->getShortTableName())
]; ];
} }

View File

@ -25,8 +25,7 @@ class ServiceTemplateUsageTable extends TemplateUsageTable
$templateType, $templateType,
$connection $connection
), ),
'objects' => ObjectsTable::create($templateType, $connection) 'objects' => ObjectsTable::create($templateType, $connection, $this->auth)
->setAuth($auth)
->setBranchUuid($this->branchUuid), ->setBranchUuid($this->branchUuid),
'applyrules' => ApplyRulesTable::create($templateType, $connection) 'applyrules' => ApplyRulesTable::create($templateType, $connection)
->setBranchUuid($this->branchUuid), ->setBranchUuid($this->branchUuid),

View File

@ -18,6 +18,9 @@ class TemplateUsageTable extends Table
use TableWithBranchSupport; use TableWithBranchSupport;
/** @var Auth */
protected $auth;
protected $defaultAttributes = ['class' => 'pivot']; protected $defaultAttributes = ['class' => 'pivot'];
protected $objectType; protected $objectType;
@ -40,14 +43,14 @@ class TemplateUsageTable extends Table
* *
* @throws ProgrammingError * @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()); $type = ucfirst($template->getShortTableName());
$class = __NAMESPACE__ . "\\{$type}TemplateUsageTable"; $class = __NAMESPACE__ . "\\{$type}TemplateUsageTable";
if (class_exists($class)) { if (class_exists($class)) {
return new $class($template, $branch); return new $class($template, $branch);
} else { } 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') { if ($template->get('object_type') !== 'template') {
throw new ProgrammingError( throw new ProgrammingError(
@ -154,8 +158,7 @@ class TemplateUsageTable extends Table
$templateType, $templateType,
$connection $connection
), ),
'objects' => ObjectsTable::create($templateType, $connection) 'objects' => ObjectsTable::create($templateType, $connection, $this->auth)
->setAuth(Auth::getInstance())
->setBranchUuid($this->branchUuid) ->setBranchUuid($this->branchUuid)
]; ];
} }