action_policy 0.7.2 → 0.7.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/lib/.rbnext/2.7/action_policy/policy/cache.rb +6 -6
- data/lib/.rbnext/3.0/action_policy/policy/cache.rb +6 -6
- data/lib/.rbnext/3.0/action_policy/policy/core.rb +34 -16
- data/lib/.rbnext/3.0/action_policy/policy/reasons.rb +2 -3
- data/lib/.rbnext/3.2/action_policy/policy/core.rb +34 -16
- data/lib/action_policy/authorizer.rb +8 -5
- data/lib/action_policy/behaviour.rb +1 -2
- data/lib/action_policy/policy/cache.rb +6 -6
- data/lib/action_policy/policy/cached_apply.rb +5 -8
- data/lib/action_policy/policy/core.rb +34 -16
- data/lib/action_policy/policy/reasons.rb +2 -3
- data/lib/action_policy/rails/authorizer.rb +4 -4
- data/lib/action_policy/rails/policy/instrumentation.rb +3 -3
- data/lib/action_policy/rspec/dsl.rb +1 -0
- data/lib/action_policy/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 21363e4337fbe0caea24309750d79d94bb9fa6c4ae0a9d0697b0e686519fa763
|
4
|
+
data.tar.gz: 8ac572534b621240640723ea5c6ec942a73b91e14f2d40e534889c2dc7ba9251
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c1dd33d739cfdf1143b7cc2402c2e8bf7ebf1f1613fa7c650c72fddf0bd4fd4b5c924c920ff143ad582ab68524836f0d0c32641372c5999fdd95f6d9113078dc
|
7
|
+
data.tar.gz: 5fb9dbd9b1f19fb6bed19ddbeb99be9bd35495b2d0f89bd3fb0cd8fed59a0513c4a627fdd25291b56dcfc7cf9b60f64d351bb2b658d97351f9a7b5db2d1c4128
|
data/CHANGELOG.md
CHANGED
@@ -50,18 +50,18 @@ module ActionPolicy # :nodoc:
|
|
50
50
|
key = rule_cache_key(rule)
|
51
51
|
|
52
52
|
ActionPolicy.cache_store.then do |store|
|
53
|
-
|
53
|
+
result = store.read(key)
|
54
54
|
unless result.nil?
|
55
55
|
result.cached!
|
56
|
-
next result
|
56
|
+
next result
|
57
|
+
end
|
58
|
+
yield.tap do |result|
|
59
|
+
store.write(key, result, options)
|
57
60
|
end
|
58
|
-
yield
|
59
|
-
store.write(key, result, options)
|
60
|
-
result.value
|
61
61
|
end
|
62
62
|
end
|
63
63
|
|
64
|
-
def
|
64
|
+
def apply_r(rule)
|
65
65
|
return super if ActionPolicy.cache_store.nil? ||
|
66
66
|
!self.class.cached_rules.key?(rule)
|
67
67
|
|
@@ -50,18 +50,18 @@ module ActionPolicy # :nodoc:
|
|
50
50
|
key = rule_cache_key(rule)
|
51
51
|
|
52
52
|
ActionPolicy.cache_store.then do |store|
|
53
|
-
|
53
|
+
result = store.read(key)
|
54
54
|
unless result.nil?
|
55
55
|
result.cached!
|
56
|
-
next result
|
56
|
+
next result
|
57
|
+
end
|
58
|
+
yield.tap do |result|
|
59
|
+
store.write(key, result, options)
|
57
60
|
end
|
58
|
-
yield
|
59
|
-
store.write(key, result, options)
|
60
|
-
result.value
|
61
61
|
end
|
62
62
|
end
|
63
63
|
|
64
|
-
def
|
64
|
+
def apply_r(rule)
|
65
65
|
return super if ActionPolicy.cache_store.nil? ||
|
66
66
|
!self.class.cached_rules.key?(rule)
|
67
67
|
|
@@ -72,7 +72,7 @@ module ActionPolicy
|
|
72
72
|
|
73
73
|
include ActionPolicy::Behaviours::PolicyFor
|
74
74
|
|
75
|
-
attr_reader :record
|
75
|
+
attr_reader :record
|
76
76
|
|
77
77
|
# NEXT_RELEASE: deprecate `record` arg, migrate to `record: nil`
|
78
78
|
def initialize(record = nil, *__rest__)
|
@@ -83,13 +83,23 @@ module ActionPolicy
|
|
83
83
|
# Unlike simply calling a predicate rule (`policy.manage?`),
|
84
84
|
# `apply` also calls pre-checks.
|
85
85
|
def apply(rule)
|
86
|
-
|
86
|
+
res = apply_r(rule)
|
87
87
|
|
88
|
-
|
89
|
-
|
90
|
-
|
88
|
+
# DEPRECATED (we still rely on it in tests)
|
89
|
+
@result = res
|
90
|
+
|
91
|
+
res.value
|
92
|
+
end
|
93
|
+
|
94
|
+
# NEXT_RELEASE: This is gonna be #apply in 1.0
|
95
|
+
def apply_r(rule) # :nodoc:
|
96
|
+
with_result(rule) do |result|
|
97
|
+
catch :policy_fulfilled do
|
98
|
+
result.load __apply__(resolve_rule(rule))
|
99
|
+
end
|
91
100
|
|
92
|
-
|
101
|
+
result
|
102
|
+
end
|
93
103
|
end
|
94
104
|
|
95
105
|
def deny!
|
@@ -107,14 +117,17 @@ module ActionPolicy
|
|
107
117
|
# (such as caching, pre checks, etc.)
|
108
118
|
def __apply__(rule) ; public_send(rule); end
|
109
119
|
|
110
|
-
#
|
111
|
-
#
|
112
|
-
def
|
113
|
-
|
114
|
-
|
115
|
-
|
120
|
+
# Prepare a new result object for the next rule application.
|
121
|
+
# It's stored in the thread-local storage to be accessible from within the policy.
|
122
|
+
def with_result(rule) # :nodoc:
|
123
|
+
result = self.class.result_class.new(self.class, rule)
|
124
|
+
|
125
|
+
Thread.current[:__action_policy_result__] ||= []
|
126
|
+
Thread.current[:__action_policy_result__] << result
|
127
|
+
|
128
|
+
yield result
|
116
129
|
ensure
|
117
|
-
|
130
|
+
Thread.current[:__action_policy_result__]&.pop
|
118
131
|
end
|
119
132
|
|
120
133
|
# Returns a result of applying the specified rule to the specified record.
|
@@ -146,6 +159,13 @@ module ActionPolicy
|
|
146
159
|
activity
|
147
160
|
end
|
148
161
|
|
162
|
+
# Returns the result object for the last rule application within the given
|
163
|
+
# execution context (Thread or Fiber)
|
164
|
+
def result
|
165
|
+
# FIXME: Remove ivar fallback after 1.0
|
166
|
+
Thread.current[:__action_policy_result__]&.last || @result
|
167
|
+
end
|
168
|
+
|
149
169
|
# Return annotated source code for the rule
|
150
170
|
# NOTE: require "method_source" and "prism" gems to be installed.
|
151
171
|
# Otherwise returns empty string.
|
@@ -155,9 +175,7 @@ module ActionPolicy
|
|
155
175
|
# Useful for debugging: type `pp :show?` within the context of the policy
|
156
176
|
# to preview the rule.
|
157
177
|
def pp(rule)
|
158
|
-
|
159
|
-
# We need result to exist for `allowed_to?` to work correctly
|
160
|
-
@result = self.class.result_class.new(self.class, rule)
|
178
|
+
with_result(rule) do
|
161
179
|
header = "#{self.class.name}##{rule}"
|
162
180
|
source = inspect_rule(rule)
|
163
181
|
$stdout.puts "#{header}\n#{source}"
|
@@ -201,13 +201,12 @@ module ActionPolicy
|
|
201
201
|
if (record == :__undef__ || record == self.record) && options.empty?
|
202
202
|
rule = resolve_rule(rule)
|
203
203
|
policy = self
|
204
|
-
|
204
|
+
apply_r(rule)
|
205
205
|
else
|
206
206
|
policy = policy_for(record: record, **options)
|
207
207
|
rule = policy.resolve_rule(rule)
|
208
208
|
|
209
|
-
policy.
|
210
|
-
policy.result
|
209
|
+
policy.apply_r(rule)
|
211
210
|
end
|
212
211
|
|
213
212
|
if res.fail? && result&.reasons
|
@@ -72,7 +72,7 @@ module ActionPolicy
|
|
72
72
|
|
73
73
|
include ActionPolicy::Behaviours::PolicyFor
|
74
74
|
|
75
|
-
attr_reader :record
|
75
|
+
attr_reader :record
|
76
76
|
|
77
77
|
# NEXT_RELEASE: deprecate `record` arg, migrate to `record: nil`
|
78
78
|
def initialize(record = nil, *__rest__)
|
@@ -83,13 +83,23 @@ module ActionPolicy
|
|
83
83
|
# Unlike simply calling a predicate rule (`policy.manage?`),
|
84
84
|
# `apply` also calls pre-checks.
|
85
85
|
def apply(rule)
|
86
|
-
|
86
|
+
res = apply_r(rule)
|
87
87
|
|
88
|
-
|
89
|
-
|
90
|
-
|
88
|
+
# DEPRECATED (we still rely on it in tests)
|
89
|
+
@result = res
|
90
|
+
|
91
|
+
res.value
|
92
|
+
end
|
93
|
+
|
94
|
+
# NEXT_RELEASE: This is gonna be #apply in 1.0
|
95
|
+
def apply_r(rule) # :nodoc:
|
96
|
+
with_result(rule) do |result|
|
97
|
+
catch :policy_fulfilled do
|
98
|
+
result.load __apply__(resolve_rule(rule))
|
99
|
+
end
|
91
100
|
|
92
|
-
|
101
|
+
result
|
102
|
+
end
|
93
103
|
end
|
94
104
|
|
95
105
|
def deny!
|
@@ -107,14 +117,17 @@ module ActionPolicy
|
|
107
117
|
# (such as caching, pre checks, etc.)
|
108
118
|
def __apply__(rule) = public_send(rule)
|
109
119
|
|
110
|
-
#
|
111
|
-
#
|
112
|
-
def
|
113
|
-
|
114
|
-
|
115
|
-
|
120
|
+
# Prepare a new result object for the next rule application.
|
121
|
+
# It's stored in the thread-local storage to be accessible from within the policy.
|
122
|
+
def with_result(rule) # :nodoc:
|
123
|
+
result = self.class.result_class.new(self.class, rule)
|
124
|
+
|
125
|
+
Thread.current[:__action_policy_result__] ||= []
|
126
|
+
Thread.current[:__action_policy_result__] << result
|
127
|
+
|
128
|
+
yield result
|
116
129
|
ensure
|
117
|
-
|
130
|
+
Thread.current[:__action_policy_result__]&.pop
|
118
131
|
end
|
119
132
|
|
120
133
|
# Returns a result of applying the specified rule to the specified record.
|
@@ -146,6 +159,13 @@ module ActionPolicy
|
|
146
159
|
activity
|
147
160
|
end
|
148
161
|
|
162
|
+
# Returns the result object for the last rule application within the given
|
163
|
+
# execution context (Thread or Fiber)
|
164
|
+
def result
|
165
|
+
# FIXME: Remove ivar fallback after 1.0
|
166
|
+
Thread.current[:__action_policy_result__]&.last || @result
|
167
|
+
end
|
168
|
+
|
149
169
|
# Return annotated source code for the rule
|
150
170
|
# NOTE: require "method_source" and "prism" gems to be installed.
|
151
171
|
# Otherwise returns empty string.
|
@@ -155,9 +175,7 @@ module ActionPolicy
|
|
155
175
|
# Useful for debugging: type `pp :show?` within the context of the policy
|
156
176
|
# to preview the rule.
|
157
177
|
def pp(rule)
|
158
|
-
|
159
|
-
# We need result to exist for `allowed_to?` to work correctly
|
160
|
-
@result = self.class.result_class.new(self.class, rule)
|
178
|
+
with_result(rule) do
|
161
179
|
header = "#{self.class.name}##{rule}"
|
162
180
|
source = inspect_rule(rule)
|
163
181
|
$stdout.puts "#{header}\n#{source}"
|
@@ -5,10 +5,11 @@ module ActionPolicy
|
|
5
5
|
class Unauthorized < Error
|
6
6
|
attr_reader :policy, :rule, :result
|
7
7
|
|
8
|
-
|
8
|
+
# NEXT_RELEASE: remove result fallback
|
9
|
+
def initialize(policy, rule, result = policy.result)
|
9
10
|
@policy = policy.class
|
10
11
|
@rule = rule
|
11
|
-
@result =
|
12
|
+
@result = result
|
12
13
|
|
13
14
|
super("Not authorized: #{@policy}##{@rule} returns false")
|
14
15
|
end
|
@@ -20,12 +21,14 @@ module ActionPolicy
|
|
20
21
|
class << self
|
21
22
|
# Performs authorization, raises an exception when check failed.
|
22
23
|
def call(policy, rule)
|
23
|
-
authorize(policy, rule)
|
24
|
-
|
24
|
+
res = authorize(policy, rule)
|
25
|
+
return if res.success?
|
26
|
+
|
27
|
+
raise(::ActionPolicy::Unauthorized.new(policy, rule, res))
|
25
28
|
end
|
26
29
|
|
27
30
|
def authorize(policy, rule)
|
28
|
-
policy.
|
31
|
+
policy.apply_r(rule)
|
29
32
|
end
|
30
33
|
|
31
34
|
# Applies scope to the target
|
@@ -53,8 +53,7 @@ module ActionPolicy
|
|
53
53
|
def allowance_to(rule, record = :__undef__, **options)
|
54
54
|
policy = lookup_authorization_policy(record, **options)
|
55
55
|
|
56
|
-
policy.
|
57
|
-
policy.result
|
56
|
+
policy.apply_r(authorization_rule_for(policy, rule))
|
58
57
|
end
|
59
58
|
|
60
59
|
def authorization_context
|
@@ -50,18 +50,18 @@ module ActionPolicy # :nodoc:
|
|
50
50
|
key = rule_cache_key(rule)
|
51
51
|
|
52
52
|
ActionPolicy.cache_store.then do |store|
|
53
|
-
|
53
|
+
result = store.read(key)
|
54
54
|
unless result.nil?
|
55
55
|
result.cached!
|
56
|
-
next result
|
56
|
+
next result
|
57
|
+
end
|
58
|
+
yield.tap do |result|
|
59
|
+
store.write(key, result, options)
|
57
60
|
end
|
58
|
-
yield
|
59
|
-
store.write(key, result, options)
|
60
|
-
result.value
|
61
61
|
end
|
62
62
|
end
|
63
63
|
|
64
|
-
def
|
64
|
+
def apply_r(rule)
|
65
65
|
return super if ActionPolicy.cache_store.nil? ||
|
66
66
|
!self.class.cached_rules.key?(rule)
|
67
67
|
|
@@ -7,19 +7,16 @@ module ActionPolicy
|
|
7
7
|
# When you call `apply` twice on the same policy and for the same rule,
|
8
8
|
# the check (and pre-checks) is only called once.
|
9
9
|
module CachedApply
|
10
|
-
def
|
10
|
+
def apply_r(rule)
|
11
11
|
@__rules_cache__ ||= {}
|
12
12
|
|
13
13
|
if @__rules_cache__.key?(rule)
|
14
|
-
|
15
|
-
return result.value
|
14
|
+
return @__rules_cache__[rule]
|
16
15
|
end
|
17
16
|
|
18
|
-
super
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
result.value
|
17
|
+
super.tap do |result|
|
18
|
+
@__rules_cache__[rule] = result
|
19
|
+
end
|
23
20
|
end
|
24
21
|
end
|
25
22
|
end
|
@@ -72,7 +72,7 @@ module ActionPolicy
|
|
72
72
|
|
73
73
|
include ActionPolicy::Behaviours::PolicyFor
|
74
74
|
|
75
|
-
attr_reader :record
|
75
|
+
attr_reader :record
|
76
76
|
|
77
77
|
# NEXT_RELEASE: deprecate `record` arg, migrate to `record: nil`
|
78
78
|
def initialize(record = nil, *)
|
@@ -83,13 +83,23 @@ module ActionPolicy
|
|
83
83
|
# Unlike simply calling a predicate rule (`policy.manage?`),
|
84
84
|
# `apply` also calls pre-checks.
|
85
85
|
def apply(rule)
|
86
|
-
|
86
|
+
res = apply_r(rule)
|
87
87
|
|
88
|
-
|
89
|
-
|
90
|
-
|
88
|
+
# DEPRECATED (we still rely on it in tests)
|
89
|
+
@result = res
|
90
|
+
|
91
|
+
res.value
|
92
|
+
end
|
93
|
+
|
94
|
+
# NEXT_RELEASE: This is gonna be #apply in 1.0
|
95
|
+
def apply_r(rule) # :nodoc:
|
96
|
+
with_result(rule) do |result|
|
97
|
+
catch :policy_fulfilled do
|
98
|
+
result.load __apply__(resolve_rule(rule))
|
99
|
+
end
|
91
100
|
|
92
|
-
|
101
|
+
result
|
102
|
+
end
|
93
103
|
end
|
94
104
|
|
95
105
|
def deny!
|
@@ -107,14 +117,17 @@ module ActionPolicy
|
|
107
117
|
# (such as caching, pre checks, etc.)
|
108
118
|
def __apply__(rule) = public_send(rule)
|
109
119
|
|
110
|
-
#
|
111
|
-
#
|
112
|
-
def
|
113
|
-
|
114
|
-
|
115
|
-
|
120
|
+
# Prepare a new result object for the next rule application.
|
121
|
+
# It's stored in the thread-local storage to be accessible from within the policy.
|
122
|
+
def with_result(rule) # :nodoc:
|
123
|
+
result = self.class.result_class.new(self.class, rule)
|
124
|
+
|
125
|
+
Thread.current[:__action_policy_result__] ||= []
|
126
|
+
Thread.current[:__action_policy_result__] << result
|
127
|
+
|
128
|
+
yield result
|
116
129
|
ensure
|
117
|
-
|
130
|
+
Thread.current[:__action_policy_result__]&.pop
|
118
131
|
end
|
119
132
|
|
120
133
|
# Returns a result of applying the specified rule to the specified record.
|
@@ -146,6 +159,13 @@ module ActionPolicy
|
|
146
159
|
activity
|
147
160
|
end
|
148
161
|
|
162
|
+
# Returns the result object for the last rule application within the given
|
163
|
+
# execution context (Thread or Fiber)
|
164
|
+
def result
|
165
|
+
# FIXME: Remove ivar fallback after 1.0
|
166
|
+
Thread.current[:__action_policy_result__]&.last || @result
|
167
|
+
end
|
168
|
+
|
149
169
|
# Return annotated source code for the rule
|
150
170
|
# NOTE: require "method_source" and "prism" gems to be installed.
|
151
171
|
# Otherwise returns empty string.
|
@@ -155,9 +175,7 @@ module ActionPolicy
|
|
155
175
|
# Useful for debugging: type `pp :show?` within the context of the policy
|
156
176
|
# to preview the rule.
|
157
177
|
def pp(rule)
|
158
|
-
|
159
|
-
# We need result to exist for `allowed_to?` to work correctly
|
160
|
-
@result = self.class.result_class.new(self.class, rule)
|
178
|
+
with_result(rule) do
|
161
179
|
header = "#{self.class.name}##{rule}"
|
162
180
|
source = inspect_rule(rule)
|
163
181
|
$stdout.puts "#{header}\n#{source}"
|
@@ -201,13 +201,12 @@ module ActionPolicy
|
|
201
201
|
if (record == :__undef__ || record == self.record) && options.empty?
|
202
202
|
rule = resolve_rule(rule)
|
203
203
|
policy = self
|
204
|
-
|
204
|
+
apply_r(rule)
|
205
205
|
else
|
206
206
|
policy = policy_for(record: record, **options)
|
207
207
|
rule = policy.resolve_rule(rule)
|
208
208
|
|
209
|
-
policy.
|
210
|
-
policy.result
|
209
|
+
policy.apply_r(rule)
|
211
210
|
end
|
212
211
|
|
213
212
|
if res.fail? && result&.reasons
|
@@ -9,10 +9,10 @@ module ActionPolicy # :nodoc:
|
|
9
9
|
def authorize(policy, rule)
|
10
10
|
event = {policy: policy.class.name, rule: rule.to_s}
|
11
11
|
ActiveSupport::Notifications.instrument(EVENT_NAME, event) do
|
12
|
-
|
13
|
-
event[:cached] =
|
14
|
-
event[:value] =
|
15
|
-
|
12
|
+
result = super
|
13
|
+
event[:cached] = result.cached?
|
14
|
+
event[:value] = result.value
|
15
|
+
result
|
16
16
|
end
|
17
17
|
end
|
18
18
|
end
|
@@ -16,13 +16,13 @@ module ActionPolicy # :nodoc:
|
|
16
16
|
ActiveSupport::Notifications.instrument(INIT_EVENT_NAME, event) { super }
|
17
17
|
end
|
18
18
|
|
19
|
-
def
|
19
|
+
def apply_r(rule)
|
20
20
|
event = {policy: self.class.name, rule: rule.to_s}
|
21
21
|
ActiveSupport::Notifications.instrument(APPLY_EVENT_NAME, event) do
|
22
|
-
|
22
|
+
result = super
|
23
23
|
event[:cached] = result.cached?
|
24
24
|
event[:value] = result.value
|
25
|
-
|
25
|
+
result
|
26
26
|
end
|
27
27
|
end
|
28
28
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: action_policy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.7.
|
4
|
+
version: 0.7.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Vladimir Dementyev
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-
|
11
|
+
date: 2024-12-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: ruby-next-core
|