Skip to content

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

Allow admin access to locked dashboards (#1710)

RobertJoonas6b22806e1fa6
diff --git a/assets/js/dashboard/site-switcher.js b/assets/js/dashboard/site-switcher.js
index 56111eede8..6f2fbd8411 100644
--- a/assets/js/dashboard/site-switcher.js
+++ b/assets/js/dashboard/site-switcher.js
@@ -104,7 +104,7 @@ export default class SiteSwitcher extends React.Component {
}
renderSettingsLink() {
- if (['owner', 'admin'].includes(this.props.currentUserRole)) {
+ if (['owner', 'admin', 'super_admin'].includes(this.props.currentUserRole)) {
return (
<React.Fragment>
<div className="py-1">
diff --git a/config/runtime.exs b/config/runtime.exs
index 64a6054f70..91a80a5025 100644
--- a/config/runtime.exs
+++ b/config/runtime.exs
@@ -61,7 +61,7 @@ db_socket_dir = get_var_from_path_or_env(config_dir, "DATABASE_SOCKET_DIR")
admin_user = get_var_from_path_or_env(config_dir, "ADMIN_USER_NAME")
admin_email = get_var_from_path_or_env(config_dir, "ADMIN_USER_EMAIL")
-admin_user_ids =
+super_admin_user_ids =
get_var_from_path_or_env(config_dir, "ADMIN_USER_IDS", "")
|> String.split(",")
|> Enum.map(fn id -> Integer.parse(id) end)
@@ -190,7 +190,7 @@ config :plausible,
admin_pwd: admin_pwd,
environment: env,
mailer_email: mailer_email,
- admin_user_ids: admin_user_ids,
+ super_admin_user_ids: super_admin_user_ids,
site_limit: site_limit,
site_limit_exempt: site_limit_exempt,
is_selfhost: is_selfhost,
diff --git a/lib/plausible/auth/auth.ex b/lib/plausible/auth/auth.ex
index 3dd8250485..cb12997ca2 100644
--- a/lib/plausible/auth/auth.ex
+++ b/lib/plausible/auth/auth.ex
@@ -92,4 +92,10 @@ defmodule Plausible.Auth do
)
)
end
+
+ def is_super_admin?(nil), do: false
+
+ def is_super_admin?(user_id) do
+ user_id in Application.get_env(:plausible, :super_admin_user_ids)
+ end
end
diff --git a/lib/plausible_web/controllers/site_controller.ex b/lib/plausible_web/controllers/site_controller.ex
index c8abc9dcea..068cd3b040 100644
--- a/lib/plausible_web/controllers/site_controller.ex
+++ b/lib/plausible_web/controllers/site_controller.ex
@@ -6,7 +6,7 @@ defmodule PlausibleWeb.SiteController do
plug PlausibleWeb.RequireAccountPlug
plug PlausibleWeb.AuthorizeSiteAccess,
- [:owner, :admin] when action not in [:index, :new, :create_site]
+ [:owner, :admin, :super_admin] when action not in [:index, :new, :create_site]
def index(conn, params) do
user = conn.assigns[:current_user]
diff --git a/lib/plausible_web/controllers/stats_controller.ex b/lib/plausible_web/controllers/stats_controller.ex
index 8c87c612a0..9352a9e1b6 100644
--- a/lib/plausible_web/controllers/stats_controller.ex
+++ b/lib/plausible_web/controllers/stats_controller.ex
@@ -8,9 +8,10 @@ defmodule PlausibleWeb.StatsController do
def stats(%{assigns: %{site: site}} = conn, _params) do
has_stats = Plausible.Sites.has_stats?(site)
+ can_see_stats = !site.locked || conn.assigns[:current_user_role] == :super_admin
cond do
- !site.locked && has_stats ->
+ has_stats && can_see_stats ->
demo = site.domain == PlausibleWeb.Endpoint.host()
offer_email_report = get_session(conn, site.domain <> "_offer_email_report")
@@ -26,7 +27,7 @@ defmodule PlausibleWeb.StatsController do
demo: demo
)
- !site.locked && !has_stats ->
+ !has_stats && can_see_stats ->
conn
|> assign(:skip_plausible_tracking, true)
|> render("waiting_first_pageview.html", site: site)
diff --git a/lib/plausible_web/plugs/authorize_site_access.ex b/lib/plausible_web/plugs/authorize_site_access.ex
index 2b1ccd7e0d..9d5987e862 100644
--- a/lib/plausible_web/plugs/authorize_site_access.ex
+++ b/lib/plausible_web/plugs/authorize_site_access.ex
@@ -2,7 +2,7 @@ defmodule PlausibleWeb.AuthorizeSiteAccess do
import Plug.Conn
use Plausible.Repo
- def init([]), do: [:public, :viewer, :admin, :owner]
+ def init([]), do: [:public, :viewer, :admin, :super_admin, :owner]
def init(allowed_roles), do: allowed_roles
def call(conn, allowed_roles) do
@@ -23,15 +23,15 @@ defmodule PlausibleWeb.AuthorizeSiteAccess do
user_id && membership_role ->
membership_role
+ Plausible.Auth.is_super_admin?(user_id) ->
+ :super_admin
+
site.public ->
:public
shared_link_record && shared_link_record.site_id == site.id ->
:public
- user_id in admin_user_ids() ->
- :admin
-
true ->
nil
end
@@ -43,8 +43,4 @@ defmodule PlausibleWeb.AuthorizeSiteAccess do
end
end
end
-
- defp admin_user_ids() do
- Application.get_env(:plausible, :admin_user_ids)
- end
end
diff --git a/lib/plausible_web/plugs/authorize_stats_api.ex b/lib/plausible_web/plugs/authorize_stats_api.ex
index d98e3ae2a3..b7515cc148 100644
--- a/lib/plausible_web/plugs/authorize_stats_api.ex
+++ b/lib/plausible_web/plugs/authorize_stats_api.ex
@@ -46,11 +46,11 @@ defmodule PlausibleWeb.AuthorizeStatsApiPlug do
defp verify_access(api_key, site_id) do
site = Repo.get_by(Plausible.Site, domain: site_id)
is_member = site && Plausible.Sites.is_member?(api_key.user_id, site)
- is_admin = api_key.user_id in admin_user_ids()
+ is_super_admin = Plausible.Auth.is_super_admin?(api_key.user_id)
cond do
site && is_member -> {:ok, site}
- site && is_admin -> {:ok, site}
+ site && is_super_admin -> {:ok, site}
true -> {:error, :invalid_api_key}
end
end
@@ -79,8 +79,4 @@ defmodule PlausibleWeb.AuthorizeStatsApiPlug do
{:deny, _} -> {:error, :rate_limit, api_key.hourly_request_limit}
end
end
-
- defp admin_user_ids() do
- Application.get_env(:plausible, :admin_user_ids)
- end
end
diff --git a/lib/plausible_web/plugs/crm_auth_plug.ex b/lib/plausible_web/plugs/crm_auth_plug.ex
index 2c42600e80..fa68864d8e 100644
--- a/lib/plausible_web/plugs/crm_auth_plug.ex
+++ b/lib/plausible_web/plugs/crm_auth_plug.ex
@@ -14,13 +14,11 @@ defmodule PlausibleWeb.CRMAuthPlug do
id ->
user = Repo.get_by(Plausible.Auth.User, id: id)
- if user && user.id in admin_user_ids() do
+ if user && Plausible.Auth.is_super_admin?(user.id) do
assign(conn, :current_user, user)
else
conn |> send_resp(403, "Not allowed") |> halt
end
end
end
-
- defp admin_user_ids(), do: Application.get_env(:plausible, :admin_user_ids)
end
diff --git a/lib/plausible_web/templates/stats/stats.html.eex b/lib/plausible_web/templates/stats/stats.html.eex
index 58c4e108b7..a47aac1505 100644
--- a/lib/plausible_web/templates/stats/stats.html.eex
+++ b/lib/plausible_web/templates/stats/stats.html.eex
@@ -4,6 +4,13 @@
<%= link("Click here to enable weekly email reports →", to: "/#{URI.encode_www_form(@site.domain)}/settings/email-reports", class: "py-2 block") %>
</div>
<% end %>
+
+ <%= if @site.locked do %>
+ <div class="w-full px-4 py-4 text-sm font-bold text-center text-yellow-800 bg-yellow-100 rounded transition" style="top: 91px" role="alert">
+ <p>This dashboard is actually locked. You are viewing it with super-admin access</p>
+ </div>
+ <% end %>
+
<div class="pt-6"></div>
<div id="stats-react-container" style="overflow-anchor: none;" data-domain="<%= @site.domain %>" data-offset="<%= Timex.Timezone.total_offset(Timex.Timezone.get(@site.timezone)) %>" data-has-goals="<%= @has_goals %>" data-logged-in="<%= [email protected][:current_user] %>" data-inserted-at="<%= @site.inserted_at %>" data-shared-link-auth="<%= assigns[:shared_link_auth] %>" data-embedded="<%= @conn.assigns[:embedded] %>" data-background="<%= @conn.assigns[:background] %>" data-selfhosted="<%= Application.get_env(:plausible, :is_selfhost) %>" data-current-user-role="<%= @conn.assigns[:current_user_role] %>"></div>
<div id="modal_root"></div>
diff --git a/lib/plausible_web/templates/stats/waiting_first_pageview.html.eex b/lib/plausible_web/templates/stats/waiting_first_pageview.html.eex
index 91c896633f..ea34a30efc 100644
--- a/lib/plausible_web/templates/stats/waiting_first_pageview.html.eex
+++ b/lib/plausible_web/templates/stats/waiting_first_pageview.html.eex
@@ -14,6 +14,11 @@
</script>
<div class="w-full max-w-md mx-auto mt-8">
+ <%= if @site.locked do %>
+ <div class="w-full px-4 py-4 text-sm font-bold text-center text-yellow-800 bg-yellow-100 rounded transition" style="top: 91px" role="alert">
+ <p>This dashboard is actually locked. You are viewing it with super-admin access</p>
+ </div>
+ <% end %>
<div class="bg-white dark:bg-gray-800 shadow-md rounded px-8 pt-6 pb-8 mb-4 mt-16 relative text-center">
<h2 class="text-xl font-bold dark:text-gray-100">Waiting for first pageview</h2>
<h2 class="text-xl font-bold dark:text-gray-100">on <%= @site.domain %></h2>
diff --git a/test/plausible_web/controllers/api/external_stats_controller/auth_test.exs b/test/plausible_web/controllers/api/external_stats_controller/auth_test.exs
index 6f40b52c1f..f9ffd3a86e 100644
--- a/test/plausible_web/controllers/api/external_stats_controller/auth_test.exs
+++ b/test/plausible_web/controllers/api/external_stats_controller/auth_test.exs
@@ -66,7 +66,7 @@ defmodule PlausibleWeb.Api.ExternalStatsController.AuthTest do
end
test "can access as an admin", %{conn: conn, user: user, api_key: api_key} do
- Application.put_env(:plausible, :admin_user_ids, [user.id])
+ Application.put_env(:plausible, :super_admin_user_ids, [user.id])
site = insert(:site)
conn =
diff --git a/test/plausible_web/controllers/stats_controller_test.exs b/test/plausible_web/controllers/stats_controller_test.exs
index 5ef6b7b5b6..dfd5d391e2 100644
--- a/test/plausible_web/controllers/stats_controller_test.exs
+++ b/test/plausible_web/controllers/stats_controller_test.exs
@@ -39,11 +39,62 @@ defmodule PlausibleWeb.StatsControllerTest do
end
test "can not view stats of someone else's website", %{conn: conn} do
- conn = get(conn, "/some-other-site.com")
+ site = insert(:site)
+ conn = get(conn, site.domain)
assert html_response(conn, 404) =~ "There&#39;s nothing here"
end
end
+ describe "GET /:website - as a super admin" do
+ setup [:create_user, :make_user_super_admin, :log_in]
+
+ test "can view a private dashboard with stats", %{conn: conn} do
+ site = insert(:site)
+ populate_stats(site, [build(:pageview)])
+
+ conn = get(conn, "/" <> site.domain)
+ assert html_response(conn, 200) =~ "stats-react-container"
+ end
+
+ test "can view a private dashboard without stats", %{conn: conn} do
+ site = insert(:site)
+
+ conn = get(conn, "/" <> site.domain)
+ assert html_response(conn, 200) =~ "Need to see the snippet again?"
+ end
+
+ test "can view a private locked dashboard with stats", %{conn: conn} do
+ user = insert(:user)
+ site = insert(:site, locked: true, members: [user])
+ populate_stats(site, [build(:pageview)])
+
+ conn = get(conn, "/" <> site.domain)
+ assert html_response(conn, 200) =~ "stats-react-container"
+ assert html_response(conn, 200) =~ "This dashboard is actually locked"
+ end
+
+ test "can view a private locked dashboard without stats", %{conn: conn} do
+ user = insert(:user)
+ site = insert(:site, locked: true, members: [user])
+
+ conn = get(conn, "/" <> site.domain)
+ assert html_response(conn, 200) =~ "Need to see the snippet again?"
+ assert html_response(conn, 200) =~ "This dashboard is actually locked"
+ end
+
+ test "can view a locked public dashboard", %{conn: conn} do
+ site = insert(:site, locked: true, public: true)
+ populate_stats(site, [build(:pageview)])
+
+ conn = get(conn, "/" <> site.domain)
+ assert html_response(conn, 200) =~ "stats-react-container"
+ end
+ end
+
+ defp make_user_super_admin(%{user: user}) do
+ Application.put_env(:plausible, :super_admin_user_ids, [user.id])
+ end
+
describe "GET /:website/export" do
setup [:create_user, :create_new_site, :log_in]