From 44c8cf77bbfeac1f390238cb189ddba2f9c2adc0 Mon Sep 17 00:00:00 2001
From: shibao
Date: Thu, 24 Nov 2022 17:15:10 -0500
Subject: [PATCH] only show action buttons when appropriate
---
lib/memex_web/live/context_live/index.ex | 11 ++++++++++-
.../live/context_live/index.html.heex | 4 +++-
lib/memex_web/live/context_live/show.ex | 11 ++++++++++-
lib/memex_web/live/context_live/show.html.heex | 7 ++++---
lib/memex_web/live/note_live/index.ex | 11 ++++++++++-
lib/memex_web/live/note_live/index.html.heex | 4 +++-
lib/memex_web/live/note_live/show.ex | 11 ++++++++++-
lib/memex_web/live/note_live/show.html.heex | 7 ++++---
lib/memex_web/live/pipeline_live/index.ex | 11 ++++++++++-
.../live/pipeline_live/index.html.heex | 4 +++-
lib/memex_web/live/pipeline_live/show.ex | 11 ++++++++++-
.../live/pipeline_live/show.html.heex | 7 ++++---
priv/gettext/actions.pot | 18 +++++++++---------
priv/gettext/prompts.pot | 12 ++++++------
14 files changed, 96 insertions(+), 33 deletions(-)
diff --git a/lib/memex_web/live/context_live/index.ex b/lib/memex_web/live/context_live/index.ex
index d4c0666..e9efdaa 100644
--- a/lib/memex_web/live/context_live/index.ex
+++ b/lib/memex_web/live/context_live/index.ex
@@ -1,6 +1,6 @@
defmodule MemexWeb.ContextLive.Index do
use MemexWeb, :live_view
- alias Memex.{Contexts, Contexts.Context}
+ alias Memex.{Accounts.User, Contexts, Contexts.Context}
@impl true
def mount(%{"search" => search}, _session, socket) do
@@ -77,4 +77,13 @@ defmodule MemexWeb.ContextLive.Index do
defp display_contexts(%{assigns: %{search: search}} = socket) do
socket |> assign(contexts: Contexts.list_public_contexts(search))
end
+
+ @spec is_owner_or_admin?(Context.t(), User.t()) :: boolean()
+ defp is_owner_or_admin?(%{user_id: user_id}, %{id: user_id}), do: true
+ defp is_owner_or_admin?(_context, %{role: :admin}), do: true
+ defp is_owner_or_admin?(_context, _other_user), do: false
+
+ @spec is_owner?(Context.t(), User.t()) :: boolean()
+ defp is_owner?(%{user_id: user_id}, %{id: user_id}), do: true
+ defp is_owner?(_context, _other_user), do: false
end
diff --git a/lib/memex_web/live/context_live/index.html.heex b/lib/memex_web/live/context_live/index.html.heex
index ab8ae63..4e8bdbb 100644
--- a/lib/memex_web/live/context_live/index.html.heex
+++ b/lib/memex_web/live/context_live/index.html.heex
@@ -30,13 +30,15 @@
contexts={@contexts}
>
<:actions :let={context}>
- <%= if @current_user do %>
+ <%= if is_owner?(context, @current_user) do %>
<.link
patch={Routes.context_index_path(@socket, :edit, context)}
data-qa={"context-edit-#{context.id}"}
>
<%= dgettext("actions", "edit") %>
+ <% end %>
+ <%= if is_owner_or_admin?(context, @current_user) do %>
<.link
href="#"
phx-click="delete"
diff --git a/lib/memex_web/live/context_live/show.ex b/lib/memex_web/live/context_live/show.ex
index 3e3239a..7215374 100644
--- a/lib/memex_web/live/context_live/show.ex
+++ b/lib/memex_web/live/context_live/show.ex
@@ -1,7 +1,7 @@
defmodule MemexWeb.ContextLive.Show do
use MemexWeb, :live_view
- alias Memex.Contexts
+ alias Memex.{Accounts.User, Contexts, Contexts.Context}
@impl true
def mount(_params, _session, socket) do
@@ -38,4 +38,13 @@ defmodule MemexWeb.ContextLive.Show do
defp page_title(:show), do: gettext("show context")
defp page_title(:edit), do: gettext("edit context")
+
+ @spec is_owner_or_admin?(Context.t(), User.t()) :: boolean()
+ defp is_owner_or_admin?(%{user_id: user_id}, %{id: user_id}), do: true
+ defp is_owner_or_admin?(_context, %{role: :admin}), do: true
+ defp is_owner_or_admin?(_context, _other_user), do: false
+
+ @spec is_owner?(Context.t(), User.t()) :: boolean()
+ defp is_owner?(%{user_id: user_id}, %{id: user_id}), do: true
+ defp is_owner?(_context, _other_user), do: false
end
diff --git a/lib/memex_web/live/context_live/show.html.heex b/lib/memex_web/live/context_live/show.html.heex
index c1fe16e..716cd07 100644
--- a/lib/memex_web/live/context_live/show.html.heex
+++ b/lib/memex_web/live/context_live/show.html.heex
@@ -19,14 +19,15 @@
- <.link class="btn btn-primary" patch={Routes.context_index_path(@socket, :index)}>
+ <.link class="btn btn-primary" navigate={Routes.context_index_path(@socket, :index)}>
<%= dgettext("actions", "back") %>
- <%= if @current_user do %>
+ <%= if is_owner?(@context, @current_user) do %>
<.link class="btn btn-primary" patch={Routes.context_show_path(@socket, :edit, @context)}>
<%= dgettext("actions", "edit") %>
-
+ <% end %>
+ <%= if is_owner_or_admin?(@context, @current_user) do %>