Skip to content

Commit

Permalink
Remove authz (#189)
Browse files Browse the repository at this point in the history
  • Loading branch information
leandrocp authored Jul 26, 2024
1 parent 66d5bc1 commit 49e57a1
Show file tree
Hide file tree
Showing 17 changed files with 46 additions and 224 deletions.
18 changes: 0 additions & 18 deletions lib/beacon/live_admin/authorization.ex

This file was deleted.

26 changes: 0 additions & 26 deletions lib/beacon/live_admin/hooks/assign_agent.ex

This file was deleted.

21 changes: 0 additions & 21 deletions lib/beacon/live_admin/hooks/authorized.ex

This file was deleted.

2 changes: 0 additions & 2 deletions lib/beacon/live_admin/live/component_editor_live/index.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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"}

Expand Down
2 changes: 0 additions & 2 deletions lib/beacon/live_admin/live/error_page_editor_live/index.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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"}

Expand Down
2 changes: 0 additions & 2 deletions lib/beacon/live_admin/live/layout_editor_live/index.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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"}

Expand Down
2 changes: 0 additions & 2 deletions lib/beacon/live_admin/live/live_data_editor_live/index.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand Down
111 changes: 37 additions & 74 deletions lib/beacon/live_admin/live/media_library_live/index.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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"}

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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</.button>
</.link>
</:actions>
Expand All @@ -150,13 +120,7 @@ defmodule Beacon.LiveAdmin.MediaLibraryLive.Index do
<:col :let={{_, asset}} label="File Name"><%= asset.file_name %></:col>
<:col :let={{_, asset}} label="type"><%= asset.media_type %></:col>
<: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]" />
</.link>
</:action>
Expand All @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -65,15 +64,7 @@ defmodule Beacon.LiveAdmin.MediaLibraryLive.UploadFormComponent do
<% end %>
</section>
<.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">
<div class="mt-2 flex justify-center rounded-lg border border-dashed border-gray-900/25 px-6 py-10" phx-drop-target={@uploads.asset.ref}>
<.live_file_input upload={@uploads.asset} tabindex="0" />
</div>
Expand Down
2 changes: 0 additions & 2 deletions lib/beacon/live_admin/live/page_editor_live/index.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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"}

Expand Down
12 changes: 5 additions & 7 deletions lib/beacon/live_admin/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 0 additions & 4 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion mix.lock
Original file line number Diff line number Diff line change
@@ -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"},
Expand Down
18 changes: 0 additions & 18 deletions test/beacon/live_admin/hooks/assign_agent_test.exs

This file was deleted.

11 changes: 1 addition & 10 deletions test/beacon/live_admin/router_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Loading

0 comments on commit 49e57a1

Please sign in to comment.