Update edit actions to prevent change other server's data.

dev
KKlochko 2 months ago
parent efbb7e2f46
commit d659d694b0

@ -10,48 +10,91 @@ defmodule DecentralisedBookIndexWeb.Components.MyComponents.AuthorCard do
def author_card(assigns) do
~H"""
<div class="w-full max-w-sm bg-white border border-gray-200 rounded-lg shadow-sm dark:bg-gray-800 dark:border-gray-700 mx-auto my-3">
<%= if @current_user != nil and Role.can_moderate?(@current_user.role) do %>
<div class="flex justify-end px-4 pt-4">
<button id={"dropdownButton#{@author.id}"} data-dropdown-toggle={"dropdown#{@author.id}"} class="inline-block text-gray-500 dark:text-gray-400 hover:bg-gray-100 dark:hover:bg-gray-700 focus:ring-4 focus:outline-none focus:ring-gray-200 dark:focus:ring-gray-700 rounded-lg text-sm p-1.5" type="button">
<span class="sr-only">Open dropdown</span>
<svg class="w-5 h-5" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="currentColor" viewBox="0 0 16 3">
<path d="M2 0a1.5 1.5 0 1 1 0 3 1.5 1.5 0 0 1 0-3Zm6.041 0a1.5 1.5 0 1 1 0 3 1.5 1.5 0 0 1 0-3ZM14 0a1.5 1.5 0 1 1 0 3 1.5 1.5 0 0 1 0-3Z"/>
</svg>
</button>
<!-- Dropdown menu -->
<div id={"dropdown#{@author.id}"} class="z-10 hidden text-base list-none bg-white divide-y divide-gray-100 rounded-lg shadow-sm w-44 dark:bg-gray-700">
<ul class="py-2" aria-labelledby="dropdownButton">
<li>
<.link navigate={~p"/authors/#{@author.id}/edit"}
class="flex items-center block px-4 py-2 text-sm text-gray-700 hover:bg-gray-100 dark:hover:bg-gray-600 dark:text-gray-200 dark:hover:text-white"
>
<svg xmlns="http://www.w3.org/2000/svg" class="h-4 w-4 mr-2 -ml-0.5" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true">
<path d="M17.414 2.586a2 2 0 00-2.828 0L7 10.172V13h2.828l7.586-7.586a2 2 0 000-2.828z"></path>
<path fill-rule="evenodd" d="M2 6a2 2 0 012-2h4a1 1 0 010 2H4v10h10v-4a1 1 0 112 0v4a2 2 0 01-2 2H4a2 2 0 01-2-2V6z" clip-rule="evenodd"></path>
</svg>
Edit
</.link>
</li>
</ul>
</div>
<%= if @current_user != nil and Role.can_moderate?(@current_user.role) and is_nil(@author.dbi_server) do %>
<div class="flex justify-end px-4 pt-4">
<button
id={"dropdownButton#{@author.id}"}
data-dropdown-toggle={"dropdown#{@author.id}"}
class="inline-block text-gray-500 dark:text-gray-400 hover:bg-gray-100 dark:hover:bg-gray-700 focus:ring-4 focus:outline-none focus:ring-gray-200 dark:focus:ring-gray-700 rounded-lg text-sm p-1.5"
type="button"
>
<span class="sr-only">Open dropdown</span>
<svg
class="w-5 h-5"
aria-hidden="true"
xmlns="http://www.w3.org/2000/svg"
fill="currentColor"
viewBox="0 0 16 3"
>
<path d="M2 0a1.5 1.5 0 1 1 0 3 1.5 1.5 0 0 1 0-3Zm6.041 0a1.5 1.5 0 1 1 0 3 1.5 1.5 0 0 1 0-3ZM14 0a1.5 1.5 0 1 1 0 3 1.5 1.5 0 0 1 0-3Z" />
</svg>
</button>
<!-- Dropdown menu -->
<div
id={"dropdown#{@author.id}"}
class="z-10 hidden text-base list-none bg-white divide-y divide-gray-100 rounded-lg shadow-sm w-44 dark:bg-gray-700"
>
<ul class="py-2" aria-labelledby="dropdownButton">
<li>
<.link
navigate={~p"/authors/#{@author.id}/edit"}
class="flex items-center block px-4 py-2 text-sm text-gray-700 hover:bg-gray-100 dark:hover:bg-gray-600 dark:text-gray-200 dark:hover:text-white"
>
<svg
xmlns="http://www.w3.org/2000/svg"
class="h-4 w-4 mr-2 -ml-0.5"
viewBox="0 0 20 20"
fill="currentColor"
aria-hidden="true"
>
<path d="M17.414 2.586a2 2 0 00-2.828 0L7 10.172V13h2.828l7.586-7.586a2 2 0 000-2.828z">
</path>
<path
fill-rule="evenodd"
d="M2 6a2 2 0 012-2h4a1 1 0 010 2H4v10h10v-4a1 1 0 112 0v4a2 2 0 01-2 2H4a2 2 0 01-2-2V6z"
clip-rule="evenodd"
>
</path>
</svg>
Edit
</.link>
</li>
</ul>
</div>
</div>
<% end %>
<div class="flex flex-col items-center py-5 text-center">
<.link navigate={~p"/authors/#{@author.id}"}>
<%= if @author.avatar_url != nil do %>
<img
class="w-36 h-36 mb-3 rounded-full shadow-lg"
src={@author.avatar_url}
alt={"#{@author.name} image"}
/>
<% else %>
<div class="relative w-36 h-36 overflow-hidden bg-gray-100 rounded-full dark:bg-gray-600">
<svg
class="absolute w-36 h-36 text-gray-400"
fill="currentColor"
viewBox="0 0 20 20"
xmlns="http://www.w3.org/2000/svg"
>
<path
fill-rule="evenodd"
d="M10 9a3 3 0 100-6 3 3 0 000 6zm-7 9a7 7 0 1114 0H3z"
clip-rule="evenodd"
>
</path>
</svg>
</div>
<% end %>
<div class="flex flex-col items-center py-5 text-center">
<.link navigate={~p"/authors/#{@author.id}"}>
<%= if @author.avatar_url != nil do %>
<img class="w-36 h-36 mb-3 rounded-full shadow-lg" src={@author.avatar_url} alt={"#{@author.name} image"} />
<% else %>
<div class="relative w-36 h-36 overflow-hidden bg-gray-100 rounded-full dark:bg-gray-600">
<svg class="absolute w-36 h-36 text-gray-400" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" d="M10 9a3 3 0 100-6 3 3 0 000 6zm-7 9a7 7 0 1114 0H3z" clip-rule="evenodd"></path></svg>
</div>
<% end %>
</.link>
<% end %>
</.link>
<.link navigate={~p"/authors/#{@author.id}"}>
<h5 class="mb-1 text-xl font-medium text-gray-900 dark:text-white">{@author.name}</h5>
</.link>
<span class="text-sm text-gray-500 dark:text-gray-400">{@author.description}</span>
</div>
<.link navigate={~p"/authors/#{@author.id}"}>
<h5 class="mb-1 text-xl font-medium text-gray-900 dark:text-white">{@author.name}</h5>
</.link>
<span class="text-sm text-gray-500 dark:text-gray-400">{@author.brief_description}</span>
</div>
</div>
"""
end

