From 97cdb8e46cbd0d3db7750962cdf83f06300eb740 Mon Sep 17 00:00:00 2001 From: Graham McIntire Date: Sun, 25 Jan 2026 09:09:50 -0600 Subject: [PATCH] fix: handle cloud pollers in agent show page and fix NoResultsError Two issues fixed: 1. Cloud pollers (organization_id = nil) were causing access denied errors when superadmins tried to view them. Now allows access if user is superadmin and agent is a cloud poller. 2. Ecto.NoResultsError was being raised without required :queryable option, causing KeyError crash. Now properly raises with queryable parameter. Error was: KeyError: key :queryable not found in [] (ecto 3.13.5) lib/ecto/exceptions.ex:203: Ecto.NoResultsError.exception/1 (towerops) lib/towerops_web/live/agent_live/show.ex:16: mount/3 --- lib/towerops_web/live/agent_live/show.ex | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/towerops_web/live/agent_live/show.ex b/lib/towerops_web/live/agent_live/show.ex index 821d203e..9fc987f7 100644 --- a/lib/towerops_web/live/agent_live/show.ex +++ b/lib/towerops_web/live/agent_live/show.ex @@ -10,10 +10,15 @@ defmodule ToweropsWeb.AgentLive.Show do def mount(%{"id" => id}, _session, socket) do organization = socket.assigns.current_organization agent_token = Agents.get_agent_token!(id) + current_user = socket.assigns.current_scope.user - # Verify the agent belongs to this organization - if agent_token.organization_id != organization.id do - raise Ecto.NoResultsError + # Verify access: agent must belong to this org OR be a cloud poller (visible to superadmin) + has_access = + agent_token.organization_id == organization.id || + (agent_token.is_cloud_poller && current_user.is_superuser) + + if !has_access do + raise Ecto.NoResultsError, queryable: Towerops.Agents.AgentToken end # device this agent is responsible for)