Skip to content

Commit 7da8892

Browse files
committed
Merge branch 'master' into 1.0.0
2 parents 8239e4e + cdfa2e9 commit 7da8892

File tree

8 files changed

+178
-26
lines changed

8 files changed

+178
-26
lines changed

gemfiles/4.1.gemfile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
source 'https://rubygems.org'
2+
3+
gem 'rails', '~> 4.1.0'
4+
gem 'sqlite3'
5+
gem 'ruby_parser'
6+
gem 'rdoc'
7+
gemspec :path => '..'

lib/declarative_authorization/authorization.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ def permit! (privilege, options = {})
164164
# Example: permit!( :edit, :object => user.posts )
165165
#
166166
if Authorization.is_a_association_proxy?(options[:object]) && options[:object].respond_to?(:new)
167-
options[:object] = (Rails.version < "3.0" ? options[:object] : options[:object].scoped).new
167+
options[:object] = (Rails.version < "3.0" ? options[:object] : options[:object].where(nil)).new
168168
end
169169

170170
options[:context] ||= options[:object] && (
@@ -625,7 +625,13 @@ def to_long_s (hash = nil)
625625
protected
626626
def object_attribute_value (object, attr)
627627
begin
628-
object.respond_to?(:proxy_association) ? object.shift.send(attr) : object.send(attr)
628+
if object.respond_to?(:proxy_association)
629+
first = object.first
630+
object.delete(first)
631+
first.send(attr)
632+
else
633+
object.send(attr)
634+
end
629635
rescue ArgumentError, NoMethodError => e
630636
raise AuthorizationUsageError, "Error occurred while validating attribute ##{attr} on #{object.inspect}: #{e}.\n" +
631637
"Please check your authorization rules and ensure the attribute is correctly spelled and \n" +

lib/declarative_authorization/in_model.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ def self.with_permissions_to (*args)
105105
privilege = (args[0] || :read).to_sym
106106
privileges = [privilege]
107107

108-
parent_scope = scoped
108+
parent_scope = where(nil)
109109
context =
110110
if options[:context]
111111
options[:context]

lib/declarative_authorization/obligation_scope.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,12 @@ def initialize (model, options)
5555
def scope
5656
if Rails.version < "3"
5757
self
58-
else
58+
elsif Rails.version < "4"
5959
# for Rails < 3: scope, after setting proxy_options
6060
self.klass.scoped(@finder_options)
61+
else
62+
# TODO Refactor this. There is certainly a better way.
63+
self.klass.joins(@finder_options[:joins]).includes(@finder_options[:include]).where(@finder_options[:conditions])
6164
end
6265
end
6366

test/model_test.rb

Lines changed: 88 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,27 @@ class TestModel < ActiveRecord::Base
1717
has_many :test_attrs
1818
has_many :test_another_attrs, :class_name => "TestAttr", :foreign_key => :test_another_model_id
1919
has_many :test_attr_throughs, :through => :test_attrs
20-
has_many :test_attrs_with_attr, :class_name => "TestAttr", :conditions => {:attr => 1}
21-
has_many :test_attr_throughs_with_attr, :through => :test_attrs,
22-
:class_name => "TestAttrThrough", :source => :test_attr_throughs,
23-
:conditions => "test_attrs.attr = 1"
2420
has_one :test_attr_has_one, :class_name => "TestAttr"
25-
has_one :test_attr_throughs_with_attr_and_has_one, :through => :test_attrs,
26-
:class_name => "TestAttrThrough", :source => :test_attr_throughs,
27-
:conditions => "test_attrs.attr = 1"
21+
has_many :branches
22+
23+
# :conditions is deprecated in Rails 4.1
24+
if Rails.version >= '4'
25+
has_many :test_attrs_with_attr, lambda { where(:attr => 1) }, :class_name => "TestAttr"
26+
has_many :test_attr_throughs_with_attr, lambda { where("test_attrs.attr = 1") }, :through => :test_attrs,
27+
:class_name => "TestAttrThrough", :source => :test_attr_throughs
28+
29+
has_one :test_attr_throughs_with_attr_and_has_one, lambda { where("test_attrs.attr = 1") }, :through => :test_attrs,
30+
:class_name => "TestAttrThrough", :source => :test_attr_throughs
31+
else
32+
has_many :test_attrs_with_attr, :class_name => "TestAttr", :conditions => {:attr => 1}
33+
has_many :test_attr_throughs_with_attr, :through => :test_attrs,
34+
:class_name => "TestAttrThrough", :source => :test_attr_throughs,
35+
:conditions => "test_attrs.attr = 1"
36+
37+
has_one :test_attr_throughs_with_attr_and_has_one, :through => :test_attrs,
38+
:class_name => "TestAttrThrough", :source => :test_attr_throughs,
39+
:conditions => "test_attrs.attr = 1"
40+
end
2841

2942
if Rails.version < '4'
3043
attr_accessible :content, :test_attr_through_id, :country_id
@@ -38,8 +51,10 @@ class TestModel < ActiveRecord::Base
3851

3952
if Rails.version < "3"
4053
named_scope :with_content, :conditions => "test_models.content IS NOT NULL"
41-
else
54+
elsif Rails.version < "4"
4255
scope :with_content, :conditions => "test_models.content IS NOT NULL"
56+
else
57+
scope :with_content, lambda { where("test_models.content IS NOT NULL") }
4358
end
4459

4560
# Primary key test
@@ -121,9 +136,10 @@ class TestModelSecurityModelWithFind < ActiveRecord::Base
121136
class Branch < ActiveRecord::Base
122137
has_many :test_attrs
123138
belongs_to :company
139+
belongs_to :test_model
124140

125141
if Rails.version < '4'
126-
attr_accessible :name, :company
142+
attr_accessible :name, :company, :test_model
127143
end
128144
end
129145
class Company < ActiveRecord::Base
@@ -170,7 +186,12 @@ def test_multiple_deep_ored_belongs_to
170186
:test_another_model_id => test_model_2.id
171187

172188
user = MockUser.new(:test_role, :id => test_attr_1)
173-
assert_equal 1, TestAttr.with_permissions_to(:read, :user => user).length
189+
if Rails.version >= '4'
190+
assert_equal 1, TestAttr.with_permissions_to(:read, :user => user).references(:test_attrs, :test_attrs_test_models, :test_attrs_test_models_2).length
191+
else
192+
assert_equal 1, TestAttr.with_permissions_to(:read, :user => user).length
193+
end
194+
174195
TestAttr.delete_all
175196
TestModel.delete_all
176197
end
@@ -802,7 +823,11 @@ def test_with_contains
802823
user = MockUser.new(:test_role,
803824
:id => test_model_1.test_attrs.first.id)
804825
assert_equal 1, TestModel.with_permissions_to(:read, :user => user).length
805-
assert_equal 1, TestModel.with_permissions_to(:read, :user => user).find(:all, :conditions => {:id => test_model_1.id}).length
826+
if Rails.version < '3'
827+
assert_equal 1, TestModel.with_permissions_to(:read, :user => user).find(:all, :conditions => {:id => test_model_1.id} ).length
828+
else
829+
assert_equal 1, TestModel.with_permissions_to(:read, :user => user).where(:id => test_model_1.id).length
830+
end
806831

807832
TestModel.delete_all
808833
TestAttr.delete_all
@@ -1185,7 +1210,11 @@ def test_with_anded_if_permitted_to
11851210
assert Authorization::Engine.instance.permit?(:read, :object => test_model_1.test_attrs.first, :user => user_with_both_roles)
11861211
assert Authorization::Engine.instance.permit?(:read, :object => test_model_for_second_role.test_attrs.first, :user => user_with_both_roles)
11871212
#p Authorization::Engine.instance.obligations(:read, :user => user_with_both_roles, :context => :test_attrs)
1188-
assert_equal 2, TestAttr.with_permissions_to(:read, :user => user_with_both_roles).length
1213+
if Rails.version >= '4'
1214+
assert_equal 2, TestAttr.with_permissions_to(:read, :user => user_with_both_roles).references(:test_attrs, :test_models).length
1215+
else
1216+
assert_equal 1, TestAttr.with_permissions_to(:read, :user => user).length
1217+
end
11891218

11901219
TestModel.delete_all
11911220
TestAttr.delete_all
@@ -1446,7 +1475,11 @@ def test_with_ored_rules_and_reoccuring_tables
14461475
test_attr_2.test_model.test_attrs.create!
14471476

14481477
user = MockUser.new(:test_role, :test_attr => test_attr_2.test_model.test_attrs.last)
1449-
assert_equal 2, TestAttr.with_permissions_to(:read, :user => user).length
1478+
if Rails.version >= '4'
1479+
assert_equal 2, TestAttr.with_permissions_to(:read, :user => user).references(:test_attrs, :test_models, :test_models_test_attrs, :test_attrs_test_models).length
1480+
else
1481+
assert_equal 2, TestAttr.with_permissions_to(:read, :user => user).length
1482+
end
14501483
TestModel.delete_all
14511484
TestAttr.delete_all
14521485
end
@@ -1483,7 +1516,11 @@ def test_with_many_ored_rules_and_reoccuring_tables
14831516

14841517
user = MockUser.new(:test_role, :test_model => country.test_models.first)
14851518

1486-
assert_equal 2, TestAttr.with_permissions_to(:read, :user => user).length
1519+
if Rails.version >= '4'
1520+
assert_equal 2, TestAttr.with_permissions_to(:read, :user => user).references(:test_attrs, :test_models, :test_models_countries).length
1521+
else
1522+
assert_equal 2, TestAttr.with_permissions_to(:read, :user => user).length
1523+
end
14871524
TestModel.delete_all
14881525
TestAttr.delete_all
14891526
end
@@ -1822,12 +1859,43 @@ def test_authorization_permit_association_proxy
18221859
test_model = TestModel.create(:content => "content")
18231860
assert engine.permit?(:read, :object => test_model.test_attrs,
18241861
:user => MockUser.new(:test_role))
1825-
assert test_model.test_attrs.empty?
18261862
assert !engine.permit?(:read, :object => TestAttr.new,
18271863
:user => MockUser.new(:test_role))
18281864
TestModel.delete_all
18291865
end
18301866

1867+
# def test_authorization_permit_nested_association_proxy
1868+
# reader = Authorization::Reader::DSLReader.new
1869+
# reader.parse %{
1870+
# authorization do
1871+
# role :test_role do
1872+
# has_permission_on :branches, :to => :read do
1873+
# if_attribute :test_model => { :test_attrs => {:attr => 1 } }
1874+
# end
1875+
# end
1876+
# end
1877+
# }
1878+
# engine = Authorization::Engine.instance(reader)
1879+
1880+
# test_model = TestModel.create!
1881+
# test_model.test_attrs.create!(:attr => 0)
1882+
# test_attr = test_model.test_attrs.create!(:attr => 1)
1883+
# test_model.test_attrs.create!(:attr => 3)
1884+
# test_branch = Branch.create!(:test_model => test_model)
1885+
1886+
# test_model_2 = TestModel.create!
1887+
# test_attr_2 = test_model_2.test_attrs.create!(:attr => 2)
1888+
# test_branch_2 = Branch.create!(:test_model => test_model_2)
1889+
1890+
# assert engine.permit?(:read, :object => test_branch,
1891+
# :user => MockUser.new(:test_role))
1892+
# assert !engine.permit?(:read, :object => test_branch_2,
1893+
# :user => MockUser.new(:test_role))
1894+
# TestModel.delete_all
1895+
# Branch.delete_all
1896+
# TestAttr.delete_all
1897+
# end
1898+
18311899
def test_multiple_roles_with_has_many_through
18321900
reader = Authorization::Reader::DSLReader.new
18331901
reader.parse %{
@@ -1860,7 +1928,11 @@ def test_multiple_roles_with_has_many_through
18601928
user = MockUser.new(:test_role_1, :test_role_2,
18611929
:test_attr_through_id => test_model_1.test_attr_throughs.first.id,
18621930
:test_attr_through_2_id => test_model_2.test_attr_throughs.first.id)
1863-
assert_equal 2, TestModel.with_permissions_to(:read, :user => user).length
1931+
if Rails.version >= '4'
1932+
assert_equal 2, TestModel.with_permissions_to(:read, :user => user).references(:test_models, :test_attr_throughs).length
1933+
else
1934+
assert_equal 2, TestModel.with_permissions_to(:read, :user => user).length
1935+
end
18641936
TestModel.delete_all
18651937
TestAttr.delete_all
18661938
TestAttrThrough.delete_all

test/schema.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ CREATE TABLE 'n_way_join_items' (
3939
CREATE TABLE 'branches' (
4040
'id' INTEGER PRIMARY KEY NOT NULL,
4141
'company_id' integer,
42+
'test_model_id' integer,
4243
'name' text
4344
);
4445

test/test_helper.rb

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
require 'test/unit'
21
require 'pathname'
32

43
ENV['RAILS_ENV'] = 'test'
@@ -13,16 +12,22 @@
1312
end
1413
Bundler.require
1514

15+
if Rails.version >= '4.1'
16+
require 'minitest/autorun'
17+
require 'test_support/minitest_compatibility'
18+
else
19+
require 'test/unit'
20+
end
21+
1622
# rails 2.3 and ruby 1.9.3 fix
1723
MissingSourceFile::REGEXPS.push([/^cannot load such file -- (.+)$/i, 1])
1824

1925
# Silence Rails 4 deprecation warnings in test suite
2026
# TODO: Model.scoped is deprecated
2127
# TODO: Eager loading Post.includes(:comments).where("comments.title = 'foo'") becomes Post.includes(:comments).where("comments.title = 'foo'").references(:comments)
22-
# TODO: has_many conditions is deprecated for a scoped block
23-
if Rails.version >= '4'
24-
ActiveSupport::Deprecation.silenced = true
25-
end
28+
# if Rails.version >= '4'
29+
# ActiveSupport::Deprecation.silenced = true
30+
# end
2631

2732
puts "Testing against rails #{Rails::VERSION::STRING}"
2833

@@ -189,11 +194,42 @@ def setup
189194
end
190195
end
191196

192-
else
197+
elsif Rails.version < '4.1'
193198
class Test::Unit::TestCase
194199
include Authorization::TestHelper
195200
end
196201

202+
class ActiveSupport::TestCase
203+
include Authorization::TestHelper
204+
205+
def request! (user, action, reader, params = {})
206+
action = action.to_sym if action.is_a?(String)
207+
@controller.current_user = user
208+
@controller.authorization_engine = Authorization::Engine.new(reader)
209+
210+
((params.delete(:clear) || []) + [:@authorized]).each do |var|
211+
@controller.instance_variable_set(var, nil)
212+
end
213+
get action, params
214+
end
215+
216+
unless Rails.version < "3"
217+
def setup
218+
#@routes = Rails::Application.routes
219+
@routes = Rails.application.routes
220+
end
221+
end
222+
end
223+
else
224+
module Test
225+
module Unit
226+
end
227+
end
228+
229+
class Test::Unit::TestCase < Minitest::Test
230+
include Authorization::TestHelper
231+
end
232+
197233
class ActiveSupport::TestCase
198234
include Authorization::TestHelper
199235

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
require 'minitest/assertions'
2+
3+
module Minitest
4+
module Assertions
5+
6+
# test/unit backwards compatibility methods
7+
alias :assert_raise :assert_raises
8+
alias :assert_not_empty :refute_empty
9+
alias :assert_not_equal :refute_equal
10+
alias :assert_not_in_delta :refute_in_delta
11+
alias :assert_not_in_epsilon :refute_in_epsilon
12+
alias :assert_not_includes :refute_includes
13+
alias :assert_not_instance_of :refute_instance_of
14+
alias :assert_not_kind_of :refute_kind_of
15+
alias :assert_no_match :refute_match
16+
alias :assert_not_nil :refute_nil
17+
alias :assert_not_operator :refute_operator
18+
alias :assert_not_predicate :refute_predicate
19+
alias :assert_not_respond_to :refute_respond_to
20+
alias :assert_not_same :refute_same
21+
22+
def assert_nothing_raised(*)
23+
yield
24+
end
25+
26+
end
27+
end

0 commit comments

Comments
 (0)