From c097747d77e931859d9e5411c81e80f3bb5cb279 Mon Sep 17 00:00:00 2001 From: Tony Murray Date: Mon, 13 May 2019 08:35:24 -0500 Subject: [PATCH] API Fix error when no fdb are found (#10125) * Fix error when no fdb are found * actually, it was totally broken... fix untested * missing device update * cleanup * Update api_functions.inc.php --- app/Models/Device.php | 5 +++++ includes/html/api_functions.inc.php | 23 ++++++++++++++--------- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/app/Models/Device.php b/app/Models/Device.php index 9fb93647a9..e596789d15 100644 --- a/app/Models/Device.php +++ b/app/Models/Device.php @@ -534,6 +534,11 @@ class Device extends BaseModel return $this->hasMany('App\Models\Port', 'device_id', 'device_id'); } + public function portsFdb() + { + return $this->hasMany('App\Models\PortsFdb', 'device_id', 'device_id'); + } + public function portsNac() { return $this->hasMany('App\Models\PortsNac', 'device_id', 'device_id'); diff --git a/includes/html/api_functions.inc.php b/includes/html/api_functions.inc.php index ca35858461..c7e7567a65 100644 --- a/includes/html/api_functions.inc.php +++ b/includes/html/api_functions.inc.php @@ -2050,8 +2050,13 @@ function get_fdb() } check_device_permission($device_id); - $fdb = \App\Models\PortsFdb::find($device_id); - api_success($fdb, 'ports_fdb'); + $device = \App\Models\Device::find($device_id); + if ($device) { + $fdb = $device->portsFdb; + api_success($fdb, 'ports_fdb'); + } + + api_error(404, 'Device does not exist'); } @@ -2063,13 +2068,13 @@ function list_fdb() $router = $app->router()->getCurrentRoute()->getParams(); $mac = $router['mac']; - if (empty($mac)) { - $fdb = \App\Models\PortsFdb::hasAccess(Auth::user())->get(); - } else { - $fdb = \App\Models\PortsFdb::find($mac); - } - $total_fdb = $fdb->count(); - if ($total_fdb == 0) { + $fdb = \App\Models\PortsFdb::hasAccess(Auth::user()) + ->when(!empty($mac), function ($query) use ($mac) { + return $query->where('mac_address', $mac); + }) + ->get(); + + if ($fdb->isEmpty()) { api_error(404, 'Fdb do not exist'); }