From 5f62025176a417d160ad3db3256cfe08ce06beed Mon Sep 17 00:00:00 2001 From: Uku Taht Date: Wed, 29 Jan 2020 11:29:11 +0200 Subject: [PATCH] Shared links (#29) * Create shared links UI * Show shared links in a list on settings page * Show dropdown for each shared link * Show icon actions for shared links * Log user in when they click non-password-protected shared link * Actually authenticate using a password * Delete shared links --- assets/css/app.css | 8 +++ lib/plausible/site/shared_link.ex | 30 +++++++++++ .../controllers/site_controller.ex | 31 +++++++++++ .../controllers/stats_controller.ex | 51 +++++++++++++++++++ lib/plausible_web/router.ex | 7 +++ .../templates/layout/_svg_icons.html.eex | 6 ++- .../templates/site/new_shared_link.html.eex | 14 +++++ .../templates/site/settings.html.eex | 37 ++++++++++++-- .../stats/shared_link_password.html.eex | 17 +++++++ lib/plausible_web/views/site_view.ex | 4 ++ mix.exs | 3 +- mix.lock | 1 + .../20200122150130_add_shared_links.exs | 13 +++++ .../controllers/site_controller_test.exs | 47 +++++++++++++++++ .../controllers/stats_controller_test.exs | 44 ++++++++++++++++ test/support/factory.ex | 6 +++ 16 files changed, 311 insertions(+), 8 deletions(-) create mode 100644 lib/plausible/site/shared_link.ex create mode 100644 lib/plausible_web/templates/site/new_shared_link.html.eex create mode 100644 lib/plausible_web/templates/stats/shared_link_password.html.eex create mode 100644 priv/repo/migrations/20200122150130_add_shared_links.exs diff --git a/assets/css/app.css b/assets/css/app.css index c3d92b837..0ecc15b44 100644 --- a/assets/css/app.css +++ b/assets/css/app.css @@ -217,6 +217,14 @@ a { height: 1em; width: 1em; transform: translateY(0.15em); + overflow: visible; +} + +.feather-sm { + height: 1em; + width: 1em; + transform: translateY(0.05em); + overflow: visible; } .table-striped tbody tr:nth-child(odd) { diff --git a/lib/plausible/site/shared_link.ex b/lib/plausible/site/shared_link.ex new file mode 100644 index 000000000..c8985aad5 --- /dev/null +++ b/lib/plausible/site/shared_link.ex @@ -0,0 +1,30 @@ +defmodule Plausible.Site.SharedLink do + use Ecto.Schema + import Ecto.Changeset + + schema "shared_links" do + belongs_to :site, Plausible.Site + field :slug, :string + field :password_hash, :string + field :password, :string, virtual: true + + timestamps() + end + + def changeset(link, attrs \\ %{}) do + link + |> cast(attrs, [:slug, :password]) + |> validate_required([:slug]) + |> unique_constraint(:slug) + |> hash_password() + end + + defp hash_password(link) do + case link.changes[:password] do + nil -> link + password -> + hash = Plausible.Auth.Password.hash(password) + change(link, password_hash: hash) + end + end +end diff --git a/lib/plausible_web/controllers/site_controller.ex b/lib/plausible_web/controllers/site_controller.ex index 8b50e033c..caa81e049 100644 --- a/lib/plausible_web/controllers/site_controller.ex +++ b/lib/plausible_web/controllers/site_controller.ex @@ -83,6 +83,7 @@ defmodule PlausibleWeb.SiteController do weekly_report = Repo.get_by(Plausible.Site.WeeklyReport, site_id: site.id) monthly_report = Repo.get_by(Plausible.Site.MonthlyReport, site_id: site.id) goals = Goals.for_site(site.domain) + shared_links = Repo.all(from l in Plausible.Site.SharedLink, where: l.site_id == ^site.id) conn |> assign(:skip_plausible_tracking, true) @@ -92,6 +93,7 @@ defmodule PlausibleWeb.SiteController do monthly_report: monthly_report, search_console_domains: search_console_domains, goals: goals, + shared_links: shared_links, changeset: Plausible.Site.changeset(site, %{}) ) end @@ -258,6 +260,35 @@ defmodule PlausibleWeb.SiteController do |> redirect(to: "/#{site.domain}/settings#email-reports") end + def new_shared_link(conn, %{"website" => website}) do + site = Sites.get_for_user!(conn.assigns[:current_user].id, website) + changeset = Plausible.Site.SharedLink.changeset(%Plausible.Site.SharedLink{}, %{}) + + render(conn, "new_shared_link.html", site: site, changeset: changeset, layout: {PlausibleWeb.LayoutView, "focus.html"}) + end + + def create_shared_link(conn, %{"website" => website, "shared_link" => link}) do + site = Sites.get_for_user!(conn.assigns[:current_user].id, website) + changes = Plausible.Site.SharedLink.changeset(%Plausible.Site.SharedLink{ + site_id: site.id, + slug: Nanoid.generate() + }, link) + + case Repo.insert(changes) do + {:ok, _created} -> + redirect(conn, to: "/#{site.domain}/settings#visibility") + {:error, changeset} -> + render(conn, "new_shared_link.html", site: site, changeset: changeset, layout: {PlausibleWeb.LayoutView, "focus.html"}) + end + end + + def delete_shared_link(conn, %{"website" => website, "slug" => slug}) do + Repo.get_by(Plausible.Site.SharedLink, slug: slug) + |> Repo.delete! + + redirect(conn, to: "/#{website}/settings#visibility") + end + defp insert_site(user_id, params) do site_changeset = Plausible.Site.changeset(%Plausible.Site{}, params) diff --git a/lib/plausible_web/controllers/stats_controller.ex b/lib/plausible_web/controllers/stats_controller.ex index 9f98c6541..d73b88594 100644 --- a/lib/plausible_web/controllers/stats_controller.ex +++ b/lib/plausible_web/controllers/stats_controller.ex @@ -59,11 +59,62 @@ defmodule PlausibleWeb.StatsController do end end + def shared_link(conn, %{"slug" => slug}) do + shared_link = Repo.get_by(Plausible.Site.SharedLink, slug: slug) + |> Repo.preload(:site) + + if shared_link do + if shared_link.password_hash do + render(conn, "shared_link_password.html", link: shared_link, layout: {PlausibleWeb.LayoutView, "focus.html"}) + else + shared_link_auth_success(conn, shared_link) + end + else + render_error(conn, 404) + end + end + + def authenticate_shared_link(conn, %{"slug" => slug, "password" => password}) do + shared_link = Repo.get_by(Plausible.Site.SharedLink, slug: slug) + |> Repo.preload(:site) + + if shared_link do + if Plausible.Auth.Password.match?(password, shared_link.password_hash) do + shared_link_auth_success(conn, shared_link) + else + render(conn, "shared_link_password.html", link: shared_link, error: "Incorrect password. Please try again.", layout: {PlausibleWeb.LayoutView, "focus.html"}) + end + else + render_error(conn, 404) + end + end + + defp shared_link_auth_success(conn, shared_link) do + site_session_key = "authorized_site__" <> shared_link.site.domain + + conn + |> put_session(site_session_key, %{ + id: shared_link.site.id, + domain: shared_link.site.domain, + timezone: shared_link.site.timezone, + valid_until: Timex.now() |> Timex.shift(minutes: 30) |> DateTime.to_unix() + }) + |> redirect(to: "/#{shared_link.site.domain}") + end + defp current_user_can_access?(_conn, %Plausible.Site{public: true}) do true end defp current_user_can_access?(conn, site) do + site_session_key = "authorized_site__" <> site.domain + site_session = get_session(conn, site_session_key) + valid_site_session = site_session && site_session[:valid_until] > DateTime.to_unix(Timex.now()) + + valid_site_session || current_user_is_owner?(conn, site) + end + + defp current_user_is_owner?(conn, site) do case conn.assigns[:current_user] do nil -> false user -> Plausible.Sites.is_owner?(user.id, site) diff --git a/lib/plausible_web/router.ex b/lib/plausible_web/router.ex index 3d5806d81..cb73832ec 100644 --- a/lib/plausible_web/router.ex +++ b/lib/plausible_web/router.ex @@ -109,9 +109,14 @@ defmodule PlausibleWeb.Router do post "/sites/:website/monthly-report/recipients", SiteController, :add_monthly_report_recipient delete "/sites/:website/monthly-report/recipients/:recipient", SiteController, :remove_monthly_report_recipient + get "/sites/:website/shared-links/new", SiteController, :new_shared_link + post "/sites/:website/shared-links", SiteController, :create_shared_link + delete "/sites/:website/shared-links/:slug", SiteController, :delete_shared_link + get "/sites/:website/weekly-report/unsubscribe", UnsubscribeController, :weekly_report get "/sites/:website/monthly-report/unsubscribe", UnsubscribeController, :monthly_report + get "/:website/snippet", SiteController, :add_snippet get "/:website/settings", SiteController, :settings get "/:website/goals", SiteController, :goals @@ -122,6 +127,8 @@ defmodule PlausibleWeb.Router do put "/:website/settings/google", SiteController, :update_google_auth delete "/:website", SiteController, :delete_site + get "/share/:slug", StatsController, :shared_link + post "/share/:slug/authenticate", StatsController, :authenticate_shared_link get "/:website/visitors.csv", StatsController, :csv_export get "/:website/*path", StatsController, :stats end diff --git a/lib/plausible_web/templates/layout/_svg_icons.html.eex b/lib/plausible_web/templates/layout/_svg_icons.html.eex index 51cbfbe86..2f0c42eab 100644 --- a/lib/plausible_web/templates/layout/_svg_icons.html.eex +++ b/lib/plausible_web/templates/layout/_svg_icons.html.eex @@ -18,12 +18,14 @@ - - + + + + diff --git a/lib/plausible_web/templates/site/new_shared_link.html.eex b/lib/plausible_web/templates/site/new_shared_link.html.eex new file mode 100644 index 000000000..f9146fd67 --- /dev/null +++ b/lib/plausible_web/templates/site/new_shared_link.html.eex @@ -0,0 +1,14 @@ +<%= form_for @changeset, "/sites/#{@site.domain}/shared-links", [class: "max-w-sm w-full mx-auto bg-white shadow-md rounded px-8 pt-6 pb-8 mb-4 mt-8"], fn f -> %> +

New shared link

+
+ Add a password or leave it blank so anyone with the link can see the stats. + Once the link is created, we cannot reveal the password. Please make sure you save it in a secure place. +
+
+ <%= label f, :password, "Password (optional)", class: "block text-sm font-bold" %> + <%= password_input f, :password, class: "transition mt-3 bg-grey-lighter appearance-none border border-transparent rounded w-full p-2 text-grey-darker leading-normal appearance-none focus:outline-none focus:bg-white focus:border-grey-light" %> + <%= error_tag f, :password %> +
+ + <%= submit "Create shared link", class: "button mt-4 w-full" %> +<% end %> diff --git a/lib/plausible_web/templates/site/settings.html.eex b/lib/plausible_web/templates/site/settings.html.eex index 67b6ec31b..fdf2d3d16 100644 --- a/lib/plausible_web/templates/site/settings.html.eex +++ b/lib/plausible_web/templates/site/settings.html.eex @@ -28,7 +28,7 @@ <% end %> -
+

Visibility

@@ -36,10 +36,12 @@
<%= if @site.public do %> Stats for <%= @site.domain %> are currently public. Anyone with the following link can view the stats: - +
+ + + + +
<%= button("Make stats private", to: "/sites/#{@site.domain}/make-private", method: "POST", class: "button mt-8") %> <% else %>
@@ -48,6 +50,31 @@
<%= button("Make stats public", to: "/sites/#{@site.domain}/make-public", method: "POST", class: "button mt-8") %> <% end %> + +
+

Shared links

+ +
+ +
+ You can share your stats privately by generating a shared link. The links are impossible to guess and + you can add password protection for extra security. +
+ + <%= for link <- @shared_links do %> +
+ + + <%= button(to: "/sites/#{@site.domain}/shared-links/#{link.slug}", method: :delete, class: "py-2 px-4 bg-grey-lighter text-red-dark rounded-l-none", data: [confirm: "Are you sure you want to delete this shared link? The stats will not be accessible with this link anymore."]) do %> + + <% end %> +
+ <% end %> +
+ + <%= link("+ New link", to: "/sites/#{@site.domain}/shared-links/new", class: "button mt-4") %>
diff --git a/lib/plausible_web/templates/stats/shared_link_password.html.eex b/lib/plausible_web/templates/stats/shared_link_password.html.eex new file mode 100644 index 000000000..eba0778e2 --- /dev/null +++ b/lib/plausible_web/templates/stats/shared_link_password.html.eex @@ -0,0 +1,17 @@ +<%= form_for @conn, "/share/#{@link.slug}/authenticate", [class: "max-w-sm w-full mx-auto bg-white shadow-md rounded px-8 pt-6 pb-8 mb-4 mt-8"], fn f -> %> +

Enter password

+
+ This link is password-protected. Please enter the password to continue to the dashboard. +
+ +
+ <%= label f, :password, "Password", class: "block text-sm font-bold" %> + <%= password_input f, :password, class: "transition mt-3 bg-grey-lighter appearance-none border border-transparent rounded w-full p-2 text-grey-darker leading-normal appearance-none focus:outline-none focus:bg-white focus:border-grey-light" %> + + <%= if @conn.assigns[:error] do %> +
<%= @conn.assigns[:error] %>
+ <% end %> +
+ + <%= submit "Continue", class: "button mt-4 w-full" %> +<% end %> diff --git a/lib/plausible_web/views/site_view.ex b/lib/plausible_web/views/site_view.ex index be6c4258b..b00243fbf 100644 --- a/lib/plausible_web/views/site_view.ex +++ b/lib/plausible_web/views/site_view.ex @@ -9,6 +9,10 @@ defmodule PlausibleWeb.SiteView do name end + def shared_link_dest(link) do + PlausibleWeb.Endpoint.url() <> "/share/" <> link.slug + end + def snippet() do """