forked from librenms/librenms
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert the inventory page to Laravel (librenms#15004)
* Convert the inventory page to Laravel Fix several XSS issues (hopefully no new ones snuck in) Small improvement to the SelectController to allow filtering by filterFields() * style fixes * Fix lint issues * Fix part device filter
- Loading branch information
Showing
14 changed files
with
345 additions
and
208 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use App\Models\EntPhysical; | ||
use Illuminate\Contracts\View\View; | ||
use Illuminate\Http\Request; | ||
|
||
class InventoryController extends Controller | ||
{ | ||
public function __invoke(Request $request): View | ||
{ | ||
$this->validate($request, [ | ||
'device' => 'nullable|int', | ||
'descr' => 'nullable|string', | ||
'model' => 'nullable|string', | ||
'serial' => 'nullable|string', | ||
]); | ||
|
||
$device = \App\Models\Device::hasAccess($request->user()) | ||
->select(['device_id', 'hostname', 'ip', 'sysName', 'display']) | ||
->firstWhere('device_id', $request->get('device')); | ||
|
||
$model_filter = ['field' => 'model']; | ||
$device_selected = ''; | ||
if ($device) { | ||
$device_selected = ['id' => $device->device_id, 'text' => $device->displayName()]; | ||
$model_filter['device_id'] = $device->device_id; | ||
} | ||
|
||
return view('inventory', [ | ||
'device_selected' => $device_selected, | ||
'filter' => [ | ||
'device' => $device?->device_id, | ||
'descr' => $request->get('descr'), | ||
'model' => $request->get('model'), | ||
'serial' => $request->get('serial'), | ||
], | ||
'model_filter' => $model_filter, | ||
'show_purge' => EntPhysical::whereDoesntHave('device')->exists(), | ||
]); | ||
} | ||
|
||
public function purge() | ||
{ | ||
EntPhysical::whereDoesntHave('device')->delete(); | ||
|
||
return redirect()->back(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
/** | ||
* EntPhysicalController.php | ||
* | ||
* -Description- | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
* | ||
* @link https://www.librenms.org | ||
* | ||
* @copyright 2023 Tony Murray | ||
* @author Tony Murray <murraytony@gmail.com> | ||
*/ | ||
|
||
namespace App\Http\Controllers\Select; | ||
|
||
use App\Models\EntPhysical; | ||
|
||
class InventoryController extends SelectController | ||
{ | ||
protected function rules() | ||
{ | ||
return [ | ||
'field' => 'required|in:name,model,descr,class', | ||
'device' => 'nullable|int', | ||
]; | ||
} | ||
|
||
protected function filterFields($request) | ||
{ | ||
return ['device_id']; | ||
} | ||
|
||
protected function searchFields($request) | ||
{ | ||
return [$this->fieldToColumn($request->get('field'))]; | ||
} | ||
|
||
protected function baseQuery($request) | ||
{ | ||
$column = $this->fieldToColumn($request->get('field')); | ||
|
||
return EntPhysical::hasAccess($request->user()) | ||
->select($column) | ||
->orderBy($column) | ||
->distinct(); | ||
} | ||
|
||
private function fieldToColumn(string $field): string | ||
{ | ||
return match ($field) { | ||
'name' => 'entPhysicalName', | ||
'model' => 'entPhysicalModelName', | ||
'descr' => 'entPhysicalDescr', | ||
'class' => 'entPhysicalClass', | ||
default => 'entPhysicalName', | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
<?php | ||
/** | ||
* InventoryController.php | ||
* | ||
* -Description- | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
* | ||
* @link https://www.librenms.org | ||
* | ||
* @copyright 2023 Tony Murray | ||
* @author Tony Murray <murraytony@gmail.com> | ||
*/ | ||
|
||
namespace App\Http\Controllers\Table; | ||
|
||
use App\Models\EntPhysical; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Support\Collection; | ||
use LibreNMS\Util\Url; | ||
|
||
class InventoryController extends TableController | ||
{ | ||
public function rules() | ||
{ | ||
return [ | ||
'device' => 'nullable|int', | ||
'descr' => 'nullable|string', | ||
'model'=> 'nullable|string', | ||
'serial' => 'nullable|string', | ||
]; | ||
} | ||
|
||
protected function filterFields($request) | ||
{ | ||
return [ | ||
'device_id' => 'device', | ||
]; | ||
} | ||
|
||
protected function searchFields($request) | ||
{ | ||
return ['entPhysicalDescr', 'entPhysicalModelName', 'entPhysicalSerialNum']; | ||
} | ||
|
||
protected function sortFields($request) | ||
{ | ||
return [ | ||
'device' => 'device_id', | ||
'name' => 'entPhysicalName', | ||
'descr' => 'entPhysicalDescr', | ||
'model' => 'entPhysicalModelName', | ||
'serial' => 'entPhysicalSerialNum', | ||
]; | ||
} | ||
|
||
protected function baseQuery($request) | ||
{ | ||
$query = EntPhysical::hasAccess($request->user()) | ||
->with('device') | ||
->select(['entPhysical_id', 'device_id', 'entPhysicalDescr', 'entPhysicalName', 'entPhysicalModelName', 'entPhysicalSerialNum']); | ||
|
||
// apply specific field filters | ||
$this->search($request->get('descr'), $query, ['entPhysicalDescr']); | ||
$this->search($request->get('model'), $query, ['entPhysicalModelName']); | ||
$this->search($request->get('serial'), $query, ['entPhysicalSerialNum']); | ||
|
||
return $query; | ||
} | ||
|
||
/** | ||
* @param EntPhysical $entPhysical | ||
* @return array|Model|Collection | ||
*/ | ||
public function formatItem($entPhysical) | ||
{ | ||
return [ | ||
'device' => Url::deviceLink($entPhysical->device), | ||
'descr' => htmlspecialchars($entPhysical->entPhysicalDescr), | ||
'name' => htmlspecialchars($entPhysical->entPhysicalName), | ||
'model' => htmlspecialchars($entPhysical->entPhysicalModelName), | ||
'serial' => htmlspecialchars($entPhysical->entPhysicalSerialNum), | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.