Skip to content

Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.

Delete teams along with users on DELETE /me (#4734)

hq1c1a6501ec1d2
diff --git a/lib/plausible/auth/auth.ex b/lib/plausible/auth/auth.ex
index ba1ecae1c2..276dec2136 100644
--- a/lib/plausible/auth/auth.ex
+++ b/lib/plausible/auth/auth.ex
@@ -107,6 +107,8 @@ defmodule Plausible.Auth do
end
end
+ {:ok, team} = Plausible.Teams.get_or_create(user)
+ Repo.delete!(team)
Repo.delete!(user)
end)
end
diff --git a/lib/plausible/data_migration/backfill_teams.ex b/lib/plausible/data_migration/backfill_teams.ex
index 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 do
end
defp 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 do
log("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) do
sites
|> 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.exs
index 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 do
site_limit: 1
)
+ {:ok, _team} = Plausible.Teams.get_or_create(user)
+
conn = delete(conn, "/me")
assert redirected_to(conn) == "/"
assert Repo.reload(site) == nil
assert Repo.reload(user) == nil
assert Repo.all(Plausible.Billing.Subscription) == []
+ assert Repo.all(Plausible.Teams.Team) == []
end
test "deletes sites that the user owns", %{conn: conn, user: user, site: owner_site} do