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

This commit is contained in:
2025-05-08 18:05:04 +03:00
parent efbb7e2f46
commit d659d694b0
11 changed files with 241 additions and 110 deletions
@@ -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