Skip to content

Commit

Permalink
fix: ensure tenant stays set after navigating from page
Browse files Browse the repository at this point in the history
  • Loading branch information
zachdaniel committed Jan 4, 2024
1 parent 48b8ca0 commit 0a671b9
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 27 deletions.
1 change: 1 addition & 0 deletions assets/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ function getCookie(name) {
let params = () => {
return {
_csrf_token: csrfToken,
tenant: getCookie("tenant"),
actor_resource: getCookie("actor_resource"),
actor_primary_key: getCookie("actor_primary_key"),
actor_action: getCookie("actor_action"),
Expand Down
6 changes: 6 additions & 0 deletions dev/resources/accounts/resources/user.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ defmodule Demo.Accounts.User do
table_columns [:id, :first_name, :last_name, :representative, :admin, :full_name, :api_key, :date_of_birth]
end

multitenancy do
strategy :attribute
attribute :id
global? true
end

policies do
bypass always() do
authorize_if actor_attribute_equals(:admin, true)
Expand Down
12 changes: 12 additions & 0 deletions lib/ash_admin/actor_plug/plug.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,24 @@ defmodule AshAdmin.ActorPlug.Plug do
actor_api: actor_api_from_session(socket.endpoint, session),
actor_resources: actor_resources(apis),
authorizing: session_bool(session["actor_authorizing"]),
tenant: session["tenant"],
actor_paused: actor_paused
]
end

@impl true
def set_actor_session(conn) do
conn =
case conn.cookies do
%{"tenant" => tenant} when tenant != "undefined" ->
conn
|> Plug.Conn.put_session(:tenant, tenant)
|> Plug.Conn.assign(:tenant, tenant)

_ ->
conn
end

case conn.cookies do
%{"actor_resource" => "undefined"} ->
conn
Expand Down
5 changes: 2 additions & 3 deletions lib/ash_admin/components/top_nav.ex
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,10 @@ defmodule AshAdmin.Components.TopNav do
api={@api}
prefix={@prefix}
/>
<.live_component
<TenantForm.tenant_form
:if={show_tenant_form?(@apis)}
module={TenantForm}
tenant={@tenant}
id="tenant_editor"
editing_tenant={@editing_tenant}
set_tenant={@set_tenant}
clear_tenant={@clear_tenant}
/>
Expand Down
23 changes: 6 additions & 17 deletions lib/ash_admin/components/top_nav/tenant_form.ex
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
defmodule AshAdmin.Components.TopNav.TenantForm do
@moduledoc false
use Phoenix.LiveComponent
use Phoenix.Component
import Tails

attr :tenant, :string, required: true
attr :clear_tenant, :string, required: true
attr :set_tenant, :string, required: true
attr :editing_tenant, :boolean, required: true

def render(assigns) do
def tenant_form(assigns) do
~H"""
<div id={@id} class="relative text-white" phx-hook="Tenant" phx-target={@myself}>
<div id="tenant-form" class="relative text-white" phx-hook="Tenant">
<.form :if={@editing_tenant} for={to_form(%{}, as: :tenant)} phx-submit={@set_tenant}>
<input
type="text"
name="tenant"
value={@tenant}
class={classes("text-black": @editing_tenant)}
/>
<button phx-click="stop_editing_tenant" phx-target={@myself}>
<button phx-click="stop_editing_tenant">
<svg
width="1em"
height="1em"
Expand All @@ -36,7 +37,7 @@ defmodule AshAdmin.Components.TopNav.TenantForm do
</svg>
</button>
</.form>
<a :if={!@editing_tenant} href="#" phx-click="start_editing_tenant" phx-target={@myself}>
<a :if={!@editing_tenant} href="#" phx-click="start_editing_tenant">
<%= if @tenant, do: "Tenant: #{@tenant}", else: "No tenant" %>
</a>
<button :if={@tenant} phx-click={@clear_tenant}>
Expand All @@ -60,16 +61,4 @@ defmodule AshAdmin.Components.TopNav.TenantForm do
</div>
"""
end

def mount(socket) do
{:ok, assign(socket, :editing_tenant, false)}
end

def handle_event("start_editing_tenant", _, socket) do
{:noreply, assign(socket, :editing_tenant, true)}
end

def handle_event("stop_editing_tenant", _, socket) do
{:noreply, assign(socket, :editing_tenant, false)}
end
end
10 changes: 10 additions & 0 deletions lib/ash_admin/pages/page_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ defmodule AshAdmin.PageLive do
|> assign(:record, nil)
|> assign(:apis, apis)
|> assign(:tenant, session["tenant"])
|> assign(:editing_tenant, false)
|> then(fn socket ->
assign(socket, AshAdmin.ActorPlug.actor_assigns(socket, session))
end)
Expand All @@ -65,6 +66,7 @@ defmodule AshAdmin.PageLive do
id="top_nav"
apis={@apis}
api={@api}
editing_tenant={@editing_tenant}
actor_api={@actor_api}
resource={@resource}
tenant={@tenant}
Expand Down Expand Up @@ -318,6 +320,14 @@ defmodule AshAdmin.PageLive do
|> push_event("clear_actor", %{})}
end

def handle_event("start_editing_tenant", _, socket) do
{:noreply, assign(socket, :editing_tenant, true)}
end

def handle_event("stop_editing_tenant", _, socket) do
{:noreply, assign(socket, :editing_tenant, false)}
end

def handle_event(
"set_actor",
%{"resource" => resource, "api" => api, "pkey" => primary_key},
Expand Down
10 changes: 5 additions & 5 deletions priv/static/assets/app.js

Large diffs are not rendered by default.

Binary file modified priv/static/assets/app.js.gz
Binary file not shown.
4 changes: 2 additions & 2 deletions priv/static/cache_manifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"!comment!":"This file was auto-generated by `mix phx.digest`. Remove it and all generated artefacts with `mix phx.digest.clean --all`",
"version":1,
"latest":{"assets/app.css":"assets/app-f480ad9fb2a3744d668ac31cc919441f.css","assets/app.js":"assets/app-aac45a999218cb837d0d4006c02b7dff.js"},
"digests":{"assets/app-aac45a999218cb837d0d4006c02b7dff.js":{"size":108631,"sha512":"Q/jSWd9SG+3D9BjNSTSo3XNU/Q0xBrdwB/OgT7W4clywBHa5VxbBNh0QBT5ZsBfTZ7MANggW8XW9rOM5UVteYg==","digest":"aac45a999218cb837d0d4006c02b7dff","logical_path":"assets/app.js","mtime":63867771918},"assets/app-f480ad9fb2a3744d668ac31cc919441f.css":{"size":37300,"sha512":"g15iWMXrhNqfFRVkrKH4c9XonkZnDWFEdGuzX0BW319sucBZFfD0lZ8GTgPJcKl3gyhUqH0Lm4jy80LgfFao7w==","digest":"f480ad9fb2a3744d668ac31cc919441f","logical_path":"assets/app.css","mtime":63867771918}}
"latest":{"assets/app.css":"assets/app-f480ad9fb2a3744d668ac31cc919441f.css","assets/app.js":"assets/app-dd7097dcf40a37e608bbcbd54509199a.js"},
"digests":{"assets/app-aac45a999218cb837d0d4006c02b7dff.js":{"digest":"aac45a999218cb837d0d4006c02b7dff","logical_path":"assets/app.js","mtime":63867771918,"sha512":"Q/jSWd9SG+3D9BjNSTSo3XNU/Q0xBrdwB/OgT7W4clywBHa5VxbBNh0QBT5ZsBfTZ7MANggW8XW9rOM5UVteYg==","size":108631},"assets/app-dd7097dcf40a37e608bbcbd54509199a.js":{"size":108651,"sha512":"40KTAfshu6/MtQVeWppL2ccJbHBaVGloBOLOOkiM7VzZqGG4NBzvXc/+PvvU43RcxELv21EMbtKAvdq1xjdG0A==","digest":"dd7097dcf40a37e608bbcbd54509199a","logical_path":"assets/app.js","mtime":63871616612},"assets/app-f480ad9fb2a3744d668ac31cc919441f.css":{"size":37300,"sha512":"g15iWMXrhNqfFRVkrKH4c9XonkZnDWFEdGuzX0BW319sucBZFfD0lZ8GTgPJcKl3gyhUqH0Lm4jy80LgfFao7w==","digest":"f480ad9fb2a3744d668ac31cc919441f","logical_path":"assets/app.css","mtime":63871616612}}
}

0 comments on commit 0a671b9

Please sign in to comment.