roberts.gulans
roberts.gulans
AEAsh Elixir
Created by roberts.gulans on 5/5/2023 in #support
About policies
It seems that policies are not triggered. I found this and tried but without success. https://discord.com/channels/711271361523351632/1079519120971812914
# api code
authorization do
authorize :by_default
end

# organization code
policies do
policy always() do
authorize_if relates_to_actor_via(:users)
end
end

# code interface usage
Accounts.Organization.list!()
# [debug] QUERY OK source="acl_organizations" db=3.4ms idle=1311.1ms
#SELECT a0."id", a0."subject" FROM "acl_organizations" AS a0 []

Accounts.Organization.list!(authorize?: true)
# Policy | 🔎:
# authorize if: record.users == actor | ✘ | 🔎

Accounts.Organization.list!(actor: current_user)
# Policy | 🔎:
# authorize if: record.users == actor | ✓ | 🔎

# NOTE: Query little bit more complex because it has many to many relationship with actor
# [debug] QUERY OK source="acl_organizations" db=6.1ms queue=0.1ms idle=1397.1ms
# SELECT a0."id", a0."subject" FROM "acl_organizations" AS a0 WHERE (exists((SELECT 1 FROM "public"."acl_users" AS sa0 INNER JOIN "public"."acl_organization_user" AS sa1 ON (sa1."user_id" = sa0."id") AND (a0."id" = sa1."organization_id") WHERE (sa0."id"::uuid = $1::uuid)))) ["feeee177-9287-42c7-9bd4-d8372814b75f"]
# api code
authorization do
authorize :by_default
end

# organization code
policies do
policy always() do
authorize_if relates_to_actor_via(:users)
end
end

# code interface usage
Accounts.Organization.list!()
# [debug] QUERY OK source="acl_organizations" db=3.4ms idle=1311.1ms
#SELECT a0."id", a0."subject" FROM "acl_organizations" AS a0 []

Accounts.Organization.list!(authorize?: true)
# Policy | 🔎:
# authorize if: record.users == actor | ✘ | 🔎

Accounts.Organization.list!(actor: current_user)
# Policy | 🔎:
# authorize if: record.users == actor | ✓ | 🔎

# NOTE: Query little bit more complex because it has many to many relationship with actor
# [debug] QUERY OK source="acl_organizations" db=6.1ms queue=0.1ms idle=1397.1ms
# SELECT a0."id", a0."subject" FROM "acl_organizations" AS a0 WHERE (exists((SELECT 1 FROM "public"."acl_users" AS sa0 INNER JOIN "public"."acl_organization_user" AS sa1 ON (sa1."user_id" = sa0."id") AND (a0."id" = sa1."organization_id") WHERE (sa0."id"::uuid = $1::uuid)))) ["feeee177-9287-42c7-9bd4-d8372814b75f"]
From examples we can see, that policies are invoked only if i explicitly pass authorize?: true or actor: current_user even tho in api i have defined authorize :by_default
12 replies
AEAsh Elixir
Created by roberts.gulans on 5/4/2023 in #support
How to create organization after user creation.
Ideally it would create organization for user after registration, but if user comes from invitation (to specific organization) than it does not create new one. No functionality is created yet, im not sure, how to even approach this. In user resource there isnt even actions for create. Apparently its definition happens somewhere in i suppose.
authentication do
api Domain.Accounts

strategies do
password :password do
identity_field :email
sign_in_tokens_enabled? true
end
end

tokens do
enabled? true
token_resource Domain.Accounts.Token
signing_secret Application.compile_env(:domain, DomainWeb.Endpoint)[:secret_key_base]
end
end
authentication do
api Domain.Accounts

strategies do
password :password do
identity_field :email
sign_in_tokens_enabled? true
end
end

tokens do
enabled? true
token_resource Domain.Accounts.Token
signing_secret Application.compile_env(:domain, DomainWeb.Endpoint)[:secret_key_base]
end
end
Sorry if i ask noob questions, i find docs rather confusing.
16 replies
AEAsh Elixir
Created by roberts.gulans on 5/3/2023 in #support
select relationship
Resource relevant parts
relationships do
belongs_to :debit_account, Budget.Account, allow_nil?: false
belongs_to :credit_account, Budget.Account, allow_nil?: false
end

