Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ if config_env() == :test do

config :ash, :disable_async?, true

config :ash_admin, :actor_plug, AshAdmin.Test.ActorPlug

# Fail on duplicate ids and on phx-change forms without an id (no form recovery).
config :phoenix_live_view, :test_warnings, :raise

Expand Down
10 changes: 10 additions & 0 deletions documentation/tutorials/getting-started-with-ash-admin.md
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,16 @@ and then configure it like so
config :ash_admin, :actor_plug, MyAppWeb.AshAdminActorPlug
```

With a custom actor plug, authorization is always enforced and the dashboard user can't change the actor: the controls for switching, pausing or clearing the actor and for bypassing authorization are hidden, and the dashboard ignores those events if a client sends them anyway. To give some users those controls (super admins, for example), return `actor_controls?: true` for them from `actor_assigns/2`:
```
def actor_assigns(socket, _session) do
user = socket.assigns[:current_user]

[actor: user, actor_controls?: user.role == :admin]
end
```
Changes made with these controls last until the page is reloaded, when your plug sets the actor again.

### Content Security Policy

If your app specifies a content security policy header, eg. via
Expand Down
10 changes: 3 additions & 7 deletions lib/ash_admin/actor_plug/plug.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ defmodule AshAdmin.ActorPlug.Plug do
session = Phoenix.LiveView.get_connect_params(socket) || session

[
# This plug takes the actor and authorization settings from the browser, so the
# dashboard user may change them. Custom actor plugs leave this off by default.
actor_controls?: true,
actor: actor_from_session(socket.endpoint, session),
actor_domain: actor_domain_from_session(socket.endpoint, session),
actor_resources: actor_resources(domains),
Expand Down Expand Up @@ -88,13 +91,6 @@ defmodule AshAdmin.ActorPlug.Plug do
end
end

defp actor_resources(domains) do
for domain <- domains,
resource <- AshAdmin.Domain.show_resources(domain),
AshAdmin.Helpers.primary_action(resource, :read) && AshAdmin.Resource.actor?(resource),
do: {domain, resource}
end

defp domains(otp_app) do
otp_app
|> Application.get_env(:ash_domains)
Expand Down
3 changes: 2 additions & 1 deletion lib/ash_admin/components/resource/data_table.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ defmodule AshAdmin.Components.Resource.DataTable do
attr :action, :any
attr :authorizing, :boolean
attr :actor, :any
attr :actor_controls, :boolean, default: false
attr :url_path, :any
attr :params, :any
attr :table, :any, required: true
Expand Down Expand Up @@ -176,7 +177,7 @@ defmodule AshAdmin.Components.Resource.DataTable do

<%!-- Set as actor --%>
<button
:if={AshAdmin.Resource.actor?(@resource)}
:if={@actor_controls && AshAdmin.Resource.actor?(@resource)}
phx-click="set_actor"
phx-value-resource={AshAdmin.Resource.name(@resource)}
phx-value-domain={AshAdmin.Domain.name(@domain)}
Expand Down
3 changes: 3 additions & 0 deletions lib/ash_admin/components/resource/resource.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ defmodule AshAdmin.Components.Resource do
attr :domain, :any, required: true
attr :action, :any
attr :actor, :any, required: true
attr :actor_controls, :boolean, default: false
attr :authorizing, :boolean, required: true
attr :tenant, :string, required: true
attr :url_path, :string, default: ""
Expand Down Expand Up @@ -90,6 +91,7 @@ defmodule AshAdmin.Components.Resource do
<.live_component
:if={match?({:ok, %_{}}, @record) && @action_type == :read}
module={Show}
actor_controls={@actor_controls}
resource={@resource}
domain={@domain}
id={show_id(@resource)}
Expand Down Expand Up @@ -122,6 +124,7 @@ defmodule AshAdmin.Components.Resource do
<.live_component
:if={@action_type == :read && !match?({:ok, %_{}}, @record)}
module={DataTable}
actor_controls={@actor_controls}
polymorphic_actions={@polymorphic_actions}
resource={@resource}
action={@action}
Expand Down
14 changes: 12 additions & 2 deletions lib/ash_admin/components/resource/show.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ defmodule AshAdmin.Components.Resource.Show do
attr :action, :any
attr :authorizing, :boolean, default: false
attr :actor, :any
attr :actor_controls, :boolean, default: false
attr :tenant, :any
attr :table, :any, required: true
attr :prefix, :any, required: true
Expand Down Expand Up @@ -74,13 +75,21 @@ defmodule AshAdmin.Components.Resource.Show do
end

def render_show(assigns, record, resource, title \\ nil, buttons? \\ true) do
assigns = assign(assigns, record: record, resource: resource, title: title, buttons: buttons?)
assigns =
assign(assigns,
record: record,
resource: resource,
title: title,
buttons: buttons?,
# render_show is also reached from other components that know nothing of actors
actor_controls: assigns[:actor_controls] == true
)

~H"""
<div class="shadow-sm ring-1 ring-slate-200 dark:ring-slate-700 overflow-hidden sm:rounded-md bg-white dark:bg-slate-900">
<h1 :if={@title} class="pt-2 pl-4 text-lg">{@title}</h1>
<button
:if={AshAdmin.Resource.actor?(@resource)}
:if={@actor_controls && AshAdmin.Resource.actor?(@resource)}
class="float-right pt-4 pr-4"
phx-click="set_actor"
phx-value-resource={AshAdmin.Resource.name(@resource)}
Expand Down Expand Up @@ -455,6 +464,7 @@ defmodule AshAdmin.Components.Resource.Show do
skip={[@destination_attribute]}
relationship_name={@relationship_name}
actor={@actor}
actor_controls={@actor_controls}
tenant={@tenant}
authorizing={@authorizing}
/>
Expand Down
3 changes: 2 additions & 1 deletion lib/ash_admin/components/resource/table.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ defmodule AshAdmin.Components.Resource.Table do
attr :format_fields, :any, default: []
attr :show_sensitive_fields, :list, default: []
attr :actor, :any, default: nil
attr :actor_controls, :boolean, default: false
attr :tenant, :any, default: nil
attr :authorizing, :boolean, default: false
attr :relationship_name, :atom, default: nil
Expand Down Expand Up @@ -107,7 +108,7 @@ defmodule AshAdmin.Components.Resource.Table do
</div>

<button
:if={AshAdmin.Resource.actor?(@resource)}
:if={@actor_controls && AshAdmin.Resource.actor?(@resource)}
phx-click="set_actor"
phx-value-resource={AshAdmin.Resource.name(@resource)}
phx-value-domain={AshAdmin.Domain.name(@domain)}
Expand Down
5 changes: 3 additions & 2 deletions lib/ash_admin/components/sidebar.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ defmodule AshAdmin.Components.Sidebar do

# Actor/auth/tenant assigns
attr :actor, :any, default: nil
attr :actor_controls, :boolean, default: false
attr :actor_domain, :any, default: nil
attr :actor_resources, :any, default: []
attr :actor_paused, :boolean, default: false
Expand Down Expand Up @@ -126,7 +127,7 @@ defmodule AshAdmin.Components.Sidebar do
<div class="border-t border-slate-700/50 px-3 py-3 space-y-2">
<%!-- Actor --%>
<div
:if={@actor_resources != []}
:if={@actor_controls && @actor_resources != []}
id={"sidebar-actor-#{@variant}"}
phx-hook="Actor"
class="space-y-1.5"
Expand Down Expand Up @@ -176,7 +177,7 @@ defmodule AshAdmin.Components.Sidebar do
</div>

<%!-- Authorizing --%>
<div :if={@actor_resources != []} class="flex items-center gap-2">
<div :if={@actor_controls && @actor_resources != []} class="flex items-center gap-2">
<button
type="button"
phx-click="toggle_authorizing"
Expand Down
9 changes: 9 additions & 0 deletions lib/ash_admin/helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,15 @@ defmodule AshAdmin.Helpers do
|> Map.new(fn {value, index} -> {to_string(index), value} end)
end

@doc false
# The resources the dashboard user can pick an actor from, as `{domain, resource}` pairs.
def actor_resources(domains) do
for domain <- domains,
resource <- AshAdmin.Domain.show_resources(domain),
primary_action(resource, :read) && AshAdmin.Resource.actor?(resource),
do: {domain, resource}
end

def primary_action(resource, type) do
actions =
case type do
Expand Down
16 changes: 14 additions & 2 deletions lib/ash_admin/pages/page_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,11 @@ defmodule AshAdmin.PageLive do
|> then(fn socket ->
assign(socket, AshAdmin.ActorPlug.actor_assigns(socket, session))
end)
|> assign_new(:actor_controls?, fn -> false end)
|> assign_new(:actor_domain, fn -> nil end)
|> assign_new(:actor_resources, fn -> [] end)
|> assign_new(:actor_resources, fn %{actor_controls?: actor_controls?} ->
if actor_controls?, do: actor_resources(domains), else: []
end)
|> assign_new(:authorizing, fn -> true end)
|> assign_new(:actor_paused, fn -> false end)
|> assign_new(:actor_tenant, fn -> nil end)}
Expand All @@ -95,6 +98,7 @@ defmodule AshAdmin.PageLive do
host_app_label={@host_app_label}
open={@sidebar_open}
actor={@actor}
actor_controls={@actor_controls?}
actor_domain={@actor_domain}
actor_resources={@actor_resources}
actor_paused={@actor_paused}
Expand Down Expand Up @@ -128,7 +132,7 @@ defmodule AshAdmin.PageLive do
module={Resource}
id={@resource}
resource={@resource}
set_actor="set_actor"
actor_controls={@actor_controls?}
primary_key={@primary_key}
record={@record}
action_metadata={@action_metadata}
Expand Down Expand Up @@ -393,6 +397,14 @@ defmodule AshAdmin.PageLive do
{:noreply, assign(socket, :sidebar_open, !socket.assigns.sidebar_open)}
end

# The actor plug decides whether the dashboard user may change the actor or turn off
# authorization. Hiding the controls is not enough, since a client can send any event.
def handle_event(event, _params, socket)
when event in ~w(toggle_authorizing toggle_actor_paused clear_actor set_actor) and
socket.assigns.actor_controls? != true do
{:noreply, socket}
end

def handle_event("toggle_authorizing", _, socket) do
{:noreply,
socket
Expand Down
145 changes: 145 additions & 0 deletions test/page_live_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,151 @@ defmodule AshAdmin.Test.PageLiveTest do
end
end

describe "actor controls with the default actor plug" do
test "toggling authorization is allowed", %{conn: conn} do
{:ok, view, _html} = live(conn, "/api/admin")

render_hook(view, "toggle_authorizing", %{})
assert_push_event(view, "toggle_authorizing", %{})

render_hook(view, "toggle_actor_paused", %{})
assert_push_event(view, "toggle_actor_paused", %{})

render_hook(view, "clear_actor", %{})
assert_push_event(view, "clear_actor", %{})
end
end

describe "actor controls with a custom actor plug" do
setup do
language = Ash.create!(AshAdmin.Test.Language, %{name: "English"})
profile = Ash.create!(AshAdmin.Test.Profile, %{name: "Laszlo"})

# Its read policy forbids reads without an actor, and the custom plug sets none.
link =
Ash.create!(
AshAdmin.Test.ProfileLanguage,
%{profile_id: profile.id, language_id: language.id},
authorize?: false
)

post =
AshAdmin.Test.Post
|> Ash.Changeset.for_create(:create, %{body: "actor post"})
|> Ash.create!()

%{link: link, post: post}
end

defp show_link_path(link) do
pkey = AshAdmin.Helpers.encode_primary_key(link)

"/api/custom-actor/admin?domain=ManyToMany&resource=ProfileLanguage" <>
"&action_type=read&primary_key=#{pkey}"
end

test "the actor and authorization controls are not rendered", %{conn: conn, post: post} do
pkey = AshAdmin.Helpers.encode_primary_key(post)

{:ok, view, _html} =
live(conn, "/api/custom-actor/admin?domain=Domain&resource=Post&action_type=read")

refute has_element?(view, "[phx-click=toggle_authorizing]")
refute has_element?(view, "[phx-click=toggle_actor_paused]")
refute has_element?(view, "[phx-click=clear_actor]")
refute has_element?(view, "[phx-click=set_actor]")

{:ok, view, _html} =
live(
conn,
"/api/custom-actor/admin?domain=Domain&resource=Post&action_type=read&primary_key=#{pkey}"
)

refute has_element?(view, "[phx-click=set_actor]")
end

test "authorization is still enforced without any toggling", %{conn: conn, link: link} do
{:ok, _view, html} = live(conn, show_link_path(link))

refute html =~ link.id
end

test "a toggle_authorizing event does not bypass authorization", %{conn: conn, link: link} do
{:ok, view, _html} = live(conn, show_link_path(link))

render_hook(view, "toggle_authorizing", %{})

refute render_patch(view, show_link_path(link)) =~ link.id
refute_push_event(view, "toggle_authorizing", %{})
end

test "a clear_actor event does not bypass authorization", %{conn: conn, link: link} do
{:ok, view, _html} = live(conn, show_link_path(link))

render_hook(view, "clear_actor", %{})

refute render_patch(view, show_link_path(link)) =~ link.id
refute_push_event(view, "clear_actor", %{})
end

test "a toggle_actor_paused event is ignored", %{conn: conn} do
{:ok, view, _html} = live(conn, "/api/custom-actor/admin")

render_hook(view, "toggle_actor_paused", %{})
refute_push_event(view, "toggle_actor_paused", %{})
end

test "a set_actor event does not change the actor", %{conn: conn, post: post} do
{:ok, view, _html} = live(conn, "/api/custom-actor/admin")

render_hook(view, "set_actor", %{
"resource" => "Post",
"domain" => AshAdmin.Domain.name(AshAdmin.Test.Domain),
"pkey" => AshAdmin.Helpers.encode_primary_key(post)
})

refute_push_event(view, "set_actor", %{})
end
end

describe "actor controls with a custom actor plug that allows them" do
test "the controls are rendered and take effect", %{conn: conn} do
language = Ash.create!(AshAdmin.Test.Language, %{name: "French"})
profile = Ash.create!(AshAdmin.Test.Profile, %{name: "Nadja"})

link =
Ash.create!(
AshAdmin.Test.ProfileLanguage,
%{profile_id: profile.id, language_id: language.id},
authorize?: false
)

path =
"/api/custom-actor-controls/admin?domain=ManyToMany&resource=ProfileLanguage" <>
"&action_type=read&primary_key=#{AshAdmin.Helpers.encode_primary_key(link)}"

{:ok, view, html} = live(conn, path)

refute html =~ link.id
assert has_element?(view, "[phx-click=toggle_authorizing]")

view
|> element("#sidebar-actor-desktop ~ div [phx-click=toggle_authorizing]")
|> render_click()

assert_push_event(view, "toggle_authorizing", %{authorizing: "false"})
assert render_patch(view, path) =~ link.id

{:ok, view, _html} =
live(
conn,
"/api/custom-actor-controls/admin?domain=Domain&resource=Post&action_type=read"
)

assert has_element?(view, "[phx-click=set_actor]")
end
end

test "allows uploading to an action with an upload argument", %{conn: conn} do
{:ok, view, _html} =
live(
Expand Down
Loading
Loading