From f66ab8dae2f3d0418759f81a3a64dc9517a62449 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 14 Apr 2024 15:17:31 +0200 Subject: Adding upstream version 1.10.2. Signed-off-by: Daniel Baumann --- library/Director/Db/Cache/GroupMembershipCache.php | 104 +++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 library/Director/Db/Cache/GroupMembershipCache.php (limited to 'library/Director/Db/Cache/GroupMembershipCache.php') 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 @@ +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); + } +} -- cgit v1.2.3