access_policy 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: feb81c43a683b9e8237ea9c967e2e23c7ff768a1
|
4
|
+
data.tar.gz: c8ec25b6998f7cc9c6ec47b5de509b3f3226a522
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f2acab607775708c0bf8eb59df2d1282e6afb7718039804fee723b248e72f6e16cd0d6dcce8219f10c77b288ec71fdd3e26941e046cf272857789166f1856926
|
7
|
+
data.tar.gz: 6b7e3d5c100fd6d1a5090a8eaeda60cf1b8daaebbb632fe713b030db9f73ef68f8dad4dbccbb62bdefc35135d212d1953a0488af7012cb88d077baf141a7f66a
|
@@ -4,7 +4,7 @@ module AccessPolicy
|
|
4
4
|
attr_accessor :current_user_or_role, :object_or_class, :query, :default_error_policy
|
5
5
|
|
6
6
|
def initialize(current_user_or_role, object_or_class, query=nil, default_error_policy=->(*) { raise })
|
7
|
-
raise NotDefinedError, 'unable to find policy class for anonymous classes'
|
7
|
+
raise NotDefinedError, 'unable to find policy class for anonymous classes' unless policy_class_can_be_found_for?(object_or_class)
|
8
8
|
|
9
9
|
self.current_user_or_role = current_user_or_role
|
10
10
|
self.object_or_class = object_or_class
|
@@ -37,6 +37,11 @@ module AccessPolicy
|
|
37
37
|
|
38
38
|
protected
|
39
39
|
|
40
|
+
def policy_class_can_be_found_for?(object_or_class)
|
41
|
+
subject = class_to_guard(object_or_class)
|
42
|
+
(!subject.name.nil? && subject.name.length > 0) || subject.respond_to?(:policy_class)
|
43
|
+
end
|
44
|
+
|
40
45
|
def class_to_guard(obj_or_class=object_or_class)
|
41
46
|
obj_or_class.is_a?(Class) ? obj_or_class : obj_or_class.class
|
42
47
|
end
|
data/lib/access_policy.rb
CHANGED
@@ -44,7 +44,7 @@ module AccessPolicy
|
|
44
44
|
module ClassMethods
|
45
45
|
|
46
46
|
def policy_guarded_method(action_name, &block)
|
47
|
-
unsafe_action_name =
|
47
|
+
unsafe_action_name = unsafe_action_name(action_name)
|
48
48
|
|
49
49
|
define_method action_name do |*args|
|
50
50
|
_authorize "#{action_name}?"
|
@@ -54,6 +54,10 @@ module AccessPolicy
|
|
54
54
|
define_method unsafe_action_name, block
|
55
55
|
end
|
56
56
|
|
57
|
+
def unsafe_action_name(action_name)
|
58
|
+
:"#{action_name}_unsafe"
|
59
|
+
end
|
60
|
+
|
57
61
|
end
|
58
62
|
|
59
63
|
end
|
@@ -54,6 +54,16 @@ module AccessPolicy
|
|
54
54
|
it 'raises an error when class to guard has no name' do
|
55
55
|
expect{AccessPolicy::PolicyEnforcer.new(current_user, Class.new.new, action, error_policy)}.to raise_error
|
56
56
|
end
|
57
|
+
|
58
|
+
it 'raises no error when class to guard has no name but an policy class is defined' do
|
59
|
+
some_class = Class.new do
|
60
|
+
def self.policy_class
|
61
|
+
Class.new
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
expect { AccessPolicy::PolicyEnforcer.new(current_user, some_class.new, action, error_policy) }.not_to raise_error
|
66
|
+
end
|
57
67
|
end
|
58
68
|
|
59
69
|
|