Skip to content

A free open source IT asset/license management system

Fixed #19778 - allow bulk scanner auditing by 2D bardcode

snipeabcb0742bd10
diff --git a/app/Http/Controllers/Api/AssetsController.php b/app/Http/Controllers/Api/AssetsController.php
index 9b9b195fc2..3a3f78d014 100644
--- a/app/Http/Controllers/Api/AssetsController.php
+++ b/app/Http/Controllers/Api/AssetsController.php
@@ -1905,6 +1905,10 @@ class AssetsController extends Controller
return Asset::where('asset_tag', '=', trim($audit_key))->first();
}
+ if ($audit_by_field === '2d_barcode' && $audit_key) {
+ return $this->resolveAssetFrom2dBarcode(trim($audit_key), $settings);
+ }
+
if ($request->filled('asset_tag')) {
return Asset::where('asset_tag', '=', $request->input('asset_tag'))->first();
}
@@ -1912,6 +1916,38 @@ class AssetsController extends Controller
return null;
}
+ /**
+ * Resolve the asset behind a scanned 2D barcode
+ *
+ * Non-unique targets (model/manufacturer/location name, etc.)
+ * aren't viable audit keys and fall through to null.
+ */
+ private function resolveAssetFrom2dBarcode(string $scanned, Setting $settings): ?Asset
+ {
+ // default 2D barcode format is /hardware/{id} (numeric primary key)
+ if (preg_match('~/hardware/(\d+)(?:[/?#]|$)~', $scanned, $m)) {
+ return Asset::find((int) $m[1]);
+ }
+
+ // legacy 2D barcode format is /ht/{asset_tag} (non-numeric, URL-encoded)
+ if (preg_match('~/ht/([^/?#]+)~', $scanned, $m)) {
+ return Asset::where('asset_tag', '=', urldecode($m[1]))->first();
+ }
+
+ // If the user has configured a custom 2D barcode prefix, strip it off
+ $prefix = (string) ($settings->label2_2d_prefix ?? '');
+ $value = ($prefix !== '' && str_starts_with($scanned, $prefix))
+ ? substr($scanned, strlen($prefix))
+ : $scanned;
+
+ return match ($settings->label2_2d_target) {
+ 'plain_asset_id' => ctype_digit($value) ? Asset::find((int) $value) : null,
+ 'plain_asset_tag' => Asset::where('asset_tag', '=', $value)->first(),
+ 'plain_serial_number' => Asset::where('serial', '=', $value)->first(),
+ default => null,
+ };
+ }
+
/**
* The "no matching asset" fail payload the legacy audit response emits.
*/
diff --git a/resources/lang/en-US/general.php b/resources/lang/en-US/general.php
index 4a29aa1ae0..5e28403e31 100644
--- a/resources/lang/en-US/general.php
+++ b/resources/lang/en-US/general.php
@@ -816,7 +816,8 @@ return [
'append' => 'Append',
'optional' => 'OPTIONAL',
'audit_by_field' => 'Audit by Field',
- 'audit_by_field_help' => 'Auditing by scanning serial numbers is only an available option if serial numbers are required to be unique in the Admin Settings.',
+ 'audit_by_field_help' => 'Auditing by scanning serial numbers is only an available option if serial numbers are required to be unique in the Admin Settings. Scanning 2D barcodes requires the 2D Barcode Content setting to be configured in the Label settings.',
+ 'audit_by_2d_barcode' => '2D Barcode',
'checkin_by_field' => 'Check in by Field',
'checkin_by_field_help' => 'Checking in by scanning serial numbers is only an available option if serial numbers are required to be unique in the Admin Settings.',
'audit_key' => 'Asset',
diff --git a/resources/views/hardware/quickscan.blade.php b/resources/views/hardware/quickscan.blade.php
index 787c56d03b..9376502352 100644
--- a/resources/views/hardware/quickscan.blade.php
+++ b/resources/views/hardware/quickscan.blade.php
@@ -21,9 +21,10 @@
<x-form id="audit-form">
<x-box>
- {{-- Audit by tag or by serial (serial disabled unless
- unique_serial is on). The audit_key label below
- switches text based on this choice via inline JS. --}}
+ {{-- Audit by tag, serial, or 2D barcode scan. Serial
+ is disabled unless unique_serial is on. The 2D
+ barcode option only shows when the install has
+ 2D barcodes enabled. --}}
<x-form.row
:label="trans('general.audit_by_field')"
name="audit_by_field"
@@ -35,17 +36,19 @@
<select name="audit_by_field" data-minimum-results-for-search="Infinity" id="audit_by_field" style="width: 100% !important" class="form-control select2" aria-label="audit_by_field" required>
<option value="asset_tag">{{ trans('general.asset_tag') }}</option>
<option value="serial" {{ ($settings->unique_serial != '1') ? 'disabled' : '' }}>{{ trans('general.serial_number') }}</option>
+ @if ($settings->label2_2d_type !== 'none')
+ <option value="2d_barcode">{{ trans('general.audit_by_2d_barcode') }}</option>
+ @endif
</select>
</x-slot:input>
</x-form.row>
- {{-- Tag or serial input. Hand-rolled because the
- <label> needs a JS-targeted id (audit_key_label)
- plus two data-* attributes carrying the "Asset
- Tag" and "Serial Number" translations that the
- audit-by-field switcher script swaps between.
- x-form.row's label wrapper doesn't accept
- pass-through attributes. --}}
+ {{-- Tag, serial, or 2D barcode input. Hand-rolled because
+ the <label> needs a JS-targeted id (audit_key_label)
+ plus data-* attributes carrying the per-option
+ translations the audit-by-field switcher script
+ swaps between. x-form.row's label wrapper doesn't
+ accept pass-through attributes. --}}
<div class="form-group {{ $errors->has('audit_key') ? 'error' : '' }}">
<label
for="audit_key"
@@ -53,6 +56,7 @@
class="col-md-3 control-label"
data-asset-tag-label="{{ trans('general.asset_tag') }}"
data-serial-label="{{ trans('general.serial_number') }}"
+ data-2d-barcode-label="{{ trans('general.audit_by_2d_barcode') }}"
>{{ trans('general.asset_tag') }}</label>
<div class="col-md-8">
<input type="text" class="form-control" name="audit_key" id="audit_key" required value="{{ old('audit_key') }}">
@@ -168,16 +172,17 @@
the ride so we don't split this into two spots. --}}
@section('moar_scripts')
<script nonce="{{ csrf_token() }}">
- // Swap the audit_key label text between "Asset Tag" and
- // "Serial Number" as the user toggles the by-field select.
- // Both translations ride on the label element's data attributes
- // so this script doesn't need to embed them.
+ // Swap the audit_key label text between "Asset Tag", "Serial
+ // Number", and "2D Barcode" as the user toggles the by-field
+ // select. All three translations ride on the label element's
+ // data attributes so this script doesn't need to embed them.
$(document.body).on('change', '#audit_by_field', function () {
var $label = $('#audit_key_label');
- var newText = this.value === 'serial'
- ? $label.data('serial-label')
- : $label.data('asset-tag-label');
- $label.text(newText);
+ var dataKey = {
+ serial: 'serial-label',
+ '2d_barcode': '2d-barcode-label',
+ }[this.value] || 'asset-tag-label';
+ $label.text($label.data(dataKey));
});
$('#audit-form').submit(function (event) {
diff --git a/tests/Feature/Assets/Api/AuditAssetTest.php b/tests/Feature/Assets/Api/AuditAssetTest.php
index e70160485a..c5436e9da0 100644
--- a/tests/Feature/Assets/Api/AuditAssetTest.php
+++ b/tests/Feature/Assets/Api/AuditAssetTest.php
@@ -110,6 +110,125 @@ class AuditAssetTest extends TestCase
$this->assertEquals($future, $asset->next_audit_date);
}
+ public function test_legacy_asset_audit_resolves_2d_barcode_scan_of_hardware_show_url()
+ {
+ // URL-shape QR codes are matched regardless of configured target
+ // so sites that printed labels under hardware_id and later changed
+ // targets can still audit the old stack. #19778
+ $this->settings->set([
+ 'label2_2d_type' => 'qrcode',
+ 'label2_2d_target' => 'plain_asset_tag',
+ 'label2_2d_prefix' => '',
+ ]);
+
+ $asset = Asset::factory()->create();
+
+ $this->actingAsForApi(User::factory()->auditAssets()->create())
+ ->postJson(route('api.asset.audit.legacy'), [
+ 'audit_by_field' => '2d_barcode',
+ 'audit_key' => 'https://snipe.example.com/hardware/'.$asset->id,
+ ])
+ ->assertStatusMessageIs('success')
+ ->assertJsonPath('payload.id', $asset->id);
+ }
+
+ public function test_legacy_asset_audit_resolves_2d_barcode_scan_of_ht_tag_url()
+ {
+ $this->settings->set([
+ 'label2_2d_type' => 'qrcode',
+ 'label2_2d_target' => 'ht_tag',
+ 'label2_2d_prefix' => '',
+ ]);
+
+ $asset = Asset::factory()->create(['asset_tag' => 'HT-TAG-001']);
+
+ $this->actingAsForApi(User::factory()->auditAssets()->create())
+ ->postJson(route('api.asset.audit.legacy'), [
+ 'audit_by_field' => '2d_barcode',
+ 'audit_key' => 'https://snipe.example.com/ht/HT-TAG-001',
+ ])
+ ->assertStatusMessageIs('success')
+ ->assertJsonPath('payload.id', $asset->id);
+ }
+
+ public function test_legacy_asset_audit_resolves_2d_barcode_plain_asset_id_with_prefix()
+ {
+ $this->settings->set([
+ 'label2_2d_type' => 'qrcode',
+ 'label2_2d_target' => 'plain_asset_id',
+ 'label2_2d_prefix' => 'ACME-',
+ ]);
+
+ $asset = Asset::factory()->create();
+
+ $this->actingAsForApi(User::factory()->auditAssets()->create())
+ ->postJson(route('api.asset.audit.legacy'), [
+ 'audit_by_field' => '2d_barcode',
+ 'audit_key' => 'ACME-'.$asset->id,
+ ])
+ ->assertStatusMessageIs('success')
+ ->assertJsonPath('payload.id', $asset->id);
+ }
+
+ public function test_legacy_asset_audit_resolves_2d_barcode_plain_asset_tag()
+ {
+ $this->settings->set([
+ 'label2_2d_type' => 'qrcode',
+ 'label2_2d_target' => 'plain_asset_tag',
+ 'label2_2d_prefix' => '',
+ ]);
+
+ $asset = Asset::factory()->create(['asset_tag' => 'PLAIN-TAG-42']);
+
+ $this->actingAsForApi(User::factory()->auditAssets()->create())
+ ->postJson(route('api.asset.audit.legacy'), [
+ 'audit_by_field' => '2d_barcode',
+ 'audit_key' => 'PLAIN-TAG-42',
+ ])
+ ->assertStatusMessageIs('success')
+ ->assertJsonPath('payload.id', $asset->id);
+ }
+
+ public function test_legacy_asset_audit_resolves_2d_barcode_plain_serial_number()
+ {
+ $this->settings->set([
+ 'label2_2d_type' => 'qrcode',
+ 'label2_2d_target' => 'plain_serial_number',
+ 'label2_2d_prefix' => '',
+ ]);
+
+ $asset = Asset::factory()->create(['serial' => 'SN-ZZ-9']);
+
+ $this->actingAsForApi(User::factory()->auditAssets()->create())
+ ->postJson(route('api.asset.audit.legacy'), [
+ 'audit_by_field' => '2d_barcode',
+ 'audit_key' => 'SN-ZZ-9',
+ ])
+ ->assertStatusMessageIs('success')
+ ->assertJsonPath('payload.id', $asset->id);
+ }
+
+ public function test_legacy_asset_audit_rejects_2d_barcode_scan_when_target_is_non_unique()
+ {
+ // Non-unique targets (model name, location name, etc.) can't be
+ // used for audit lookup. Fall through to "not found" rather than
+ // guessing which asset the admin meant.
+ $this->settings->set([
+ 'label2_2d_type' => 'qrcode',
+ 'label2_2d_target' => 'plain_model_name',
+ 'label2_2d_prefix' => '',
+ ]);
+
+ Asset::factory()->create();
+
+ $this->actingAsForApi(User::factory()->auditAssets()->create())
+ ->postJson(route('api.asset.audit.legacy'), [
+ 'audit_by_field' => '2d_barcode',
+ 'audit_key' => 'Macbook Pro 16-inch',
+ ])
+ ->assertStatusMessageIs('error');
+ }
+
public function test_asset_audit_is_saved()
{
$asset = Asset::factory()->create(['next_audit_date' => now()->subMonth()->toDateString()]);