Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: make sure generic actions are authorized #100

Merged
merged 1 commit into from
Nov 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit confused about this because it seems like context is already the context, and those values are never attached to the action.

This looks for another nested context or ash_context inside the context. This is also done in all the other resolvers, and it seems like the actor is only set in the context because of the opts. Not sure if I'm missing something there?

Copy link
Contributor Author

@barnabasJ barnabasJ Nov 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think everything is working for now. I just wanted to make sure I understood this.

|> 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
Loading