Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you restore the ADD_JAVASCRIPT hook, this statement is not true anymore.

Suggested change
- 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 SQL queries and page weight added by the plugin: no plugin state check on every request, index on mobile devices, lighter merge page.

- Fix Jamf API calls without timeout (5 s to connect, 30 s in total)

## [3.2.3] - 2026-10-06

Expand Down
22 changes: 11 additions & 11 deletions front/merge.php
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand All @@ -92,6 +92,6 @@
TemplateRenderer::getInstance()->display('@jamf/merge.html.twig', [
'pending' => $pending,
'total_count' => $importcount,
'linked' => $linked,
'conditions' => $conditions,
]);
Html::footer();
31 changes: 21 additions & 10 deletions inc/abstractdevice.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()]);
}

/**
Expand Down Expand Up @@ -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<string, ?string>
*/
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',
],
Expand All @@ -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
Expand Down
15 changes: 14 additions & 1 deletion inc/connection.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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);
Expand Down Expand Up @@ -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'])) {
Expand All @@ -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',
Expand Down
3 changes: 3 additions & 0 deletions inc/migration.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
65 changes: 31 additions & 34 deletions setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#55 Makes js/jamf.js holding all click handlers. Suppressing this hook might disable buttons effects all over the plugin, once the changes will be rebased.

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()
Expand Down
2 changes: 1 addition & 1 deletion templates/inventory_info.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
{% if section.buttons is defined %}
<div class="inventory-info-buttons d-flex justify-content-center">
{% for button_name, button in section.buttons %}
{% if button.url %}
{% if button.url is defined %}
<a class="btn btn-outline-secondary {{ loop.index0 != 0 ? 'ms-3' : '' }}" href="{{ button.url }}">{{ button.caption }}</a>
{% else %}
<button class="btn btn-outline-secondary {{ loop.index0 != 0 ? 'ms-3' : '' }}" type="button" onclick="{{ button.on_click|default('') }}">{{ button.caption }}</button>
Expand Down
1 change: 1 addition & 0 deletions templates/mdm_commands.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
</tbody>
</table>

<script src="{{ js_path('/plugins/jamf/js/jamf.js') }}"></script>
<script>
$(function() {
jamfPlugin = new JamfPlugin();
Expand Down
2 changes: 1 addition & 1 deletion templates/merge.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
{{ fields.dropdownField(data.type, 'items_id', data.guessed_item, null, {
no_label: true,
full_width: true,
used: linked[data.type]|default([])|column('items_id')
condition: conditions[data.type]
}) }}
</td>
</tr>
Expand Down