actions do
defaults [:create, :read, :update, :destroy]
end
relationships do
belongs_to :debit_account, Budget.Account, allow_nil?: false
belongs_to :credit_account, Budget.Account, allow_nil?: false
end

actions do
defaults [:create, :read, :update, :destroy]
end
In live view form_component i have form where i show select to select relationship
# update
assign_new(assigns, :accounts, fn _ -> Budget.Account.list!() |> Enum.map(&{&1.subject, &1.id}) end)}

# Form preperation
AshPhoenix.Form.for_create(Budget.Payment, :create, api: Budget, actor: current_user)

# render
<.input field={{f, :credit_account_id}} type="select" prompt="Select credit account" label="Credit account" options={@accounts} />
<.input field={{f, :debit_account_id}} type="select" prompt="Select debit account" label="Debit account" options={@accounts} />
# update
assign_new(assigns, :accounts, fn _ -> Budget.Account.list!() |> Enum.map(&{&1.subject, &1.id}) end)}

# Form preperation
AshPhoenix.Form.for_create(Budget.Payment, :create, api: Budget, actor: current_user)

# render
<.input field={{f, :credit_account_id}} type="select" prompt="Select credit account" label="Credit account" options={@accounts} />
<.input field={{f, :debit_account_id}} type="select" prompt="Select debit account" label="Debit account" options={@accounts} />
Input component
# Input preperation
def input(%{field: {f, field}} = assigns) do
assigns
|> assign(field: nil)
|> assign_new(:name, fn ->
name = Phoenix.HTML.Form.input_name(f, field)
if assigns.multiple, do: name <> "[]", else: name
end)
|> assign_new(:id, fn -> Phoenix.HTML.Form.input_id(f, field) end)
|> assign(:value, AshPhoenix.Form.value(f, field))
|> assign(:errors, AshPhoenix.Form.errors(f, format: :simple, for_path: [])[field] |> List.wrap)
|> input()
end
# Input preperation
def input(%{field: {f, field}} = assigns) do
assigns
|> assign(field: nil)
|> assign_new(:name, fn ->
name = Phoenix.HTML.Form.input_name(f, field)
if assigns.multiple, do: name <> "[]", else: name
end)
|> assign_new(:id, fn -> Phoenix.HTML.Form.input_id(f, field) end)
|> assign(:value, AshPhoenix.Form.value(f, field))
|> assign(:errors, AshPhoenix.Form.errors(f, format: :simple, for_path: [])[field] |> List.wrap)
|> input()
end
I dont understand why but selects does not hold their selected value. I can select one, but once i select other first looses value. For plain string value AshPhoenix.Form.value(f, field) returns correct input value, but for selects not. Unrelated: for errors i had to do: AshPhoenix.Form.errors(f, format: :simple, for_path: [])[field], because AshPhoenix.Form.errors(f, format: :simple) returned %{[] => %{key => value}} If some relevant info is missing please let me know, i will provide it.
18 replies
AEAsh Elixir
Created by roberts.gulans on 5/2/2023 in #support
Union relationships
Hello i have 2 resources. Accounts and Payments. Each payment has 2 relationships to accounts
relationships do
belongs_to :debit_account, Budget.Account
belongs_to :credit_account, Budget.Account
end
relationships do
belongs_to :debit_account, Budget.Account
belongs_to :credit_account, Budget.Account
end
And i want to have one relationship in Account
relationships do
has_many :payments, Budget.Payment
end
relationships do
has_many :payments, Budget.Payment
end
Is there a way how i could have payments relationship to hold both those so i could have one list with all payments to order/paginate/filter on it.
6 replies
AEAsh Elixir
Created by roberts.gulans on 4/30/2023 in #support
All extensions list.
In one of the talks i heard cqrs/commanded mentioned. Is there some package at least in beta or smth? And broader question is where are list of all extensions, even 3rd party? I understand that ash currently is pretty young and theres probably not much, but i see huge potential in reusability and having something like https://tailwindcomponents.com/ but for ash resources.
3 replies