mirror of
https://github.com/philomena-dev/philomena.git
synced 2024-11-23 20:18:00 +01:00
add lockout plugs
This commit is contained in:
parent
f45c7dfce0
commit
e76c3cdcba
3 changed files with 77 additions and 0 deletions
37
lib/philomena_web/plugs/ensure_user_enabled_plug.ex
Normal file
37
lib/philomena_web/plugs/ensure_user_enabled_plug.ex
Normal file
|
@ -0,0 +1,37 @@
|
|||
defmodule PhilomenaWeb.Plugs.EnsureUserEnabledPlug do
|
||||
@moduledoc """
|
||||
This plug ensures that a user is enabled.
|
||||
|
||||
## Example
|
||||
|
||||
plug PhilomenaWeb.Plugs.EnsureUserEnabledPlug
|
||||
"""
|
||||
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
|
38
lib/philomena_web/plugs/ensure_user_not_locked_plug.ex
Normal file
38
lib/philomena_web/plugs/ensure_user_not_locked_plug.ex
Normal file
|
@ -0,0 +1,38 @@
|
|||
defmodule PhilomenaWeb.Plugs.EnsureUserNotLockedPlug do
|
||||
@moduledoc """
|
||||
This plug ensures that a user isn't locked.
|
||||
|
||||
## Example
|
||||
|
||||
plug PhilomenaWeb.EnsureUserNotLockedPlug
|
||||
"""
|
||||
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()
|
||||
|> locked?()
|
||||
|> maybe_halt(conn)
|
||||
end
|
||||
|
||||
defp locked?(%{locked_at: locked_at}) when not is_nil(locked_at), do: true
|
||||
defp locked?(_user), do: false
|
||||
|
||||
defp maybe_halt(true, conn) do
|
||||
{:ok, conn} = Plug.clear_authenticated_user(conn)
|
||||
|
||||
conn
|
||||
|> Controller.put_flash(:error, "Sorry, your account is locked.")
|
||||
|> Controller.redirect(to: Routes.pow_session_path(conn, :new))
|
||||
end
|
||||
defp maybe_halt(_any, conn), do: conn
|
||||
end
|
|
@ -11,6 +11,8 @@ defmodule PhilomenaWeb.Router do
|
|||
plug :put_secure_browser_headers
|
||||
plug PhilomenaWeb.Plugs.ImageFilter
|
||||
plug PhilomenaWeb.Plugs.Pagination
|
||||
plug PhilomenaWeb.Plugs.EnsureUserEnabledPlug
|
||||
plug PhilomenaWeb.Plugs.EnsureUserNotLockedPlug
|
||||
end
|
||||
|
||||
pipeline :api do
|
||||
|
|
Loading…
Reference in a new issue