Icingadb (Host/Service)Actions: Fix permissions and code style

This commit is contained in:
Sukhwinder Dhillon 2023-11-20 15:20:15 +01:00
parent 1048c33fa4
commit 26f8769d28
2 changed files with 25 additions and 22 deletions

View File

@ -4,6 +4,7 @@ namespace Icinga\Module\Director\ProvidedHook\Icingadb;
use Exception; use Exception;
use Icinga\Application\Config; use Icinga\Application\Config;
use Icinga\Module\Director\Auth\Permission;
use Icinga\Module\Director\Db; use Icinga\Module\Director\Db;
use Icinga\Module\Director\Integration\Icingadb\IcingadbBackend; use Icinga\Module\Director\Integration\Icingadb\IcingadbBackend;
use Icinga\Module\Director\Objects\IcingaHost; use Icinga\Module\Director\Objects\IcingaHost;
@ -20,30 +21,33 @@ class HostActions extends HostActionsHook
try { try {
return $this->getThem($host); return $this->getThem($host);
} catch (Exception $e) { } catch (Exception $e) {
return array(); return [];
} }
} }
protected function getThem(Host $host): array protected function getThem(Host $host): array
{ {
$actions = array(); $actions = [];
$db = $this->db(); $db = $this->db();
if (! $db) { if (! $db) {
return $actions; return $actions;
} }
$hostname = $host->name; $hostname = $host->name;
if (Util::hasPermission('director/inspect')) { if (Util::hasPermission(Permission::INSPECT)) {
$actions[mt('director', 'Inspect')] = Url::fromPath( $actions[] = new Link(
'director/inspect/object', mt('director', 'Inspect'),
array('type' => 'host', 'plural' => 'hosts', 'name' => $hostname) Url::fromPath(
'director/inspect/object',
['type' => 'host', 'plural' => 'hosts', 'name' => $hostname]
)
); );
} }
$allowEdit = false; $allowEdit = false;
if (Util::hasPermission('director/hosts') && IcingaHost::exists($hostname, $db)) { if (Util::hasPermission(Permission::HOSTS) && IcingaHost::exists($hostname, $db)) {
$allowEdit = true; $allowEdit = true;
} }
if (Util::hasPermission('director/monitoring/hosts')) { if (Util::hasPermission(Permission::ICINGADB_HOSTS)) {
if ((new IcingadbBackend())->canModifyHost($hostname)) { if ((new IcingadbBackend())->canModifyHost($hostname)) {
$allowEdit = IcingaHost::exists($hostname, $db); $allowEdit = IcingaHost::exists($hostname, $db);
} }

View File

@ -4,6 +4,7 @@ namespace Icinga\Module\Director\ProvidedHook\Icingadb;
use Exception; use Exception;
use Icinga\Application\Config; use Icinga\Application\Config;
use Icinga\Module\Director\Auth\Permission;
use Icinga\Module\Director\Db; use Icinga\Module\Director\Db;
use Icinga\Module\Director\Integration\Icingadb\IcingadbBackend; use Icinga\Module\Director\Integration\Icingadb\IcingadbBackend;
use Icinga\Module\Director\Objects\IcingaHost; use Icinga\Module\Director\Objects\IcingaHost;
@ -20,7 +21,6 @@ class ServiceActions extends ServiceActionsHook
try { try {
return $this->getThem($service); return $this->getThem($service);
} catch (Exception $e) { } catch (Exception $e) {
die($e);
return []; return [];
} }
} }
@ -40,26 +40,25 @@ class ServiceActions extends ServiceActionsHook
$hostname = $service->host->name; $hostname = $service->host->name;
$serviceName = $service->name; $serviceName = $service->name;
if (Util::hasPermission('director/inspect')) { if (Util::hasPermission(Permission::INSPECT)) {
$actions[mt('director', 'Inspect')] = Url::fromPath('director/inspect/object', [ $actions[] = new Link(
'type' => 'service', mt('director', 'Inspect'),
'plural' => 'services', Url::fromPath('director/inspect/object', [
'name' => sprintf( 'type' => 'service',
'%s!%s', 'plural' => 'services',
$hostname, 'name' => sprintf('%s!%s', $hostname, $serviceName)
$serviceName ])
) );
]);
} }
$title = null; $title = null;
if (Util::hasPermission('director/hosts')) { if (Util::hasPermission(Permission::HOSTS)) {
$title = mt('director', 'Modify'); $title = mt('director', 'Modify');
} elseif (Util::hasPermission('director/monitoring/services')) { } elseif (Util::hasPermission(Permission::ICINGADB_SERVICES)) {
if ((new IcingadbBackend())->canModifyService($hostname, $serviceName)) { if ((new IcingadbBackend())->canModifyService($hostname, $serviceName)) {
$title = mt('director', 'Modify'); $title = mt('director', 'Modify');
} }
} elseif (Util::hasPermission('director/monitoring/services-ro')) { } elseif (Util::hasPermission(Permission::ICINGADB_SERVICES_RO)) {
$title = mt('director', 'Configuration'); $title = mt('director', 'Configuration');
} }