Monitoring/IcingadbBackend: Handle if $hostName or $serviceName is null

This commit is contained in:
Sukhwinder Dhillon 2023-11-17 14:13:03 +01:00
parent 956cce84cb
commit 677f5dc72a
3 changed files with 54 additions and 32 deletions

View File

@ -16,47 +16,47 @@ interface BackendInterface
/**
* Whether the backend has the given host
*
* @param string $hostname
* @param ?string $hostName
*
* @return bool
*/
public function hasHost(string $hostname): bool;
public function hasHost(?string $hostName): bool;
/**
* Whether the backend has the given service of the specified host
*
* @param string $hostname
* @param string $service
* @param ?string $hostName
* @param ?string $serviceName
*
* @return bool
*/
public function hasService(string $hostname, string $service): bool;
public function hasService(?string $hostName, ?string $serviceName): bool;
/**
* Whether an authenticated user has the permission (is not restricted) to modify given host
*
* @param string $hostName
* @param ?string $hostName
*
* @return bool
*/
public function canModifyHost(string $hostName): bool;
public function canModifyHost(?string $hostName): bool;
/**
* Whether an authenticated user has the permission (is not restricted) to modify given service of specified host
*
* @param string $hostName
* @param string $serviceName
* @param ?string $hostName
* @param ?string $serviceName
*
* @return bool
*/
public function canModifyService(string $hostName, string $serviceName): bool;
public function canModifyService(?string $hostName, ?string $serviceName): bool;
/**
* Get the url of given host
*
* @param string $hostname
* @param ?string $hostName
*
* @return Url
*/
public function getHostUrl(string $hostname): Url;
public function getHostUrl(?string $hostName): ?Url;
}

View File

@ -21,22 +21,30 @@ class IcingadbBackend implements BackendInterface
return Module::exists('icingadb');
}
public function hasHost($hostname): bool
public function hasHost(?string $hostName): bool
{
if ($hostName === null) {
return false;
}
$query = Host::on($this->getDb())
->filter(Filter::equal('host.name', $hostname));
->filter(Filter::equal('host.name', $hostName));
$this->applyRestrictions($query);
return $query->first() !== null;
}
public function hasService($hostname, $service): bool
public function hasService(?string $hostName, ?string $serviceName): bool
{
if ($hostName === null || $serviceName === null) {
return false;
}
$query = Service::on($this->getDb())
->filter(Filter::all(
Filter::equal('service.name', $service),
Filter::equal('host.name', $hostname)
Filter::equal('service.name', $serviceName),
Filter::equal('host.name', $hostName)
));
$this->applyRestrictions($query);
@ -44,19 +52,29 @@ class IcingadbBackend implements BackendInterface
return $query->first() !== null;
}
public function getHostUrl(string $hostname): Url
public function getHostUrl(?string $hostName): ?Url
{
return Url::fromPath('icingadb/host', ['name' => $hostname]);
if ($hostName === null) {
return null;
}
return Url::fromPath('icingadb/host', ['name' => $hostName]);
}
public function canModifyHost(string $hostName): bool
public function canModifyHost(?string $hostName): bool
{
if ($hostName === null) {
return false;
}
// TODO: Implement canModifyService() method.
return false;
}
public function canModifyService(string $hostName, string $serviceName): bool
public function canModifyService(?string $hostName, ?string $serviceName): bool
{
if ($hostName === null || $serviceName === null) {
return false;
}
// TODO: Implement canModifyService() method.
return false;
}

View File

@ -33,38 +33,42 @@ class Monitoring implements BackendInterface
return $this->backend !== null;
}
public function getHostUrl(string $hostname): Url
public function getHostUrl(?string $hostName): ?Url
{
return Url::fromPath('monitoring/host/show', ['host' => $hostname]);
if ($hostName === null) {
return null;
}
return Url::fromPath('monitoring/host/show', ['host' => $hostName]);
}
public function hasHost($hostname): bool
public function hasHost(?string $hostName): bool
{
if (! $this->isAvailable()) {
if ($hostName === null || ! $this->isAvailable()) {
return false;
}
try {
return $this->selectHost($hostname)->fetchOne() === $hostname;
return $this->selectHost($hostName)->fetchOne() === $hostName;
} catch (Exception $_) {
return false;
}
}
public function hasService($hostname, $service): bool
public function hasService(?string $hostName, ?string $serviceName): bool
{
if (! $this->isAvailable()) {
if ($hostName === null || $serviceName === null || ! $this->isAvailable()) {
return false;
}
try {
return $this->rowIsService($this->selectService($hostname, $service)->fetchRow(), $hostname, $service);
return $this->rowIsService($this->selectService($hostName, $serviceName)->fetchRow(), $hostName, $serviceName);
} catch (Exception $_) {
return false;
}
}
public function canModifyService(string $hostName, string $serviceName): bool
public function canModifyService(?string $hostName, ?string $serviceName): bool
{
if (! $this->isAvailable() || $hostName === null || $serviceName === null) {
return false;
@ -84,9 +88,9 @@ class Monitoring implements BackendInterface
return false;
}
public function canModifyHost(string $hostName): bool
public function canModifyHost(?string $hostName): bool
{
if ($this->isAvailable() && $this->auth->hasPermission(Permission::MONITORING_HOSTS)) {
if ($hostName !== null && $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))) {