diff --git a/app/Models/CustomMap.php b/app/Models/CustomMap.php index 5a1f4111fc10..b480217c4950 100644 --- a/app/Models/CustomMap.php +++ b/app/Models/CustomMap.php @@ -101,23 +101,15 @@ public function hasReadAccess(User $user): bool return true; } - public function scopeHasAccess($query, User $user) + public function scopeHasAccess(Builder $query, User $user): Builder { if ($user->hasGlobalRead()) { return $query; } - // Allow only if the user has access to all devices on the map - return $query->withCount([ - 'nodes as device_nodes_count' => function (Builder $q): void { - $q->whereNotNull('device_id'); - }, - 'nodes as device_nodes_allowed_count' => function (Builder $q) use ($user): void { - $this->hasDeviceAccess($q, $user, 'custom_map_nodes'); - }, - ]) - ->havingRaw('device_nodes_count = device_nodes_allowed_count') - ->having('device_nodes_count', '>', 0); + // Only show maps where ALL device nodes are accessible by the user + return $query->whereHas('nodes', fn ($q) => $q->whereNotNull('device_id')) + ->whereDoesntHave('nodes', fn ($q) => $q->whereNotNull('device_id')->whereNotIn('device_id', \Permissions::devicesForUser($user))); } /**