diff --git a/lib/beacon/live_admin/authorization.ex b/lib/beacon/live_admin/authorization.ex deleted file mode 100644 index b8584e70..00000000 --- a/lib/beacon/live_admin/authorization.ex +++ /dev/null @@ -1,18 +0,0 @@ -defmodule Beacon.LiveAdmin.Authorization do - @moduledoc """ - Calls Beacon Authorization API through the cluster. - - The function call is made on the first available node for a site, - which may be running in multiple nodes. - """ - - import Beacon.LiveAdmin.Cluster, only: [call: 4] - - def get_agent(site, session) do - call(site, Beacon.Authorization, :get_agent, [site, session]) - end - - def authorized?(site, agent, operation, context) do - call(site, Beacon.Authorization, :authorized?, [site, agent, operation, context]) - end -end diff --git a/lib/beacon/live_admin/hooks/assign_agent.ex b/lib/beacon/live_admin/hooks/assign_agent.ex deleted file mode 100644 index 0619c926..00000000 --- a/lib/beacon/live_admin/hooks/assign_agent.ex +++ /dev/null @@ -1,26 +0,0 @@ -defmodule Beacon.LiveAdmin.Hooks.AssignAgent do - @moduledoc """ - Assigns the agent on the socket to be used by `Beacon.Authorization`. - - It is presumed you will have already authenticated the agent with your own hook. - See `Beacon.LiveAdmin.Router.beacon_live_admin/2` for details on adding hooks. - """ - - import Phoenix.Component - - def on_mount(:default, %{"site" => site}, session, socket) do - site = String.to_existing_atom(site) - - socket = - assign_new(socket, :agent, fn -> - Beacon.LiveAdmin.Authorization.get_agent(site, session) - end) - - {:cont, socket} - end - - # site is not defined on the initial page - def on_mount(:default, _params, _session, socket) do - {:cont, socket} - end -end diff --git a/lib/beacon/live_admin/hooks/authorized.ex b/lib/beacon/live_admin/hooks/authorized.ex deleted file mode 100644 index 402410d8..00000000 --- a/lib/beacon/live_admin/hooks/authorized.ex +++ /dev/null @@ -1,21 +0,0 @@ -defmodule Beacon.LiveAdmin.Hooks.Authorized do - @moduledoc false - - import Phoenix.LiveView - - def on_mount({mod, action}, %{"site" => site}, _session, socket) do - site = String.to_existing_atom(site) - - if Beacon.LiveAdmin.Authorization.authorized?(site, socket.assigns.agent, action, %{mod: mod}) do - {:cont, socket} - else - redirect_to = Beacon.LiveAdmin.Router.beacon_live_admin_path(socket) - {:halt, redirect(socket, to: redirect_to)} - end - end - - # site is not defined on the initial page - def on_mount({_mod, _action}, _params, _session, socket) do - {:cont, socket} - end -end diff --git a/lib/beacon/live_admin/live/component_editor_live/index.ex b/lib/beacon/live_admin/live/component_editor_live/index.ex index 41bcb53b..8a14eaaf 100644 --- a/lib/beacon/live_admin/live/component_editor_live/index.ex +++ b/lib/beacon/live_admin/live/component_editor_live/index.ex @@ -4,8 +4,6 @@ defmodule Beacon.LiveAdmin.ComponentEditorLive.Index do use Beacon.LiveAdmin.PageBuilder, table: [sort_by: "name"] alias Beacon.LiveAdmin.Content - on_mount {Beacon.LiveAdmin.Hooks.Authorized, {:components, :index}} - @impl true def menu_link(_, :index), do: {:root, "Components"} diff --git a/lib/beacon/live_admin/live/error_page_editor_live/index.ex b/lib/beacon/live_admin/live/error_page_editor_live/index.ex index 4602729a..a2b5cd23 100644 --- a/lib/beacon/live_admin/live/error_page_editor_live/index.ex +++ b/lib/beacon/live_admin/live/error_page_editor_live/index.ex @@ -5,8 +5,6 @@ defmodule Beacon.LiveAdmin.ErrorPageEditorLive.Index do alias Beacon.LiveAdmin.Content - on_mount {Beacon.LiveAdmin.Hooks.Authorized, {:error_pages, :index}} - @impl true def menu_link(_, :index), do: {:root, "Error Pages"} diff --git a/lib/beacon/live_admin/live/layout_editor_live/index.ex b/lib/beacon/live_admin/live/layout_editor_live/index.ex index aaa31fbd..e38163b1 100644 --- a/lib/beacon/live_admin/live/layout_editor_live/index.ex +++ b/lib/beacon/live_admin/live/layout_editor_live/index.ex @@ -4,8 +4,6 @@ defmodule Beacon.LiveAdmin.LayoutEditorLive.Index do use Beacon.LiveAdmin.PageBuilder, table: [sort_by: "title"] alias Beacon.LiveAdmin.Content - on_mount {Beacon.LiveAdmin.Hooks.Authorized, {:layout_editor, :index}} - @impl true def menu_link(_, :index), do: {:root, "Layouts"} diff --git a/lib/beacon/live_admin/live/live_data_editor_live/index.ex b/lib/beacon/live_admin/live/live_data_editor_live/index.ex index 2004a1b5..0419f3c5 100644 --- a/lib/beacon/live_admin/live/live_data_editor_live/index.ex +++ b/lib/beacon/live_admin/live/live_data_editor_live/index.ex @@ -5,8 +5,6 @@ defmodule Beacon.LiveAdmin.LiveDataEditorLive.Index do alias Beacon.LiveAdmin.Content - on_mount {Beacon.LiveAdmin.Hooks.Authorized, {:live_data, :index}} - def menu_link(_, :index), do: {:root, "Live Data"} def menu_link(_, :new), do: {:root, "Live Data"} def menu_link(_, :edit), do: {:root, "Live Data"} diff --git a/lib/beacon/live_admin/live/media_library_live/index.ex b/lib/beacon/live_admin/live/media_library_live/index.ex index 6fc08cba..2612f22f 100644 --- a/lib/beacon/live_admin/live/media_library_live/index.ex +++ b/lib/beacon/live_admin/live/media_library_live/index.ex @@ -4,11 +4,8 @@ defmodule Beacon.LiveAdmin.MediaLibraryLive.Index do use Beacon.LiveAdmin.PageBuilder, table: [sort_by: "file_name"] alias Beacon.LiveAdmin.MediaLibrary - alias Beacon.LiveAdmin.Authorization alias Beacon.MediaLibrary.Asset - on_mount {Beacon.LiveAdmin.Hooks.Authorized, {:media_library, :index}} - @impl true def menu_link(_, action) when action in [:index, :upload, :show], do: {:root, "Media Library"} @@ -22,37 +19,28 @@ defmodule Beacon.LiveAdmin.MediaLibraryLive.Index do @impl true def handle_params(params, _url, %{assigns: assigns} = socket) do - if Authorization.authorized?( - assigns.beacon_page.site, - assigns.agent, - assigns.live_action, - assigns.authn_context - ) do - socket = - Table.handle_params( - socket, - params, - &MediaLibrary.count_assets(&1.site, query: params["query"]) - ) - - %{per_page: per_page, current_page: page, query: query, sort_by: sort_by} = - socket.assigns.beacon_page.table - - assets = - MediaLibrary.list_assets(assigns.beacon_page.site, - per_page: per_page, - page: page, - query: query, - sort: sort_by - ) - - {:noreply, - socket - |> stream(:assets, assets, reset: true) - |> apply_action(assigns.live_action, params)} - else - {:noreply, socket} - end + socket = + Table.handle_params( + socket, + params, + &MediaLibrary.count_assets(&1.site, query: params["query"]) + ) + + %{per_page: per_page, current_page: page, query: query, sort_by: sort_by} = + socket.assigns.beacon_page.table + + assets = + MediaLibrary.list_assets(assigns.beacon_page.site, + per_page: per_page, + page: page, + query: query, + sort: sort_by + ) + + {:noreply, + socket + |> stream(:assets, assets, reset: true) + |> apply_action(assigns.live_action, params)} end defp apply_action(socket, :index, %{"search" => search}) when search not in ["", nil] do @@ -85,42 +73,24 @@ defmodule Beacon.LiveAdmin.MediaLibraryLive.Index do end @impl true - def handle_event("delete", %{"id" => id}, %{assigns: assigns} = socket) do + def handle_event("delete", %{"id" => id}, socket) do site = socket.assigns.beacon_page.site - if Authorization.authorized?( - site, - assigns.agent, - :delete, - Map.put(assigns.authn_context, :resource_id, id) - ) do - asset = MediaLibrary.get_asset_by(site, id: id) - {:ok, _} = MediaLibrary.soft_delete(site, asset) - - path = beacon_live_admin_path(socket, site, "/media_library", search: socket.assigns.search) - socket = push_patch(socket, to: path) - - {:noreply, socket} - else - {:noreply, socket} - end + asset = MediaLibrary.get_asset_by(site, id: id) + {:ok, _} = MediaLibrary.soft_delete(site, asset) + + path = beacon_live_admin_path(socket, site, "/media_library", search: socket.assigns.search) + socket = push_patch(socket, to: path) + + {:noreply, socket} end def handle_event("search", %{"search" => search}, %{assigns: assigns} = socket) do - if Authorization.authorized?( - assigns.beacon_page.site, - assigns.agent, - :search, - assigns.authn_context - ) do - path = - beacon_live_admin_path(socket, assigns.beacon_page.site, "/media_library", search: search) - - socket = push_patch(socket, to: path) - {:noreply, socket} - else - {:noreply, socket} - end + path = + beacon_live_admin_path(socket, assigns.beacon_page.site, "/media_library", search: search) + + socket = push_patch(socket, to: path) + {:noreply, socket} end @impl true @@ -129,7 +99,7 @@ defmodule Beacon.LiveAdmin.MediaLibraryLive.Index do <.header> Media Library <:actions> - <.link :if={Authorization.authorized?(@beacon_page.site, @agent, :upload, @authn_context)} patch={beacon_live_admin_path(@socket, @beacon_page.site, "/media_library/upload")}> + <.link patch={beacon_live_admin_path(@socket, @beacon_page.site, "/media_library/upload")}> <.button class="uppercase">Upload new media @@ -150,13 +120,7 @@ defmodule Beacon.LiveAdmin.MediaLibraryLive.Index do <:col :let={{_, asset}} label="File Name"><%= asset.file_name %> <:col :let={{_, asset}} label="type"><%= asset.media_type %> <:action :let={{_, asset}}> - <.link - :if={Authorization.authorized?(@beacon_page.site, @agent, :upload, @authn_context)} - aria-label="View asset" - title="View asset" - class="flex items-center justify-center w-10 h-10" - patch={beacon_live_admin_path(@socket, @beacon_page.site, "/media_library/#{asset.id}")} - > + <.link aria-label="View asset" title="View asset" class="flex items-center justify-center w-10 h-10" patch={beacon_live_admin_path(@socket, @beacon_page.site, "/media_library/#{asset.id}")}> <.icon name="hero-eye text-[#61758A] hover:text-[#304254]" /> @@ -174,7 +138,6 @@ defmodule Beacon.LiveAdmin.MediaLibraryLive.Index do <:action :let={{_, asset}}> <.link - :if={Authorization.authorized?(@beacon_page.site, @agent, :delete, Map.put(@authn_context, :resource, asset))} phx-click={JS.push("delete", value: %{id: asset.id})} aria-label="Delete asset" title="Delete asset" diff --git a/lib/beacon/live_admin/live/media_library_live/upload_form_component.ex b/lib/beacon/live_admin/live/media_library_live/upload_form_component.ex index 98529b9d..b0ff06b3 100644 --- a/lib/beacon/live_admin/live/media_library_live/upload_form_component.ex +++ b/lib/beacon/live_admin/live/media_library_live/upload_form_component.ex @@ -2,7 +2,6 @@ defmodule Beacon.LiveAdmin.MediaLibraryLive.UploadFormComponent do @moduledoc false use Beacon.LiveAdmin.Web, :live_component - alias Beacon.LiveAdmin.Authorization alias Beacon.LiveAdmin.Config alias Beacon.LiveAdmin.MediaLibrary @@ -65,15 +64,7 @@ defmodule Beacon.LiveAdmin.MediaLibraryLive.UploadFormComponent do <% end %> - <.form - :if={Authorization.authorized?(@site, @agent, :upload, %{mod: :media_library})} - for={%{"site" => @site}} - as={:assets} - id="asset-form" - phx-target={@myself} - phx-change="validate" - phx-submit="save" - > + <.form for={%{"site" => @site}} as={:assets} id="asset-form" phx-target={@myself} phx-change="validate" phx-submit="save">