Skip to content

Commit bfdfa55

Browse files
authored
fix: pass full context to Ash.load! in cascade changes (#2537)
* fix: pass full context to `Ash.load!` in cascade changes The `Ash.load!` calls in `cascade_destroy` and `cascade_update` were only passing `tenant: tenant`, missing the actor and other context options. This caused policy failures when policies require an actor. Use `Ash.Context.to_opts(context)` to pass all relevant context options (actor, tenant, tracer, context, authorize?), consistent with how `context_opts` is already passed to `Ash.bulk_destroy!`/`Ash.bulk_update!` in the same code path. Closes #2536 * fix: use scope option instead of to_opts for Ash.load! in cascade changes * fix: avoid shadowing context variable in cascade changes Rename the rebound `context` variable to `action_context` to preserve the original context parameter containing tenant, actor, and tracer. This ensures `scope: context` in `Ash.load!` receives the full context. * fix: remove `authorize?: false` from cascade `Ash.load!` calls Authorization was intentionally removed in #1948 in favour of `accessing_from` policies. Passing `authorize?: false` here would bypass that intent.
1 parent c5088a8 commit bfdfa55

File tree

2 files changed

+14
-14
lines changed

2 files changed

+14
-14
lines changed

lib/ash/resource/change/cascade_destroy.ex

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ defmodule Ash.Resource.Change.CascadeDestroy do
283283

284284
defp destroy_related([], _, _, _), do: :ok
285285

286-
defp destroy_related(data, opts, %{tenant: tenant} = context, changeset) do
286+
defp destroy_related(data, opts, context, changeset) do
287287
action = opts.action
288288
relationship = opts.relationship
289289

@@ -296,7 +296,7 @@ defmodule Ash.Resource.Change.CascadeDestroy do
296296
return_notifications?: opts.return_notifications?
297297
)
298298

299-
context =
299+
action_context =
300300
Map.merge(relationship.context || %{}, %{
301301
cascade_destroy: true,
302302
accessing_from: %{source: relationship.source, name: relationship.name}
@@ -306,8 +306,8 @@ defmodule Ash.Resource.Change.CascadeDestroy do
306306
Keyword.update(
307307
context_opts,
308308
:context,
309-
context,
310-
&Map.merge(&1, context)
309+
action_context,
310+
&Map.merge(&1, action_context)
311311
)
312312

313313
case related_query(data, opts, context_opts) do
@@ -346,7 +346,7 @@ defmodule Ash.Resource.Change.CascadeDestroy do
346346
|> List.wrap()
347347
|> Ash.load!(
348348
[{relationship.name, load_query}],
349-
tenant: tenant
349+
scope: context
350350
)
351351
|> Enum.flat_map(fn record ->
352352
record
@@ -359,8 +359,8 @@ defmodule Ash.Resource.Change.CascadeDestroy do
359359
Keyword.update(
360360
context_opts,
361361
:context,
362-
context,
363-
&Map.merge(&1, context)
362+
action_context,
363+
&Map.merge(&1, action_context)
364364
)
365365
)
366366
end

lib/ash/resource/change/cascade_update.ex

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ defmodule Ash.Resource.Change.CascadeUpdate do
188188

189189
defp update_related(_, [], _, _), do: :ok
190190

191-
defp update_related(changeset, data, opts, %{tenant: tenant} = context) do
191+
defp update_related(changeset, data, opts, context) do
192192
action = opts.action
193193
relationship = opts.relationship
194194
copies = opts.copy_inputs
@@ -214,7 +214,7 @@ defmodule Ash.Resource.Change.CascadeUpdate do
214214
return_notifications?: opts.return_notifications?
215215
)
216216

217-
context =
217+
action_context =
218218
Map.merge(relationship.context || %{}, %{
219219
cascade_update: true,
220220
accessing_from: %{source: relationship.source, name: relationship.name}
@@ -224,8 +224,8 @@ defmodule Ash.Resource.Change.CascadeUpdate do
224224
Keyword.update(
225225
context_opts,
226226
:context,
227-
context,
228-
&Map.merge(&1, context)
227+
action_context,
228+
&Map.merge(&1, action_context)
229229
)
230230

231231
case related_query(data, opts, context_opts) do
@@ -260,7 +260,7 @@ defmodule Ash.Resource.Change.CascadeUpdate do
260260
|> List.wrap()
261261
|> Ash.load!(
262262
[{relationship.name, load_query}],
263-
tenant: tenant
263+
scope: context
264264
)
265265
|> Enum.flat_map(fn record ->
266266
record
@@ -273,8 +273,8 @@ defmodule Ash.Resource.Change.CascadeUpdate do
273273
Keyword.update(
274274
context_opts,
275275
:context,
276-
context,
277-
&Map.merge(&1, context)
276+
action_context,
277+
&Map.merge(&1, action_context)
278278
)
279279
)
280280
end

0 commit comments

Comments
 (0)