Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Authorization::Reader::DSLFileNotFoundError #99

Closed
wants to merge 1 commit into from

5 participants

@joshkrueger

This fixes what seems to be a 'load order' bug cause by Rails.root being unavailable.

The environment this was experienced in is Rails 3.0.3, Ruby 1.8.7, declarative_authorization 0.5.2

Since constants are declared at load time and not run time, this bug pops up even though at run time, we have a fully functional Rails.root

99% of the time however, it is a non-issue as it falls back to the relative path of 'config/authorization_rules.rb', which when you are inside your Rails.root, works just fine.

The bug happens when you run code that invokes declarative_authorization outside of Rails.root. Manually running integration tests inside your test folder for example or stand-alone scripts in a bin folder.

Considering AUTH_DSL_FILES is referenced only once in the entire project, moving it into the Reader::DSLReader's factory method call seemed like a somewhat sane idea.

@stffn
Owner

We need to be careful here. There are people using that constant to configure the location. Given that the constant occurs in the documentation, we should try to keep it.

As an alternative approach: What about not setting the constant by default, but use it instead of Rails.root if defined at that point?

@joshkrueger

While people may be using it, some thought needs to be put into whether or not the mechanism for this configuration option should remain a constant.

Your suggestion for the alternative approach does solve this issue, but still requires the use of a constant, which some would consider sloppy.

An even better approach would be to deprecate the constant in a future release, while introducing a method call to explicitly set the file path.

@stffn
Owner

Absolutely. I'm fully with you there. There also is a issue on a better configuration infrastructure, in which the file path setting could be integrated. This would be a good point to make a first step. Only, we should keep the constant as a deprecated way in there.

@stffn
Owner

I was referring to #44

@joshkrueger

Awesome, I'll play around with some ideas and if anything decent comes of it I'll send it your way.

@inkdeep

I know probably a silly idea but what about walking up the directory tree looking for a config directory?

Dir.chdir('..') while !Dir.entries(Dir.pwd).include?('config')

Then the file path is correct for 'config/authorization_rules.rb'

I've hit this problem with name spaced controllers in functional tests.

@eonflare

I'm hitting this problem currently when deploying via Capistrano and Thin. I attempt to change the directory so that the file path fallback functions properly:

run "cd /var/wwwpath/to/app/current && bundle exec rails server thin -e qa -p 4002 --daemon"

However it still seems unable to find the file:

Error reading authorization rules file with path 'config/authorization_rules.rb'!  Please ensure it exists and that it is accessible.

Using Ruby 1.9.2, Rails 3.0.7, and Thin 1.2.11. I will continue to dig and let you know what I find, as it seems more likely to be an issue on my end.

@wolframarnold

It's an issue, so +1 from here.
The best way is probably to use the engine architecture to set the config path. And then just require the file without path. See Jose Valim's new book "Crafting Rails Applications"

@stffn
Owner

+1 for a patch on this, using the engine architecture. It needs to be backwards compatible to Rails 2.3, though.

@stffn
Owner

This is better tracked in #44

@stffn stffn closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 20, 2011
  1. @joshkrueger

    Fixed a 'load order' bug caused by constants being declared at load t…

    joshkrueger authored
    …ime, when Rails.root is unavailable
This page is out of date. Refresh to see the latest.
Showing with 2 additions and 4 deletions.
  1. +2 −4 lib/declarative_authorization/authorization.rb
View
6 lib/declarative_authorization/authorization.rb
@@ -20,8 +20,6 @@ class AuthorizationUsageError < AuthorizationError ; end
# The exception is raised to ensure that the entire rule is invalidated.
class NilAttributeValueError < AuthorizationError; end
- AUTH_DSL_FILES = [Pathname.new(Rails.root || '').join("config", "authorization_rules.rb").to_s] unless defined? AUTH_DSL_FILES
-
# Controller-independent method for retrieving the current user.
# Needed for model security where the current controller is not available.
def self.current_user
@@ -71,10 +69,10 @@ class Engine
:rev_role_hierarchy
# If +reader+ is not given, a new one is created with the default
- # authorization configuration of +AUTH_DSL_FILES+. If given, may be either
+ # authorization configuration of config/authorization_rules.rb inside the Rails.root. If given, may be either
# a Reader object or a path to a configuration file.
def initialize (reader = nil)
- reader = Reader::DSLReader.factory(reader || AUTH_DSL_FILES)
+ reader = Reader::DSLReader.factory(reader || [Pathname.new(Rails.root || '').join("config", "authorization_rules.rb").to_s])
@privileges = reader.privileges_reader.privileges
# {priv => [[priv, ctx],...]}
Something went wrong with that request. Please try again.