summaryrefslogtreecommitdiffstats
path: root/library/Director/Db/Cache/GroupMembershipCache.php
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 12:43:12 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 12:43:12 +0000
commitcd989f9c3aff968e19a3aeabc4eb9085787a6673 (patch)
treefbff2135e7013f196b891bbde54618eb050e4aaf /library/Director/Db/Cache/GroupMembershipCache.php
parentInitial commit. (diff)
downloadicingaweb2-module-director-cd989f9c3aff968e19a3aeabc4eb9085787a6673.tar.xz
icingaweb2-module-director-cd989f9c3aff968e19a3aeabc4eb9085787a6673.zip
Adding upstream version 1.10.2.upstream/1.10.2upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'library/Director/Db/Cache/GroupMembershipCache.php')
-rw-r--r--library/Director/Db/Cache/GroupMembershipCache.php104
1 files changed, 104 insertions, 0 deletions
diff --git a/library/Director/Db/Cache/GroupMembershipCache.php b/library/Director/Db/Cache/GroupMembershipCache.php
new file mode 100644
index 0000000..d6d9e8b
--- /dev/null
+++ b/library/Director/Db/Cache/GroupMembershipCache.php
@@ -0,0 +1,104 @@
+<?php
+
+namespace Icinga\Module\Director\Db\Cache;
+
+use Icinga\Application\Benchmark;
+use Icinga\Module\Director\Db;
+use Icinga\Module\Director\Objects\IcingaObject;
+
+class GroupMembershipCache
+{
+ protected $type;
+
+ protected $table;
+
+ protected $groupClass;
+
+ protected $memberships;
+
+ /** @var Db Director database connection */
+ protected $connection;
+
+ public function __construct(IcingaObject $object)
+ {
+ $this->table = $object->getTableName();
+ $this->type = $object->getShortTableName();
+
+ $this->groupClass = 'Icinga\\Module\\Director\\Objects\\Icinga'
+ . ucfirst($this->type) . 'Group';
+
+ Benchmark::measure('Initializing GroupMemberShipCache');
+ $this->connection = $object->getConnection();
+ $this->loadAllMemberships();
+ Benchmark::measure('Filled GroupMemberShipCache');
+ }
+
+ protected function loadAllMemberships()
+ {
+ $db = $this->connection->getDbAdapter();
+ $this->memberships = array();
+
+ $type = $this->type;
+ $table = $this->table;
+
+ $query = $db->select()->from(
+ array('o' => $table),
+ array(
+ 'object_id' => 'o.id',
+ 'group_id' => 'g.id',
+ 'group_name' => 'g.object_name',
+ )
+ )->join(
+ array('go' => $table . 'group_' . $type),
+ 'o.id = go.' . $type . '_id',
+ array()
+ )->join(
+ array('g' => $table . 'group'),
+ 'go.' . $type . 'group_id = g.id',
+ array()
+ )->order('g.object_name');
+
+ foreach ($db->fetchAll($query) as $row) {
+ if (! array_key_exists($row->object_id, $this->memberships)) {
+ $this->memberships[$row->object_id] = array();
+ }
+
+ $this->memberships[$row->object_id][$row->group_id] = $row->group_name;
+ }
+ }
+
+ public function listGroupNamesForObject(IcingaObject $object)
+ {
+ if (array_key_exists($object->id, $this->memberships)) {
+ return array_values($this->memberships[$object->id]);
+ }
+
+ return array();
+ }
+
+ public function listGroupIdsForObject(IcingaObject $object)
+ {
+ if (array_key_exists($object->id, $this->memberships)) {
+ return array_keys($this->memberships[$object->id]);
+ }
+
+ return array();
+ }
+
+ public function getGroupsForObject(IcingaObject $object)
+ {
+ $groups = array();
+ $class = $this->groupClass;
+ foreach ($this->listGroupIdsForObject($object) as $id) {
+ $object = $class::loadWithAutoIncId($id, $this->connection);
+ $groups[$object->object_name] = $object;
+ }
+
+ return $groups;
+ }
+
+ public function __destruct()
+ {
+ unset($this->connection);
+ }
+}