From 26f8769d28451caefac4aad2ac109401d802aeda Mon Sep 17 00:00:00 2001 From: Sukhwinder Dhillon Date: Mon, 20 Nov 2023 15:20:15 +0100 Subject: [PATCH] Icingadb (Host/Service)Actions: Fix permissions and code style --- .../ProvidedHook/Icingadb/HostActions.php | 20 ++++++++------ .../ProvidedHook/Icingadb/ServiceActions.php | 27 +++++++++---------- 2 files changed, 25 insertions(+), 22 deletions(-) diff --git a/library/Director/ProvidedHook/Icingadb/HostActions.php b/library/Director/ProvidedHook/Icingadb/HostActions.php index f69f24c6..d7332eae 100644 --- a/library/Director/ProvidedHook/Icingadb/HostActions.php +++ b/library/Director/ProvidedHook/Icingadb/HostActions.php @@ -4,6 +4,7 @@ namespace Icinga\Module\Director\ProvidedHook\Icingadb; use Exception; use Icinga\Application\Config; +use Icinga\Module\Director\Auth\Permission; use Icinga\Module\Director\Db; use Icinga\Module\Director\Integration\Icingadb\IcingadbBackend; use Icinga\Module\Director\Objects\IcingaHost; @@ -20,30 +21,33 @@ class HostActions extends HostActionsHook try { return $this->getThem($host); } catch (Exception $e) { - return array(); + return []; } } protected function getThem(Host $host): array { - $actions = array(); + $actions = []; $db = $this->db(); if (! $db) { return $actions; } $hostname = $host->name; - if (Util::hasPermission('director/inspect')) { - $actions[mt('director', 'Inspect')] = Url::fromPath( - 'director/inspect/object', - array('type' => 'host', 'plural' => 'hosts', 'name' => $hostname) + if (Util::hasPermission(Permission::INSPECT)) { + $actions[] = new Link( + mt('director', 'Inspect'), + Url::fromPath( + 'director/inspect/object', + ['type' => 'host', 'plural' => 'hosts', 'name' => $hostname] + ) ); } $allowEdit = false; - if (Util::hasPermission('director/hosts') && IcingaHost::exists($hostname, $db)) { + if (Util::hasPermission(Permission::HOSTS) && IcingaHost::exists($hostname, $db)) { $allowEdit = true; } - if (Util::hasPermission('director/monitoring/hosts')) { + if (Util::hasPermission(Permission::ICINGADB_HOSTS)) { if ((new IcingadbBackend())->canModifyHost($hostname)) { $allowEdit = IcingaHost::exists($hostname, $db); } diff --git a/library/Director/ProvidedHook/Icingadb/ServiceActions.php b/library/Director/ProvidedHook/Icingadb/ServiceActions.php index 337754e9..1603dc37 100644 --- a/library/Director/ProvidedHook/Icingadb/ServiceActions.php +++ b/library/Director/ProvidedHook/Icingadb/ServiceActions.php @@ -4,6 +4,7 @@ namespace Icinga\Module\Director\ProvidedHook\Icingadb; use Exception; use Icinga\Application\Config; +use Icinga\Module\Director\Auth\Permission; use Icinga\Module\Director\Db; use Icinga\Module\Director\Integration\Icingadb\IcingadbBackend; use Icinga\Module\Director\Objects\IcingaHost; @@ -20,7 +21,6 @@ class ServiceActions extends ServiceActionsHook try { return $this->getThem($service); } catch (Exception $e) { - die($e); return []; } } @@ -40,26 +40,25 @@ class ServiceActions extends ServiceActionsHook $hostname = $service->host->name; $serviceName = $service->name; - if (Util::hasPermission('director/inspect')) { - $actions[mt('director', 'Inspect')] = Url::fromPath('director/inspect/object', [ - 'type' => 'service', - 'plural' => 'services', - 'name' => sprintf( - '%s!%s', - $hostname, - $serviceName - ) - ]); + if (Util::hasPermission(Permission::INSPECT)) { + $actions[] = new Link( + mt('director', 'Inspect'), + Url::fromPath('director/inspect/object', [ + 'type' => 'service', + 'plural' => 'services', + 'name' => sprintf('%s!%s', $hostname, $serviceName) + ]) + ); } $title = null; - if (Util::hasPermission('director/hosts')) { + if (Util::hasPermission(Permission::HOSTS)) { $title = mt('director', 'Modify'); - } elseif (Util::hasPermission('director/monitoring/services')) { + } elseif (Util::hasPermission(Permission::ICINGADB_SERVICES)) { if ((new IcingadbBackend())->canModifyService($hostname, $serviceName)) { $title = mt('director', 'Modify'); } - } elseif (Util::hasPermission('director/monitoring/services-ro')) { + } elseif (Util::hasPermission(Permission::ICINGADB_SERVICES_RO)) { $title = mt('director', 'Configuration'); }