palkan/action_policy-graphql

Unable to override `authorization_namespace`

Tyflomate opened this issue · 11 comments

Tell us about your environment

Ruby Version: 2.7.1

Framework Version (Rails, whatever): Rails 6.1.4.1

Action Policy Version: 0.6.0

Action Policy GraphQL Version: 0.5.3

What did you do?

I'm trying to redefine authorization_namespace but nothing worked. For example for a mutation, the namespace is Mutations. When i tried to override it in BaseMutation, it was still Mutations. So i set a pry inside my definition but it was never reached. I also tried overriding it inside BaseObject or the GraphqlController directly but nothing worked.

What did you expect to happen?

To be able to custom the authorization_namespace

What actually happened?

My definition of authorization_namespace was never acknowledged.

Can you tell me how are we supposed to do this ? Thanks a lot

Could you please provide a code example? Where do you include Action Policy behaviour and where do you override (and how) #authorization_namespace? I will try to reproduce it

It is a classic RoR implementation of graphql. It is hard to give you a code example since it is a private repository :/ I tried overriding it in BaseMutation/BaseObject/ApplicationController(root of my graphql controller) classes. I put a pry inside each of my implementations but none was reached. In each of these classes i just did this:

def authorization_namespace
     ::UserApplication
end

If we have for example a policy like this: UserApplication::TotoPolicy.
I also put a pry inside the library to see the output of this method but for a mutation Mutations::UserApplication::TestMutation, it would always return Mutations::UserApplication.

It may be good to notice that in BaseMutation and BaseObject I have included include ActionPolicy::GraphQL::Behaviour

Thanks for the details! (And sorry for late response)

I will do my best to work on this till the end of the year 🙂

@Tyflomate I tried to reproduce the problem here but with no luck: 58e89be

Maybe, you can try to tweak our test examples to demonstrate the failing case?

Hey @Tyflomate! Was this issue resolved? And if not, could you please help with the reproduction (see the prev comment)?

Hello @palkan

I encountered the same problem yesterday, maybe in another form.

Our environment is pretty much the same as @Tyflomate. To deduplicate behaviour declaration between our GraphQL types and mutations, I used an ActiveSupport::Concern to include in both base mutation and base object.

So far, it looks like:

module Authorizations
  extend ActiveSupport::Concern

  included do
    include ActionPolicy::GraphQL::Behaviour
    prepend LookupMethods
  end

  module LookupMethods
    def authorization_namespace = ::Api
    def default_authorization_policy_class = ::ApiPolicy
  end
end

We need to use prepend or lookup methods declared in action_policy/behaviours/namespaced.rb take precedence over our version of authorization_namespace.

On the other side, default_authorization_policy_class works however we define it: prepended, included or extended. I would have expected authorization_namespace to work the same.

Hey @gael-ian!

Thanks for the snippet.

I see what's going on here: we define the #authorization_namespace method right on the class:

def base.authorization_namespace

That's why include doesn't work.

Agree, this is confusing, let's fix it.

Nope, the code I linked above only affects authorized? callbacks—is this your case @gael-ian ? Otherwise, overriding #authorization_namespace works fine, see f0129eb

Sorry didn't have the time to work again on this issue, but we might try again in another context we we'll see the result

Okay, thanks; closing for now; will re-open if the problem occurs again.