diff --git a/CHANGELOG.md b/CHANGELOG.md
index 55f97d833..e6077ebbf 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -34,6 +34,8 @@ All notable changes to this project will be documented in this file.
- New width=manual mode for embedded dashboards plausible/analytics#2148
- Add more timezone options
- Add new strategy to recommend timezone when creating a new site
+- Alert outgrown enterprise users of their usage plausible/analytics#2197
+- Manually lock and unlock enterprise users plausible/analytics#2197
### Fixed
- Hash part of the URL can now be used when excluding pages with `script.exclusions.hash.js`.
diff --git a/guides/features/account-locking.md b/guides/features/account-locking.md
new file mode 100644
index 000000000..c1fb5ca81
--- /dev/null
+++ b/guides/features/account-locking.md
@@ -0,0 +1,35 @@
+# Account locking
+This document explains the account locking feature from a technical perspective. Account locking happens when users have outgrown their accounts, and despite e-mail and dashboard alerts, don't upgrade after a grace period.
+
+## Usage alert and grace period
+The `Plausible.Workers.CheckUsage` daily background job alerts users they have reached their subscription limits. This runs for outgrown users one day after their last billing date.
+
+When users reach the number of sites limit, or use >110% of their pageview limit for 2 consecutive billing cycles, the background job sends them an e-mail alert. The e-mail suggests a suitable subscription plan based on usage. For enterprise users, only an internal e-mail is sent to `enterprise@plausible.io`.
+
+The user is given 7 days to upgrade their account after the alert, and this is called grace period. The background starts this grace period by adding a `users.grace_period` JSON to the user record:
+
+```json
+// SELECT grace_period FROM users LIMIT 1
+
+{
+ "id": "1aa855bd-022d-4dfc-b572-6853442c3f19",
+ "is_over": true,
+ "end_date": "2022-03-09",
+ "allowance_required": 100,
+ "manual_lock": false
+}
+```
+
+
+During this period, the following alert pops up on the dashboard for both standard and enterprise users:
+
+
+
+If the user upgrades to a suitable plan, the grace period is removed (check `Plausible.Billing.subscription_updated/1`), otherwise [Account locking](#Account\ locking) follows. For enterprise users, the grace period has a manual lock flag set to true, meaning enterprise accounts can only be locked/unlocked through the internal CRM.
+
+## Account locking
+The grace period is checked daily by the `Plausible.Workers.LockSites` background job.
+
+For users that expired their grace period, `sites.locked` is is set to `true`, restricting access to dashboards. This does not stop event ingestion, so users can have their stats up to date when they finally upgrade.
+
+
diff --git a/lib/plausible/auth/auth.ex b/lib/plausible/auth/auth.ex
index cb12997ca..e0c38ce74 100644
--- a/lib/plausible/auth/auth.ex
+++ b/lib/plausible/auth/auth.ex
@@ -98,4 +98,11 @@ defmodule Plausible.Auth do
def is_super_admin?(user_id) do
user_id in Application.get_env(:plausible, :super_admin_user_ids)
end
+
+ def enterprise?(nil), do: false
+
+ def enterprise?(%Plausible.Auth.User{} = user) do
+ user = Repo.preload(user, :enterprise_plan)
+ user.enterprise_plan != nil
+ end
end
diff --git a/lib/plausible/auth/grace_period.ex b/lib/plausible/auth/grace_period.ex
new file mode 100644
index 000000000..9d3afdf87
--- /dev/null
+++ b/lib/plausible/auth/grace_period.ex
@@ -0,0 +1,106 @@
+defmodule Plausible.Auth.GracePeriod do
+ @moduledoc """
+ This embedded schema stores information about the account locking grace
+ period.
+
+ Users are given this 7-day grace period to upgrade their account after
+ outgrowing their subscriptions. The actual account locking happens in
+ background with `Plausible.Workers.LockSites`.
+
+ The grace period can also be manual, without an end date, being controlled
+ manually from the CRM, and not by the background site locker job. This is
+ useful for enterprise subscriptions.
+ """
+
+ use Ecto.Schema
+ alias Plausible.Auth.User
+
+ @type t() :: %__MODULE__{
+ end_date: Date.t() | nil,
+ allowance_required: non_neg_integer(),
+ is_over: boolean(),
+ manual_lock: boolean()
+ }
+
+ embedded_schema do
+ field :end_date, :date
+ field :allowance_required, :integer
+ field :is_over, :boolean
+ field :manual_lock, :boolean
+ end
+
+ @spec start_changeset(User.t(), non_neg_integer()) :: Ecto.Changeset.t()
+ @doc """
+ Starts a account locking grace period of 7 days by changing the User struct.
+ """
+ def start_changeset(%User{} = user, allowance_required) do
+ grace_period = %__MODULE__{
+ end_date: Timex.shift(Timex.today(), days: 7),
+ allowance_required: allowance_required,
+ is_over: false,
+ manual_lock: false
+ }
+
+ Ecto.Changeset.change(user, grace_period: grace_period)
+ end
+
+ @spec start_manual_lock_changeset(User.t(), non_neg_integer()) :: Ecto.Changeset.t()
+ @doc """
+ Starts a manual account locking grace period by changing the User struct.
+ Manual locking means the grace period can only be removed manually from the
+ CRM.
+ """
+ def start_manual_lock_changeset(%User{} = user, allowance_required) do
+ grace_period = %__MODULE__{
+ end_date: nil,
+ allowance_required: allowance_required,
+ is_over: false,
+ manual_lock: true
+ }
+
+ Ecto.Changeset.change(user, grace_period: grace_period)
+ end
+
+ @spec end_changeset(User.t()) :: Ecto.Changeset.t()
+ @doc """
+ Ends an existing grace period by `setting users.grace_period.is_over` to true.
+ This means the grace period has expired.
+ """
+ def end_changeset(%User{} = user) do
+ Ecto.Changeset.change(user, grace_period: %{is_over: true})
+ end
+
+ @spec remove_changeset(User.t()) :: Ecto.Changeset.t()
+ @doc """
+ Removes the grace period from the User completely.
+ """
+ def remove_changeset(%User{} = user) do
+ Ecto.Changeset.change(user, grace_period: nil)
+ end
+
+ @spec active?(User.t()) :: boolean()
+ @doc """
+ Returns whether the grace period is still active for a User. Defaults to
+ false if the user is nil or there is no grace period.
+ """
+ def active?(user)
+
+ def active?(%User{grace_period: %__MODULE__{end_date: %Date{} = end_date}}) do
+ Timex.diff(end_date, Timex.today(), :days) >= 0
+ end
+
+ def active?(%User{grace_period: %__MODULE__{manual_lock: true}}) do
+ true
+ end
+
+ def active?(_user), do: false
+
+ @spec expired?(User.t()) :: boolean()
+ @doc """
+ Returns whether the grace period has already expired for a User. Defaults to
+ false if the user is nil or there is no grace period.
+ """
+ def expired?(user) do
+ if user && user.grace_period, do: !active?(user), else: false
+ end
+end
diff --git a/lib/plausible/auth/user.ex b/lib/plausible/auth/user.ex
index b1c49b24b..bdc6c6c2e 100644
--- a/lib/plausible/auth/user.ex
+++ b/lib/plausible/auth/user.ex
@@ -10,20 +10,12 @@ defimpl FunWithFlags.Actor, for: Plausible.Auth.User do
end
end
-defmodule Plausible.Auth.GracePeriod do
- use Ecto.Schema
-
- embedded_schema do
- field :end_date, :date
- field :allowance_required, :integer
- field :is_over, :boolean
- end
-end
-
defmodule Plausible.Auth.User do
use Ecto.Schema
import Ecto.Changeset
+ @type t() :: %__MODULE__{}
+
@required [:email, :name, :password, :password_confirmation]
schema "users" do
field :email, :string
@@ -96,24 +88,6 @@ defmodule Plausible.Auth.User do
change(user, trial_expiry_date: Timex.today() |> Timex.shift(days: -1))
end
- def start_grace_period(user, allowance_required) do
- grace_period = %Plausible.Auth.GracePeriod{
- end_date: Timex.today() |> Timex.shift(days: 7),
- allowance_required: allowance_required,
- is_over: false
- }
-
- change(user, grace_period: grace_period)
- end
-
- def end_grace_period(user) do
- change(user, grace_period: %{is_over: true})
- end
-
- def remove_grace_period(user) do
- change(user, grace_period: nil)
- end
-
defp trial_expiry() do
if Application.get_env(:plausible, :is_selfhost) do
Timex.today() |> Timex.shift(years: 100)
diff --git a/lib/plausible/auth/user_admin.ex b/lib/plausible/auth/user_admin.ex
index ec0cfa93f..38b57ee46 100644
--- a/lib/plausible/auth/user_admin.ex
+++ b/lib/plausible/auth/user_admin.ex
@@ -31,13 +31,26 @@ defmodule Plausible.Auth.UserAdmin do
unlock: %{
name: "Unlock",
action: fn _, user -> unlock(user) end
+ },
+ lock: %{
+ name: "Lock",
+ action: fn _, user -> lock(user) end
}
]
end
+ defp lock(user) do
+ if user.grace_period do
+ Plausible.Billing.SiteLocker.set_lock_status_for(user, true)
+ user |> Plausible.Auth.GracePeriod.end_changeset() |> Repo.update()
+ else
+ {:error, user, "No active grace period on this user"}
+ end
+ end
+
defp unlock(user) do
if user.grace_period do
- Plausible.Auth.User.remove_grace_period(user) |> Repo.update()
+ Plausible.Auth.GracePeriod.remove_changeset(user) |> Repo.update()
Plausible.Billing.SiteLocker.set_lock_status_for(user, false)
{:ok, user}
else
@@ -45,14 +58,23 @@ defmodule Plausible.Auth.UserAdmin do
end
end
- defp grace_period_status(%{grace_period: nil}), do: "--"
+ defp grace_period_status(%{grace_period: grace_period}) do
+ case grace_period do
+ nil ->
+ "--"
- defp grace_period_status(user) do
- if user.grace_period.is_over do
- "ended"
- else
- days_left = Timex.diff(user.grace_period.end_date, Timex.now(), :days)
- "#{days_left} days left"
+ %{manual_lock: true, is_over: true} ->
+ "Manually locked"
+
+ %{manual_lock: true, is_over: false} ->
+ "Waiting for manual lock"
+
+ %{is_over: true} ->
+ "ended"
+
+ %{end_date: %Date{} = end_date} ->
+ days_left = Timex.diff(end_date, Timex.now(), :days)
+ "#{days_left} days left"
end
end
diff --git a/lib/plausible/billing/billing.ex b/lib/plausible/billing/billing.ex
index 885dc8731..ef28e5e87 100644
--- a/lib/plausible/billing/billing.ex
+++ b/lib/plausible/billing/billing.ex
@@ -131,12 +131,9 @@ defmodule Plausible.Billing do
trial_is_over = Timex.before?(user.trial_expiry_date, Timex.today())
subscription_active = subscription_is_active?(user.subscription)
- grace_period_ended =
- user.grace_period && Timex.before?(user.grace_period.end_date, Timex.today())
-
cond do
trial_is_over && !subscription_active -> {true, :no_active_subscription}
- grace_period_ended -> {true, :grace_period_ended}
+ Plausible.Auth.GracePeriod.expired?(user) -> {true, :grace_period_ended}
true -> false
end
end
@@ -274,7 +271,8 @@ defmodule Plausible.Billing do
new_allowance = Plausible.Billing.Plans.allowance(user.subscription)
if new_allowance > allowance_required do
- Plausible.Auth.User.remove_grace_period(user)
+ user
+ |> Plausible.Auth.GracePeriod.remove_changeset()
|> Repo.update()
else
{:ok, user}
diff --git a/lib/plausible/billing/plans.ex b/lib/plausible/billing/plans.ex
index d18859662..5454e8623 100644
--- a/lib/plausible/billing/plans.ex
+++ b/lib/plausible/billing/plans.ex
@@ -26,6 +26,16 @@ defmodule Plausible.Billing.Plans do
}
]
+ @type plan() ::
+ %{
+ limit: non_neg_integer(),
+ monthly_product_id: String.t(),
+ yearly_product_id: String.t(),
+ monthly_cost: String.t(),
+ yearly_cost: String.t()
+ }
+ | :enterprise
+
def plans_for(user) do
user = Repo.preload(user, :subscription)
sandbox_plans = plans_sandbox()
@@ -109,15 +119,21 @@ defmodule Plausible.Billing.Plans do
end
end
- def get_enterprise_plan(subscription) do
+ defp get_enterprise_plan(%Plausible.Billing.Subscription{} = subscription) do
Repo.get_by(Plausible.Billing.EnterprisePlan,
user_id: subscription.user_id,
paddle_plan_id: subscription.paddle_plan_id
)
end
- def suggested_plan(user, usage) do
- Enum.find(plans_for(user), fn plan -> usage < plan[:limit] end)
+ @enterprise_level_usage 20_000_000
+ @spec suggested_plan(Plausible.Auth.User.t(), non_neg_integer()) :: plan()
+ def suggested_plan(user, usage_during_cycle) do
+ cond do
+ usage_during_cycle > @enterprise_level_usage -> :enterprise
+ Plausible.Auth.enterprise?(user) -> :enterprise
+ true -> Enum.find(plans_for(user), fn plan -> usage_during_cycle < plan[:limit] end)
+ end
end
defp contains?(_plans, nil), do: false
diff --git a/lib/plausible/billing/site_locker.ex b/lib/plausible/billing/site_locker.ex
index f12f78297..ee5755f2b 100644
--- a/lib/plausible/billing/site_locker.ex
+++ b/lib/plausible/billing/site_locker.ex
@@ -8,7 +8,7 @@ defmodule Plausible.Billing.SiteLocker do
if !user.grace_period.is_over do
send_grace_period_end_email(user)
- Plausible.Auth.User.end_grace_period(user) |> Repo.update()
+ Plausible.Auth.GracePeriod.end_changeset(user) |> Repo.update()
end
{true, _} ->
diff --git a/lib/plausible_web/email.ex b/lib/plausible_web/email.ex
index 2508babdb..08568f929 100644
--- a/lib/plausible_web/email.ex
+++ b/lib/plausible_web/email.ex
@@ -128,12 +128,12 @@ defmodule PlausibleWeb.Email do
})
end
- def enterprise_over_limit_email(user, usage, last_cycle, site_usage, site_allowance) do
+ def enterprise_over_limit_internal_email(user, usage, last_cycle, site_usage, site_allowance) do
base_email()
|> to("enterprise@plausible.io")
|> tag("enterprise-over-limit")
|> subject("#{user.email} has outgrown their enterprise plan")
- |> render("enterprise_over_limit.html", %{
+ |> render("enterprise_over_limit_internal.html", %{
user: user,
usage: usage,
last_cycle: last_cycle,
diff --git a/lib/plausible_web/templates/email/dashboard_locked.html.eex b/lib/plausible_web/templates/email/dashboard_locked.html.eex
index 031070c53..67b9d867a 100644
--- a/lib/plausible_web/templates/email/dashboard_locked.html.eex
+++ b/lib/plausible_web/templates/email/dashboard_locked.html.eex
@@ -5,12 +5,12 @@ Last week we sent a reminder that your traffic has exceeded your Plausible Analy
Your dashboard is now locked. We're still counting your stats, but you no longer have access to the stats. As you have outgrown your subscription tier, we kindly ask you to upgrade your subscription to accommodate your new traffic levels.
In the last billing cycle (<%= date_format(@last_cycle.first) %> to <%= date_format(@last_cycle.last) %>), your account has used <%= PlausibleWeb.StatsView.large_number_format(@usage) %> billable pageviews.
-<%= if @usage <= 20_000_000 do %>
+<%= if @suggested_plan == :enterprise do %>
+This is more than our standard plans, so please reply back to this email to get a quote for your volume.
+<% else %>
Based on that we recommend you select the <%= @suggested_plan[:volume] %>/mo plan.
Click here to go to your site settings. You can upgrade your subscription tier by clicking the 'change plan' link.
-<% else %>
-This is more than our standard plans, so please reply back to this email to get a quote for your volume.
<% end %>
The new charge will be prorated to reflect the amount you have already paid and the time until your current subscription is supposed to expire. We will unlock your dashboard immediately after your account has been upgraded to the appropriate tier.
diff --git a/lib/plausible_web/templates/email/enterprise_over_limit.html.eex b/lib/plausible_web/templates/email/enterprise_over_limit_internal.html.eex
similarity index 100%
rename from lib/plausible_web/templates/email/enterprise_over_limit.html.eex
rename to lib/plausible_web/templates/email/enterprise_over_limit_internal.html.eex
diff --git a/lib/plausible_web/templates/email/over_limit.html.eex b/lib/plausible_web/templates/email/over_limit.html.eex
index 265344984..f0ebea30d 100644
--- a/lib/plausible_web/templates/email/over_limit.html.eex
+++ b/lib/plausible_web/templates/email/over_limit.html.eex
@@ -7,14 +7,14 @@ This is a notice that your traffic has exceeded your subscription tier two month
In order to keep your stats running, we require you to upgrade your account to accommodate your new traffic levels. If you do not upgrade your account within the next 7 days, we will lock your stats and they won't be accessible.
In the last billing cycle (<%= date_format(@last_cycle.first) %> to <%= date_format(@last_cycle.last) %>), your account has used <%= PlausibleWeb.StatsView.large_number_format(@usage) %> billable pageviews.
-<%= if @usage <= 20_000_000 do %>
+<%= if @suggested_plan == :enterprise do %>
+This is more than our standard plans, so please reply back to this email to get a quote for your volume.
+<% else %>
Based on that we recommend you select the <%= @suggested_plan[:volume] %>/mo plan.
You can upgrade your subscription using our self-serve platform. The new charge will be prorated to reflect the amount you have already paid and the time until your current subscription is supposed to expire.
Click here to go to your site settings. You can upgrade your subscription tier by clicking the 'change plan' link.
-<% else %>
-This is more than our standard plans, so please reply back to this email to get a quote for your volume.
<% end %>
Have questions or need help with anything? Just reply to this email and we'll gladly help.
diff --git a/lib/plausible_web/templates/layout/_notice.html.eex b/lib/plausible_web/templates/layout/_notice.html.eex
index 33035d90b..7d2f8094d 100644
--- a/lib/plausible_web/templates/layout/_notice.html.eex
+++ b/lib/plausible_web/templates/layout/_notice.html.eex
@@ -3,7 +3,7 @@
<% end %>
-<%= if on_grace_period?(@conn.assigns[:current_user]) do %>
+<%= if Plausible.Auth.GracePeriod.active?(@conn.assigns[:current_user]) do %>
- In order to keep your stats running, we require you to upgrade your account. If you do not upgrade your account <%= grace_period_end(@conn.assigns[:current_user]) %>, we will lock your stats and they won't be accessible. <%= link("Upgrade now →", to: "/settings", class: "text-sm font-medium text-yellow-800") %> + <%= if Plausible.Auth.enterprise?(@conn.assigns[:current_user]) do %> + In order to keep your stats running, we require you to upgrade + your account to accommodate your new usage levels. Please contact + us to discuss a new custom enterprise plan. + <%= link("Contact us →", to: "mailto:enterprise@plausible.io", class: "text-sm font-medium text-yellow-800") %> + <% else %> + In order to keep your stats running, we require you to upgrade + your account. If you do not upgrade your account + <%= grace_period_end(@conn.assigns[:current_user]) %>, we will + lock your stats and they won't be accessible. + <%= link("Upgrade now →", to: "/settings", class: "text-sm font-medium text-yellow-800") %> + <% end %>
- As you have outgrown your subscription tier, we kindly ask you to upgrade your subscription to accommodate your new traffic levels. <%= link("Upgrade now →", to: "/settings", class: "text-sm font-medium text-yellow-800") %> + As you have outgrown your subscription tier, we kindly ask you to + upgrade your subscription to accommodate your new traffic levels. + <%= link("Upgrade now →", to: "/settings", class: "text-sm font-medium text-yellow-800") %>