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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5c3b8d128bdf4e37224446e7c5a65698327a927c743e99757cf6f5363db93ff5
4
- data.tar.gz: f046c13d93cff63aa06fd15f4d6375a8498c04d0637110df0ff86891a206fb84
3
+ metadata.gz: 21363e4337fbe0caea24309750d79d94bb9fa6c4ae0a9d0697b0e686519fa763
4
+ data.tar.gz: 8ac572534b621240640723ea5c6ec942a73b91e14f2d40e534889c2dc7ba9251
5
5
  SHA512:
6
- metadata.gz: b66f496343688a55907070ba6b6e82e24a5d358934d6809e1577e80f2c67a81342fe449cbf46942c1e8ff064f79ae898235efa32b63e9fc402e5b416bc223efc
7
- data.tar.gz: 57088a306e642a0dd22a68c4da019dbd8669e7fa63ec1e99fb7bb819a563ca514be2200c669486d40f5a4b152a325ee0adc3abcaa6187b45c1f0ef37ddcb7a86
6
+ metadata.gz: c1dd33d739cfdf1143b7cc2402c2e8bf7ebf1f1613fa7c650c72fddf0bd4fd4b5c924c920ff143ad582ab68524836f0d0c32641372c5999fdd95f6d9113078dc
7
+ data.tar.gz: 5fb9dbd9b1f19fb6bed19ddbeb99be9bd35495b2d0f89bd3fb0cd8fed59a0513c4a627fdd25291b56dcfc7cf9b60f64d351bb2b658d97351f9a7b5db2d1c4128
data/CHANGELOG.md CHANGED
@@ -2,6 +2,10 @@
2
2
 
3
3
  ## master
4
4
 
5
+ ## 0.7.3 (2024-12-18)
6
+
7
+ - Fix keeping the result object in concurrent (Fiber-ed) execution environments. ([@palkan][])
8
+
5
9
  ## 0.7.2 (2024-11-21)
6
10
 
7
11
  - Fix missing details in deny! message interpolation. ([@palkan][])
@@ -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
- @result = store.read(key)
53
+ result = store.read(key)
54
54
  unless result.nil?
55
55
  result.cached!
56
- next result.value
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 apply(rule)
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
- @result = store.read(key)
53
+ result = store.read(key)
54
54
  unless result.nil?
55
55
  result.cached!
56
- next result.value
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 apply(rule)
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, :result
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
- @result = self.class.result_class.new(self.class, rule)
86
+ res = apply_r(rule)
87
87
 
88
- catch :policy_fulfilled do
89
- result.load __apply__(resolve_rule(rule))
90
- end
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
- result.value
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
- # Wrap code that could modify result
111
- # to prevent the current result modification
112
- def with_clean_result # :nodoc:
113
- was_result = @result
114
- yield
115
- @result
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
- @result = was_result
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
- with_clean_result do
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
- with_clean_result { apply(rule) }
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.apply(rule)
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, :result
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
- @result = self.class.result_class.new(self.class, rule)
86
+ res = apply_r(rule)
87
87
 
88
- catch :policy_fulfilled do
89
- result.load __apply__(resolve_rule(rule))
90
- end
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
- result.value
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
- # Wrap code that could modify result
111
- # to prevent the current result modification
112
- def with_clean_result # :nodoc:
113
- was_result = @result
114
- yield
115
- @result
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
- @result = was_result
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
- with_clean_result do
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
- def initialize(policy, rule)
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 = policy.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
- raise(::ActionPolicy::Unauthorized.new(policy, rule))
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.apply(rule)
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.apply(authorization_rule_for(policy, rule))
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
- @result = store.read(key)
53
+ result = store.read(key)
54
54
  unless result.nil?
55
55
  result.cached!
56
- next result.value
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 apply(rule)
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 apply(rule)
10
+ def apply_r(rule)
11
11
  @__rules_cache__ ||= {}
12
12
 
13
13
  if @__rules_cache__.key?(rule)
14
- @result = @__rules_cache__[rule]
15
- return result.value
14
+ return @__rules_cache__[rule]
16
15
  end
17
16
 
18
- super
19
-
20
- @__rules_cache__[rule] = result
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, :result
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
- @result = self.class.result_class.new(self.class, rule)
86
+ res = apply_r(rule)
87
87
 
88
- catch :policy_fulfilled do
89
- result.load __apply__(resolve_rule(rule))
90
- end
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
- result.value
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
- # Wrap code that could modify result
111
- # to prevent the current result modification
112
- def with_clean_result # :nodoc:
113
- was_result = @result
114
- yield
115
- @result
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
- @result = was_result
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
- with_clean_result do
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
- with_clean_result { apply(rule) }
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.apply(rule)
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
- res = super
13
- event[:cached] = policy.result.cached?
14
- event[:value] = policy.result.value
15
- res
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 apply(rule)
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
- res = super
22
+ result = super
23
23
  event[:cached] = result.cached?
24
24
  event[:value] = result.value
25
- res
25
+ result
26
26
  end
27
27
  end
28
28
  end
@@ -39,6 +39,7 @@ module ActionPolicy
39
39
  super
40
40
  end
41
41
 
42
+ # FIXME(1.0): Update to use result object directly
42
43
  def formatted_policy(policy)
43
44
  "#{policy.result.inspect}\n#{policy.inspect_rule(policy.result.rule)}"
44
45
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActionPolicy
4
- VERSION = "0.7.2"
4
+ VERSION = "0.7.3"
5
5
  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.2
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-21 00:00:00.000000000 Z
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