Skip to content

Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.

1.0 KB
Raw Blame
defmodule PlausibleWeb.Plugs.FeatureFlagCheckPlug do
  @moduledoc """
  plug(PlausibleWeb.Plugs.FeatureFlagCheckPlug, [:flag_foo, :flag_bar])
  to halt any API connections with 404 where conn.assigns.current_user or conn.assigns.site
  don't have both feature flags true.
  """
  def init(feature_flags) when is_list(feature_flags) and feature_flags != [] do
    feature_flags
  end

  def init(_),
    do: raise(ArgumentError, "The first argument must be a non-empty list of feature flags")

  def call(%Plug.Conn{} = conn, flags) do
    if validate(conn.assigns[:current_user], conn.assigns.site, flags) do
      conn
    else
      PlausibleWeb.Api.Helpers.not_found(conn, "Not found")
    end
  end

  defp validate(nil = _current_user, site, flags),
    do: Enum.all?(flags, fn flag -> FunWithFlags.enabled?(flag, for: site) end)

  defp validate(current_user, site, flags),
    do:
      Enum.all?(flags, fn flag ->
        FunWithFlags.enabled?(flag, for: current_user) ||
          FunWithFlags.enabled?(flag, for: site)
      end)
end