mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
Fix device groups showing multiple times. (#10247)
Update hasAccess to not use a join. Move into The Permissions facade so we only query the db once per page load for the permissions.
This commit is contained in:
@@ -37,6 +37,7 @@ class Permissions
|
||||
private $devicePermissions;
|
||||
private $portPermissions;
|
||||
private $billPermissions;
|
||||
private $deviceGroupMap;
|
||||
|
||||
/**
|
||||
* Check if a device can be accessed by user (non-global read/admin)
|
||||
@@ -164,6 +165,26 @@ class Permissions
|
||||
->pluck('bill_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the ids of all device groups the user can access
|
||||
*
|
||||
* @param User|int $user
|
||||
* @return \Illuminate\Support\Collection
|
||||
*/
|
||||
public function deviceGroupsForUser($user = null)
|
||||
{
|
||||
$user_id = $this->getUserId($user);
|
||||
|
||||
// if we don't have a map for this user yet, populate it.
|
||||
if (!isset($this->deviceGroupMap[$user_id])) {
|
||||
$this->deviceGroupMap[$user_id] = DB::table('device_group_device')
|
||||
->whereIn('device_id', $this->devicesForUser($user))
|
||||
->pluck('device_group_id');
|
||||
}
|
||||
|
||||
return $this->deviceGroupMap[$user_id];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the cached data for device permissions. Use helpers instead.
|
||||
*
|
||||
|
||||
@@ -25,6 +25,7 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Permissions;
|
||||
use DB;
|
||||
|
||||
class DeviceGroup extends BaseModel
|
||||
@@ -291,11 +292,7 @@ class DeviceGroup extends BaseModel
|
||||
return $query;
|
||||
}
|
||||
|
||||
if (!$this->isJoined($query, 'device_group_device')) {
|
||||
$query->join('device_group_device', 'device_group_device.device_group_id', 'device_groups.id');
|
||||
}
|
||||
|
||||
return $this->hasDeviceAccess($query, $user, 'device_group_device');
|
||||
return $query->whereIn('id', Permissions::deviceGroupsForUser($user));
|
||||
}
|
||||
|
||||
// ---- Define Relationships ----
|
||||
|
||||
Reference in New Issue
Block a user