centralize allow registration check logic

This commit is contained in:
shibao 2021-09-10 00:18:15 -04:00 committed by oliviasculley
parent 707b7215e7
commit b1c4d824bc
3 changed files with 23 additions and 4 deletions

View File

@ -238,6 +238,14 @@ defmodule Cannery.Accounts do
:ok :ok
end 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 ## Confirmation
@doc """ @doc """

View File

@ -48,24 +48,31 @@
<%= link @current_user.email, class: "hover:underline", <%= link @current_user.email, class: "hover:underline",
to: Routes.user_settings_path(CanneryWeb.Endpoint, :edit) %> to: Routes.user_settings_path(CanneryWeb.Endpoint, :edit) %>
</li> </li>
<li> <li>
<%= 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 %>
<i class="fas fa-sign-out-alt"></i> <i class="fas fa-sign-out-alt"></i>
<% end %> <% end %>
</li> </li>
<%= if function_exported?(Routes, :live_dashboard_path, 2) do %> <%= if @current_user.role == :admin and function_exported?(Routes, :live_dashboard_path, 2) do %>
<li> <li>
<%= link to: Routes.live_dashboard_path(CanneryWeb.Endpoint, :home) do %> <%= link to: Routes.live_dashboard_path(CanneryWeb.Endpoint, :home) do %>
<i class="fas fa-tachometer-alt"></i> <i class="fas fa-tachometer-alt"></i>
<% end %> <% end %>
</li> </li>
<% end %> <% end %>
<% else %> <% else %>
<%= if Accounts.allow_registration?() do %>
<li> <li>
<%= link "Register", class: "hover:underline", <%= link "Register", class: "hover:underline",
to: Routes.user_registration_path(CanneryWeb.Endpoint, :new) %> to: Routes.user_registration_path(CanneryWeb.Endpoint, :new) %>
</li> </li>
<% end %>
<li> <li>
<%= link "Log in", class: "hover:underline", <%= link "Log in", class: "hover:underline",
to: Routes.user_session_path(CanneryWeb.Endpoint, :new) %> to: Routes.user_session_path(CanneryWeb.Endpoint, :new) %>

View File

@ -13,15 +13,19 @@
to: Routes.user_settings_path(CanneryWeb.Endpoint, :edit) %> to: Routes.user_settings_path(CanneryWeb.Endpoint, :edit) %>
</li> </li>
<li> <li>
<%= 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 %>
<i class="fas fa-sign-out-alt"></i> <i class="fas fa-sign-out-alt"></i>
<% end %> <% end %>
</li> </li>
<% else %> <% else %>
<%= if Accounts.allow_registration?() do %>
<li> <li>
<%= link "Register", class: "hover:underline", <%= link "Register", class: "hover:underline",
to: Routes.user_registration_path(CanneryWeb.Endpoint, :new) %> to: Routes.user_registration_path(CanneryWeb.Endpoint, :new) %>
</li> </li>
<% end %>
<li> <li>
<%= link "Log in", class: "hover:underline", <%= link "Log in", class: "hover:underline",
to: Routes.user_session_path(CanneryWeb.Endpoint, :new) %> to: Routes.user_session_path(CanneryWeb.Endpoint, :new) %>
@ -29,4 +33,4 @@
<% end %> <% end %>
</ul> </ul>
</div> </div>
</nav> </nav>