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
Delete teams along with users on DELETE /me (#4734)
hq1c1a6501ec1d2
diff --git a/lib/plausible/auth/auth.ex b/lib/plausible/auth/auth.exindex ba1ecae1c2..276dec2136 100644--- a/lib/plausible/auth/auth.ex+++ b/lib/plausible/auth/auth.ex@@ -107,6 +107,8 @@ defmodule Plausible.Auth doendend+ {:ok, team} = Plausible.Teams.get_or_create(user)+ Repo.delete!(team)Repo.delete!(user)end)enddiff --git a/lib/plausible/data_migration/backfill_teams.ex b/lib/plausible/data_migration/backfill_teams.exindex 70cf839234..68647b3ad6 100644--- a/lib/plausible/data_migration/backfill_teams.ex+++ b/lib/plausible/data_migration/backfill_teams.ex@@ -31,6 +31,24 @@ defmodule Plausible.DataMigration.BackfillTeams doenddefp backfill() do+ orphaned_teams =+ from(+ t in Plausible.Teams.Team,+ left_join: tm in assoc(t, :team_memberships),+ where: is_nil(tm.id),+ left_join: sub in assoc(t, :subscription),+ where: is_nil(sub.id),+ left_join: s in assoc(t, :sites),+ where: is_nil(s.id)+ )+ |> @repo.all(timeout: :infinity)++ log("Found #{length(orphaned_teams)} orphaned teams...")++ delete_orphaned_teams(orphaned_teams)++ log("Deleted orphaned teams")+sites_without_teams =from(s in Plausible.Site,@@ -387,6 +405,10 @@ defmodule Plausible.DataMigration.BackfillTeams dolog("All data are up to date now!")end+ def delete_orphaned_teams(teams) do+ Enum.each(teams, &@repo.delete!(&1))+ end+defp backfill_teams(sites) dosites|> Enum.map(fn %{id: site_id, memberships: [%{user: owner, role: :owner}]} ->diff --git a/test/plausible_web/controllers/auth_controller_test.exs b/test/plausible_web/controllers/auth_controller_test.exsindex b678437157..8bfb495180 100644--- a/test/plausible_web/controllers/auth_controller_test.exs+++ b/test/plausible_web/controllers/auth_controller_test.exs@@ -591,11 +591,14 @@ defmodule PlausibleWeb.AuthControllerTest dosite_limit: 1)+ {:ok, _team} = Plausible.Teams.get_or_create(user)+conn = delete(conn, "/me")assert redirected_to(conn) == "/"assert Repo.reload(site) == nilassert Repo.reload(user) == nilassert Repo.all(Plausible.Billing.Subscription) == []+ assert Repo.all(Plausible.Teams.Team) == []endtest "deletes sites that the user owns", %{conn: conn, user: user, site: owner_site} do