diff --git a/CHANGELOG.md b/CHANGELOG.md index e81bad8..007700c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,9 @@ ### Fixed - Fix `migration:unsigned_keys` failure on `glpi_plugin_jamf_devices.jamf_items_id` (negative default value) +- Fix main tab of Jamf-linked computers and phones crashing when Twig strict variables are enabled +- Fix SQL queries and page weight added by the plugin: no plugin state check on every request, index on mobile devices, lighter merge page, `jamf.js` only loaded on the MDM commands tab +- Fix Jamf API calls without timeout (5 s to connect, 30 s in total) ## [3.2.3] - 2026-10-06 diff --git a/front/merge.php b/front/merge.php index a45b2f2..b2feeaa 100644 --- a/front/merge.php +++ b/front/merge.php @@ -54,22 +54,22 @@ 'LIMIT' => $_SESSION['glpilist_limit'], ])); -$linked_devices = $DB->request([ - 'SELECT' => ['jamf_type', 'itemtype', 'items_id'], - 'FROM' => 'glpi_plugin_jamf_devices', -]); - -$linked = []; -foreach ($linked_devices as $data) { - $linked[$data['itemtype']][] = $data; -} - +// Hide already linked items with a subquery: the dropdown keeps it in session under a short hash, +// instead of inlining every linked id in each row +$conditions = []; foreach ($pending as &$data) { $itemtype = $data['type']; if (is_a($itemtype, 'CommonDBTM', true) === false) { throw new BadRequestHttpException(); } + $conditions[$itemtype] ??= [ + new QueryExpression( + $DB->quoteName($itemtype::getTable() . '.id') . ' NOT IN (SELECT `items_id` FROM `glpi_plugin_jamf_devices` WHERE `itemtype` = ' + . $DB->quoteValue($itemtype) . ')', + ), + ]; + $item = new $itemtype(); $jamftype = ('PluginJamf' . $data['jamf_type']); $guesses = $DB->request([ @@ -92,6 +92,6 @@ TemplateRenderer::getInstance()->display('@jamf/merge.html.twig', [ 'pending' => $pending, 'total_count' => $importcount, - 'linked' => $linked, + 'conditions' => $conditions, ]); Html::footer(); diff --git a/inc/abstractdevice.class.php b/inc/abstractdevice.class.php index 4d0e147..976a408 100644 --- a/inc/abstractdevice.class.php +++ b/inc/abstractdevice.class.php @@ -86,6 +86,8 @@ private static function purgeItemCommon(CommonDBTM $item) 'glpi_plugin_jamf_devices_id' => $device['id'], ]); } + + unset(self::$jamf_item_class_cache[$item::getType() . ':' . $item->getID()]); } /** @@ -120,12 +122,24 @@ public static function preUpdatePhone($item) } } + /** + * Jamf class of each GLPI item already looked up in this request: forms ask for it once per tab and once for the main block. + * @var array + */ + private static array $jamf_item_class_cache = []; + public static function getJamfItemClassForGLPIItem(string $itemtype, $items_id): ?string { /** @var DBmysql $DB */ global $DB; - $iterator = $DB->request([ + $cache_key = $itemtype . ':' . $items_id; + if (array_key_exists($cache_key, self::$jamf_item_class_cache)) { + return self::$jamf_item_class_cache[$cache_key]; + } + + $jamf_class = null; + $iterator = $DB->request([ 'SELECT' => [ 'jamf_type', ], @@ -136,17 +150,14 @@ public static function getJamfItemClassForGLPIItem(string $itemtype, $items_id): ], ]); if (count($iterator) > 0) { - $jamf_type = $iterator->current()['jamf_type']; - if ($jamf_type === 'Computer') { - return PluginJamfComputer::class; - } - - if ($jamf_type === 'MobileDevice') { - return PluginJamfMobileDevice::class; - } + $jamf_class = match ($iterator->current()['jamf_type']) { + 'Computer' => PluginJamfComputer::class, + 'MobileDevice' => PluginJamfMobileDevice::class, + default => null, + }; } - return null; + return self::$jamf_item_class_cache[$cache_key] = $jamf_class; } public static function getJamfItemForGLPIItem(CommonDBTM $item, $limit_to_type = false): ?PluginJamfAbstractDevice diff --git a/inc/connection.class.php b/inc/connection.class.php index 51740d4..b359a6c 100644 --- a/inc/connection.class.php +++ b/inc/connection.class.php @@ -42,6 +42,11 @@ */ class PluginJamfConnection { + /** Seconds; a slow or unreachable Jamf server must not hold a GLPI page or worker forever */ + private const CONNECT_TIMEOUT = 5; + + private const TIMEOUT = 30; + private $config; private ?string $bearer_token = null; @@ -135,6 +140,8 @@ public function setCurlOptions(&$curl): void // Set user agent curl_setopt($curl, CURLOPT_USERAGENT, self::getUserAgentString()); + curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, self::CONNECT_TIMEOUT); + curl_setopt($curl, CURLOPT_TIMEOUT, self::TIMEOUT); } /** @@ -157,6 +164,8 @@ protected function setCurlAuth(&$curl) curl_setopt($auth_curl, CURLOPT_POST, true); curl_setopt($auth_curl, CURLOPT_POSTFIELDS, []); curl_setopt($auth_curl, CURLOPT_RETURNTRANSFER, true); + curl_setopt($auth_curl, CURLOPT_CONNECTTIMEOUT, self::CONNECT_TIMEOUT); + curl_setopt($auth_curl, CURLOPT_TIMEOUT, self::TIMEOUT); // token will be in 'token' property of the json response $response = json_decode(curl_exec($auth_curl), true); curl_close($auth_curl); @@ -199,6 +208,8 @@ private function fetchBearerToken() curl_setopt($auth_curl, CURLOPT_POST, true); curl_setopt($auth_curl, CURLOPT_POSTFIELDS, []); curl_setopt($auth_curl, CURLOPT_RETURNTRANSFER, true); + curl_setopt($auth_curl, CURLOPT_CONNECTTIMEOUT, self::CONNECT_TIMEOUT); + curl_setopt($auth_curl, CURLOPT_TIMEOUT, self::TIMEOUT); $response = json_decode(curl_exec($auth_curl), true); curl_close($auth_curl); if (isset($response['token'])) { @@ -220,7 +231,9 @@ public function getClient() } $options = [ - 'headers' => [ + 'connect_timeout' => self::CONNECT_TIMEOUT, + 'timeout' => self::TIMEOUT, + 'headers' => [ 'Authorization' => 'Bearer ' . $this->bearer_token, 'User-Agent' => self::getUserAgentString(), 'Content-Type' => 'application/json', diff --git a/inc/migration.class.php b/inc/migration.class.php index 2cf9136..bef8c6c 100644 --- a/inc/migration.class.php +++ b/inc/migration.class.php @@ -655,5 +655,8 @@ public function apply_3_2_4_migration(): void $this->db->update('glpi_plugin_jamf_devices', ['jamf_items_id' => 0], ['jamf_items_id' => ['<', 0]]); $this->glpiMigration->changeField('glpi_plugin_jamf_devices', 'jamf_items_id', 'jamf_items_id', 'fkey'); $this->glpiMigration->migrationOneTable('glpi_plugin_jamf_devices'); + + // Joined from glpi_plugin_jamf_devices on every Phone form; without it the whole table is scanned + $this->glpiMigration->addKey('glpi_plugin_jamf_mobiledevices', 'glpi_plugin_jamf_devices_id'); } } diff --git a/setup.php b/setup.php index 65a03dc..418ff37 100644 --- a/setup.php +++ b/setup.php @@ -44,41 +44,38 @@ function plugin_init_jamf() $PLUGIN_HOOKS['csrf_compliant']['jamf'] = true; - if (Plugin::isPluginActive('jamf')) { - $PLUGIN_HOOKS['add_javascript']['jamf'][] = 'js/jamf.js'; - Plugin::registerClass('PluginJamfConfig', ['addtabon' => 'Config']); - $PLUGIN_HOOKS['post_item_form']['jamf'] = 'plugin_jamf_showJamfInfoForItem'; - $PLUGIN_HOOKS['pre_item_update']['jamf']['Phone'] = ['PluginJamfMobileDevice', 'preUpdatePhone']; - $PLUGIN_HOOKS['undiscloseConfigValue']['jamf'] = PluginJamfConfig::undiscloseConfigValue(...); - Plugin::registerClass('PluginJamfRuleImportCollection', ['rulecollections_types' => true]); - Plugin::registerClass('PluginJamfProfile', ['addtabon' => ['Profile']]); - Plugin::registerClass('PluginJamfItem_ExtensionAttribute', ['addtabon' => [ - 'Computer', - 'Phone', - ]]); - Plugin::registerClass('PluginJamfItem_MDMCommand', ['addtabon' => [ - 'Computer', - 'Phone', - ]]); - Plugin::registerClass('PluginJamfUser_JSSAccount', ['addtabon' => ['User']]); - if (Session::haveRight(PluginJamfMobileDevice::$rightname, READ)) { - $PLUGIN_HOOKS['menu_toadd']['jamf'] = ['tools' => 'PluginJamfMenu']; - } - - $PLUGIN_HOOKS['post_init']['jamf'] = 'plugin_jamf_postinit'; - $PLUGIN_HOOKS['item_purge']['jamf'] = [ - 'Computer' => ['PluginJamfAbstractDevice', 'plugin_jamf_purgeComputer'], - 'Phone' => ['PluginJamfAbstractDevice', 'plugin_jamf_purgePhone'], - 'Software' => ['PluginJamfSoftware', 'plugin_jamf_purgeSoftware'], - ]; - - $PLUGIN_HOOKS[Hooks::CONFIG_PAGE]['jamf'] = 'front/menu.php'; - - // Dashboards - $PLUGIN_HOOKS['dashboard_cards']['jamf'] = 'plugin_jamf_dashboardCards'; - - $PLUGIN_HOOKS['secured_configs']['Jamf'] = ['jsspassword']; + Plugin::registerClass('PluginJamfConfig', ['addtabon' => 'Config']); + $PLUGIN_HOOKS['post_item_form']['jamf'] = 'plugin_jamf_showJamfInfoForItem'; + $PLUGIN_HOOKS['pre_item_update']['jamf']['Phone'] = ['PluginJamfMobileDevice', 'preUpdatePhone']; + $PLUGIN_HOOKS['undiscloseConfigValue']['jamf'] = PluginJamfConfig::undiscloseConfigValue(...); + Plugin::registerClass('PluginJamfRuleImportCollection', ['rulecollections_types' => true]); + Plugin::registerClass('PluginJamfProfile', ['addtabon' => ['Profile']]); + Plugin::registerClass('PluginJamfItem_ExtensionAttribute', ['addtabon' => [ + 'Computer', + 'Phone', + ]]); + Plugin::registerClass('PluginJamfItem_MDMCommand', ['addtabon' => [ + 'Computer', + 'Phone', + ]]); + Plugin::registerClass('PluginJamfUser_JSSAccount', ['addtabon' => ['User']]); + if (Session::haveRight(PluginJamfMobileDevice::$rightname, READ)) { + $PLUGIN_HOOKS['menu_toadd']['jamf'] = ['tools' => 'PluginJamfMenu']; } + + $PLUGIN_HOOKS['post_init']['jamf'] = 'plugin_jamf_postinit'; + $PLUGIN_HOOKS['item_purge']['jamf'] = [ + 'Computer' => ['PluginJamfAbstractDevice', 'plugin_jamf_purgeComputer'], + 'Phone' => ['PluginJamfAbstractDevice', 'plugin_jamf_purgePhone'], + 'Software' => ['PluginJamfSoftware', 'plugin_jamf_purgeSoftware'], + ]; + + $PLUGIN_HOOKS[Hooks::CONFIG_PAGE]['jamf'] = 'front/menu.php'; + + // Dashboards + $PLUGIN_HOOKS['dashboard_cards']['jamf'] = 'plugin_jamf_dashboardCards'; + + $PLUGIN_HOOKS['secured_configs']['Jamf'] = ['jsspassword']; } function plugin_version_jamf() diff --git a/templates/inventory_info.html.twig b/templates/inventory_info.html.twig index 5396056..c3e348a 100644 --- a/templates/inventory_info.html.twig +++ b/templates/inventory_info.html.twig @@ -40,7 +40,7 @@ {% if section.buttons is defined %}
{% for button_name, button in section.buttons %} - {% if button.url %} + {% if button.url is defined %} {{ button.caption }} {% else %} diff --git a/templates/mdm_commands.html.twig b/templates/mdm_commands.html.twig index b858553..db59be8 100644 --- a/templates/mdm_commands.html.twig +++ b/templates/mdm_commands.html.twig @@ -94,6 +94,7 @@ +