Skip to content

Commit 70153b4

Browse files
committed
fix: allow Ash @global_opts authorize? to accept boolean or nil
- Revert 7e34bbd change that updated Ash @global_opts `authorize?` to only accept boolean. fixes #2224
1 parent c5e17c9 commit 70153b4

File tree

2 files changed

+83
-3
lines changed

2 files changed

+83
-3
lines changed

lib/ash.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ defmodule Ash do
5858
doc: "The action to use, either an Action struct or the name of the action"
5959
],
6060
authorize?: [
61-
type: :boolean,
61+
type: {:or, [:boolean, {:literal, nil}]},
6262
doc:
6363
"If an actor option is provided (even if it is `nil`), authorization happens automatically. If not, this flag can be used to authorize with no user."
6464
],

test/actions/stream_test.exs

Lines changed: 82 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,53 @@ defmodule Ash.Test.Actions.StreamTest do
44

55
alias Ash.Test.Domain, as: Domain
66

7+
defmodule Author do
8+
@moduledoc false
9+
use Ash.Resource,
10+
domain: Domain,
11+
data_layer: Ash.DataLayer.Ets
12+
13+
ets do
14+
private? true
15+
end
16+
17+
actions do
18+
default_accept :*
19+
defaults [:read, :destroy, create: :*, update: :*]
20+
end
21+
22+
attributes do
23+
uuid_primary_key :id
24+
end
25+
end
26+
727
defmodule Post do
828
@moduledoc false
9-
use Ash.Resource, domain: Domain, data_layer: Ash.DataLayer.Ets
29+
use Ash.Resource,
30+
domain: Domain,
31+
data_layer: Ash.DataLayer.Ets,
32+
authorizers: [Ash.Policy.Authorizer]
1033

1134
ets do
1235
private? true
1336
end
1437

1538
actions do
1639
default_accept :*
17-
defaults [:destroy, create: :*, update: :*]
40+
defaults [:destroy, update: :*]
1841

1942
read :read do
2043
primary? true
2144
pagination keyset?: true, offset?: true, required?: false
2245
end
2346

2447
read :read_with_no_pagination
48+
49+
create :create do
50+
primary? true
51+
52+
change relate_actor(:author, allow_nil?: true)
53+
end
2554
end
2655

2756
attributes do
@@ -30,6 +59,24 @@ defmodule Ash.Test.Actions.StreamTest do
3059

3160
timestamps()
3261
end
62+
63+
relationships do
64+
belongs_to :author, Author, public?: true
65+
end
66+
67+
policies do
68+
policy action_type(:read) do
69+
authorize_if always()
70+
end
71+
72+
policy action_type(:create) do
73+
authorize_if always()
74+
end
75+
76+
policy action_type(:update) do
77+
authorize_if relates_to_actor_via(:author)
78+
end
79+
end
3380
end
3481

3582
test "records can be streamed" do
@@ -122,4 +169,37 @@ defmodule Ash.Test.Actions.StreamTest do
122169

123170
assert count == 7
124171
end
172+
173+
test "runs successfully when actor is provided without authorize? - Issue #2224" do
174+
actor =
175+
Author
176+
|> Ash.Changeset.for_create(:create)
177+
|> Ash.create!()
178+
179+
assert %Ash.BulkResult{
180+
records: [
181+
%{title: "updated"},
182+
%{title: "updated"}
183+
]
184+
} =
185+
Ash.bulk_create!(
186+
[%{title: "foo"}, %{title: "bar"}],
187+
Post,
188+
:create,
189+
return_stream?: true,
190+
return_records?: true,
191+
actor: actor
192+
)
193+
|> Stream.map(fn {:ok, result} ->
194+
result
195+
end)
196+
|> Ash.bulk_update!(
197+
:update,
198+
%{title: "updated"},
199+
resource: Post,
200+
strategy: :stream,
201+
return_records?: true,
202+
actor: actor
203+
)
204+
end
125205
end

0 commit comments

Comments
 (0)