Remove obsolate class `Backend` and its usages

- Create the backend directly in ActionController::backend()
- Simplify the code
This commit is contained in:
Sukhwinder Dhillon 2023-11-17 10:27:25 +01:00
parent b28b36f815
commit 89134e0366
7 changed files with 46 additions and 152 deletions

View File

@ -1,139 +0,0 @@
<?php
namespace Icinga\Module\Director;
use Icinga\Application\Icinga;
use Icinga\Authentication\Auth;
use Icinga\Data\Filter\Filter;
use Icinga\Module\Director\Backend\MonitorBackend;
use Icinga\Module\Director\Backend\MonitorBackendIcingadb;
use Icinga\Module\Director\Backend\MonitorBackendMonitoring;
class Backend implements MonitorBackend
{
const MONITORING = 'monitoring';
const ICINGADB = 'icingadb';
protected $backend = null;
/**
* @param string|null $backend_name backend to use, 'icingadb' or 'monitoring'
* <code>null</code> will use either, preferring icingadb
*/
public function __construct($backend_name = null)
{
$app = Icinga::app();
$modules = $app->getModuleManager();
$tried_loading = false;
if (is_null($backend_name) || ($backend_name == self::ICINGADB)) {
if (!$modules->hasLoaded(self::ICINGADB) && $app->isCli()) {
$modules->loadEnabledModules();
$tried_loading = true;
}
if ($modules->hasLoaded(self::ICINGADB)) {
$this->backend = new MonitorBackendIcingadb();
}
}
if (is_null($this->backend)
&& (is_null($backend_name) || ($backend_name == self::MONITORING))) {
if (!$tried_loading && !$modules->hasLoaded(self::MONITORING) && $app->isCli()) {
$modules->loadEnabledModules();
}
if ($modules->hasLoaded(self::MONITORING)) {
$this->backend = new MonitorBackendMonitoring();
}
}
}
public function isAvailable()
{
return (($this->backend !== null) && ($this->backend->isAvailable()));
}
public function hasHost($hostname)
{
return (($this->backend !== null) && $this->backend->hasHost($hostname));
}
public function hasHostWithExtraFilter($hostname, Filter $filter)
{
return (($this->backend !== null) && $this->backend->hasHostWithExtraFilter($hostname, $filter));
}
public function hasService($hostname, $service)
{
return (($this->backend !== null) && $this->backend->hasService($hostname, $service));
}
public function hasServiceWithExtraFilter($hostname, $service, Filter $filter)
{
return (($this->backend !== null)
&& $this->backend->hasServiceWithExtraFilter($hostname, $service, $filter));
}
public function authCanEditHost(Auth $auth, $hostname)
{
if ($auth->hasPermission('director/monitoring/hosts')) {
$restriction = null;
foreach ($auth->getRestrictions('director/monitoring/rw-object-filter') as $restriction) {
if ($this->hasHostWithExtraFilter($hostname, Filter::fromQueryString($restriction))) {
return true;
}
}
if ($restriction === null) {
return $this->hasHost($hostname);
}
}
return false;
}
public function authCanEditService(Auth $auth, $hostname, $service)
{
if ($hostname === null || $service === null) {
// TODO: UUID support!
return false;
}
if ($auth->hasPermission('director/monitoring/services')) {
$restriction = null;
foreach ($auth->getRestrictions('director/monitoring/rw-object-filter') as $restriction) {
if ($this->hasServiceWithExtraFilter($hostname, $service, Filter::fromQueryString($restriction))) {
return true;
}
}
if ($restriction === null) {
return $this->hasService($hostname, $service);
}
}
return false;
}
public function getHostLink($title, $hostname, array $attributes = null)
{
if ($this->backend !== null) {
return $this->backend->getHostLink($title, $hostname, $attributes);
}
return null;
}
public function getHostState($hostname)
{
if ($this->backend === null) {
return (object) [
'hostname' => $hostname,
'state' => '99',
'problem' => '0',
'acknowledged' => '0',
'in_downtime' => '0',
'output' => null,
];
} else {
return $this->backend->getHostState($hostname);
}
}
}

View File

@ -19,4 +19,8 @@ interface MonitorBackend
public function getHostLink($title, $hostname, array $attributes = null);
public function getHostState($hostname);
public function canModifyHost(string $hostName): bool;
public function canModifyService(string $hostName, string $serviceName): bool;
}

View File

