From 6b22806e1fa6b07771e1a8e9bed8cee5c5712e33 Mon Sep 17 00:00:00 2001 From: RobertJoonas <56999674+RobertJoonas@users.noreply.github.com> Date: Wed, 23 Feb 2022 21:48:33 +0200 Subject: [PATCH] Allow admin access to locked dashboards (#1710) * added super-admin access to locked dashboards * fixed formatting --- assets/js/dashboard/site-switcher.js | 2 +- config/runtime.exs | 4 +- lib/plausible/auth/auth.ex | 6 +++ .../controllers/site_controller.ex | 2 +- .../controllers/stats_controller.ex | 5 +- .../plugs/authorize_site_access.ex | 12 ++--- .../plugs/authorize_stats_api.ex | 8 +-- lib/plausible_web/plugs/crm_auth_plug.ex | 4 +- .../templates/stats/stats.html.eex | 7 +++ .../stats/waiting_first_pageview.html.eex | 5 ++ .../external_stats_controller/auth_test.exs | 2 +- .../controllers/stats_controller_test.exs | 53 ++++++++++++++++++- 12 files changed, 85 insertions(+), 25 deletions(-) diff --git a/assets/js/dashboard/site-switcher.js b/assets/js/dashboard/site-switcher.js index 56111eede..6f2fbd841 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 (
diff --git a/config/runtime.exs b/config/runtime.exs index 64a6054f7..91a80a502 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 3dd825048..cb12997ca 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 c8abc9dce..068cd3b04 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 8c87c612a..9352a9e1b 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 2b1ccd7e0..9d5987e86 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 d98e3ae2a..b7515cc14 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 2c42600e8..fa68864d8 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 58c4e108b..a47aac150 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") %>
<% end %> + + <%= if @site.locked do %> + + <% end %> +
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 91c896633..ea34a30ef 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 @@
+ <%= if @site.locked do %> + + <% end %>

Waiting for first pageview

on <%= @site.domain %>

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 6f40b52c1..f9ffd3a86 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 5ef6b7b5b..dfd5d391e 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'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]