From 9fb4ea0c3ebadf69dd5be43b31a6667102622f10 Mon Sep 17 00:00:00 2001 From: hq1 Date: Thu, 1 Feb 2024 08:15:04 +0100 Subject: [PATCH] Bugfix: available features for expired trials, no subscriptions (#3740) * Reorganize how subscriptions/trials are evaluated * Bugfix: expired trial+no subscriptions should not have access to extra features * Make self-hosted users always on trial * Seed secondary user with password * Format * Fix docs * Fix small_test run * Run the test only on full_build * More tweaks to small builds * Allow [Goals] for expired trials with no subscription --- lib/plausible/auth/user_admin.ex | 2 +- lib/plausible/billing/billing.ex | 41 +++++++------------ lib/plausible/billing/feature.ex | 2 +- lib/plausible/billing/quota.ex | 36 ++++++++++------ lib/plausible/billing/subscriptions.ex | 11 +++++ lib/plausible/site/gate_keeper.ex | 2 +- lib/plausible/site/memberships/invitations.ex | 2 +- lib/plausible/users.ex | 21 +++++++++- .../components/billing/notice.ex | 2 +- .../email/site_setup_help_email.html.eex | 2 +- .../email/site_setup_success_email.html.eex | 2 +- .../templates/layout/_header.html.heex | 2 +- lib/plausible_web/views/layout_view.ex | 2 +- priv/repo/seeds.exs | 2 +- test/plausible/auth/users_test.exs | 35 ++++++++++++++++ test/plausible/billing/billing_test.exs | 35 ---------------- test/plausible/billing/quota_test.exs | 7 ++++ .../components/billing/notice_test.exs | 2 + 18 files changed, 122 insertions(+), 86 deletions(-) diff --git a/lib/plausible/auth/user_admin.ex b/lib/plausible/auth/user_admin.ex index 690e34c1a..19cdcdd5a 100644 --- a/lib/plausible/auth/user_admin.ex +++ b/lib/plausible/auth/user_admin.ex @@ -118,7 +118,7 @@ defmodule Plausible.Auth.UserAdmin do user.subscription -> PlausibleWeb.AuthView.present_subscription_status(user.subscription.status) - Plausible.Billing.on_trial?(user) -> + Plausible.Users.on_trial?(user) -> "On trial" true -> diff --git a/lib/plausible/billing/billing.ex b/lib/plausible/billing/billing.ex index 268958a26..10488656a 100644 --- a/lib/plausible/billing/billing.ex +++ b/lib/plausible/billing/billing.ex @@ -2,6 +2,7 @@ defmodule Plausible.Billing do use Plausible use Plausible.Repo require Plausible.Billing.Subscription.Status + alias Plausible.Billing.Subscriptions alias Plausible.Billing.{Subscription, Plans, Quota} alias Plausible.Auth.User @@ -94,40 +95,26 @@ defmodule Plausible.Billing do | :no_upgrade_needed def check_needs_to_upgrade(user) do user = Plausible.Users.with_subscription(user) - trial_is_over = user.trial_expiry_date && Timex.before?(user.trial_expiry_date, Timex.today()) - subscription_active = subscription_is_active?(user.subscription) - cond do - !user.trial_expiry_date && !subscription_active -> {:needs_to_upgrade, :no_trial} - trial_is_over && !subscription_active -> {:needs_to_upgrade, :no_active_subscription} - Plausible.Auth.GracePeriod.expired?(user) -> {:needs_to_upgrade, :grace_period_ended} - true -> :no_upgrade_needed - end - end + trial_over? = + not is_nil(user.trial_expiry_date) and + Date.before?(user.trial_expiry_date, Date.utc_today()) - def subscription_is_active?(%Subscription{status: Subscription.Status.active()}), do: true - def subscription_is_active?(%Subscription{status: Subscription.Status.past_due()}), do: true + subscription_active? = Subscriptions.active?(user.subscription) - def subscription_is_active?(%Subscription{status: Subscription.Status.deleted()} = subscription) do - subscription.next_bill_date && !Timex.before?(subscription.next_bill_date, Timex.today()) - end + cond do + is_nil(user.trial_expiry_date) and not subscription_active? -> + {:needs_to_upgrade, :no_trial} - def subscription_is_active?(%Subscription{}), do: false - def subscription_is_active?(nil), do: false + trial_over? and not subscription_active? -> + {:needs_to_upgrade, :no_active_subscription} - on_full_build do - def on_trial?(%User{trial_expiry_date: nil}), do: false + Plausible.Auth.GracePeriod.expired?(user) -> + {:needs_to_upgrade, :grace_period_ended} - def on_trial?(user) do - user = Plausible.Users.with_subscription(user) - !subscription_is_active?(user.subscription) && trial_days_left(user) >= 0 + true -> + :no_upgrade_needed end - else - def on_trial?(_), do: false - end - - def trial_days_left(user) do - Timex.diff(user.trial_expiry_date, Timex.today(), :days) end defp handle_subscription_created(params) do diff --git a/lib/plausible/billing/feature.ex b/lib/plausible/billing/feature.ex index 420084deb..7246302aa 100644 --- a/lib/plausible/billing/feature.ex +++ b/lib/plausible/billing/feature.ex @@ -194,7 +194,7 @@ defmodule Plausible.Billing.Feature.StatsAPI do @doc """ Checks whether the user has access to Stats API or not. - Before the the business tier, users who had not yet started their trial had + Before the business tier, users who had not yet started their trial had access to Stats API. With the business tier work, access is blocked and they must either start their trial or subscribe to a plan. This is common when a site owner invites a new user. In such cases, using the owner's API key is diff --git a/lib/plausible/billing/quota.ex b/lib/plausible/billing/quota.ex index 42d3b8f3c..b6b6e4560 100644 --- a/lib/plausible/billing/quota.ex +++ b/lib/plausible/billing/quota.ex @@ -5,9 +5,10 @@ defmodule Plausible.Billing.Quota do use Plausible import Ecto.Query + alias Plausible.Users alias Plausible.Auth.User alias Plausible.Site - alias Plausible.Billing.{Plan, Plans, Subscription, EnterprisePlan, Feature} + alias Plausible.Billing.{Plan, Plans, Subscription, Subscriptions, EnterprisePlan, Feature} alias Plausible.Billing.Feature.{Goals, RevenueGoals, Funnels, Props, StatsAPI} @type limit() :: :site_limit | :pageview_limit | :team_member_limit @@ -59,7 +60,7 @@ defmodule Plausible.Billing.Quota do end defp get_site_limit_from_plan(user) do - user = Plausible.Users.with_subscription(user) + user = Users.with_subscription(user) case Plans.get_subscription_plan(user.subscription) do %{site_limit: site_limit} -> site_limit @@ -70,7 +71,7 @@ defmodule Plausible.Billing.Quota do @spec team_member_limit(User.t()) :: non_neg_integer() def team_member_limit(user) do - user = Plausible.Users.with_subscription(user) + user = Users.with_subscription(user) case Plans.get_subscription_plan(user.subscription) do %{team_member_limit: limit} -> limit @@ -102,7 +103,7 @@ defmodule Plausible.Billing.Quota do in a background job instead (see `check_usage.ex`). """ def ensure_can_add_new_site(user) do - user = Plausible.Users.with_subscription(user) + user = Users.with_subscription(user) case Plans.get_subscription_plan(user.subscription) do %EnterprisePlan{} -> @@ -122,7 +123,7 @@ defmodule Plausible.Billing.Quota do @spec monthly_pageview_limit(User.t() | Subscription.t()) :: non_neg_integer() | :unlimited def monthly_pageview_limit(%User{} = user) do - user = Plausible.Users.with_subscription(user) + user = Users.with_subscription(user) monthly_pageview_limit(user.subscription) end @@ -180,7 +181,7 @@ defmodule Plausible.Billing.Quota do end def monthly_pageview_usage(user, site_ids) do - active_subscription? = Plausible.Billing.subscription_is_active?(user.subscription) + active_subscription? = Subscriptions.active?(user.subscription) if active_subscription? && user.subscription.last_bill_date do [:current_cycle, :last_cycle, :penultimate_cycle] @@ -217,7 +218,7 @@ defmodule Plausible.Billing.Quota do end def usage_cycle(user, cycle, owned_site_ids, today) do - user = Plausible.Users.with_subscription(user) + user = Users.with_subscription(user) last_bill_date = user.subscription.last_bill_date normalized_last_bill_date = @@ -435,13 +436,24 @@ defmodule Plausible.Billing.Quota do ability to use all features during their trial. """ def allowed_features_for(user) do - user = Plausible.Users.with_subscription(user) + user = Users.with_subscription(user) case Plans.get_subscription_plan(user.subscription) do - %EnterprisePlan{features: features} -> features - %Plan{features: features} -> features - :free_10k -> [Goals, Props, StatsAPI] - nil -> Feature.list() + %EnterprisePlan{features: features} -> + features + + %Plan{features: features} -> + features + + :free_10k -> + [Goals, Props, StatsAPI] + + nil -> + if Users.on_trial?(user) do + Feature.list() + else + [Goals] + end end end diff --git a/lib/plausible/billing/subscriptions.ex b/lib/plausible/billing/subscriptions.ex index 2e2d8ca19..8cc5138a7 100644 --- a/lib/plausible/billing/subscriptions.ex +++ b/lib/plausible/billing/subscriptions.ex @@ -4,6 +4,17 @@ defmodule Plausible.Billing.Subscriptions do require Plausible.Billing.Subscription.Status alias Plausible.Billing.Subscription + def active?(%Subscription{status: Subscription.Status.active()}), do: true + def active?(%Subscription{status: Subscription.Status.past_due()}), do: true + + def active?(%Subscription{status: Subscription.Status.deleted()} = subscription) do + not is_nil(subscription.next_bill_date) and + not Date.before?(subscription.next_bill_date, Date.utc_today()) + end + + def active?(%Subscription{}), do: false + def active?(nil), do: false + @spec expired?(Subscription.t()) :: boolean() @doc """ Returns whether the given subscription is expired. That means that the diff --git a/lib/plausible/site/gate_keeper.ex b/lib/plausible/site/gate_keeper.ex index e552f1637..7515f8ca9 100644 --- a/lib/plausible/site/gate_keeper.ex +++ b/lib/plausible/site/gate_keeper.ex @@ -20,7 +20,7 @@ defmodule Plausible.Site.GateKeeper do a Site by domain using `Plausible.Cache` interface. The module defines two policies outside the regular bucket inspection: - * when the the site is not found in cache: #{@policy_for_non_existing_sites} + * when the site is not found in cache: #{@policy_for_non_existing_sites} * when the underlying rate limiting mechanism returns an internal error: :allow """ diff --git a/lib/plausible/site/memberships/invitations.ex b/lib/plausible/site/memberships/invitations.ex index 4fd0df020..f602d0cd0 100644 --- a/lib/plausible/site/memberships/invitations.ex +++ b/lib/plausible/site/memberships/invitations.ex @@ -72,7 +72,7 @@ defmodule Plausible.Site.Memberships.Invitations do new_owner = Plausible.Users.with_subscription(new_owner) plan = Plausible.Billing.Plans.get_subscription_plan(new_owner.subscription) - active_subscription? = Plausible.Billing.subscription_is_active?(new_owner.subscription) + active_subscription? = Plausible.Billing.Subscriptions.active?(new_owner.subscription) if active_subscription? && plan != :free_10k do usage_after_transfer = %{ diff --git a/lib/plausible/users.ex b/lib/plausible/users.ex index 3cd22453b..727e35bb4 100644 --- a/lib/plausible/users.ex +++ b/lib/plausible/users.ex @@ -11,6 +11,23 @@ defmodule Plausible.Users do alias Plausible.Billing.Subscription alias Plausible.Repo + @spec on_trial?(Auth.User.t()) :: boolean() + on_full_build do + def on_trial?(%Auth.User{trial_expiry_date: nil}), do: false + + def on_trial?(user) do + user = with_subscription(user) + not Plausible.Billing.Subscriptions.active?(user.subscription) && trial_days_left(user) >= 0 + end + else + def on_trial?(_), do: true + end + + @spec trial_days_left(Auth.User.t()) :: integer() + def trial_days_left(user) do + Timex.diff(user.trial_expiry_date, Timex.today(), :days) + end + @spec update_accept_traffic_until(Auth.User.t()) :: Auth.User.t() def update_accept_traffic_until(user) do user @@ -24,7 +41,7 @@ defmodule Plausible.Users do user = with_subscription(user) cond do - Plausible.Billing.on_trial?(user) -> + Plausible.Users.on_trial?(user) -> Timex.shift(user.trial_expiry_date, days: Auth.User.trial_accept_traffic_until_offset_days() ) @@ -86,7 +103,7 @@ defmodule Plausible.Users do end defp last_subscription_query(user_id) do - from(subscription in Plausible.Billing.Subscription, + from(subscription in Subscription, where: subscription.user_id == ^user_id, order_by: [desc: subscription.inserted_at], limit: 1 diff --git a/lib/plausible_web/components/billing/notice.ex b/lib/plausible_web/components/billing/notice.ex index 650b2193d..159e9a48a 100644 --- a/lib/plausible_web/components/billing/notice.ex +++ b/lib/plausible_web/components/billing/notice.ex @@ -277,7 +277,7 @@ defmodule PlausibleWeb.Components.Billing.Notice do plan = Plans.get_regular_plan(billable_user.subscription, only_non_expired: true) - trial? = Plausible.Billing.on_trial?(assigns.billable_user) + trial? = Plausible.Users.on_trial?(assigns.billable_user) growth? = plan && plan.kind == :growth cond do diff --git a/lib/plausible_web/templates/email/site_setup_help_email.html.eex b/lib/plausible_web/templates/email/site_setup_help_email.html.eex index 7366790c1..e42847faf 100644 --- a/lib/plausible_web/templates/email/site_setup_help_email.html.eex +++ b/lib/plausible_web/templates/email/site_setup_help_email.html.eex @@ -1,4 +1,4 @@ -<%= if Plausible.Billing.on_trial?(@user) do %> +<%= if Plausible.Users.on_trial?(@user) do %> You signed up for a free 30-day trial of Plausible, a simple and privacy-friendly website analytics tool.

<% end %> diff --git a/lib/plausible_web/templates/email/site_setup_success_email.html.eex b/lib/plausible_web/templates/email/site_setup_success_email.html.eex index 93bf93822..357c5aed3 100644 --- a/lib/plausible_web/templates/email/site_setup_success_email.html.eex +++ b/lib/plausible_web/templates/email/site_setup_success_email.html.eex @@ -4,7 +4,7 @@ Do check out your <%= link("easy to use, fast-loading and privacy-friendly dashb

Something looks off? Take a look at our <%= link("installation troubleshooting guide", to: "https://plausible.io/docs/troubleshoot-integration") %>.

-<%= if Plausible.Billing.on_trial?(@user) do %> +<%= if Plausible.Users.on_trial?(@user) do %> You're on a 30-day free trial with no obligations so do take your time to explore Plausible. Here's how to get <%= link("the most out of your Plausible experience", to: "https://plausible.io/docs/your-plausible-experience") %>.

<% end %> diff --git a/lib/plausible_web/templates/layout/_header.html.heex b/lib/plausible_web/templates/layout/_header.html.heex index 450a5daa7..7702b9e95 100644 --- a/lib/plausible_web/templates/layout/_header.html.heex +++ b/lib/plausible_web/templates/layout/_header.html.heex @@ -25,7 +25,7 @@ <% @conn.assigns[:current_user] -> %>