diff --git a/CHANGELOG.md b/CHANGELOG.md index e81bad8..ad3dba2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Jamf Plugin for GLPI Changelog +## [Unreleased] + +### Changed + +- Make the plugin compatible with the Content Security Policy + ## [3.2.4] - 2026-11-05 ### Fixed diff --git a/inc/computer.class.php b/inc/computer.class.php index 0224568..bb543e0 100644 --- a/inc/computer.class.php +++ b/inc/computer.class.php @@ -55,9 +55,6 @@ public static function getTypeName($nb = 1) */ public static function showForItem(array $params) { - /** @var array $CFG_GLPI */ - global $CFG_GLPI; - $item = $params['item']; if (!self::canView() || $item::getType() !== 'Computer') { @@ -74,22 +71,13 @@ public static function showForItem(array $params) $match = $jamf_item->fields; $match = array_merge($match, $jamf_item->getJamfDeviceData()); - $js = ''; + $sync_attributes = []; if ($item->canUpdate()) { - $ajax_url = $CFG_GLPI['root_doc'] . '/plugins/jamf/ajax/sync.php'; - $js = << '', + 'data-jamf-sync-itemtype' => $item::getType(), + 'data-jamf-sync-items-id' => $item->getID(), + ]; } $info = [ @@ -135,11 +123,10 @@ function syncDevice(itemtype, items_id) { 'url' => self::getJamfDeviceURL($match['jamf_items_id']), ], 'sync' => [ - 'caption' => _x('action', 'Sync now', 'jamf'), - 'on_click' => sprintf('syncDevice("%s", %s); return false;', $item::getType(), $item->getID()), + 'caption' => _x('action', 'Sync now', 'jamf'), + 'attributes' => $sync_attributes, ], ], - 'extra_js' => $js, ], ]; diff --git a/inc/mobiledevice.class.php b/inc/mobiledevice.class.php index de980f9..65180db 100644 --- a/inc/mobiledevice.class.php +++ b/inc/mobiledevice.class.php @@ -265,9 +265,6 @@ public static function cardSupervisedProvider($params = []) public static function showForItem(array $params) { - /** @var array $CFG_GLPI */ - global $CFG_GLPI; - /** @var CommonDBTM $item */ $item = $params['item']; @@ -286,22 +283,13 @@ public static function showForItem(array $params) $match = $jamf_item->fields; $match = array_merge($match, $jamf_item->getJamfDeviceData()); - $js = ''; + $sync_attributes = []; if ($item->canUpdate()) { - $ajax_url = $CFG_GLPI['root_doc'] . '/plugins/jamf/ajax/sync.php'; - $js = << '', + 'data-jamf-sync-itemtype' => $item::getType(), + 'data-jamf-sync-items-id' => $item->getID(), + ]; } $info = [ @@ -355,11 +343,10 @@ function syncDevice(itemtype, items_id) { 'url' => self::getJamfDeviceURL($match['jamf_items_id']), ], 'sync' => [ - 'caption' => _x('action', 'Sync now', 'jamf'), - 'on_click' => sprintf('syncDevice("%s", %d); return false;', $item::getType(), $item->getID()), + 'caption' => _x('action', 'Sync now', 'jamf'), + 'attributes' => $sync_attributes, ], ], - 'extra_js' => $js, ], 'lost_mode' => [ 'caption' => _x('form_section', 'Jamf Lost Mode Information', 'jamf'), @@ -401,9 +388,11 @@ function syncDevice(itemtype, items_id) { ], 'lost_location' => [ 'caption' => _x('field', 'GPS', 'jamf'), - 'value' => Html::link(sprintf('%s, %s', $lat, $long), sprintf('https://www.google.com/maps/place/%s,%s', $lat, $long), [ - 'display' => false, - ]), + 'value' => sprintf( + '%s', + htmlescape(sprintf('https://www.google.com/maps/place/%s,%s', $lat, $long)), + htmlescape(sprintf('%s, %s', $lat, $long)), + ), ], 'lost_location_altitude' => [ 'caption' => _x('field', 'Altitude', 'jamf'), diff --git a/public/js/jamf.js b/public/js/jamf.js index 50ac621..d7fb90a 100644 --- a/public/js/jamf.js +++ b/public/js/jamf.js @@ -173,3 +173,170 @@ }; }; })(); + +(function () { + /** + * Replace the button content by a spinner and a label, and disable it. + * The label comes from the button's data-jamf-busy-label attribute. + */ + const setBusy = (button, spinner_class, text_prefix) => { + button.prop('disabled', true).empty() + .append($('', { + class: `spinner-border spinner-border-sm ${spinner_class}`, + role: 'status', + 'aria-hidden': 'true' + })) + .append(document.createTextNode(text_prefix + button.data('jamfBusyLabel'))); + }; + + const importDevices = () => { + const import_ids = $(':checkbox:checked').filter(':not([name^="_checkall"])').map(function () { + return this.name.replace("import", "").substring(1).split('_'); + }).toArray(); + + $.ajax({ + type: "POST", + url: `${CFG_GLPI.root_doc}/plugins/jamf/ajax/import.php`, + data: { + action: "import", + import_ids: import_ids + }, + contentType: 'application/json', + beforeSend: () => { + setBusy($('[data-jamf-action="import"]'), 'me-2', ''); + $('[data-jamf-action="discover"]').prop('disabled', true); + $('[data-jamf-action="clear"]').prop('disabled', true); + }, + complete: () => { + window.location.reload(); + } + }); + }; + + const discoverNow = () => { + $.ajax({ + type: "POST", + url: `${CFG_GLPI.root_doc}/plugins/jamf/ajax/cron.php`, + data: { crontask: "importJamf" }, + contentType: 'application/json', + beforeSend: () => { + $('[data-jamf-action="import"]').prop('disabled', true); + setBusy($('[data-jamf-action="discover"]'), 'me-2', ''); + $('[data-jamf-action="clear"]').prop('disabled', true); + }, + complete: () => { + window.location.reload(); + } + }); + }; + + const clearPendingImports = () => { + $.ajax({ + type: "POST", + url: `${CFG_GLPI.root_doc}/plugins/jamf/ajax/import.php`, + data: { + action: "clear" + }, + contentType: 'application/json', + beforeSend: () => { + $('[data-jamf-action="import"]').prop('disabled', true); + $('[data-jamf-action="discover"]').prop('disabled', true); + setBusy($('[data-jamf-action="clear"]'), 'me-2', ''); + }, + complete: () => { + window.location.reload(); + } + }); + }; + + const mergeDevices = () => { + const post_data = {}; + const table = $("#merge_table")[0]; + const row_count = table.rows.length; + for (let i = 1; i < row_count; i++) { + const row = table.rows[i]; + const jamf_id = row.cells[0].innerText; + const itemtype = row.cells[2].innerText; + const jamf_type = row.cells[3].innerText; + const glpi_sel = $(row.cells[6]).find('select')[0]; + const glpi_id = glpi_sel.value; + if (glpi_id && glpi_id > 0) { + post_data[glpi_id] = {'itemtype': itemtype, 'jamf_id': jamf_id, 'jamf_type': jamf_type}; + } + } + $.ajax({ + type: "POST", + url: `${CFG_GLPI.root_doc}/plugins/jamf/ajax/merge.php`, + data: {action: "merge", item_ids: post_data}, + contentType: 'application/json', + beforeSend: () => { + setBusy($('[data-jamf-action="merge"]'), '', ' '); + }, + complete: () => { + location.reload(); + } + }); + }; + + $(document).on('click', '[data-jamf-action="import"]', () => importDevices()); + $(document).on('click', '[data-jamf-action="discover"]', () => discoverNow()); + $(document).on('click', '[data-jamf-action="clear"]', () => clearPendingImports()); + $(document).on('click', '[data-jamf-action="merge"]', () => mergeDevices()); + + $(document).on('click', '[data-jamf-action="reset-password"]', (e) => { + const password_input = $('input[name="jsspassword"]'); + $(e.currentTarget).closest('div.form-field').hide(); + password_input.val(''); + password_input.closest('div.form-field').attr('hidden', null); + }); + + $(document).on('click', '[data-jamf-sync]', function () { + $.ajax({ + type: "POST", + url: `${CFG_GLPI.root_doc}/plugins/jamf/ajax/sync.php`, + data: {"itemtype": this.dataset.jamfSyncItemtype, "items_id": this.dataset.jamfSyncItemsId}, + contentType: 'application/json', + success: () => { + location.reload(); + } + }); + }); + + const initMdmCommands = () => { + $('[data-jamf-mdm-commands]:not([data-jamf-mdm-ready])').each(function () { + $(this).attr('data-jamf-mdm-ready', ''); + window.jamfPlugin = new window.JamfPlugin(); + window.jamfPlugin.init({ + commands: JSON.parse(this.dataset.jamfMdmCommands), + jamf_id: this.dataset.jamfId, + itemtype: this.dataset.jamfItemtype, + items_id: this.dataset.jamfItemsId, + }); + }); + }; + + $(document).on('click', '[data-jamf-mdm-command]', (e) => { + window.jamfPlugin.onMDMCommandButtonClick(e.currentTarget.dataset.jamfMdmCommand, e); + }); + + const initInventoryInfo = () => { + const markers = $('[data-jamf-inventory-info]:not([data-jamf-inventory-info-ready])'); + if (markers.length === 0) { + return; + } + markers.attr('data-jamf-inventory-info-ready', ''); + // Assets are marked dynamic by this plugin, so the native inventory section is removed + const card_title_icon = $('form[name="asset_form"]').next('.card').find('.card-header .ribbon i'); + if (card_title_icon.length > 0 && card_title_icon.eq(0).hasClass('fa-cloud-download-alt')) { + card_title_icon.closest('.card').remove(); + } + }; + + const initPluginMarkup = () => { + initMdmCommands(); + initInventoryInfo(); + }; + + $(initPluginMarkup); + $(document).on('glpi.tab.loaded', initPluginMarkup); +})(); diff --git a/setup.php b/setup.php index 65a03dc..b12ab63 100644 --- a/setup.php +++ b/setup.php @@ -89,6 +89,7 @@ function plugin_version_jamf() 'author' => "Teclib' & Curtis Conard", 'license' => 'GPLv2', 'homepage' => 'https://github.com/pluginsGLPI/jamf', + 'csp_compliant' => true, 'requirements' => [ 'glpi' => [ 'min' => PLUGIN_JAMF_MIN_GLPI, diff --git a/templates/config.html.twig b/templates/config.html.twig index 5a94cbb..344ed2f 100644 --- a/templates/config.html.twig +++ b/templates/config.html.twig @@ -44,7 +44,7 @@ {% set password_set = config.jsspassword is defined and config.jsspassword|length > 0 %} {% if password_set %} {% set field_value %} - @@ -52,14 +52,6 @@ {{ fields.field('password_reset_btn', field_value, _x('config', 'JSS Password', 'jamf'), { add_field_class: 'align-items-center', }) }} - {% endif %} {{ fields.passwordField('jsspassword', config.jsspassword|default(null), _x('config', 'JSS Password', 'jamf'), { add_field_attribs: password_set ? {'hidden': 'hidden'} : {}, diff --git a/templates/import.html.twig b/templates/import.html.twig index 8b06298..ccd4711 100644 --- a/templates/import.html.twig +++ b/templates/import.html.twig @@ -80,73 +80,8 @@
- - - - + + +
diff --git a/templates/inventory_info.html.twig b/templates/inventory_info.html.twig index 5396056..5277cb9 100644 --- a/templates/inventory_info.html.twig +++ b/templates/inventory_info.html.twig @@ -29,6 +29,7 @@ {% import 'components/form/fields_macros.html.twig' as fields %} +
{% for section in info %} {{ fields.smallTitle(section.caption) }} {% for field_name, field in section.fields %} @@ -43,25 +44,10 @@ {% if button.url %} {{ button.caption }} {% else %} - + {% endif %} {% endfor %}
{% endif %} - {% if section.extra_js is defined %} - - {% endif %} {% endfor %} - - + diff --git a/templates/mdm_commands.html.twig b/templates/mdm_commands.html.twig index b858553..68e7332 100644 --- a/templates/mdm_commands.html.twig +++ b/templates/mdm_commands.html.twig @@ -36,9 +36,9 @@ no_label: true, }) }} -
+
{% for command, command_params in commands %} -
{{ command_params.name }} @@ -93,15 +93,3 @@ {% endfor %} - - diff --git a/templates/merge.html.twig b/templates/merge.html.twig index ebc63db..68a31d6 100644 --- a/templates/merge.html.twig +++ b/templates/merge.html.twig @@ -82,37 +82,6 @@

- - +