Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.
TypeScript analyticsanalytics-dashboardclickhouseelixirgoogle-analyticsmarketingopen-source-analyticsphoenixplausible-analyticspostgresqlprivacyprivacy-friendlyself-hostedsimple-analyticsstatisticstailwindcssweb-analyticswebsite-analyticswebsite-statswebsite-tracking
Wrap user deletion in DB transaction (#2964)
Uku Taht1a94456586a1
diff --git a/lib/plausible/auth/auth.ex b/lib/plausible/auth/auth.exindex e0c38ce74b..af84276bf1 100644--- a/lib/plausible/auth/auth.ex+++ b/lib/plausible/auth/auth.ex@@ -82,6 +82,26 @@ defmodule Plausible.Auth doEnum.any?(sites, &Plausible.Sites.has_stats?/1)end+ def delete_user(user) do+ Repo.transaction(fn ->+ user =+ user+ |> Repo.preload(site_memberships: :site)+ |> Repo.preload(:subscription)++ for membership <- user.site_memberships do+ Repo.delete!(membership)++ if membership.role == :owner do+ Plausible.Site.Removal.run(membership.site.domain)+ end+ end++ if user.subscription, do: Repo.delete!(user.subscription)+ Repo.delete!(user)+ end)+ end+def user_owns_sites?(user) doRepo.exists?(from(s in Plausible.Site,diff --git a/lib/plausible_web/controllers/auth_controller.ex b/lib/plausible_web/controllers/auth_controller.exindex 4b269937c7..2def542c3f 100644--- a/lib/plausible_web/controllers/auth_controller.ex+++ b/lib/plausible_web/controllers/auth_controller.ex@@ -533,21 +533,7 @@ defmodule PlausibleWeb.AuthController doenddef delete_me(conn, params) do- user =- conn.assigns[:current_user]- |> Repo.preload(site_memberships: :site)- |> Repo.preload(:subscription)-- for membership <- user.site_memberships do- Repo.delete!(membership)-- if membership.role == :owner do- Plausible.Site.Removal.run(membership.site.domain)- end- end-- if user.subscription, do: Repo.delete!(user.subscription)- Repo.delete!(user)+ Plausible.Auth.delete_user(conn.assigns[:current_user])logout(conn, params)enddiff --git a/test/plausible_web/controllers/auth_controller_test.exs b/test/plausible_web/controllers/auth_controller_test.exsindex 73514abe98..f7c888fa53 100644--- a/test/plausible_web/controllers/auth_controller_test.exs+++ b/test/plausible_web/controllers/auth_controller_test.exs@@ -692,6 +692,8 @@ defmodule PlausibleWeb.AuthControllerTest doconn = delete(conn, "/me")assert redirected_to(conn) == "/"+ assert Repo.reload(site) == nil+ assert Repo.reload(user) == nilendtest "deletes sites that the user owns", %{conn: conn, user: user, site: owner_site} do