sodapopcan

sodapopcan

Ensure actor can only create resource for themselves

The docs give this example of a policy:

policy action_type([:create, :update, :destroy]) do
  authorize_if expr(owner_id == ^actor(:id))
end

This is in the context of a policy group. Using it throws an error that filter expressions cannot be used for creations. I also get if I do it outside a group:

policy action_type(:create) do
  authorize_if expr(owner_id == ^actor(:id))
end

I read and understood the error message and it makes sense since there is no data yet for create, but how would you ensure that an actor can only create a resource for themselves?

I would like to do this:

MyApp.Items.create_item(%{
    title: "New item",
    user_id: user.id
  },
  actor: user
)

I’m also wondering more out of curiosity (even though I don’t want to do it this way) is there is a way to reference actor in action in change? For example, I tried to do this:

create :create do
  accept [:title]
  change set_attribute(:user_id, actor(:id))
end

though I see actor/1 is only available in expr (I think?)

Thanks!

Marked As Solved

zachdaniel

zachdaniel

Creator of Ash

You actually can do what you described, but there is a more expressive builtin for it.

change relate_actor(:user)

And what it could look like done manually:

change fn changeset, context -> 
  Ash.Changeset.force_change_attribute(changeset, :user_id, context.actor.id)
end

Also Liked

zachdaniel

zachdaniel

Creator of Ash

Oh, it’s very much possible. There are various ways you could do it.

policy :create do
  authorize_if relating_to_actor(:user)
end
defmodule MyApp.Checks.SomeCustomCheck do
  use Ash.Policy.SimpleCheck

  def match?(actor, %{subject: changeset}, _) do
    # arbitrary logic here
  end
end

policy action_type(:create) do
  authorize_if MyApp.Checks.SomeCustomCheck
end

Where Next?

Popular in Questions Top

lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
mgjohns61585
Could someone help me? I’m making my first elixir program, number guessing game. I can’t figure out how to convert the user’s guess from ...
New
vac
Hi, I’m quite new in Elixir and I’m trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and I...
New
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: <h1>Create Post</h1> <%= ...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New

Other popular topics Top

albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39297 209
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement