feat: allow disabling registration
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
This commit is contained in:
parent
eb18209669
commit
09e4b64663
14 changed files with 344 additions and 5 deletions
55
lib/mv_web/plugs/registration_enabled.ex
Normal file
55
lib/mv_web/plugs/registration_enabled.ex
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
defmodule MvWeb.Plugs.RegistrationEnabled do
|
||||
@moduledoc """
|
||||
When direct registration is disabled in settings:
|
||||
- GET /register is redirected to /sign-in with a flash message.
|
||||
Puts registration_enabled from settings into session for /sign-in and /register
|
||||
so the sign-in LiveView can show or hide the register link.
|
||||
"""
|
||||
import Plug.Conn
|
||||
import Phoenix.Controller
|
||||
|
||||
alias Mv.Membership
|
||||
|
||||
def init(opts), do: opts
|
||||
|
||||
def call(conn, _opts) do
|
||||
conn
|
||||
|> maybe_redirect_register()
|
||||
|> maybe_put_registration_enabled_in_session()
|
||||
end
|
||||
|
||||
defp maybe_redirect_register(conn) do
|
||||
if conn.request_path == "/register" and conn.method == "GET" do
|
||||
case Membership.get_settings() do
|
||||
{:ok, %{registration_enabled: true}} ->
|
||||
conn
|
||||
|
||||
_ ->
|
||||
conn
|
||||
|> put_flash(:info, get_flash_message(conn))
|
||||
|> redirect(to: "/sign-in")
|
||||
|> halt()
|
||||
end
|
||||
else
|
||||
conn
|
||||
end
|
||||
end
|
||||
|
||||
defp get_flash_message(_conn) do
|
||||
Gettext.dgettext(MvWeb.Gettext, "default", "Registration is disabled.")
|
||||
end
|
||||
|
||||
defp maybe_put_registration_enabled_in_session(conn) do
|
||||
if conn.request_path in ["/sign-in", "/register"] do
|
||||
enabled =
|
||||
case Membership.get_settings() do
|
||||
{:ok, %{registration_enabled: enabled?}} -> enabled?
|
||||
_ -> true
|
||||
end
|
||||
|
||||
put_session(conn, "registration_enabled", enabled)
|
||||
else
|
||||
conn
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue