Allow device url by hostname (#11831)

* Allow device url by hostname
/device/hostname
/device/hostname/ports

* slightly different code style
without else ;)
This commit is contained in:
Tony Murray
2020-06-17 15:13:06 -05:00
committed by GitHub
parent 62f850aef9
commit cd9c349297
2 changed files with 9 additions and 7 deletions

View File

@@ -55,18 +55,20 @@ class DeviceController extends Controller
'capture' => \App\Http\Controllers\Device\Tabs\CaptureController::class, 'capture' => \App\Http\Controllers\Device\Tabs\CaptureController::class,
]; ];
public function index(Request $request, $device_id, $current_tab = 'overview', $vars = '') public function index(Request $request, $device, $current_tab = 'overview', $vars = '')
{ {
$device_id = (int)str_replace('device=', '', $device_id); $device = str_replace('device=', '', $device);
$current_tab = str_replace('tab=', '', $current_tab); $device = is_numeric($device) ? DeviceCache::get($device) : DeviceCache::getByHostname($device);
$current_tab = array_key_exists($current_tab, $this->tabs) ? $current_tab : 'overview'; $device_id = $device->device_id;
DeviceCache::setPrimary($device_id); DeviceCache::setPrimary($device_id);
$device = DeviceCache::getPrimary();
if (!$device->exists) { if (!$device->exists) {
abort(404); abort(404);
} }
$current_tab = str_replace('tab=', '', $current_tab);
$current_tab = array_key_exists($current_tab, $this->tabs) ? $current_tab : 'overview';
if ($current_tab == 'port') { if ($current_tab == 'port') {
$vars = Url::parseLegacyPath($request->path()); $vars = Url::parseLegacyPath($request->path());
$port = Port::findOrFail($vars->get('port')); $port = Port::findOrFail($vars->get('port'));

View File

@@ -34,8 +34,8 @@ Route::group(['middleware' => ['auth'], 'guard' => 'auth'], function () {
Route::get('authlog', 'UserController@authlog'); Route::get('authlog', 'UserController@authlog');
Route::get('overview', 'OverviewController@index')->name('overview'); Route::get('overview', 'OverviewController@index')->name('overview');
Route::get('/', 'OverviewController@index'); Route::get('/', 'OverviewController@index');
Route::match(['get', 'post'], 'device/{device_id}/{tab?}/{vars?}', 'DeviceController@index') Route::match(['get', 'post'], 'device/{device}/{tab?}/{vars?}', 'DeviceController@index')
->name('device')->where(['device_id' => '(device=)?[0-9]+', 'vars' => '.*']); ->name('device')->where(['vars' => '.*']);
// Maps // Maps
Route::group(['prefix' => 'maps', 'namespace' => 'Maps'], function () { Route::group(['prefix' => 'maps', 'namespace' => 'Maps'], function () {