Skip to content

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

Live session (#6417)

Adam Rutkowskiba5ef0e7314e
diff --git a/lib/plausible_web/live/super_admin_live_auth.ex b/lib/plausible_web/live/super_admin_live_auth.ex
new file mode 100644
index 0000000000..10d14069af
--- /dev/null
+++ b/lib/plausible_web/live/super_admin_live_auth.ex
@@ -0,0 +1,24 @@
+defmodule PlausibleWeb.Live.SuperAdminLiveAuth do
+ @moduledoc """
+ LiveView `on_mount` hook that enforces super-admin access on every WebSocket
+ mount and reconnect.
+ """
+
+ import Phoenix.LiveView, only: [redirect: 2]
+
+ alias PlausibleWeb.UserAuth
+
+ def on_mount(:default, _params, session, socket) do
+ current_user =
+ case UserAuth.get_user_session(session) do
+ {:ok, %{user: user}} -> user
+ _ -> nil
+ end
+
+ if Plausible.Auth.super_admin?(current_user) do
+ {:cont, socket}
+ else
+ {:halt, redirect(socket, to: "/")}
+ end
+ end
+end
diff --git a/lib/plausible_web/router.ex b/lib/plausible_web/router.ex
index e655de73e0..1af5d5d2d2 100644
--- a/lib/plausible_web/router.ex
+++ b/lib/plausible_web/router.ex
@@ -109,21 +109,27 @@ defmodule PlausibleWeb.Router do
end
on_ee do
- scope alias: PlausibleWeb.Live,
- assigns: %{connect_live_socket: true, skip_plausible_tracking: true} do
- pipe_through [:browser, :csrf, :app_layout, :flags]
-
- live "/cs", CustomerSupport, :index, as: :customer_support
- live "/cs/teams/team/:id", CustomerSupport.Team, :show, as: :customer_support_team
- live "/cs/users/user/:id", CustomerSupport.User, :show, as: :customer_support_user
- live "/cs/sites/site/:id", CustomerSupport.Site, :show, as: :customer_support_site
+ live_session :customer_support,
+ on_mount: PlausibleWeb.Live.SuperAdminLiveAuth do
+ scope alias: PlausibleWeb.Live,
+ assigns: %{connect_live_socket: true, skip_plausible_tracking: true} do
+ pipe_through [:browser, :csrf, :app_layout, :flags]
+
+ live "/cs", CustomerSupport, :index, as: :customer_support
+ live "/cs/teams/team/:id", CustomerSupport.Team, :show, as: :customer_support_team
+ live "/cs/users/user/:id", CustomerSupport.User, :show, as: :customer_support_user
+ live "/cs/sites/site/:id", CustomerSupport.Site, :show, as: :customer_support_site
+ end
end
end
on_ee do
- scope path: "/flags" do
- pipe_through :flags
- forward "/", FunWithFlags.UI.Router, namespace: "flags"
+ live_session :flags,
+ on_mount: PlausibleWeb.Live.SuperAdminLiveAuth do
+ scope path: "/flags" do
+ pipe_through :flags
+ forward "/", FunWithFlags.UI.Router, namespace: "flags"
+ end
end
end