From 632c2b348013585a8769886e22963f7b67ac40b6 Mon Sep 17 00:00:00 2001 From: shibao Date: Thu, 24 Nov 2022 17:14:53 -0500 Subject: [PATCH] add Accounts.is_already_admin? check --- lib/memex/accounts.ex | 10 ++++++++-- lib/memex_web/components/topbar.ex | 2 +- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/memex/accounts.ex b/lib/memex/accounts.ex index 6ddbc49..c0cc6cf 100644 --- a/lib/memex/accounts.ex +++ b/lib/memex/accounts.ex @@ -363,10 +363,16 @@ defmodule Memex.Accounts do """ @spec is_admin?(User.t()) :: boolean() def is_admin?(%User{id: user_id}) do - Repo.one(from u in User, where: u.id == ^user_id and u.role == :admin) - |> is_nil() + Repo.exists?(from u in User, where: u.id == ^user_id and u.role == :admin) end + @doc """ + Checks to see if user has the admin role + """ + @spec is_already_admin?(User.t() | nil) :: boolean() + def is_already_admin?(%User{role: :admin}), do: true + def is_already_admin?(_invalid_user), do: false + ## Confirmation @doc """ diff --git a/lib/memex_web/components/topbar.ex b/lib/memex_web/components/topbar.ex index 48e24dc..abaa8c7 100644 --- a/lib/memex_web/components/topbar.ex +++ b/lib/memex_web/components/topbar.ex @@ -65,7 +65,7 @@ defmodule MemexWeb.Components.Topbar do
  • <%= if @current_user do %> - <%= if @current_user.role == :admin do %> + <%= if @current_user |> Accounts.is_already_admin?() do %>
  • <.link navigate={Routes.invite_index_path(Endpoint, :index)}