From b1c4d824bc7c08b82fcd63d2fd5443fb0b1b6da6 Mon Sep 17 00:00:00 2001 From: shibao Date: Fri, 10 Sep 2021 00:18:15 -0400 Subject: [PATCH] centralize allow registration check logic --- lib/cannery/accounts.ex | 8 ++++++++ lib/cannery_web/live/component/topbar.html.leex | 11 +++++++++-- lib/cannery_web/templates/layout/topbar.html.eex | 8 ++++++-- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/lib/cannery/accounts.ex b/lib/cannery/accounts.ex index 688097d0..b1858749 100644 --- a/lib/cannery/accounts.ex +++ b/lib/cannery/accounts.ex @@ -238,6 +238,14 @@ defmodule Cannery.Accounts do :ok end + @doc """ + Returns a boolean if registration is allowed or not + """ + def allow_registration?() do + Application.get_env(:cannery, CanneryWeb.Endpoint)[:registration] == "public" or + list_users_by_role(:admin) |> Enum.empty?() + end + ## Confirmation @doc """ diff --git a/lib/cannery_web/live/component/topbar.html.leex b/lib/cannery_web/live/component/topbar.html.leex index bbf61e9e..fd6d9d09 100644 --- a/lib/cannery_web/live/component/topbar.html.leex +++ b/lib/cannery_web/live/component/topbar.html.leex @@ -48,24 +48,31 @@ <%= link @current_user.email, class: "hover:underline", to: Routes.user_settings_path(CanneryWeb.Endpoint, :edit) %> +
  • - <%= link to: Routes.user_session_path(CanneryWeb.Endpoint, :delete), method: :delete do %> + <%= link to: Routes.user_session_path(CanneryWeb.Endpoint, :delete), method: :delete, + data: [confirm: "Are you sure you want to log out?"] do %> <% end %>
  • - <%= if function_exported?(Routes, :live_dashboard_path, 2) do %> + <%= if @current_user.role == :admin and function_exported?(Routes, :live_dashboard_path, 2) do %>
  • <%= link to: Routes.live_dashboard_path(CanneryWeb.Endpoint, :home) do %> <% end %>
  • <% end %> + <% else %> + + <%= if Accounts.allow_registration?() do %>
  • <%= link "Register", class: "hover:underline", to: Routes.user_registration_path(CanneryWeb.Endpoint, :new) %>
  • + <% end %> +
  • <%= link "Log in", class: "hover:underline", to: Routes.user_session_path(CanneryWeb.Endpoint, :new) %> diff --git a/lib/cannery_web/templates/layout/topbar.html.eex b/lib/cannery_web/templates/layout/topbar.html.eex index 6d327119..884a7715 100644 --- a/lib/cannery_web/templates/layout/topbar.html.eex +++ b/lib/cannery_web/templates/layout/topbar.html.eex @@ -13,15 +13,19 @@ to: Routes.user_settings_path(CanneryWeb.Endpoint, :edit) %>
  • - <%= link to: Routes.user_session_path(CanneryWeb.Endpoint, :delete), method: :delete do %> + <%= link to: Routes.user_session_path(CanneryWeb.Endpoint, :delete), method: :delete, + data: [confirm: "Are you sure you want to log out?"] do %> <% end %>
  • <% else %> + <%= if Accounts.allow_registration?() do %>
  • <%= link "Register", class: "hover:underline", to: Routes.user_registration_path(CanneryWeb.Endpoint, :new) %>
  • + <% end %> +
  • <%= link "Log in", class: "hover:underline", to: Routes.user_session_path(CanneryWeb.Endpoint, :new) %> @@ -29,4 +33,4 @@ <% end %> - \ No newline at end of file +