@ -10,50 +10,89 @@ defmodule DecentralisedBookIndexWeb.Components.MyComponents.BookCard do
def book_card(assigns) do
~H"""
<div class="w-full max-w-sm bg-white border border-gray-200 rounded-lg shadow-sm dark:bg-gray-800 dark:border-gray-700 mx-auto my-3">
<%= if @current_user != nil and Role.can_moderate?(@current_user.role) do %>
<div class="flex justify-end px-4 pt-4">
<button id={"dropdownButton#{@book.id}"} data-dropdown-toggle={"dropdown#{@book.id}"} class="inline-block text-gray-500 dark:text-gray-400 hover:bg-gray-100 dark:hover:bg-gray-700 focus:ring-4 focus:outline-none focus:ring-gray-200 dark:focus:ring-gray-700 rounded-lg text-sm p-1.5" type="button">
<span class="sr-only">Open dropdown</span>
<svg class="w-5 h-5" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="currentColor" viewBox="0 0 16 3">
<path d="M2 0a1.5 1.5 0 1 1 0 3 1.5 1.5 0 0 1 0-3Zm6.041 0a1.5 1.5 0 1 1 0 3 1.5 1.5 0 0 1 0-3ZM14 0a1.5 1.5 0 1 1 0 3 1.5 1.5 0 0 1 0-3Z"/>
</svg>
</button>
<!-- Dropdown menu -->
<div id={"dropdown#{@book.id}"} class="z-10 hidden text-base list-none bg-white divide-y divide-gray-100 rounded-lg shadow-sm w-44 dark:bg-gray-700">
<ul class="py-2" aria-labelledby="dropdownButton">
<li>
<.link navigate={~p"/books/#{@book.id}/edit"}
class="flex items-center block px-4 py-2 text-sm text-gray-700 hover:bg-gray-100 dark:hover:bg-gray-600 dark:text-gray-200 dark:hover:text-white"
>
<svg xmlns="http://www.w3.org/2000/svg" class="h-4 w-4 mr-2 -ml-0.5" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true">
<path d="M17.414 2.586a2 2 0 00-2.828 0L7 10.172V13h2.828l7.586-7.586a2 2 0 000-2.828z"></path>
<path fill-rule="evenodd" d="M2 6a2 2 0 012-2h4a1 1 0 010 2H4v10h10v-4a1 1 0 112 0v4a2 2 0 01-2 2H4a2 2 0 01-2-2V6z" clip-rule="evenodd"></path>
</svg>
Edit
</.link>
</li>
</ul>
</div>
<%= if @current_user != nil and Role.can_moderate?(@current_user.role) and is_nil(@book.dbi_server) do %>
<div class="flex justify-end px-4 pt-4">
<button
id={"dropdownButton#{@book.id}"}
data-dropdown-toggle={"dropdown#{@book.id}"}
class="inline-block text-gray-500 dark:text-gray-400 hover:bg-gray-100 dark:hover:bg-gray-700 focus:ring-4 focus:outline-none focus:ring-gray-200 dark:focus:ring-gray-700 rounded-lg text-sm p-1.5"
type="button"
>
<span class="sr-only">Open dropdown</span>
<svg
class="w-5 h-5"
aria-hidden="true"
xmlns="http://www.w3.org/2000/svg"
fill="currentColor"
viewBox="0 0 16 3"
>
<path d="M2 0a1.5 1.5 0 1 1 0 3 1.5 1.5 0 0 1 0-3Zm6.041 0a1.5 1.5 0 1 1 0 3 1.5 1.5 0 0 1 0-3ZM14 0a1.5 1.5 0 1 1 0 3 1.5 1.5 0 0 1 0-3Z" />
</svg>
</button>
<!-- Dropdown menu -->
<div
id={"dropdown#{@book.id}"}
class="z-10 hidden text-base list-none bg-white divide-y divide-gray-100 rounded-lg shadow-sm w-44 dark:bg-gray-700"
>
<ul class="py-2" aria-labelledby="dropdownButton">
<li>
<.link
navigate={~p"/books/#{@book.id}/edit"}
class="flex items-center block px-4 py-2 text-sm text-gray-700 hover:bg-gray-100 dark:hover:bg-gray-600 dark:text-gray-200 dark:hover:text-white"
>
<svg
xmlns="http://www.w3.org/2000/svg"
class="h-4 w-4 mr-2 -ml-0.5"
viewBox="0 0 20 20"
fill="currentColor"
aria-hidden="true"
>
<path d="M17.414 2.586a2 2 0 00-2.828 0L7 10.172V13h2.828l7.586-7.586a2 2 0 000-2.828z">
</path>
<path
fill-rule="evenodd"
d="M2 6a2 2 0 012-2h4a1 1 0 010 2H4v10h10v-4a1 1 0 112 0v4a2 2 0 01-2 2H4a2 2 0 01-2-2V6z"
clip-rule="evenodd"
>
</path>
</svg>
Edit
</.link>
</li>
</ul>
</div>
</div>
<% end %>
<div class="flex flex-col items-center py-5 text-center">
<.link navigate={~p"/books/#{@book.id}"}>
<%= if @book.cover_image_url != nil do %>
<img class="w-36 mb-3 shadow-lg" src={@book.cover_image_url} alt={"#{@book.title} image"} />
<% else %>
<div class="relative w-36 h-36 overflow-hidden">
<svg
class="w-36 h-36 text-gray-800 dark:text-white"
aria-hidden="true"
xmlns="http://www.w3.org/2000/svg"
width="24"
height="24"
fill="currentColor"
viewBox="0 0 24 24"
>
<path
fill-rule="evenodd"
d="M6 2a2 2 0 0 0-2 2v15a3 3 0 0 0 3 3h12a1 1 0 1 0 0-2h-2v-2h2a1 1 0 0 0 1-1V4a2 2 0 0 0-2-2h-8v16h5v2H7a1 1 0 1 1 0-2h1V2H6Z"
clip-rule="evenodd"
/>
</svg>
</div>
<% end %>
<div class="flex flex-col items-center py-5 text-center">
<.link navigate={~p"/books/#{@book.id}"}>
<%= if @book.cover_image_url != nil do %>
<img class="w-36 mb-3 shadow-lg" src={@book.cover_image_url} alt={"#{@book.title} image"} />
<% else %>
<div class="relative w-36 h-36 overflow-hidden">
<svg class="w-36 h-36 text-gray-800 dark:text-white" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="currentColor" viewBox="0 0 24 24">
<path fill-rule="evenodd" d="M6 2a2 2 0 0 0-2 2v15a3 3 0 0 0 3 3h12a1 1 0 1 0 0-2h-2v-2h2a1 1 0 0 0 1-1V4a2 2 0 0 0-2-2h-8v16h5v2H7a1 1 0 1 1 0-2h1V2H6Z" clip-rule="evenodd"/>
</svg>
</div>
<% end %>
</.link>
<% end %>
</.link>
<.link navigate={~p"/books/#{@book.id}"}>
<h5 class="mb-1 text-xl font-medium text-gray-900 dark:text-white">{@book.title}</h5>
</.link>
<span class="text-sm text-gray-500 dark:text-gray-400">{@book.description}</span>
</div>
<.link navigate={~p"/books/#{@book.id}"}>
<h5 class="mb-1 text-xl font-medium text-gray-900 dark:text-white">{@book.title}</h5>
</.link>
<span class="text-sm text-gray-500 dark:text-gray-400">{@book.brief_description}</span>
</div>
</div>
"""
end

