From c5853fc2aa2843110c19a943fd89bc6eea292b45 Mon Sep 17 00:00:00 2001 From: Thelonius Kort Date: Tue, 31 Jan 2023 18:16:28 +0100 Subject: [PATCH] Add configurable prevention of user registration Should be done appropriately soon. --- .../components/layouts/root.html.heex | 3 --- lib/outlook_web/live/user_login_live.ex | 2 ++ lib/outlook_web/prevent_registration.ex | 20 +++++++++++++++++++ lib/outlook_web/router.ex | 8 +++++++- 4 files changed, 29 insertions(+), 4 deletions(-) create mode 100644 lib/outlook_web/prevent_registration.ex diff --git a/lib/outlook_web/components/layouts/root.html.heex b/lib/outlook_web/components/layouts/root.html.heex index f32ad82..a20365f 100644 --- a/lib/outlook_web/components/layouts/root.html.heex +++ b/lib/outlook_web/components/layouts/root.html.heex @@ -24,9 +24,6 @@ <.link href={~p"/users/log_out"} method="delete">Log out <% else %> -
  • - <.link href={~p"/users/register"}>Register -
  • <.link href={~p"/users/log_in"}>Log in
  • diff --git a/lib/outlook_web/live/user_login_live.ex b/lib/outlook_web/live/user_login_live.ex index 5dbe6ea..b3ebd0e 100644 --- a/lib/outlook_web/live/user_login_live.ex +++ b/lib/outlook_web/live/user_login_live.ex @@ -7,11 +7,13 @@ defmodule OutlookWeb.UserLoginLive do <.header class="text-center"> Sign in to account <:subtitle> + <%= unless System.get_env("DISABLE_REGISTRATION") do %> Don't have an account? <.link navigate={~p"/users/register"} class="font-semibold text-brand hover:underline"> Sign up for an account now. + <% end %> diff --git a/lib/outlook_web/prevent_registration.ex b/lib/outlook_web/prevent_registration.ex new file mode 100644 index 0000000..3d3e0c0 --- /dev/null +++ b/lib/outlook_web/prevent_registration.ex @@ -0,0 +1,20 @@ +defmodule Outlook.PreventRegistration do + + import Plug.Conn + import Phoenix.Controller + + def prevent_registration(conn, _) do + if System.get_env("DISABLE_REGISTRATION") && is_registration_path(conn) do + conn + |> put_flash(:error, "User Registration is disabled.") + |> redirect(to: "/users/log_in") + |> halt() + else + conn + end + end + + defp is_registration_path(conn) do + "/users/register" == current_path(conn, %{}) + end +end diff --git a/lib/outlook_web/router.ex b/lib/outlook_web/router.ex index 3ae341c..934a178 100644 --- a/lib/outlook_web/router.ex +++ b/lib/outlook_web/router.ex @@ -3,6 +3,8 @@ defmodule OutlookWeb.Router do import OutlookWeb.UserAuth + import Outlook.PreventRegistration + pipeline :browser do plug :accepts, ["html"] plug :fetch_session @@ -18,6 +20,10 @@ defmodule OutlookWeb.Router do plug :put_layout, {OutlookWeb.Layouts, :public} end + pipeline :check_registration do + plug :prevent_registration + end + pipeline :api do plug :accepts, ["json"] end @@ -56,7 +62,7 @@ defmodule OutlookWeb.Router do ## Authentication routes scope "/", OutlookWeb do - pipe_through [:browser, :redirect_if_user_is_authenticated] + pipe_through [:browser, :redirect_if_user_is_authenticated, :check_registration] live_session :redirect_if_user_is_authenticated, on_mount: [{OutlookWeb.UserAuth, :redirect_if_user_is_authenticated}] do