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();
$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)

View File

@ -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());
}

View File

@ -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

View File

@ -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);
}

View File

@ -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) {

View File

@ -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());

View File

@ -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())

View File

@ -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())
];
}

View File

@ -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),

View File

@ -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)
];
}