objectbouncer 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
@@ -95,18 +95,22 @@ module ObjectBouncer
|
|
95
95
|
end
|
96
96
|
|
97
97
|
def protect_method!(method, force = false)
|
98
|
-
renamed_method = "#{method}_without_objectbouncer".to_sym
|
99
98
|
if method_defined?(method)
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
99
|
+
renamed_method = "#{method}_without_objectbouncer".to_sym
|
100
|
+
new_method = "#{method}_with_objectbouncer".to_sym
|
101
|
+
unless method_defined?(new_method)
|
102
|
+
define_method new_method do |*args, &block|
|
103
|
+
if call_denied?(method, *args)
|
104
|
+
raise ObjectBouncer::PermissionDenied.new
|
105
|
+
else
|
106
|
+
send(renamed_method, *args, &block)
|
107
|
+
end
|
108
108
|
end
|
109
109
|
end
|
110
|
+
if instance_method(method) != instance_method(new_method)
|
111
|
+
alias_method renamed_method, method
|
112
|
+
alias_method method, new_method
|
113
|
+
end
|
110
114
|
end
|
111
115
|
end
|
112
116
|
|
@@ -12,9 +12,9 @@ class Book < ActiveRecord::Base
|
|
12
12
|
deny :destroy
|
13
13
|
end
|
14
14
|
|
15
|
-
|
16
|
-
|
17
|
-
|
15
|
+
def destroy
|
16
|
+
super
|
17
|
+
end
|
18
18
|
end
|
19
19
|
|
20
20
|
class Author
|
@@ -66,8 +66,6 @@ class ActiveRecordTest < Test::Unit::TestCase
|
|
66
66
|
@book = Book.create!(:name => "On Food & Cooking",
|
67
67
|
:author => "Harold McGee",
|
68
68
|
:price => 4900)
|
69
|
-
@book_as_author = Book.as(@author).find(@book.id)
|
70
|
-
@book_as_reader = Book.as(@author).find(@book.id)
|
71
69
|
end
|
72
70
|
|
73
71
|
should "prevent all users from destroying (even with an overridden method)" do
|
@@ -89,18 +89,6 @@ class ObjectBouncerTest < Test::Unit::TestCase
|
|
89
89
|
end
|
90
90
|
end
|
91
91
|
|
92
|
-
should "be able to specify user on creation" do
|
93
|
-
joe_public = JoePublic.new
|
94
|
-
@president = President.as(joe_public).new
|
95
|
-
assert_raise ObjectBouncer::PermissionDenied do
|
96
|
-
@president.shake_hands
|
97
|
-
end
|
98
|
-
|
99
|
-
first_lady = MichelleObama.new
|
100
|
-
@president = President.as(first_lady).new
|
101
|
-
assert_equal "shaking hands", @president.shake_hands
|
102
|
-
end
|
103
|
-
|
104
92
|
end
|
105
93
|
|
106
94
|
end
|