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">
<.live_file_input upload={@uploads.asset} tabindex="0" />
diff --git a/lib/beacon/live_admin/live/page_editor_live/index.ex b/lib/beacon/live_admin/live/page_editor_live/index.ex index 91709b53..ff59cae0 100644 --- a/lib/beacon/live_admin/live/page_editor_live/index.ex +++ b/lib/beacon/live_admin/live/page_editor_live/index.ex @@ -5,8 +5,6 @@ defmodule Beacon.LiveAdmin.PageEditorLive.Index do alias Beacon.LiveAdmin.Content - on_mount {Beacon.LiveAdmin.Hooks.Authorized, {:page_editor, :index}} - @impl true def menu_link(_, :index), do: {:root, "Pages"} diff --git a/lib/beacon/live_admin/router.ex b/lib/beacon/live_admin/router.ex index 2a76fd4b..07738ede 100644 --- a/lib/beacon/live_admin/router.ex +++ b/lib/beacon/live_admin/router.ex @@ -227,18 +227,16 @@ defmodule Beacon.LiveAdmin.Router do end defp get_on_mount_list(on_mounts) when is_list(on_mounts) do - if Enum.member?(on_mounts, Beacon.LiveAdmin.Hooks.AssignAgent) do - on_mounts - else - on_mounts ++ [Beacon.LiveAdmin.Hooks.AssignAgent] - end + on_mounts end defp get_on_mount_list(on_mounts) do raise ArgumentError, """ - expected `on_mount` option to be a list. + expected `on_mount` option to be a list + + Got: - Got: #{inspect(on_mounts)} + #{inspect(on_mounts)} """ end diff --git a/mix.exs b/mix.exs index e11e0c79..3cee4ef5 100644 --- a/mix.exs +++ b/mix.exs @@ -120,10 +120,6 @@ defmodule Beacon.LiveAdmin.MixProject do Beacon.LiveAdmin.Plug, Beacon.LiveAdmin.Cluster ], - "Authn and Authz": [ - Beacon.LiveAdmin.Authorization, - Beacon.LiveAdmin.Hooks.AssignAgent - ], Extensibility: [ Beacon.LiveAdmin.PageBuilder, Beacon.LiveAdmin.PageBuilder.Page, diff --git a/mix.lock b/mix.lock index b84afdb2..6666290a 100644 --- a/mix.lock +++ b/mix.lock @@ -1,6 +1,6 @@ %{ "accent": {:hex, :accent, "1.1.1", "20257356446d45078b19b91608f74669b407b39af891ee3db9ee6824d1cae19d", [:mix], [{:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:plug, "~> 1.3", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "6d5afa50d4886e3370e04fa501468cbaa6c4b5fe926f72ccfa844ad9e259adae"}, - "beacon": {:git, "https://github.com/BeaconCMS/beacon.git", "4784f40548b71eee7c3ca3a5e793104306d92804", []}, + "beacon": {:git, "https://github.com/BeaconCMS/beacon.git", "88343566c93dec307f7c4d957d26485ec3b2ab99", []}, "castore": {:hex, :castore, "1.0.7", "b651241514e5f6956028147fe6637f7ac13802537e895a724f90bf3e36ddd1dd", [:mix], [], "hexpm", "da7785a4b0d2a021cd1292a60875a784b6caef71e76bf4917bdee1f390455cf5"}, "cc_precompiler": {:hex, :cc_precompiler, "0.1.10", "47c9c08d8869cf09b41da36538f62bc1abd3e19e41701c2cea2675b53c704258", [:mix], [{:elixir_make, "~> 0.7", [hex: :elixir_make, repo: "hexpm", optional: false]}], "hexpm", "f6e046254e53cd6b41c6bacd70ae728011aa82b2742a80d6e2214855c6e06b22"}, "certifi": {:hex, :certifi, "2.12.0", "2d1cca2ec95f59643862af91f001478c9863c2ac9cb6e2f89780bfd8de987329", [:rebar3], [], "hexpm", "ee68d85df22e554040cdb4be100f33873ac6051387baf6a8f6ce82272340ff1c"}, diff --git a/test/beacon/live_admin/hooks/assign_agent_test.exs b/test/beacon/live_admin/hooks/assign_agent_test.exs deleted file mode 100644 index 30ce901c..00000000 --- a/test/beacon/live_admin/hooks/assign_agent_test.exs +++ /dev/null @@ -1,18 +0,0 @@ -defmodule Beacon.LiveAdmin.Hooks.AssignAgentTest do - use Beacon.LiveAdmin.ConnCase, async: false - - alias Beacon.LiveAdmin.Hooks.AssignAgent - - test "on_mount", %{conn: conn} do - session = - conn - |> init_test_session(%{}) - |> put_session(:session_id, "admin_session_123") - |> get_session() - - assert {:cont, %{assigns: %{agent: agent}}} = - AssignAgent.on_mount(:default, %{"site" => "site_a"}, session, @socket) - - assert %{role: :admin, session_id: "admin_session_123"} = agent - end -end diff --git a/test/beacon/live_admin/router_test.exs b/test/beacon/live_admin/router_test.exs index 668289eb..0c0506bd 100644 --- a/test/beacon/live_admin/router_test.exs +++ b/test/beacon/live_admin/router_test.exs @@ -63,21 +63,12 @@ defmodule Beacon.LiveAdmin.RouterTest do assert {:beacon_live_admin_prefix, _} = Router.__session_options__("prefix", [], []) end - test "options are optional but always assign Hooks.AssignAgent" do - assert {:beacon_live_admin_prefix, - [ - {:root_layout, {Beacon.LiveAdmin.Layouts, :admin}}, - {:session, {Beacon.LiveAdmin.Router, :__session__, [[]]}}, - {:on_mount, [Beacon.LiveAdmin.Hooks.AssignAgent]} - ]} = Router.__session_options__("prefix", [], []) - end - test "allow adding custom mount hooks" do assert {:beacon_live_admin_prefix, [ root_layout: {Beacon.LiveAdmin.Layouts, :admin}, session: {Beacon.LiveAdmin.Router, :__session__, [[]]}, - on_mount: [SomeHook, Beacon.LiveAdmin.Hooks.AssignAgent] + on_mount: [SomeHook] ]} = Router.__session_options__("prefix", [], on_mount: [SomeHook]) end diff --git a/test/support/site.ex b/test/support/site.ex index 56cfa679..0d8c6434 100644 --- a/test/support/site.ex +++ b/test/support/site.ex @@ -33,28 +33,6 @@ defmodule MyAppWeb.Endpoint do plug MyApp.Router end -defmodule MyApp.AuthorizationSource do - @behaviour Beacon.Authorization.Policy - - @impl true - def get_agent(%{"session_id" => "admin_session_123"}) do - %{role: :admin, session_id: "admin_session_123"} - end - - def get_agent(%{"session_id" => "editor_session_123"}) do - %{role: :editor, session_id: "editor_session_123"} - end - - def get_agent(%{"session_id" => "other_session_123"}) do - %{role: :other, session_id: "other_session_123"} - end - - def get_agent(_), do: %{} - - @impl true - def authorized?(_agent, _operation, _context), do: true -end - defmodule MyApp.PageField.Type do @moduledoc false @behaviour Beacon.Content.PageField diff --git a/test/test_helper.exs b/test/test_helper.exs index 8f434189..49ff17d3 100644 --- a/test/test_helper.exs +++ b/test/test_helper.exs @@ -90,7 +90,6 @@ Beacon.LiveAdminTest.Cluster.start_beacon(:"node1@127.0.0.1", repo: MyApp.Repo, endpoint: MyAppWeb.Endpoint, router: MyApp.Router, - authorization_source: MyApp.AuthorizationSource, extra_page_fields: [ MyApp.PageField.Type ] @@ -105,8 +104,7 @@ Beacon.LiveAdminTest.Cluster.start_beacon(:"node2@127.0.0.1", skip_boot?: true, repo: MyApp.Repo, endpoint: MyAppWeb.Endpoint, - router: MyApp.Router, - authorization_source: MyApp.AuthorizationSource + router: MyApp.Router ], [ site: :site_c,