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
This commit is contained in:
Tony Murray
2019-05-13 08:35:24 -05:00
committed by GitHub
parent 2187967854
commit c097747d77
2 changed files with 19 additions and 9 deletions

View File

@@ -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');

View File

@@ -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');
}