Cleanup code

- Remove superfluous methods/usages
- Simplify the code
This commit is contained in:
Sukhwinder Dhillon 2023-11-17 13:43:08 +01:00
parent 8ea8a62ef4
commit 956cce84cb
4 changed files with 15 additions and 49 deletions

View File

@ -68,7 +68,7 @@ class ServiceFinder
}
if ($this->auth->hasPermission(Permission::MONITORING_HOSTS)) {
if ($info = $this::find($this->host, $serviceName)) {
if ((new Monitoring($this->auth))->canModifyService($this->host->getObjectName(), $serviceName)) {
if ((new Monitoring())->canModifyService($this->host->getObjectName(), $serviceName)) {
return $info->getUrl();
}
}

View File

@ -2,16 +2,12 @@
namespace Icinga\Module\Director\Integration\Icingadb;
use gipfl\IcingaWeb2\Link;
use Icinga\Application\Icinga;
use Icinga\Data\Filter\Filter as DataFilter;
use Icinga\Application\Modules\Module;
use Icinga\Module\Director\Integration\BackendInterface;
use Icinga\Module\Director\Objects\IcingaHost;
use Icinga\Module\Icingadb\Common\Auth;
use Icinga\Module\Icingadb\Common\Database;
use Icinga\Module\Icingadb\Model\Host;
use Icinga\Module\Icingadb\Model\Service;
use Icinga\Module\Icingadb\Redis\VolatileStateResults;
use Icinga\Web\Url;
use ipl\Stdlib\Filter;
@ -22,28 +18,22 @@ class IcingadbBackend implements BackendInterface
public function isAvailable(): bool
{
$app = Icinga::app();
$modules = $app->getModuleManager();
return $modules->hasLoaded('icingadb');
return Module::exists('icingadb');
}
public function hasHost($hostname): bool
{
$query = Host::on($this->getDb());
$query->filter(Filter::equal('host.name', $hostname));
$query = Host::on($this->getDb())
->filter(Filter::equal('host.name', $hostname));
$this->applyRestrictions($query);
/** @var Host $host */
$host = $query->first();
return ($host !== null);
return $query->first() !== null;
}
public function hasService($hostname, $service): bool
{
$query = Service::on($this->getDb());
$query
$query = Service::on($this->getDb())
->filter(Filter::all(
Filter::equal('service.name', $service),
Filter::equal('host.name', $hostname)
@ -51,10 +41,7 @@ class IcingadbBackend implements BackendInterface
$this->applyRestrictions($query);
/** @var Service $service */
$service = $query->first();
return ($service !== null);
return $query->first() !== null;
}
public function getHostUrl(string $hostname): Url

View File

@ -39,11 +39,6 @@ class Monitoring implements BackendInterface
}
public function hasHost($hostname): bool
{
return $this->hasHostByName($hostname);
}
public function hasHostByName($hostname): bool
{
if (! $this->isAvailable()) {
return false;
@ -56,13 +51,7 @@ class Monitoring implements BackendInterface
}
}
public function hasService($hostname, $service): bool
{
return $this->hasServiceByName($hostname, $service);
}
public function hasServiceByName($hostname, $service): bool
{
if (! $this->isAvailable()) {
return false;
@ -77,23 +66,18 @@ class Monitoring implements BackendInterface
public function canModifyService(string $hostName, string $serviceName): bool
{
return $this->canModifyServiceByName($hostName, $serviceName);
}
public function canModifyServiceByName($hostname, $service): bool
{
if (! $this->isAvailable() || $hostname === null || $service === null) {
if (! $this->isAvailable() || $hostName === null || $serviceName === null) {
return false;
}
if ($this->auth->hasPermission(Permission::MONITORING_SERVICES)) {
$restriction = null;
foreach ($this->auth->getRestrictions(Restriction::MONITORING_RW_OBJECT_FILTER) as $restriction) {
if ($this->hasServiceWithFilter($hostname, $service, Filter::fromQueryString($restriction))) {
if ($this->hasServiceWithFilter($hostName, $serviceName, Filter::fromQueryString($restriction))) {
return true;
}
}
if ($restriction === null) {
return $this->hasServiceByName($hostname, $service);
return $this->hasService($hostName, $serviceName);
}
}
@ -101,21 +85,16 @@ class Monitoring implements BackendInterface
}
public function canModifyHost(string $hostName): bool
{
return $this->canModifyHostByName($hostName);
}
public function canModifyHostByName($hostname): bool
{
if ($this->isAvailable() && $this->auth->hasPermission(Permission::MONITORING_HOSTS)) {
$restriction = null;
foreach ($this->auth->getRestrictions(Restriction::MONITORING_RW_OBJECT_FILTER) as $restriction) {
if ($this->hasHostWithFilter($hostname, Filter::fromQueryString($restriction))) {
if ($this->hasHostWithFilter($hostName, Filter::fromQueryString($restriction))) {
return true;
}
}
if ($restriction === null) {
return $this->hasHostByName($hostname);
return $this->hasHost($hostName);
}
}
@ -131,7 +110,7 @@ class Monitoring implements BackendInterface
}
}
public function hasServiceWithFilter($hostname, $service, Filter $filter): bool
protected function hasServiceWithFilter($hostname, $service, Filter $filter): bool
{
try {
return $this->rowIsService(

View File

@ -251,7 +251,7 @@ abstract class ActionController extends Controller implements ControlsAndContent
if (Module::exists('icingadb')) {
$this->backend = new IcingadbBackend();
} else {
$this->backend = new Monitoring($this->Auth());
$this->backend = new Monitoring($this->getAuth());
}
}