defmodule Outlook.PreventRegistration do

  import Plug.Conn
  import Phoenix.Controller

  def prevent_registration(conn, _) do
    if System.get_env("DISABLE_REGISTRATION") && is_registration_path(conn) do
      conn
      |> put_flash(:error, "User Registration is disabled.")
      |> redirect(to: "/users/log_in")
      |> halt()
    else
      conn
    end
  end

  defp is_registration_path(conn) do
    "/users/register" == current_path(conn, %{})
  end
end
