philomena/lib/philomena_web/plugs/ensure_user_enabled_plug.ex

39 lines
932 B
Elixir
Raw Normal View History

2019-11-17 19:18:21 +01:00
defmodule PhilomenaWeb.EnsureUserEnabledPlug do
2019-11-15 01:59:51 +01:00
@moduledoc """
This plug ensures that a user is enabled.
## Example
2019-11-17 19:18:21 +01:00
plug PhilomenaWeb.EnsureUserEnabledPlug
2019-11-15 01:59:51 +01:00
"""
2019-11-15 01:59:51 +01:00
alias Phoenix.Controller
alias Plug.Conn
alias PhilomenaWeb.UserAuth
2019-11-15 01:59:51 +01:00
@doc false
@spec init(any()) :: any()
def init(opts), do: opts
@doc false
@spec call(Conn.t(), any()) :: Conn.t()
def call(conn, _opts) do
conn.assigns.current_user
|> disabled_or_unconfirmed?()
2019-11-15 01:59:51 +01:00
|> maybe_halt(conn)
end
defp disabled_or_unconfirmed?(%{deleted_at: deleted_at}) when not is_nil(deleted_at), do: true
defp disabled_or_unconfirmed?(%{confirmed_at: nil}), do: true
defp disabled_or_unconfirmed?(_user), do: false
2019-11-15 01:59:51 +01:00
defp maybe_halt(true, conn) do
conn
|> Controller.put_flash(:error, "Your account is not currently active.")
|> UserAuth.log_out_user()
|> Conn.halt()
2019-11-15 01:59:51 +01:00
end
2020-01-11 05:20:19 +01:00
2019-11-15 01:59:51 +01:00
defp maybe_halt(_any, conn), do: conn
end