Skip to content

Commit

Permalink
fix: make sure generic actions are authorized (#100)
Browse files Browse the repository at this point in the history
  • Loading branch information
barnabasJ authored Nov 24, 2023
1 parent c1845b2 commit b9a3051
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
8 changes: 7 additions & 1 deletion lib/graphql/resolver.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,17 @@ defmodule AshGraphql.Graphql.Resolver do
:gql_query,
query_name,
metadata do
opts = [
actor: Map.get(context, :actor),
authorize?: AshGraphql.Api.Info.authorize?(api),
tenant: Map.get(context, :tenant)
]

result =
%Ash.ActionInput{api: api, resource: resource}
|> Ash.ActionInput.set_context(get_context(context))
|> Ash.ActionInput.for_action(action.name, arguments)
|> api.run_action()
|> api.run_action(opts)
|> case do
{:ok, result} ->
load_opts =
Expand Down
2 changes: 1 addition & 1 deletion test/generic_actions_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ defmodule AshGraphql.GenericActionsTest do
postCount
}
"""
|> Absinthe.run(AshGraphql.Test.Schema)
|> Absinthe.run(AshGraphql.Test.Schema, context: %{actor: %{id: "an-actor"}})

assert {:ok, result} = resp

Expand Down
11 changes: 11 additions & 0 deletions test/support/resources/post.ex
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,21 @@ defmodule AshGraphql.Test.Post do

use Ash.Resource,
data_layer: Ash.DataLayer.Ets,
authorizers: [Ash.Policy.Authorizer],
extensions: [AshGraphql.Resource]

require Ash.Query

policies do
policy always() do
authorize_if(always())
end

policy action(:count) do
authorize_if(actor_present())
end
end

graphql do
type :post

Expand Down

0 comments on commit b9a3051

Please sign in to comment.