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
|
|
|
"""
|
|
|
|
alias PhilomenaWeb.Router.Helpers, as: Routes
|
|
|
|
alias Phoenix.Controller
|
|
|
|
alias Plug.Conn
|
|
|
|
alias Pow.Plug
|
|
|
|
|
|
|
|
@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
|
|
|
|
|> Plug.current_user()
|
|
|
|
|> disabled?()
|
|
|
|
|> maybe_halt(conn)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp disabled?(%{deleted_at: deleted_at}) when not is_nil(deleted_at), do: true
|
|
|
|
defp disabled?(_user), do: false
|
|
|
|
|
|
|
|
defp maybe_halt(true, conn) do
|
|
|
|
{:ok, conn} = Plug.clear_authenticated_user(conn)
|
|
|
|
|
|
|
|
conn
|
|
|
|
|> Controller.redirect(to: Routes.pow_session_path(conn, :new))
|
|
|
|
end
|
|
|
|
defp maybe_halt(_any, conn), do: conn
|
|
|
|
end
|