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
Fix CE Stats API access (#4244)
ruslandoga9cbb9ba79cb7
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 95c6f25733..12152f111b 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -14,6 +14,8 @@ All notable changes to this project will be documented in this file.### Fixed+- Fix access to Stats API feature in CE plausible/analytics#4244+## v2.1.1 - 2024-06-06### Addeddiff --git a/lib/plausible/auth/auth.ex b/lib/plausible/auth/auth.exindex 4eaf5df78a..026e9fc5d4 100644--- a/lib/plausible/auth/auth.ex+++ b/lib/plausible/auth/auth.ex@@ -77,7 +77,7 @@ defmodule Plausible.Auth doend@spec create_api_key(Auth.User.t(), String.t(), String.t()) ::- {:ok, Auth.ApiKey.t()} | {:error, Ecto.Changeset.t()}+ {:ok, Auth.ApiKey.t()} | {:error, Ecto.Changeset.t() | :upgrade_required}def create_api_key(user, name, key) doparams = %{name: name, user_id: user.id, key: key}changeset = Auth.ApiKey.changeset(%Auth.ApiKey{}, params)diff --git a/lib/plausible/billing/feature.ex b/lib/plausible/billing/feature.exindex 11aeb5d2e7..4339b6f7b5 100644--- a/lib/plausible/billing/feature.ex+++ b/lib/plausible/billing/feature.ex@@ -202,33 +202,38 @@ defmodule Plausible.Billing.Feature.StatsAPI doname: :stats_api,display_name: "Stats API"- @impl true- @doc """- Checks whether the user has access to Stats API or not.-- 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- recommended.- """- def check_availability(%Plausible.Auth.User{} = user) do- user = Plausible.Users.with_subscription(user)- unlimited_trial? = is_nil(user.trial_expiry_date)- subscription? = Plausible.Billing.Subscriptions.active?(user.subscription)-- pre_business_tier_account? =- Timex.before?(user.inserted_at, Plausible.Billing.Plans.business_tier_launch())-- cond do- !subscription? && unlimited_trial? && pre_business_tier_account? ->- :ok-- !subscription? && unlimited_trial? && !pre_business_tier_account? ->- {:error, :upgrade_required}-- true ->- super(user)+ if Plausible.ee?() do+ @impl true+ @doc """+ Checks whether the user has access to Stats API or not.++ 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+ recommended.+ """+ def check_availability(%Plausible.Auth.User{} = user) do+ user = Plausible.Users.with_subscription(user)+ unlimited_trial? = is_nil(user.trial_expiry_date)+ subscription? = Plausible.Billing.Subscriptions.active?(user.subscription)++ pre_business_tier_account? =+ Timex.before?(user.inserted_at, Plausible.Billing.Plans.business_tier_launch())++ cond do+ !subscription? && unlimited_trial? && pre_business_tier_account? ->+ :ok++ !subscription? && unlimited_trial? && !pre_business_tier_account? ->+ {:error, :upgrade_required}++ true ->+ super(user)+ endend+ else+ @impl true+ def check_availability(_user), do: :okendenddiff --git a/test/plausible/auth/auth_test.exs b/test/plausible/auth/auth_test.exsindex 95f4ae588c..79589261d9 100644--- a/test/plausible/auth/auth_test.exs+++ b/test/plausible/auth/auth_test.exs@@ -89,6 +89,7 @@ defmodule Plausible.AuthTest do[constraint: :unique, constraint_name: "api_keys_key_hash_index"]}end+ @tag :ee_onlytest "returns error when user is on a growth plan" douser = insert(:user, subscription: build(:growth_subscription))diff --git a/test/plausible/billing/feature_test.exs b/test/plausible/billing/feature_test.exsindex 7336588874..6e37acaa6d 100644--- a/test/plausible/billing/feature_test.exs+++ b/test/plausible/billing/feature_test.exs@@ -61,6 +61,7 @@ defmodule Plausible.Billing.FeatureTest doassert :ok == Plausible.Billing.Feature.StatsAPI.check_availability(user)end+ @tag :ee_onlytest "Plausible.Billing.Feature.StatsAPI.check_availability/2 returns error when user is on a growth plan" douser = insert(:user, subscription: build(:growth_subscription))@@ -79,6 +80,7 @@ defmodule Plausible.Billing.FeatureTest doassert :ok == Plausible.Billing.Feature.StatsAPI.check_availability(user)end+ @tag :ee_onlytest "Plausible.Billing.Feature.StatsAPI.check_availability/2 returns error when user trial hasn't started and was created after the business tier launch" douser = insert(:user, trial_expiry_date: nil)diff --git a/test/plausible_web/controllers/api/external_stats_controller/auth_test.exs b/test/plausible_web/controllers/api/external_stats_controller/auth_test.exsindex 3d66538c06..9107fd3818 100644--- a/test/plausible_web/controllers/api/external_stats_controller/auth_test.exs+++ b/test/plausible_web/controllers/api/external_stats_controller/auth_test.exs@@ -150,6 +150,7 @@ defmodule PlausibleWeb.Api.ExternalStatsController.AuthTest do})end+ @tag :ee_onlytest "returns HTTP 402 when user is on a growth plan", %{conn: conn,user: user,diff --git a/test/plausible_web/plugins/api/controllers/capabilities_test.exs b/test/plausible_web/plugins/api/controllers/capabilities_test.exsindex 162c7a5e75..250ff809fb 100644--- a/test/plausible_web/plugins/api/controllers/capabilities_test.exs+++ b/test/plausible_web/plugins/api/controllers/capabilities_test.exs@@ -85,6 +85,7 @@ defmodule PlausibleWeb.Plugins.API.Controllers.CapabilitiesTest doassert_schema(resp, "Capabilities", spec())end+ @tag :ee_onlytest "growth", %{conn: conn, site: site, token: token} dosite = Plausible.Repo.preload(site, :owner)insert(:growth_subscription, user: site.owner)