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 @@