nathanl / authority

*CURRENTLY UNMAINTAINED*. Authority helps you authorize actions in your Rails app. It's ORM-neutral and has very little fancy syntax; just group your models under one or more Authorizer classes and write plain Ruby methods on them.
MIT License
1.21k stars 67 forks source link

authorize_action_for: wrong number of arguments #36

Closed JeanMertz closed 11 years ago

JeanMertz commented 11 years ago

I'm having some issues implementing Authority for my controller actions. It seems that Ruby chokes somewhere in the method_missing call stack. I am using Ruby 2.0, could it be related to this?

Here's the stack trace:

ArgumentError - wrong number of arguments (2 for 1):
   ~/.rbenv/versions/2.0.0-p0/lib/ruby/2.0.0/forwardable.rb:171:in `readable_by?'
   ~/.rbenv/versions/2.0.0-p0/lib/ruby/2.0.0/forwardable.rb:171:in `readable_by?'
   (gem) authority-2.5.0/lib/authority/user_abilities.rb:15:in `can_read?'
   (gem) authority-2.5.0/lib/authority.rb:43:in `action_authorized?'
   (gem) authority-2.5.0/lib/authority.rb:35:in `enforce'
   (gem) authority-2.5.0/lib/authority/controller.rb:76:in `authorize_action_for'
   app/controllers/accounts_controller.rb:7:in `call'

It all started with resource not being available in AccountAuthorizer#readable_by?, so I tried setting some options, this broke things:

authorize_action_for(accounts.first, :testing => accounts)

adamhunter commented 11 years ago

@JeanMertz,

For backwards compatibility, Authority lets you define your authorization methods with one or two arguments using some @nathanl black magic. Do your AccountAuthorizer's class and instance both take two arguments? (user, options={})

JeanMertz commented 11 years ago

@adamhunter Thank you for explaining. This was indeed the problem. I've read through the documentation again and did find the warning about this:

This would ultimately call creatable_by? on the designated authorizer with two arguments: the user and {:for => @post}. If you've defined that method yourself, you'd need to ensure that it accepts the options hash before doing this, or you'd get a "wrong number of arguments" error.

adamhunter commented 11 years ago

No problem! Glad you're liking Authority.

nathanl commented 11 years ago

Thanks @adamhunter for answering this question. Thanks to @JeanMertz for raising this issue, too. Having the stack trace point to Forwardable doesn't make it obvious where the problem is, so maybe I can try raising a more explanatory exception. I'll give it some thought.

nathanl commented 11 years ago

Reopening so I won't forget to look into this. :)

nathanl commented 11 years ago

@JeanMertz - would you try duplicating this issue using the remove_forwardable branch in your project? I think it will give you a clearer backtrace. If so, I will merge it to master.

nathanl commented 11 years ago

Merged pull request.

JeanMertz commented 11 years ago

@nathanl sorry for not responding sooner. I've been pulled off into other projects that didn't use Authority (yet), so haven't had a chance to look at this.

Anyway, awesome of you to fix this, should ease on the head-scratching when things break.

nathanl commented 11 years ago

You're very welcome. I want Authority to be easy to work with, so I appreciate hearing about anything confusing.