2022-08-14 09:14:42 -04:00
|
|
|
defmodule Shift73kWeb.EnsureAllowRegistrationPlug do
|
|
|
|
@moduledoc """
|
|
|
|
This plug ensures that there is at least one known User.
|
|
|
|
"""
|
|
|
|
import Plug.Conn
|
|
|
|
import Phoenix.Controller
|
2023-01-28 08:14:32 -05:00
|
|
|
import Shift73k, only: [get_app_allow_reg: 0]
|
2022-08-14 09:14:42 -04:00
|
|
|
|
|
|
|
alias Shift73k.Repo
|
|
|
|
alias Shift73k.Accounts.User
|
|
|
|
|
|
|
|
|
|
|
|
@doc false
|
|
|
|
@spec init(any()) :: any()
|
|
|
|
def init(config), do: config
|
|
|
|
|
|
|
|
@doc false
|
|
|
|
@spec call(Conn.t(), atom() | [atom()]) :: Conn.t()
|
|
|
|
def call(conn, _opts) do
|
|
|
|
# If there aren't even any users, or registration is allowed
|
2023-01-28 08:14:32 -05:00
|
|
|
if !Repo.exists?(User) || get_app_allow_reg() do
|
2022-08-14 09:14:42 -04:00
|
|
|
# We will allow registration
|
|
|
|
conn
|
|
|
|
else
|
|
|
|
# Otherwise,
|
|
|
|
# if app is configured to not allow registration,
|
|
|
|
# and there is a user,
|
|
|
|
# then we redirect to root URL
|
|
|
|
conn
|
|
|
|
|> redirect(to: "/")
|
|
|
|
|> halt()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|