Manually lock outgrown enterprise accounts (#2197)
* Rename enterprise_over_limit e-mail template * Change suggested_plan/2 function to suggest enterprise plans * Start grace period for enterprise accounts * Create separate module for GracePeriod schema * Enable manual locking of enterprise accounts * Update outgrown account alert for enterprise accounts * Document account locking feature * fixup! Enable manual locking of enterprise accountspull/2246/head
parent
75264f8f1c
commit
40182f6d6e
@ -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.
|
||||
|
||||

|
||||
@ -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
|
||||
@ -0,0 +1,105 @@
|
||||
defmodule Plausible.Auth.GracePeriodTest do
|
||||
use Plausible.DataCase, async: true
|
||||
|
||||
test "active?/1 returns false when grace period cannot be telled" do
|
||||
without_grace_period = build(:user, grace_period: nil)
|
||||
refute Plausible.Auth.GracePeriod.active?(without_grace_period)
|
||||
|
||||
without_user = nil
|
||||
refute Plausible.Auth.GracePeriod.active?(without_user)
|
||||
end
|
||||
|
||||
test "active?/1 returns false when grace period is expired" do
|
||||
yesterday = Date.add(Date.utc_today(), -1)
|
||||
|
||||
grace_period = %Plausible.Auth.GracePeriod{
|
||||
end_date: yesterday,
|
||||
allowance_required: 100,
|
||||
is_over: false
|
||||
}
|
||||
|
||||
user = build(:user, grace_period: grace_period)
|
||||
|
||||
refute Plausible.Auth.GracePeriod.active?(user)
|
||||
end
|
||||
|
||||
test "active?/1 returns true when grace period is still active" do
|
||||
tomorrow = Date.add(Date.utc_today(), 1)
|
||||
|
||||
grace_period = %Plausible.Auth.GracePeriod{
|
||||
end_date: tomorrow,
|
||||
allowance_required: 100,
|
||||
is_over: false
|
||||
}
|
||||
|
||||
user = build(:user, grace_period: grace_period)
|
||||
|
||||
assert Plausible.Auth.GracePeriod.active?(user)
|
||||
end
|
||||
|
||||
test "expired?/1 returns false when grace period cannot be telled" do
|
||||
without_grace_period = build(:user, grace_period: nil)
|
||||
refute Plausible.Auth.GracePeriod.expired?(without_grace_period)
|
||||
|
||||
without_user = nil
|
||||
refute Plausible.Auth.GracePeriod.expired?(without_user)
|
||||
end
|
||||
|
||||
test "expired?/1 returns true when grace period is expired" do
|
||||
yesterday = Date.add(Date.utc_today(), -1)
|
||||
|
||||
grace_period = %Plausible.Auth.GracePeriod{
|
||||
end_date: yesterday,
|
||||
allowance_required: 100,
|
||||
is_over: true
|
||||
}
|
||||
|
||||
user = build(:user, grace_period: grace_period)
|
||||
|
||||
assert Plausible.Auth.GracePeriod.expired?(user)
|
||||
end
|
||||
|
||||
test "expired?/1 returns false when grace period is still active" do
|
||||
tomorrow = Date.add(Date.utc_today(), 1)
|
||||
|
||||
grace_period = %Plausible.Auth.GracePeriod{
|
||||
end_date: tomorrow,
|
||||
allowance_required: 100,
|
||||
is_over: false
|
||||
}
|
||||
|
||||
user = build(:user, grace_period: grace_period)
|
||||
|
||||
refute Plausible.Auth.GracePeriod.expired?(user)
|
||||
end
|
||||
|
||||
test "start_manual_lock_changeset/1 creates an active grace period" do
|
||||
user = build(:user)
|
||||
changeset = Plausible.Auth.GracePeriod.start_manual_lock_changeset(user, 1)
|
||||
user = Ecto.Changeset.apply_changes(changeset)
|
||||
|
||||
assert Plausible.Auth.GracePeriod.active?(user)
|
||||
refute Plausible.Auth.GracePeriod.expired?(user)
|
||||
end
|
||||
|
||||
test "start_changeset/1 creates an active grace period" do
|
||||
user = build(:user)
|
||||
changeset = Plausible.Auth.GracePeriod.start_changeset(user, 1)
|
||||
user = Ecto.Changeset.apply_changes(changeset)
|
||||
|
||||
assert Plausible.Auth.GracePeriod.active?(user)
|
||||
refute Plausible.Auth.GracePeriod.expired?(user)
|
||||
end
|
||||
|
||||
test "remove_changeset/1 removes the active grace period" do
|
||||
user = build(:user)
|
||||
start_changeset = Plausible.Auth.GracePeriod.start_changeset(user, 1)
|
||||
user = Ecto.Changeset.apply_changes(start_changeset)
|
||||
|
||||
remove_changeset = Plausible.Auth.GracePeriod.remove_changeset(user)
|
||||
user = Ecto.Changeset.apply_changes(remove_changeset)
|
||||
|
||||
refute Plausible.Auth.GracePeriod.active?(user)
|
||||
refute Plausible.Auth.GracePeriod.expired?(user)
|
||||
end
|
||||
end
|
||||
Loading…
Reference in New Issue