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
This commit is contained in:
snipe 2020-03-06 14:55:20 -08:00 committed by GitHub
parent 61bdb88ba5
commit ca43554327
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 72 additions and 15 deletions

View file

@ -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] [<snipe@snipe.net>]
* @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] [<snipe@snipe.net>]
* @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
*

View file

@ -320,15 +320,20 @@ 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', [

View file

@ -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',