Skip to content

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

CRM: team (un)lock regardless of grace period (#5440)

hq1bb63c0d0e4e6
diff --git a/extra/lib/plausible_web/live/customer_support/live/site.ex b/extra/lib/plausible_web/live/customer_support/live/site.ex
index 269965fdb0..bf716f9d8e 100644
--- a/extra/lib/plausible_web/live/customer_support/live/site.ex
+++ b/extra/lib/plausible_web/live/customer_support/live/site.ex
@@ -79,6 +79,13 @@ 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>
@@ -237,6 +244,10 @@ 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.ex
index 81a1a77cd1..a9a0b35344 100644
--- a/extra/lib/plausible_web/live/customer_support/live/team.ex
+++ b/extra/lib/plausible_web/live/customer_support/live/team.ex
@@ -139,17 +139,20 @@ defmodule PlausibleWeb.CustomerSupport.Live.Team do
</p>
</div>
</div>
- <div :if={@team.grace_period}>
- <span :if={@team.locked} class="flex items-center">
- <Heroicons.lock_closed solid class="inline stroke-2 w-4 h-4 text-red-400 mr-2" />
- <.styled_link phx-click="unlock" phx-target={@myself}>Unlock Team</.styled_link>
- </span>
-
- <span :if={[email protected]} class="flex items-center">
- <Heroicons.lock_open class="inline stroke-2 w-4 h-4 text-gray-800 mr-2" />
- <.styled_link phx-click="lock" phx-target={@myself}>Lock Team</.styled_link>
- </span>
- </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">
+ <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_clipboard
id="team-identifier"
@@ -498,10 +501,10 @@ defmodule PlausibleWeb.CustomerSupport.Live.Team do
def render_result(assigns) do
~H"""
<div class="flex-1 -mt-px w-full">
- <div class="w-full flex items-center justify-between space-x-4">
+ <div class="w-full flex items-center justify-between space-x-2">
<div class={[
team_bg(@resource.object.identifier),
- "rounded-full p-1 flex items-center justify-center"
+ "rounded-full p-1 flex items-center justify-center mr-2"
]}>
<Heroicons.user_group class="h-4 w-4 text-white" />
</div>
@@ -522,6 +525,10 @@ 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" />
@@ -755,31 +762,15 @@ defmodule PlausibleWeb.CustomerSupport.Live.Team do
end
defp lock_team(socket) do
- if socket.assigns.team.grace_period do
- team = socket.assigns.team
- Plausible.Billing.SiteLocker.set_lock_status_for(team, true)
- Plausible.Teams.end_grace_period(team)
-
- success(socket, "Team locked. Grace period ended.")
- assign(socket, team: team)
- else
- failure(socket, "No grace period")
- socket
- end
+ team = Teams.admin_lock!(socket.assigns.team)
+ success(socket, "Team locked")
+ assign(socket, team: team)
end
defp unlock_team(socket) do
- if socket.assigns.team.grace_period do
- team =
- socket.assigns.team
- |> Plausible.Teams.remove_grace_period()
- |> Plausible.Billing.SiteLocker.set_lock_status_for(false)
-
- success(socket, "Team unlocked. Grace period removed.")
- assign(socket, team: team)
- else
- socket
- end
+ team = Teams.admin_unlock!(socket.assigns.team)
+ success(socket, "Team unlocked")
+ assign(socket, team: team)
end
defp monthly_pageviews_usage(usage, limit) do
diff --git a/lib/plausible/teams.ex b/lib/plausible/teams.ex
index a7023051fe..6349b0cc01 100644
--- a/lib/plausible/teams.ex
+++ b/lib/plausible/teams.ex
@@ -69,7 +69,40 @@ defmodule Plausible.Teams do
@spec locked?(Teams.Team.t() | nil) :: boolean()
def locked?(nil), do: false
- def locked?(%Teams.Team{locked: locked}), do: locked
+ 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
@spec trial_days_left(Teams.Team.t()) :: integer()
def trial_days_left(nil) do
diff --git a/lib/plausible/teams/team.ex b/lib/plausible/teams/team.ex
index 8ba1fa23e0..126dfbe2ad 100644
--- a/lib/plausible/teams/team.ex
+++ b/lib/plausible/teams/team.ex
@@ -28,6 +28,7 @@ defmodule Plausible.Teams.Team do
field :accept_traffic_until, :date
field :allow_next_upgrade_override, :boolean, default: false
field :locked, :boolean, default: false
+ field :locked_by_admin, :boolean, default: false
field :setup_complete, :boolean, default: false
field :setup_at, :naive_datetime
diff --git a/lib/plausible_web/templates/layout/_notice.html.heex b/lib/plausible_web/templates/layout/_notice.html.heex
index 969c3d1364..d0728020ec 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.GracePeriod.expired?(@current_team)} />
+ <Notice.dashboard_locked :if={Plausible.Teams.locked?(@current_team)} />
<Notice.subscription_cancelled subscription={@current_team.subscription} />
diff --git a/lib/workers/schedule_email_reports.ex b/lib/workers/schedule_email_reports.ex
index 1551618741..c5f8ef50fc 100644
--- a/lib/workers/schedule_email_reports.ex
+++ b/lib/workers/schedule_email_reports.ex
@@ -35,7 +35,7 @@ defmodule Plausible.Workers.ScheduleEmailReports do
fragment("(? -> 'site_id')::int", job.args) == s.id and
job.state not in ["completed", "discarded"],
where: is_nil(job),
- where: not t.locked,
+ where: not t.locked and not t.locked_by_admin,
preload: [weekly_report: wr]
)
@@ -76,7 +76,7 @@ defmodule Plausible.Workers.ScheduleEmailReports do
fragment("(? -> 'site_id')::int", job.args) == s.id and
job.state not in ["completed", "discarded"],
where: is_nil(job),
- where: not t.locked,
+ where: not t.locked and not t.locked_by_admin,
preload: [monthly_report: mr]
)
diff --git a/lib/workers/traffic_change_notifier.ex b/lib/workers/traffic_change_notifier.ex
index 02f93d18a1..1fe6ba909d 100644
--- a/lib/workers/traffic_change_notifier.ex
+++ b/lib/workers/traffic_change_notifier.ex
@@ -23,7 +23,7 @@ defmodule Plausible.Workers.TrafficChangeNotifier do
sn.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,
+ where: not t.locked and not t.locked_by_admin,
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.exs
index aba652d12b..6b78dc5617 100644
--- a/test/plausible_web/live/customer_support/teams_test.exs
+++ b/test/plausible_web/live/customer_support/teams_test.exs
@@ -8,6 +8,8 @@ defmodule PlausibleWeb.Live.CustomerSupport.TeamsTest do
import Phoenix.LiveViewTest
import Plausible.Test.Support.HTML
+ alias Plausible.Teams
+
defp open_team(id, qs \\ []) do
Routes.customer_support_resource_path(
PlausibleWeb.Endpoint,
@@ -37,6 +39,27 @@ defmodule PlausibleWeb.Live.CustomerSupport.TeamsTest do
{:ok, _lv, _html} = live(conn, open_team(9999))
end
end
+
+ test "lock/unlock a team", %{user: user, conn: conn} do
+ team = team_of(user)
+ {: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 text(html) =~ "Team locked"
+ assert Teams.locked?(Plausible.Repo.reload!(team))
+
+ refute element_exists?(html, "#lock-dashboards")
+ assert element_exists?(html, "#unlock-dashboards")
+
+ lv |> element("#unlock-dashboards") |> render_click()
+ html = render(lv)
+
+ assert text(html) =~ "Team unlocked"
+ refute Teams.locked?(Plausible.Repo.reload!(team))
+ end
end
describe "billing" do
diff --git a/test/workers/schedule_email_reports_test.exs b/test/workers/schedule_email_reports_test.exs
index 211611b860..612bd5f479 100644
--- a/test/workers/schedule_email_reports_test.exs
+++ b/test/workers/schedule_email_reports_test.exs
@@ -38,6 +38,16 @@ defmodule Plausible.Workers.ScheduleEmailReportsTest do
assert 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" do
site = new_site(domain: "test-site.com", timezone: "US/Eastern")
insert(:weekly_report, site: site, recipients: ["[email protected]"])
@@ -84,6 +94,16 @@ defmodule Plausible.Workers.ScheduleEmailReportsTest do
assert 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" do
site = 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.exs
index e602bfa00b..92554abe97 100644
--- a/test/workers/traffic_change_notifier_test.exs
+++ b/test/workers/traffic_change_notifier_test.exs
@@ -288,6 +288,27 @@ defmodule Plausible.Workers.TrafficChangeNotifierTest do
assert_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" do
site = new_site()
insert(:spike_notification, site: site, threshold: 1, recipients: ["[email protected]"])