diff --git a/lib/plausible/auth/api_key.ex b/lib/plausible/auth/api_key.ex index adb77ddf8..78fa5b3a9 100644 --- a/lib/plausible/auth/api_key.ex +++ b/lib/plausible/auth/api_key.ex @@ -2,6 +2,8 @@ defmodule Plausible.Auth.ApiKey do use Ecto.Schema import Ecto.Changeset + @type t() :: %__MODULE__{} + @required [:user_id, :name] @optional [:key, :scopes, :hourly_request_limit] schema "api_keys" do @@ -22,7 +24,7 @@ defmodule Plausible.Auth.ApiKey do schema |> cast(attrs, @required ++ @optional) |> validate_required(@required) - |> generate_key() + |> maybe_put_key() |> process_key() |> unique_constraint(:key_hash, error_key: :key) end @@ -50,12 +52,12 @@ defmodule Plausible.Auth.ApiKey do def process_key(changeset), do: changeset - defp generate_key(changeset) do - if !changeset.changes[:key] do - key = :crypto.strong_rand_bytes(64) |> Base.url_encode64() |> binary_part(0, 64) - change(changeset, key: key) - else + defp maybe_put_key(changeset) do + if get_change(changeset, :key) do changeset + else + key = :crypto.strong_rand_bytes(64) |> Base.url_encode64() |> binary_part(0, 64) + put_change(changeset, :key, key) end end diff --git a/lib/plausible/auth/auth.ex b/lib/plausible/auth/auth.ex index 126e27553..4f48e99f2 100644 --- a/lib/plausible/auth/auth.ex +++ b/lib/plausible/auth/auth.ex @@ -125,4 +125,42 @@ defmodule Plausible.Auth do |> Ecto.assoc(:enterprise_plan) |> Repo.exists?() end + + @spec create_api_key(Auth.User.t(), String.t(), String.t()) :: + {:ok, Auth.ApiKey.t()} | {:error, Ecto.Changeset.t()} + def create_api_key(user, name, key) do + params = %{name: name, user_id: user.id, key: key} + changeset = Auth.ApiKey.changeset(%Auth.ApiKey{}, params) + + with :ok <- Plausible.Billing.Feature.StatsAPI.check_availability(user), + do: Repo.insert(changeset) + end + + @spec delete_api_key(Auth.User.t(), integer()) :: :ok | {:error, :not_found} + def delete_api_key(user, id) do + query = from(api_key in Auth.ApiKey, where: api_key.id == ^id and api_key.user_id == ^user.id) + + case Repo.delete_all(query) do + {1, _} -> :ok + {0, _} -> {:error, :not_found} + end + end + + @spec find_api_key(String.t()) :: {:ok, Auth.ApiKey.t()} | {:error, :invalid_api_key} + def find_api_key(raw_key) do + hashed_key = Auth.ApiKey.do_hash(raw_key) + + query = + from(api_key in Auth.ApiKey, + join: user in assoc(api_key, :user), + where: api_key.key_hash == ^hashed_key, + preload: [user: user] + ) + + if found = Repo.one(query) do + {:ok, found} + else + {:error, :invalid_api_key} + end + end end diff --git a/lib/plausible/billing/feature.ex b/lib/plausible/billing/feature.ex index 37f9d8aa7..3fe5677ed 100644 --- a/lib/plausible/billing/feature.ex +++ b/lib/plausible/billing/feature.ex @@ -47,8 +47,8 @@ defmodule Plausible.Billing.Feature do @doc """ Checks whether the site owner or the user plan includes the given feature. """ - @callback check_availability(Plausible.Site.t() | Plausible.Auth.User.t()) :: - :ok | {:error, :upgrade_required} + @callback check_availability(Plausible.Auth.User.t()) :: + :ok | {:error, :upgrade_required} | {:error, :not_implemented} @features [ Plausible.Billing.Feature.Funnels, @@ -77,22 +77,17 @@ defmodule Plausible.Billing.Feature do def toggle_field, do: Keyword.get(unquote(opts), :toggle_field) @impl true - def enabled?(site) do + def enabled?(%Plausible.Site{} = site) do + site = Plausible.Repo.preload(site, :owner) + cond do - check_availability(site) !== :ok -> false + check_availability(site.owner) !== :ok -> false is_nil(toggle_field()) -> true - true -> Map.get(site, toggle_field()) + true -> Map.fetch!(site, toggle_field()) end end @impl true - def check_availability(site_or_user) - - def check_availability(%Plausible.Site{} = site) do - site = Plausible.Repo.preload(site, :owner) - check_availability(site.owner) - end - def check_availability(%Plausible.Auth.User{} = user) do extra_feature = Keyword.get(unquote(opts), :extra_feature) @@ -105,9 +100,10 @@ defmodule Plausible.Billing.Feature do end @impl true - def toggle(site, opts \\ []) do + def toggle(%Plausible.Site{} = site, opts \\ []) do with key when not is_nil(key) <- toggle_field(), - :ok <- check_availability(site) do + site <- Plausible.Repo.preload(site, :owner), + :ok <- check_availability(site.owner) do override = Keyword.get(opts, :override) toggle = if is_boolean(override), do: override, else: !Map.fetch!(site, toggle_field()) @@ -119,8 +115,6 @@ defmodule Plausible.Billing.Feature do {:error, :upgrade_required} -> {:error, :upgrade_required} end end - - defoverridable check_availability: 1, toggle: 1, toggle: 2 end end end @@ -154,3 +148,10 @@ defmodule Plausible.Billing.Feature.Props do toggle_field: :props_enabled, extra_feature: :props end + +defmodule Plausible.Billing.Feature.StatsAPI do + @moduledoc false + use Plausible.Billing.Feature, + display_name: "Stats API", + extra_feature: :stats_api +end diff --git a/lib/plausible/billing/plans.ex b/lib/plausible/billing/plans.ex index 7b4368be2..0b9cfe592 100644 --- a/lib/plausible/billing/plans.ex +++ b/lib/plausible/billing/plans.ex @@ -31,7 +31,7 @@ defmodule Plausible.Billing.Plans do alias Plausible.Billing.{Subscription, Plan, EnterprisePlan} alias Plausible.Auth.User - @available_features ["props", "revenue_goals", "funnels"] + @available_features ["props", "revenue_goals", "funnels", "stats_api"] for f <- [ :plans_v1, diff --git a/lib/plausible/billing/quota.ex b/lib/plausible/billing/quota.ex index 0879d9bcc..491d550bb 100644 --- a/lib/plausible/billing/quota.ex +++ b/lib/plausible/billing/quota.ex @@ -162,7 +162,7 @@ defmodule Plausible.Billing.Quota do end) end - @all_features [:props, :revenue_goals, :funnels] + @all_features [:props, :revenue_goals, :funnels, :stats_api] @doc """ Returns a list of extra features the user can use. Trial users have the ability to use all features during their trial. diff --git a/lib/plausible/goals.ex b/lib/plausible/goals.ex index 7d2f3f479..170ae0ee2 100644 --- a/lib/plausible/goals.ex +++ b/lib/plausible/goals.ex @@ -214,7 +214,8 @@ defmodule Plausible.Goals do defp maybe_check_feature_access(site, changeset) do if Ecto.Changeset.get_field(changeset, :currency) do - Plausible.Billing.Feature.RevenueGoals.check_availability(site) + site = Plausible.Repo.preload(site, :owner) + Plausible.Billing.Feature.RevenueGoals.check_availability(site.owner) else :ok end diff --git a/lib/plausible/props.ex b/lib/plausible/props.ex index 8a36a43b3..726da24fe 100644 --- a/lib/plausible/props.ex +++ b/lib/plausible/props.ex @@ -24,7 +24,8 @@ defmodule Plausible.Props do data to be dropped or lost. """ def allow(site, prop_or_props) do - with :ok <- Plausible.Billing.Feature.Props.check_availability(site) do + with site <- Plausible.Repo.preload(site, :owner), + :ok <- Plausible.Billing.Feature.Props.check_availability(site.owner) do site |> allow_changeset(prop_or_props) |> Plausible.Repo.update() diff --git a/lib/plausible_web/components/billing.ex b/lib/plausible_web/components/billing.ex index ee68b6b3b..9f039bb0b 100644 --- a/lib/plausible_web/components/billing.ex +++ b/lib/plausible_web/components/billing.ex @@ -7,7 +7,7 @@ defmodule PlausibleWeb.Components.Billing do alias PlausibleWeb.Router.Helpers, as: Routes alias Plausible.Billing.Subscription - attr(:site, Plausible.Site, required: true) + attr(:billable_user, Plausible.Auth.User, required: true) attr(:current_user, Plausible.Auth.User, required: true) attr(:feature_mod, :atom, required: true, values: Plausible.Billing.Feature.list()) attr(:grandfathered?, :boolean, default: false) @@ -17,8 +17,8 @@ defmodule PlausibleWeb.Components.Billing do # credo:disable-for-next-line Credo.Check.Refactor.CyclomaticComplexity def extra_feature_notice(assigns) do private_preview? = not FunWithFlags.enabled?(:business_tier, for: assigns.current_user) - owner? = assigns.current_user.id == assigns.site.owner.id - has_access? = assigns.feature_mod.check_availability(assigns.site.owner) == :ok + display_upgrade_link? = assigns.current_user.id == assigns.billable_user.id + has_access? = assigns.feature_mod.check_availability(assigns.billable_user) == :ok message = cond do @@ -28,10 +28,10 @@ defmodule PlausibleWeb.Components.Billing do private_preview? -> "#{assigns.feature_mod.display_name()} is an upcoming premium functionality that's free-to-use during the private preview. Pricing will be announced soon." - Plausible.Billing.on_trial?(assigns.site.owner) -> + Plausible.Billing.on_trial?(assigns.billable_user) -> "#{assigns.feature_mod.display_name()} is part of the Plausible Business plan. You can access it during your trial, but you'll need to subscribe to the Business plan to retain access after the trial ends." - not has_access? && owner? -> + not has_access? && display_upgrade_link? -> ~H""" <%= @feature_mod.display_name() %> is part of the Plausible Business plan. To get access to it, please <.link class="underline" href={Routes.billing_path(PlausibleWeb.Endpoint, :upgrade)}> @@ -39,7 +39,7 @@ defmodule PlausibleWeb.Components.Billing do to the Business plan. """ - not has_access? && not owner? -> + not has_access? && not display_upgrade_link? -> "#{assigns.feature_mod.display_name()} is part of the Plausible Business plan. To get access to it, please reach out to the site owner to upgrade your subscription to the Business plan." true -> diff --git a/lib/plausible_web/components/site/feature.ex b/lib/plausible_web/components/site/feature.ex index fbaa8bba9..07239108a 100644 --- a/lib/plausible_web/components/site/feature.ex +++ b/lib/plausible_web/components/site/feature.ex @@ -14,7 +14,7 @@ defmodule PlausibleWeb.Components.Site.Feature do assigns = assigns |> assign(:current_setting, assigns.feature_mod.enabled?(assigns.site)) - |> assign(:disabled?, assigns.feature_mod.check_availability(assigns.site) !== :ok) + |> assign(:disabled?, assigns.feature_mod.check_availability(assigns.site.owner) !== :ok) ~H"""