@ -34,6 +34,7 @@ defmodule DecentralisedBookIndexWeb.AuthorLive.Edit do
socket
|> assign(:params, params)
|> apply_action(socket.assigns.live_action, params)
|> redirect_if_not_editable()
{:noreply, socket}
end
@ -43,7 +44,7 @@ defmodule DecentralisedBookIndexWeb.AuthorLive.Edit do
|> assign(:page_title, "Edit Author")
|> assign(
:author,
Ash.get!(Metadata.Author, id, actor: socket.assigns.current_user)
Ash.get!(Metadata.Author, id, load: [:dbi_server], actor: socket.assigns.current_user)
)
end
@ -57,4 +58,15 @@ defmodule DecentralisedBookIndexWeb.AuthorLive.Edit do
def handle_info({DecentralisedBookIndexWeb.AuthorLive.FormComponent, {:saved, _author}}, socket) do
{:noreply, socket}
end
defp redirect_if_not_editable(socket) do
if not is_nil(socket.assigns.author.dbi_server) do
socket =
socket
|> Phoenix.LiveView.put_flash(:error, "Can't edit other server's data!")
|> Phoenix.LiveView.redirect(to: ~p"/authors/#{socket.assigns.author}")
else
socket
end
end
end

@ -63,7 +63,7 @@ defmodule DecentralisedBookIndexWeb.AuthorLive.Index do
page =
Metadata.search_author!(
search_query,
load: [:brief_description],
load: [:brief_description, :dbi_server],
query: [sort_input: sort_by],
page: page_params ++ [count: true],
actor: socket.assigns.current_user

@ -15,11 +15,13 @@ defmodule DecentralisedBookIndexWeb.AuthorLive.Show do
<.add_button_link patch={~p"/authors/#{@author}/new"}>
Alias
</.add_button_link>
<.link patch={~p"/authors/#{@author}/edit"}>
<.edit_button>
Edit
</.edit_button>
</.link>
<%= if is_nil(@author.dbi_server) do %>
<.link patch={~p"/authors/#{@author}/edit"}>
<.edit_button>
Edit
</.edit_button>
</.link>
<% end %>
</div>
<% end %>
</:actions>
@ -87,7 +89,7 @@ defmodule DecentralisedBookIndexWeb.AuthorLive.Show do
@impl true
def handle_params(%{"id" => id}, _, socket) do
author =
Ash.get!(DecentralisedBookIndex.Metadata.Author, id, actor: socket.assigns.current_user)
Ash.get!(DecentralisedBookIndex.Metadata.Author, id, load: [:dbi_server], actor: socket.assigns.current_user)
alternative_names = DecentralisedBookIndex.Metadata.get_author_alternative_names!(author)

@ -28,7 +28,12 @@ defmodule DecentralisedBookIndexWeb.BookLive.Edit do
@impl true
def handle_params(params, _url, socket) do
{:noreply, apply_action(socket, socket.assigns.live_action, params)}
socket =
socket
|> apply_action(socket.assigns.live_action, params)
|> redirect_if_not_editable()
{:noreply, socket}
end
defp apply_action(socket, :edit, %{"id" => id}) do
@ -36,7 +41,7 @@ defmodule DecentralisedBookIndexWeb.BookLive.Edit do
|> assign(:page_title, "Edit Book")
|> assign(
:book,
Ash.get!(DecentralisedBookIndex.Metadata.Book, id, actor: socket.assigns.current_user)
Ash.get!(DecentralisedBookIndex.Metadata.Book, id, load: [:dbi_server], actor: socket.assigns.current_user)
)
end
@ -50,4 +55,15 @@ defmodule DecentralisedBookIndexWeb.BookLive.Edit do
def handle_info({DecentralisedBookIndexWeb.BookLive.FormComponent, {:saved, _book}}, socket) do
{:noreply, socket}
end
defp redirect_if_not_editable(socket) do
if not is_nil(socket.assigns.book.dbi_server) do
socket =
socket
|> Phoenix.LiveView.put_flash(:error, "Can't edit other server's data!")
|> Phoenix.LiveView.redirect(to: ~p"/books/#{socket.assigns.book}")
else
socket
end
end
end

@ -189,7 +189,7 @@ defmodule DecentralisedBookIndexWeb.BookLive.Index do
"title" ->
Metadata.search_book!(
search_query,
load: [:brief_description],
load: [:brief_description, :dbi_server],
query: [sort_input: sort_by],
page: page_params ++ [count: true],
actor: actor
@ -199,7 +199,7 @@ defmodule DecentralisedBookIndexWeb.BookLive.Index do
Metadata.search_book_by_bid!(
type,
search_query,
load: [:brief_description],
load: [:brief_description, :dbi_server],
query: [sort_input: sort_by],
page: page_params ++ [count: true],
actor: actor

@ -19,11 +19,13 @@ defmodule DecentralisedBookIndexWeb.BookLive.Show do
<.add_button_link patch={~p"/books/#{@book}/new"}>
Edition
</.add_button_link>
<.link patch={~p"/books/#{@book}/edit"}>
<.edit_button>
Edit
</.edit_button>
</.link>
<%= if is_nil(@book.dbi_server) do %>
<.link patch={~p"/books/#{@book}/edit"}>
<.edit_button>
Edit
</.edit_button>
</.link>
<% end %>
</div>
<% end %>
</:actions>
@ -100,7 +102,7 @@ defmodule DecentralisedBookIndexWeb.BookLive.Show do
book =
Ash.get!(Metadata.Book, id,
actor: socket.assigns.current_user,
load: [:bids, :author_roles, :publisher]
load: [:bids, :author_roles, :publisher, :dbi_server]
)
alternative_editions = Metadata.get_book_alternative_editions!(book)

@ -30,6 +30,7 @@ defmodule DecentralisedBookIndexWeb.PublisherLive.Edit do
socket
|> assign(:params, params)
|> apply_action(socket.assigns.live_action, params)
|> redirect_if_not_editable()
{:noreply, socket}
end
@ -39,7 +40,7 @@ defmodule DecentralisedBookIndexWeb.PublisherLive.Edit do
|> assign(:page_title, "Edit Publisher")
|> assign(
:publisher,
Ash.get!(Metadata.Publisher, id, actor: socket.assigns.current_user)
Ash.get!(Metadata.Publisher, id, load: [:dbi_server], actor: socket.assigns.current_user)
)
end
@ -56,4 +57,15 @@ defmodule DecentralisedBookIndexWeb.PublisherLive.Edit do
) do
{:noreply, socket}
end
defp redirect_if_not_editable(socket) do
if not is_nil(socket.assigns.publisher.dbi_server) do
socket =
socket
|> Phoenix.LiveView.put_flash(:error, "Can't edit other server's data!")
|> Phoenix.LiveView.redirect(to: ~p"/publishers/#{socket.assigns.publisher}")
else
socket
end
end
end

@ -43,7 +43,9 @@ defmodule DecentralisedBookIndexWeb.PublisherLive.Index do
<.link navigate={~p"/publishers/#{publisher}"}>Show</.link>
</div>
<.link patch={~p"/publishers/#{publisher}/edit"}>Edit</.link>
<%= if is_nil(publisher.dbi_server) do %>
<.link patch={~p"/publishers/#{publisher}/edit"}>Edit</.link>
<% end %>
</:action>
</.table>
@ -76,6 +78,7 @@ defmodule DecentralisedBookIndexWeb.PublisherLive.Index do
search_query,
query: [sort_input: sort_by],
page: page_params ++ [count: true],
load: [:dbi_server],
actor: socket.assigns.current_user
)

@ -8,11 +8,13 @@ defmodule DecentralisedBookIndexWeb.PublisherLive.Show do
{@publisher.name}
<:actions>
<.link patch={~p"/publishers/#{@publisher}/edit"} phx-click={JS.push_focus()}>
<.edit_button>
Edit
</.edit_button>
</.link>
<%= if is_nil(@publisher.dbi_server) do %>
<.link patch={~p"/publishers/#{@publisher}/edit"} phx-click={JS.push_focus()}>
<.edit_button>
Edit
</.edit_button>
</.link>
<% end %>
</:actions>
</.header>
@ -32,7 +34,7 @@ defmodule DecentralisedBookIndexWeb.PublisherLive.Show do
|> assign(:page_title, page_title(socket.assigns.live_action))
|> assign(
:publisher,
Ash.get!(DecentralisedBookIndex.Metadata.Publisher, id, actor: socket.assigns.current_user)
Ash.get!(DecentralisedBookIndex.Metadata.Publisher, id, load: [:dbi_server], actor: socket.assigns.current_user)
)}
end

Loading…
Cancel
Save