Skip to content

Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.

Don't crash audit browser with weak user association missing (#5589)

Adam Rutkowski01e801942e3c
diff --git a/extra/lib/plausible_web/live/customer_support/live/team.ex b/extra/lib/plausible_web/live/customer_support/live/team.ex
index 8ebc4a50bc..20a2b422e8 100644
--- a/extra/lib/plausible_web/live/customer_support/live/team.ex
+++ b/extra/lib/plausible_web/live/customer_support/live/team.ex
@@ -52,7 +52,7 @@ defmodule PlausibleWeb.CustomerSupport.Live.Team do
meta =
if entry.user_id && entry.user_id > 0 do
- user = Plausible.Repo.get!(Plausible.Auth.User, entry.user_id)
+ user = Plausible.Repo.get(Plausible.Auth.User, entry.user_id)
Map.put(meta, :user, user)
else
meta
@@ -60,7 +60,7 @@ defmodule PlausibleWeb.CustomerSupport.Live.Team do
meta =
if entry.entity == "Plausible.Auth.User" do
- user = Plausible.Repo.get!(Plausible.Auth.User, String.to_integer(entry.entity_id))
+ user = Plausible.Repo.get(Plausible.Auth.User, String.to_integer(entry.entity_id))
Map.put(meta, :entity, user)
else
meta
@@ -1229,6 +1229,12 @@ defmodule PlausibleWeb.CustomerSupport.Live.Team do
attr :user, Plausible.Auth.User
+ defp audit_user(%{user: nil} = assigns) do
+ ~H"""
+ (N/A)
+ """
+ end
+
defp audit_user(assigns) do
~H"""
<div class="flex items-center gap-x-1">
diff --git a/test/plausible_web/live/customer_support/teams_test.exs b/test/plausible_web/live/customer_support/teams_test.exs
index 866ebf2916..26119cc7e8 100644
--- a/test/plausible_web/live/customer_support/teams_test.exs
+++ b/test/plausible_web/live/customer_support/teams_test.exs
@@ -658,6 +658,27 @@ defmodule PlausibleWeb.Live.CustomerSupport.TeamsTest do
assert text_of_element(html, ~s|textarea|) ==
"{ &amp;quot;foo&amp;quot;: &amp;quot;bar&amp;quot; }"
end
+
+ test "shows audit entries when user id does not exists", %{conn: conn, user: user} do
+ team = team_of(user)
+
+ %Plausible.Audit.Entry{
+ name: "Reveal Test",
+ entity: "Plausible.Auth.User",
+ entity_id: "666111",
+ team_id: team.id,
+ datetime: NaiveDateTime.utc_now(),
+ user_id: 666_111,
+ actor_type: :user
+ }
+ |> Plausible.Repo.insert!()
+
+ {:ok, lv, _html} = live(conn, open_team(team.id, tab: :audit))
+
+ text = lv |> render() |> text()
+
+ assert text =~ "(N/A) (N/A)"
+ end
end
end
end