From ca4355432755b8acf5de09e897aff4f7cbe44036 Mon Sep 17 00:00:00 2001 From: snipe Date: Fri, 6 Mar 2020 14:55:20 -0800 Subject: [PATCH] Fixes search by serial or tag even if they have slashes in them (#7879) * Fixes search by serial or tag even if they have slashes in them * Added support for url param byTag and bySerial * Fixed typo comments * Sojme additional comments to clarify use-cases * Updated comments for clarity --- app/Http/Controllers/AssetsController.php | 47 +++++++++++++++++++++-- routes/api.php | 21 ++++++---- routes/web/hardware.php | 19 +++++++-- 3 files changed, 72 insertions(+), 15 deletions(-) diff --git a/app/Http/Controllers/AssetsController.php b/app/Http/Controllers/AssetsController.php index c5388007c1..8a146e8709 100755 --- a/app/Http/Controllers/AssetsController.php +++ b/app/Http/Controllers/AssetsController.php @@ -439,22 +439,63 @@ class AssetsController extends Controller /** - * Searches the assets table by asset tag, and redirects if it finds one + * Searches the assets table by tag, and redirects if it finds one. + * + * This is used by the top search box in Snipe-IT, but as of 4.9.x + * can also be used as a url segment. + * + * https://yoursnipe.com/hardware/bytag/?assetTag=foo + * + * OR + * + * https://yoursnipe.com/hardware/bytag/foo + * + * The latter is useful if you're doing home-grown barcodes, or + * some other automation where you don't always know the internal ID of + * an asset and don't want to query for it. * * @author [A. Gianotto] [] + * @param string $tag * @since [v3.0] * @return Redirect */ - public function getAssetByTag(Request $request) + public function getAssetByTag(Request $request, $tag = null) { + $topsearch = ($request->get('topsearch')=="true"); - if (!$asset = Asset::where('asset_tag', '=', $request->get('assetTag'))->first()) { + // We need this part to determine whether a url query parameter has been passed, OR + // whether it's the url fragment we need to look at + $tag = ($request->get('assetTag')) ? $request->get('assetTag') : $tag; + + if (!$asset = Asset::where('asset_tag', '=', $tag)->first()) { return redirect()->route('hardware.index')->with('error', trans('admin/hardware/message.does_not_exist')); } $this->authorize('view', $asset); return redirect()->route('hardware.show', $asset->id)->with('topsearch', $topsearch); } + + + /** + * Searches the assets table by serial, and redirects if it finds one + * + * @author [A. Gianotto] [] + * @param string $serial + * @since [v4.9.1] + * @return Redirect + */ + public function getAssetBySerial(Request $request, $serial = null) + { + + $serial = ($request->get('serial')) ? $request->get('serial') : $serial; + if (!$asset = Asset::where('serial', '=', $serial)->first()) { + return redirect()->route('hardware.index')->with('error', trans('admin/hardware/message.does_not_exist')); + } + $this->authorize('view', $asset); + return redirect()->route('hardware.show', $asset->id); + } + + /** * Return a QR code for the asset * diff --git a/routes/api.php b/routes/api.php index 509b957f1b..039723797b 100644 --- a/routes/api.php +++ b/routes/api.php @@ -320,16 +320,21 @@ Route::group(['prefix' => 'v1','namespace' => 'Api', 'middleware' => 'api'], fun Route::group(['prefix' => 'hardware'], function () { - Route::get( 'bytag/{tag}', [ - 'as' => 'assets.show.bytag', - 'uses' => 'AssetsController@showByTag' - ]); + Route::get('bytag/{any}', + [ + 'as' => 'api.assets.show.bytag', + 'uses' => 'AssetsController@showByTag' + ] + )->where('any', '.*'); - Route::get( 'byserial/{serial}', [ - 'as' => 'assets.show.byserial', - 'uses' => 'AssetsController@showBySerial' - ]); + Route::get('byserial/{any}', + [ + 'as' => 'api.assets.show.byserial', + 'uses' => 'AssetsController@showBySerial' + ] + )->where('any', '.*'); + Route::get( 'selectlist', [ 'as' => 'assets.selectlist', diff --git a/routes/web/hardware.php b/routes/web/hardware.php index 35decdc2b5..0d192efa47 100644 --- a/routes/web/hardware.php +++ b/routes/web/hardware.php @@ -60,10 +60,21 @@ Route::group( 'uses' => 'AssetsController@postImportHistory' ]); - Route::get('/bytag', [ - 'as' => 'findbytag/hardware', - 'uses' => 'AssetsController@getAssetByTag' - ]); + Route::get('bytag/{any?}', + [ + 'as' => 'findbytag/hardware', + 'uses' => 'AssetsController@getAssetByTag' + ] + )->where('any', '.*'); + + Route::get('byserial/{any?}', + [ + 'as' => 'findbyserial/hardware', + 'uses' => 'AssetsController@getAssetBySerial' + ] + )->where('any', '.*'); + + Route::get('{assetId}/clone', [ 'as' => 'clone/hardware',