@ -121,4 +121,16 @@ class MonitorBackendIcingadb implements MonitorBackend
$result->state = $hostStates[$result->state];
return $result;
}
public function canModifyHost(string $hostName): bool
{
// TODO: Implement canModifyService() method.
return false;
}
public function canModifyService(string $hostName, string $serviceName): bool
{
// TODO: Implement canModifyService() method.
return false;
}
}

View File

@ -134,4 +134,16 @@ class MonitorBackendMonitoring implements MonitorBackend
->where('host_name', $hostname)
->where('service_description', $service);
}
public function canModifyHost(string $hostName): bool
{
// TODO: Implement canModifyHost() method.
return false;
}
public function canModifyService(string $hostName, string $serviceName): bool
{
// TODO: Implement canModifyService() method.
return false;
}
}

View File

@ -4,9 +4,8 @@ namespace Icinga\Module\Director\ProvidedHook\Icingadb;
use Exception;
use Icinga\Application\Config;
use Icinga\Authentication\Auth;
use Icinga\Module\Director\Backend\MonitorBackendIcingadb;
use Icinga\Module\Director\Db;
use Icinga\Module\Director\Backend;
use Icinga\Module\Director\Objects\IcingaHost;
use Icinga\Module\Director\Util;
use Icinga\Module\Icingadb\Hook\HostActionsHook;
@ -44,10 +43,9 @@ class HostActions extends HostActionsHook
if (Util::hasPermission('director/hosts') && IcingaHost::exists($hostname, $db)) {
$allowEdit = true;
}
$auth = Auth::getInstance();
if (Util::hasPermission('director/monitoring/hosts')) {
$backend = new Backend(Backend::ICINGADB);
if ($backend->isAvailable() && $backend->authCanEditHost($auth, $hostname)) {
$backend = new MonitorBackendIcingadb();
if ($backend->isAvailable() && $backend->canModifyHost($hostname)) {
$allowEdit = IcingaHost::exists($hostname, $db);
}
}

View File

@ -4,9 +4,8 @@ namespace Icinga\Module\Director\ProvidedHook\Icingadb;
use Exception;
use Icinga\Application\Config;
use Icinga\Authentication\Auth;
use Icinga\Module\Director\Backend\MonitorBackendIcingadb;
use Icinga\Module\Director\Db;
use Icinga\Module\Director\Backend;
use Icinga\Module\Director\Objects\IcingaHost;
use Icinga\Module\Director\Util;
use Icinga\Module\Icingadb\Hook\ServiceActionsHook;
@ -57,9 +56,9 @@ class ServiceActions extends ServiceActionsHook
if (Util::hasPermission('director/hosts')) {
$title = mt('director', 'Modify');
} elseif (Util::hasPermission('director/monitoring/services')) {
$backend = new Backend(Backend::ICINGADB);
$backend = new MonitorBackendIcingadb();
if ($backend->isAvailable()
&& $backend->authCanEditService(Auth::getInstance(), $hostname, $serviceName)
&& $backend->canModifyService($hostname, $serviceName)
) {
$title = mt('director', 'Modify');
}

View File

@ -4,10 +4,14 @@ namespace Icinga\Module\Director\Web\Controller;
use gipfl\Translation\StaticTranslator;
use Icinga\Application\Benchmark;
use Icinga\Application\Icinga;
use Icinga\Application\Modules\Module;
use Icinga\Data\Paginatable;
use Icinga\Exception\NotFoundError;
use Icinga\Exception\ProgrammingError;
use Icinga\Module\Director\Backend;
use Icinga\Module\Director\Backend\MonitorBackend;
use Icinga\Module\Director\Backend\MonitorBackendIcingadb;
use Icinga\Module\Director\Backend\MonitorBackendMonitoring;
use Icinga\Module\Director\Web\Controller\Extension\CoreApi;
use Icinga\Module\Director\Web\Controller\Extension\DirectorDb;
use Icinga\Module\Director\Web\Controller\Extension\RestApi;
@ -36,7 +40,7 @@ abstract class ActionController extends Controller implements ControlsAndContent
/** @var UrlParams Hint for IDE, somehow does not work in web */
protected $params;
/** @var Backend */
/** @var MonitorBackend */
private $backend;
/**
@ -240,12 +244,16 @@ abstract class ActionController extends Controller implements ControlsAndContent
}
/**
* @return Backend
* @return MonitorBackend
*/
protected function backend()
{
if ($this->backend === null) {
$this->backend = new Backend();
if (Module::exists('icingadb')) {
$this->backend = new MonitorBackendIcingadb();
} else {
$this->backend = new MonitorBackendMonitoring();
}
}
return $this->backend;