Skip to content

Commit

Permalink
Fixes search by serial or tag even if they have slashes in them (snip…
Browse files Browse the repository at this point in the history
…e#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
  • Loading branch information
snipe authored Mar 6, 2020
1 parent 61bdb88 commit ca43554
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 15 deletions.
47 changes: 44 additions & 3 deletions app/Http/Controllers/AssetsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -439,22 +439,63 @@ public function destroy($assetId)


/**
* 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] [<[email protected]>]
* @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] [<[email protected]>]
* @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
*
Expand Down
21 changes: 13 additions & 8 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -320,16 +320,21 @@

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',
Expand Down
19 changes: 15 additions & 4 deletions routes/web/hardware.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,21 @@ function () {
'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',
Expand Down

0 comments on commit ca43554

Please sign in to comment.