defmodule BasicSignupAllowlist do import FunCore.BasicSignupAllowlist @signup_allowlist_emails "SIGNUP_ALLOWLIST_EMAILS" @moduledoc """ Checks if an email address is allowed based on the SIGNUP_ALLOWLIST_EMAILS env variable. """ @doc """ Returns the name of the environment variable used for the signup allowlist. """ @spec signup_allowlist_emails() :: String.t() def signup_allowlist_emails, do: @signup_allowlist_emails @doc """ Checks if an email address is allowed based on the SIGNUP_ALLOWLIST_EMAILS env variable. Rules: - Returns true if ALLOWED_EMAILS == "*" (all emails allowed) - Returns true if email matches any entry in the comma-separated list - Returns false if: * Environment variable doesn't exist * Environment variable is empty string * Email not found in allowlist ## Examples iex> BasicSignupAllowlist.forbid_signup_with_any_email() iex> BasicSignupAllowlist.signup_allowed?("joe@example.com") false iex> BasicSignupAllowlist.allow_signup_with_all_emails() iex> BasicSignupAllowlist.signup_allowed?("joe@example.com") true """ @spec signup_allowed?(String.t()) :: boolean() def signup_allowed?(email) do env_value = System.get_env(@signup_allowlist_emails) signup_allowed_fun(env_value, email) end def allow_signup_with_all_emails() do System.put_env(signup_allowlist_emails(), "*") end def forbid_signup_with_any_email() do System.delete_env(signup_allowlist_emails()) end end