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
CRM: revert #5440; implement lock/unlock for grace period (#5475)
hq1df8727f4140b
diff --git a/extra/lib/plausible_web/live/customer_support/live/site.ex b/extra/lib/plausible_web/live/customer_support/live/site.exindex 9981045775..37ab13a027 100644--- a/extra/lib/plausible_web/live/customer_support/live/site.ex+++ b/extra/lib/plausible_web/live/customer_support/live/site.ex@@ -79,13 +79,6 @@ defmodule PlausibleWeb.CustomerSupport.Live.Site do<.styled_link patch={"/cs/teams/team/#{@site.team.id}"}>{@site.team.name}</.styled_link>-- <span- :if={Plausible.Teams.locked?(@site.team)}- class="inline-flex items-center px-2 py-0.5 rounded text-xs font-medium bg-red-100 text-red-800"- >- Locked- </span></p><p class="text-sm font-medium"><span :if={@site.domain_changed_from}>(previously: {@site.domain_changed_from})</span>@@ -269,10 +262,6 @@ defmodule PlausibleWeb.CustomerSupport.Live.Site do<span class="inline-flex items-center px-2 py-0.5 rounded text-xs font-medium bg-green-100 text-green-800">Site</span>-- <span :if={Plausible.Teams.locked?(@resource.object.team)} class="inline-flex items-center">- <Heroicons.lock_closed solid class="inline stroke-2 w-4 h-4 text-red-400 mr-2" />- </span></div><hr class="mt-4 mb-4 flex-grow border-t border-gray-200 dark:border-gray-600" />diff --git a/extra/lib/plausible_web/live/customer_support/live/team.ex b/extra/lib/plausible_web/live/customer_support/live/team.exindex e59f8d14ca..2ca4fefa83 100644--- a/extra/lib/plausible_web/live/customer_support/live/team.ex+++ b/extra/lib/plausible_web/live/customer_support/live/team.ex@@ -140,19 +140,6 @@ defmodule PlausibleWeb.CustomerSupport.Live.Team do</div></div>- <span :if={Teams.locked?(@team)} class="flex items-center">- <Heroicons.lock_closed solid class="inline stroke-2 w-4 h-4 text-red-400 mr-2" />- <.styled_link id="unlock-dashboards" phx-click="unlock" phx-target={@myself}>- Unlock Dashboards- </.styled_link>- </span>-- <span :if={not Teams.locked?(@team)} class="flex items-center font-xs">- <Heroicons.lock_open class="inline stroke-2 w-4 h-4 text-gray-800 mr-2" />- <.styled_link id="lock-dashboards" phx-click="lock" phx-target={@myself}>- Lock Dashboards- </.styled_link>- </span><div class="mt-5 flex justify-center sm:mt-0"><.input_with_clipboardid="team-identifier"@@ -199,6 +186,20 @@ defmodule PlausibleWeb.CustomerSupport.Live.Team do<div class="px-6 py-5 text-center text-sm font-medium"><span><strong>Grace Period</strong> <br />{grace_period_status(@team)}++ <div :if={@team.grace_period}>+ <span class="flex items-center gap-x-8 justify-center mt-1">+ <div>+ <Heroicons.lock_closed solid class="inline stroke-2 w-4 h-4 text-red-400 mr-1" />+ <.styled_link phx-click="unlock" phx-target={@myself}>Unlock</.styled_link>+ </div>++ <div>+ <Heroicons.lock_closed solid class="inline stroke-2 w-4 h-4 text-red-400 mr-1" />+ <.styled_link phx-click="lock" phx-target={@myself}>Lock</.styled_link>+ </div>+ </span>+ </div></span></div></div>@@ -501,10 +502,10 @@ defmodule PlausibleWeb.CustomerSupport.Live.Team dodef render_result(assigns) do~H"""<div class="flex-1 -mt-px w-full">- <div class="w-full flex items-center justify-between space-x-2">+ <div class="w-full flex items-center justify-between space-x-4"><div class={[team_bg(@resource.object.identifier),- "rounded-full p-1 flex items-center justify-center mr-2"+ "rounded-full p-1 flex items-center justify-center"]}><Heroicons.user_group class="h-4 w-4 text-white" /></div>@@ -525,10 +526,6 @@ defmodule PlausibleWeb.CustomerSupport.Live.Team do>$</span>-- <span :if={Teams.locked?(@resource.object)} class="inline-flex items-center">- <Heroicons.lock_closed solid class="inline stroke-2 w-4 h-4 text-red-400 mr-2" />- </span></div><hr class="mt-4 mb-4 flex-grow border-t border-gray-200 dark:border-gray-600" />@@ -762,15 +759,28 @@ defmodule PlausibleWeb.CustomerSupport.Live.Team doenddefp lock_team(socket) do- team = Teams.admin_lock!(socket.assigns.team)- success(socket, "Team locked")- assign(socket, team: team)+ if socket.assigns.team.grace_period do+ team = Plausible.Teams.end_grace_period(socket.assigns.team)+ Plausible.Billing.SiteLocker.set_lock_status_for(team, true)++ success(socket, "Team locked. Grace period ended.")+ assign(socket, team: team)+ else+ failure(socket, "No grace period")+ socket+ endenddefp unlock_team(socket) do- team = Teams.admin_unlock!(socket.assigns.team)- success(socket, "Team unlocked")- assign(socket, team: team)+ if socket.assigns.team.grace_period do+ team = Plausible.Teams.remove_grace_period(socket.assigns.team)+ Plausible.Billing.SiteLocker.set_lock_status_for(team, false)++ success(socket, "Team unlocked. Grace period removed.")+ assign(socket, team: team)+ else+ socket+ endenddefp monthly_pageviews_usage(usage, limit) dodiff --git a/lib/plausible/teams.ex b/lib/plausible/teams.exindex 6349b0cc01..a7023051fe 100644--- a/lib/plausible/teams.ex+++ b/lib/plausible/teams.ex@@ -69,40 +69,7 @@ defmodule Plausible.Teams do@spec locked?(Teams.Team.t() | nil) :: boolean()def locked?(nil), do: false- def locked?(%Teams.Team{locked: locked, locked_by_admin: locked_by_admin}),- do: locked or locked_by_admin-- def admin_lock!(%Teams.Team{} = team) do- {:ok, team} =- Repo.transaction(fn ->- if team.grace_period do- Plausible.Billing.SiteLocker.set_lock_status_for(team, true)- Plausible.Teams.end_grace_period(team)- else- team- end- |> Ecto.Changeset.change(locked_by_admin: true)- |> Repo.update!()- end)-- team- end-- def admin_unlock!(%Teams.Team{} = team) do- {:ok, team} =- Repo.transaction(fn ->- if team.grace_period do- Plausible.Billing.SiteLocker.set_lock_status_for(team, false)- Plausible.Teams.remove_grace_period(team)- else- team- end- |> Ecto.Changeset.change(locked_by_admin: false)- |> Repo.update!()- end)-- team- end+ def locked?(%Teams.Team{locked: locked}), do: locked@spec trial_days_left(Teams.Team.t()) :: integer()def trial_days_left(nil) dodiff --git a/lib/plausible/teams/team.ex b/lib/plausible/teams/team.exindex 126dfbe2ad..8ba1fa23e0 100644--- a/lib/plausible/teams/team.ex+++ b/lib/plausible/teams/team.ex@@ -28,7 +28,6 @@ defmodule Plausible.Teams.Team dofield :accept_traffic_until, :datefield :allow_next_upgrade_override, :boolean, default: falsefield :locked, :boolean, default: false- field :locked_by_admin, :boolean, default: falsefield :setup_complete, :boolean, default: falsefield :setup_at, :naive_datetimediff --git a/lib/plausible_web/templates/layout/_notice.html.heex b/lib/plausible_web/templates/layout/_notice.html.heexindex d0728020ec..969c3d1364 100644--- a/lib/plausible_web/templates/layout/_notice.html.heex+++ b/lib/plausible_web/templates/layout/_notice.html.heex@@ -9,7 +9,7 @@grace_period_end={grace_period_end(@current_team)}/>- <Notice.dashboard_locked :if={Plausible.Teams.locked?(@current_team)} />+ <Notice.dashboard_locked :if={Plausible.Teams.GracePeriod.expired?(@current_team)} /><Notice.subscription_cancelled subscription={@current_team.subscription} />diff --git a/lib/workers/schedule_email_reports.ex b/lib/workers/schedule_email_reports.exindex c5f8ef50fc..1551618741 100644--- a/lib/workers/schedule_email_reports.ex+++ b/lib/workers/schedule_email_reports.ex@@ -35,7 +35,7 @@ defmodule Plausible.Workers.ScheduleEmailReports dofragment("(? -> 'site_id')::int", job.args) == s.id andjob.state not in ["completed", "discarded"],where: is_nil(job),- where: not t.locked and not t.locked_by_admin,+ where: not t.locked,preload: [weekly_report: wr])@@ -76,7 +76,7 @@ defmodule Plausible.Workers.ScheduleEmailReports dofragment("(? -> 'site_id')::int", job.args) == s.id andjob.state not in ["completed", "discarded"],where: is_nil(job),- where: not t.locked and not t.locked_by_admin,+ where: not t.locked,preload: [monthly_report: mr])diff --git a/lib/workers/traffic_change_notifier.ex b/lib/workers/traffic_change_notifier.exindex 1fe6ba909d..02f93d18a1 100644--- a/lib/workers/traffic_change_notifier.ex+++ b/lib/workers/traffic_change_notifier.ex@@ -23,7 +23,7 @@ defmodule Plausible.Workers.TrafficChangeNotifier dosn.last_sent < ^NaiveDateTime.add(now, -@min_interval_hours, :hour),inner_join: s in assoc(sn, :site),inner_join: t in assoc(s, :team),- where: not t.locked and not t.locked_by_admin,+ where: not t.locked,where: is_nil(t.accept_traffic_until) or t.accept_traffic_until > ^today,preload: [site: {s, team: t}])diff --git a/test/plausible_web/live/customer_support/teams_test.exs b/test/plausible_web/live/customer_support/teams_test.exsindex 660d28ed09..1685737511 100644--- a/test/plausible_web/live/customer_support/teams_test.exs+++ b/test/plausible_web/live/customer_support/teams_test.exs@@ -8,8 +8,6 @@ defmodule PlausibleWeb.Live.CustomerSupport.TeamsTest doimport Phoenix.LiveViewTestimport Plausible.Test.Support.HTML- alias Plausible.Teams-defp open_team(id, qs \\ []) doRoutes.customer_support_resource_path(PlausibleWeb.Endpoint,@@ -34,31 +32,38 @@ defmodule PlausibleWeb.Live.CustomerSupport.TeamsTest doassert text(html) =~ team.nameend- test "404", %{conn: conn} do- assert_raise Ecto.NoResultsError, fn ->- {:ok, _lv, _html} = live(conn, open_team(9999))- end- end-- test "lock/unlock a team", %{user: user, conn: conn} do+ test "grace period handling", %{conn: conn, user: user} doteam = team_of(user)+ {:ok, _, html} = live(conn, open_team(team.id))+ refute text(html) =~ "Lock"+ refute text(html) =~ "Unlock"++ Plausible.Teams.start_grace_period(team)+{:ok, lv, html} = live(conn, open_team(team.id))- refute element_exists?(html, "#unlock-dashboards")- lv |> element("#lock-dashboards") |> render_click()- html = render(lv)+ assert element_exists?(html, ~s|a[phx-click="lock"]|)+ assert element_exists?(html, ~s|a[phx-click="unlock"]|)- assert text(html) =~ "Team locked"- assert Teams.locked?(Plausible.Repo.reload!(team))+ refute Plausible.Repo.reload!(team).locked- refute element_exists?(html, "#lock-dashboards")- assert element_exists?(html, "#unlock-dashboards")+ lv |> element(~s|a[phx-click="lock"]|) |> render_click()- lv |> element("#unlock-dashboards") |> render_click()- html = render(lv)+ team = Plausible.Repo.reload!(team)+ assert team.locked+ assert team.grace_period.is_over++ lv |> element(~s|a[phx-click="unlock"]|) |> render_click()++ team = Plausible.Repo.reload!(team)+ refute team.locked+ refute team.grace_period+ end- assert text(html) =~ "Team unlocked"- refute Teams.locked?(Plausible.Repo.reload!(team))+ test "404", %{conn: conn} do+ assert_raise Ecto.NoResultsError, fn ->+ {:ok, _lv, _html} = live(conn, open_team(9999))+ endendenddiff --git a/test/workers/schedule_email_reports_test.exs b/test/workers/schedule_email_reports_test.exsindex 612bd5f479..211611b860 100644--- a/test/workers/schedule_email_reports_test.exs+++ b/test/workers/schedule_email_reports_test.exs@@ -38,16 +38,6 @@ defmodule Plausible.Workers.ScheduleEmailReportsTest doassert Enum.empty?(all_enqueued(worker: SendEmailReport))end- test "does not schedule a weekly report for admin-locked site" do- site = new_site(domain: "test-site.com", timezone: "US/Eastern")- Plausible.Teams.admin_lock!(site.team)- insert(:weekly_report, site: site, recipients: ["[email protected]"])-- perform_job(ScheduleEmailReports, %{})-- assert Enum.empty?(all_enqueued(worker: SendEmailReport))- end-test "schedules a new report as soon as a previous one is completed" dosite = new_site(domain: "test-site.com", timezone: "US/Eastern")insert(:weekly_report, site: site, recipients: ["[email protected]"])@@ -94,16 +84,6 @@ defmodule Plausible.Workers.ScheduleEmailReportsTest doassert Enum.empty?(all_enqueued(worker: SendEmailReport))end- test "does not schedule a monthly report for admin-locked site" do- site = new_site(domain: "test-site.com", timezone: "US/Eastern")- Plausible.Teams.admin_lock!(site.team)- insert(:monthly_report, site: site, recipients: ["[email protected]"])-- perform_job(ScheduleEmailReports, %{})-- assert Enum.empty?(all_enqueued(worker: SendEmailReport))- end-test "schedules a new report as soon as a previous one is completed" dosite = new_site(domain: "test-site.com", timezone: "US/Eastern")insert(:monthly_report, site: site, recipients: ["[email protected]"])diff --git a/test/workers/traffic_change_notifier_test.exs b/test/workers/traffic_change_notifier_test.exsindex 92554abe97..e602bfa00b 100644--- a/test/workers/traffic_change_notifier_test.exs+++ b/test/workers/traffic_change_notifier_test.exs@@ -288,27 +288,6 @@ defmodule Plausible.Workers.TrafficChangeNotifierTest doassert_no_emails_delivered()end- test "does not check site if it is admin-locked" do- site = new_site()-- Plausible.Teams.admin_lock!(site.team)-- insert(:spike_notification,- site: site,- threshold: 1,- recipients: ["[email protected]"]- )-- populate_stats(site, [- build(:pageview, timestamp: minutes_ago(1)),- build(:pageview, timestamp: minutes_ago(1))- ])-- TrafficChangeNotifier.perform(nil)-- assert_no_emails_delivered()- end-test "does not send notifications more than once every 12 hours" dosite = new_site()insert(:spike_notification, site: site, threshold: 1, recipients: ["[email protected]"])