Skip to content

Commit

Permalink
improvement: support authorize_with option in Ash.get (#1732)
Browse files Browse the repository at this point in the history
  • Loading branch information
ken-kost authored Jan 24, 2025
1 parent 261864f commit 0975ea5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
6 changes: 6 additions & 0 deletions lib/ash.ex
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,12 @@ defmodule Ash do
See `Ash.Query.load/2`.
"""
],
authorize_with: [
type: {:one_of, [:filter, :error]},
default: :filter,
doc:
"If set to `:error`, instead of applying authorization filters as a filter, any records not matching the authroization filter will cause an error to be returned."
]
]
|> Spark.Options.merge(@global_opts, "Global Options")
Expand Down
16 changes: 11 additions & 5 deletions test/policy/simple_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -426,9 +426,10 @@ defmodule Ash.Test.Policy.SimpleTest do
|> Ash.Changeset.for_create(:create, %{author: user.id, text: "aaa"})
|> Ash.create!(authorize?: false)

Post
|> Ash.Changeset.for_create(:create, %{text: "invalid"})
|> Ash.create!(authorize?: false)
post2 =
Post
|> Ash.Changeset.for_create(:create, %{text: "invalid"})
|> Ash.create!(authorize?: false)

ids =
Post
Expand All @@ -439,8 +440,13 @@ defmodule Ash.Test.Policy.SimpleTest do
assert ids == Enum.sort([post1.id])

assert_raise Ash.Error.Forbidden, fn ->
Post
|> Ash.read!(actor: user, authorize_with: :error)
Ash.read!(Post, actor: user, authorize_with: :error)
end

Ash.get!(Post, post1.id, actor: user, authorize_with: :error)

assert_raise Ash.Error.Forbidden, fn ->
Ash.get!(Post, post2.id, actor: user, authorize_with: :error)
end
end

Expand Down

0 comments on commit 0975ea5

Please sign in to comment.