From d659d694b0c0707556c6144e9e18558e815e59f2 Mon Sep 17 00:00:00 2001 From: KKlochko Date: Thu, 8 May 2025 18:03:50 +0300 Subject: [PATCH] Update edit actions to prevent change other server's data. --- .../components/my_components/author_card.ex | 123 ++++++++++++------ .../components/my_components/book_card.ex | 123 ++++++++++++------ .../live/author_live/edit.ex | 14 +- .../live/author_live/index.ex | 2 +- .../live/author_live/show.ex | 14 +- .../live/book_live/edit.ex | 20 ++- .../live/book_live/index.ex | 4 +- .../live/book_live/show.ex | 14 +- .../live/publisher_live/edit.ex | 14 +- .../live/publisher_live/index.ex | 5 +- .../live/publisher_live/show.ex | 14 +- 11 files changed, 239 insertions(+), 108 deletions(-) diff --git a/lib/decentralised_book_index_web/components/my_components/author_card.ex b/lib/decentralised_book_index_web/components/my_components/author_card.ex index dbc3b50..471b500 100644 --- a/lib/decentralised_book_index_web/components/my_components/author_card.ex +++ b/lib/decentralised_book_index_web/components/my_components/author_card.ex @@ -10,48 +10,91 @@ defmodule DecentralisedBookIndexWeb.Components.MyComponents.AuthorCard do def author_card(assigns) do ~H"""
- <%= if @current_user != nil and Role.can_moderate?(@current_user.role) do %> -
- - - + <%= if @current_user != nil and Role.can_moderate?(@current_user.role) and is_nil(@author.dbi_server) do %> +
+ + + +
+ <% end %> +
+ <.link navigate={~p"/authors/#{@author.id}"}> + <%= if @author.avatar_url != nil do %> + {"#{@author.name} + <% else %> +
+ + + +
- <% end %> -
- <.link navigate={~p"/authors/#{@author.id}"}> - <%= if @author.avatar_url != nil do %> - {"#{@author.name} - <% else %> -
- -
- <% end %> - + <% end %> + - <.link navigate={~p"/authors/#{@author.id}"}> -
{@author.name}
- - {@author.description} -
+ <.link navigate={~p"/authors/#{@author.id}"}> +
{@author.name}
+ + {@author.brief_description} +
""" end diff --git a/lib/decentralised_book_index_web/components/my_components/book_card.ex b/lib/decentralised_book_index_web/components/my_components/book_card.ex index 7283fa6..fdfd5c5 100644 --- a/lib/decentralised_book_index_web/components/my_components/book_card.ex +++ b/lib/decentralised_book_index_web/components/my_components/book_card.ex @@ -10,50 +10,89 @@ defmodule DecentralisedBookIndexWeb.Components.MyComponents.BookCard do def book_card(assigns) do ~H"""
- <%= if @current_user != nil and Role.can_moderate?(@current_user.role) do %> -
- - - + <%= if @current_user != nil and Role.can_moderate?(@current_user.role) and is_nil(@book.dbi_server) do %> +
+ + + +
+ <% end %> +
+ <.link navigate={~p"/books/#{@book.id}"}> + <%= if @book.cover_image_url != nil do %> + {"#{@book.title} + <% else %> +
+
- <% end %> -
- <.link navigate={~p"/books/#{@book.id}"}> - <%= if @book.cover_image_url != nil do %> - {"#{@book.title} - <% else %> -
- -
- <% end %> - + <% end %> + - <.link navigate={~p"/books/#{@book.id}"}> -
{@book.title}
- - {@book.description} -
+ <.link navigate={~p"/books/#{@book.id}"}> +
{@book.title}
+ + {@book.brief_description} +
""" end diff --git a/lib/decentralised_book_index_web/live/author_live/edit.ex b/lib/decentralised_book_index_web/live/author_live/edit.ex index 421702c..e0fa43e 100644 --- a/lib/decentralised_book_index_web/live/author_live/edit.ex +++ b/lib/decentralised_book_index_web/live/author_live/edit.ex @@ -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 diff --git a/lib/decentralised_book_index_web/live/author_live/index.ex b/lib/decentralised_book_index_web/live/author_live/index.ex index 34487d2..19fba02 100644 --- a/lib/decentralised_book_index_web/live/author_live/index.ex +++ b/lib/decentralised_book_index_web/live/author_live/index.ex @@ -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 diff --git a/lib/decentralised_book_index_web/live/author_live/show.ex b/lib/decentralised_book_index_web/live/author_live/show.ex index 8c3fc60..89fcc35 100644 --- a/lib/decentralised_book_index_web/live/author_live/show.ex +++ b/lib/decentralised_book_index_web/live/author_live/show.ex @@ -15,11 +15,13 @@ defmodule DecentralisedBookIndexWeb.AuthorLive.Show do <.add_button_link patch={~p"/authors/#{@author}/new"}> Alias - <.link patch={~p"/authors/#{@author}/edit"}> - <.edit_button> - Edit - - + <%= if is_nil(@author.dbi_server) do %> + <.link patch={~p"/authors/#{@author}/edit"}> + <.edit_button> + Edit + + + <% end %>
<% end %> @@ -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) diff --git a/lib/decentralised_book_index_web/live/book_live/edit.ex b/lib/decentralised_book_index_web/live/book_live/edit.ex index 21f56b3..f256b27 100644 --- a/lib/decentralised_book_index_web/live/book_live/edit.ex +++ b/lib/decentralised_book_index_web/live/book_live/edit.ex @@ -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 diff --git a/lib/decentralised_book_index_web/live/book_live/index.ex b/lib/decentralised_book_index_web/live/book_live/index.ex index d97dc23..458b15e 100644 --- a/lib/decentralised_book_index_web/live/book_live/index.ex +++ b/lib/decentralised_book_index_web/live/book_live/index.ex @@ -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 diff --git a/lib/decentralised_book_index_web/live/book_live/show.ex b/lib/decentralised_book_index_web/live/book_live/show.ex index 7e7fc27..f46448d 100644 --- a/lib/decentralised_book_index_web/live/book_live/show.ex +++ b/lib/decentralised_book_index_web/live/book_live/show.ex @@ -19,11 +19,13 @@ defmodule DecentralisedBookIndexWeb.BookLive.Show do <.add_button_link patch={~p"/books/#{@book}/new"}> Edition - <.link patch={~p"/books/#{@book}/edit"}> - <.edit_button> - Edit - - + <%= if is_nil(@book.dbi_server) do %> + <.link patch={~p"/books/#{@book}/edit"}> + <.edit_button> + Edit + + + <% end %>
<% end %> @@ -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) diff --git a/lib/decentralised_book_index_web/live/publisher_live/edit.ex b/lib/decentralised_book_index_web/live/publisher_live/edit.ex index df7ac70..1522140 100644 --- a/lib/decentralised_book_index_web/live/publisher_live/edit.ex +++ b/lib/decentralised_book_index_web/live/publisher_live/edit.ex @@ -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 diff --git a/lib/decentralised_book_index_web/live/publisher_live/index.ex b/lib/decentralised_book_index_web/live/publisher_live/index.ex index a943bfb..f2a5542 100644 --- a/lib/decentralised_book_index_web/live/publisher_live/index.ex +++ b/lib/decentralised_book_index_web/live/publisher_live/index.ex @@ -43,7 +43,9 @@ defmodule DecentralisedBookIndexWeb.PublisherLive.Index do <.link navigate={~p"/publishers/#{publisher}"}>Show - <.link patch={~p"/publishers/#{publisher}/edit"}>Edit + <%= if is_nil(publisher.dbi_server) do %> + <.link patch={~p"/publishers/#{publisher}/edit"}>Edit + <% end %> @@ -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 ) diff --git a/lib/decentralised_book_index_web/live/publisher_live/show.ex b/lib/decentralised_book_index_web/live/publisher_live/show.ex index abb9c3f..57dd9aa 100644 --- a/lib/decentralised_book_index_web/live/publisher_live/show.ex +++ b/lib/decentralised_book_index_web/live/publisher_live/show.ex @@ -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 - - + <%= if is_nil(@publisher.dbi_server) do %> + <.link patch={~p"/publishers/#{@publisher}/edit"} phx-click={JS.push_focus()}> + <.edit_button> + Edit + + + <% end %> @@ -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