mirror of
https://github.com/Icinga/icingaweb2-module-director.git
synced 2025-07-29 16:54:06 +02:00
Cleanup code
- Remove superfluous methods/usages - Simplify the code
This commit is contained in:
parent
8ea8a62ef4
commit
956cce84cb
@ -68,7 +68,7 @@ class ServiceFinder
|
|||||||
}
|
}
|
||||||
if ($this->auth->hasPermission(Permission::MONITORING_HOSTS)) {
|
if ($this->auth->hasPermission(Permission::MONITORING_HOSTS)) {
|
||||||
if ($info = $this::find($this->host, $serviceName)) {
|
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();
|
return $info->getUrl();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,16 +2,12 @@
|
|||||||
|
|
||||||
namespace Icinga\Module\Director\Integration\Icingadb;
|
namespace Icinga\Module\Director\Integration\Icingadb;
|
||||||
|
|
||||||
use gipfl\IcingaWeb2\Link;
|
use Icinga\Application\Modules\Module;
|
||||||
use Icinga\Application\Icinga;
|
|
||||||
use Icinga\Data\Filter\Filter as DataFilter;
|
|
||||||
use Icinga\Module\Director\Integration\BackendInterface;
|
use Icinga\Module\Director\Integration\BackendInterface;
|
||||||
use Icinga\Module\Director\Objects\IcingaHost;
|
|
||||||
use Icinga\Module\Icingadb\Common\Auth;
|
use Icinga\Module\Icingadb\Common\Auth;
|
||||||
use Icinga\Module\Icingadb\Common\Database;
|
use Icinga\Module\Icingadb\Common\Database;
|
||||||
use Icinga\Module\Icingadb\Model\Host;
|
use Icinga\Module\Icingadb\Model\Host;
|
||||||
use Icinga\Module\Icingadb\Model\Service;
|
use Icinga\Module\Icingadb\Model\Service;
|
||||||
use Icinga\Module\Icingadb\Redis\VolatileStateResults;
|
|
||||||
use Icinga\Web\Url;
|
use Icinga\Web\Url;
|
||||||
use ipl\Stdlib\Filter;
|
use ipl\Stdlib\Filter;
|
||||||
|
|
||||||
@ -22,28 +18,22 @@ class IcingadbBackend implements BackendInterface
|
|||||||
|
|
||||||
public function isAvailable(): bool
|
public function isAvailable(): bool
|
||||||
{
|
{
|
||||||
$app = Icinga::app();
|
return Module::exists('icingadb');
|
||||||
$modules = $app->getModuleManager();
|
|
||||||
return $modules->hasLoaded('icingadb');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function hasHost($hostname): bool
|
public function hasHost($hostname): bool
|
||||||
{
|
{
|
||||||
$query = Host::on($this->getDb());
|
$query = Host::on($this->getDb())
|
||||||
$query->filter(Filter::equal('host.name', $hostname));
|
->filter(Filter::equal('host.name', $hostname));
|
||||||
|
|
||||||
$this->applyRestrictions($query);
|
$this->applyRestrictions($query);
|
||||||
|
|
||||||
/** @var Host $host */
|
return $query->first() !== null;
|
||||||
$host = $query->first();
|
|
||||||
|
|
||||||
return ($host !== null);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function hasService($hostname, $service): bool
|
public function hasService($hostname, $service): bool
|
||||||
{
|
{
|
||||||
$query = Service::on($this->getDb());
|
$query = Service::on($this->getDb())
|
||||||
$query
|
|
||||||
->filter(Filter::all(
|
->filter(Filter::all(
|
||||||
Filter::equal('service.name', $service),
|
Filter::equal('service.name', $service),
|
||||||
Filter::equal('host.name', $hostname)
|
Filter::equal('host.name', $hostname)
|
||||||
@ -51,10 +41,7 @@ class IcingadbBackend implements BackendInterface
|
|||||||
|
|
||||||
$this->applyRestrictions($query);
|
$this->applyRestrictions($query);
|
||||||
|
|
||||||
/** @var Service $service */
|
return $query->first() !== null;
|
||||||
$service = $query->first();
|
|
||||||
|
|
||||||
return ($service !== null);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getHostUrl(string $hostname): Url
|
public function getHostUrl(string $hostname): Url
|
||||||
|
@ -39,11 +39,6 @@ class Monitoring implements BackendInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function hasHost($hostname): bool
|
public function hasHost($hostname): bool
|
||||||
{
|
|
||||||
return $this->hasHostByName($hostname);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function hasHostByName($hostname): bool
|
|
||||||
{
|
{
|
||||||
if (! $this->isAvailable()) {
|
if (! $this->isAvailable()) {
|
||||||
return false;
|
return false;
|
||||||
@ -56,13 +51,7 @@ class Monitoring implements BackendInterface
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public function hasService($hostname, $service): bool
|
public function hasService($hostname, $service): bool
|
||||||
{
|
|
||||||
return $this->hasServiceByName($hostname, $service);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function hasServiceByName($hostname, $service): bool
|
|
||||||
{
|
{
|
||||||
if (! $this->isAvailable()) {
|
if (! $this->isAvailable()) {
|
||||||
return false;
|
return false;
|
||||||
@ -77,23 +66,18 @@ class Monitoring implements BackendInterface
|
|||||||
|
|
||||||
public function canModifyService(string $hostName, string $serviceName): bool
|
public function canModifyService(string $hostName, string $serviceName): bool
|
||||||
{
|
{
|
||||||
return $this->canModifyServiceByName($hostName, $serviceName);
|
if (! $this->isAvailable() || $hostName === null || $serviceName === null) {
|
||||||
}
|
|
||||||
|
|
||||||
public function canModifyServiceByName($hostname, $service): bool
|
|
||||||
{
|
|
||||||
if (! $this->isAvailable() || $hostname === null || $service === null) {
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if ($this->auth->hasPermission(Permission::MONITORING_SERVICES)) {
|
if ($this->auth->hasPermission(Permission::MONITORING_SERVICES)) {
|
||||||
$restriction = null;
|
$restriction = null;
|
||||||
foreach ($this->auth->getRestrictions(Restriction::MONITORING_RW_OBJECT_FILTER) as $restriction) {
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ($restriction === null) {
|
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
|
public function canModifyHost(string $hostName): bool
|
||||||
{
|
|
||||||
return $this->canModifyHostByName($hostName);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function canModifyHostByName($hostname): bool
|
|
||||||
{
|
{
|
||||||
if ($this->isAvailable() && $this->auth->hasPermission(Permission::MONITORING_HOSTS)) {
|
if ($this->isAvailable() && $this->auth->hasPermission(Permission::MONITORING_HOSTS)) {
|
||||||
$restriction = null;
|
$restriction = null;
|
||||||
foreach ($this->auth->getRestrictions(Restriction::MONITORING_RW_OBJECT_FILTER) as $restriction) {
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ($restriction === null) {
|
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 {
|
try {
|
||||||
return $this->rowIsService(
|
return $this->rowIsService(
|
||||||
|
@ -251,7 +251,7 @@ abstract class ActionController extends Controller implements ControlsAndContent
|
|||||||
if (Module::exists('icingadb')) {
|
if (Module::exists('icingadb')) {
|
||||||
$this->backend = new IcingadbBackend();
|
$this->backend = new IcingadbBackend();
|
||||||
} else {
|
} else {
|
||||||
$this->backend = new Monitoring($this->Auth());
|
$this->backend = new Monitoring($this->getAuth());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user