action_policy 0.7.4 → 0.7.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (31) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +7 -3
  3. data/lib/.rbnext/3.0/action_policy/behaviours/policy_for.rb +1 -1
  4. data/lib/.rbnext/3.0/action_policy/policy/cache.rb +1 -1
  5. data/lib/.rbnext/3.0/action_policy/policy/execution_result.rb +1 -1
  6. data/lib/.rbnext/3.0/action_policy/policy/pre_check.rb +3 -3
  7. data/lib/.rbnext/3.0/action_policy/rspec/be_authorized_to.rb +2 -2
  8. data/lib/.rbnext/3.0/action_policy/rspec/have_authorized_scope.rb +2 -2
  9. data/lib/.rbnext/3.0/action_policy/utils/pretty_print.rb +1 -1
  10. data/lib/.rbnext/3.1/action_policy/behaviours/policy_for.rb +1 -1
  11. data/lib/.rbnext/3.2/action_policy/behaviours/policy_for.rb +1 -1
  12. data/lib/.rbnext/3.2/action_policy/rspec/be_authorized_to.rb +2 -2
  13. data/lib/.rbnext/3.2/action_policy/rspec/have_authorized_scope.rb +2 -2
  14. data/lib/.rbnext/3.4/action_policy/behaviours/policy_for.rb +70 -0
  15. data/lib/.rbnext/3.4/action_policy/i18n.rb +56 -0
  16. data/lib/.rbnext/3.4/action_policy/policy/cache.rb +101 -0
  17. data/lib/.rbnext/3.4/action_policy/policy/pre_check.rb +160 -0
  18. data/lib/.rbnext/3.4/action_policy/rspec/be_authorized_to.rb +96 -0
  19. data/lib/.rbnext/3.4/action_policy/rspec/have_authorized_scope.rb +130 -0
  20. data/lib/.rbnext/3.4/action_policy/utils/pretty_print.rb +155 -0
  21. data/lib/action_policy/behaviours/policy_for.rb +1 -1
  22. data/lib/action_policy/i18n.rb +1 -1
  23. data/lib/action_policy/policy/cache.rb +1 -1
  24. data/lib/action_policy/policy/execution_result.rb +1 -1
  25. data/lib/action_policy/policy/pre_check.rb +3 -3
  26. data/lib/action_policy/rspec/be_authorized_to.rb +2 -2
  27. data/lib/action_policy/rspec/have_authorized_scope.rb +2 -2
  28. data/lib/action_policy/utils/pretty_print.rb +1 -1
  29. data/lib/action_policy/version.rb +1 -1
  30. data/lib/ruby_lsp/action_policy/addon.rb +170 -0
  31. metadata +10 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c2ca6d3ab4293001cc4b07e7fe5503a8c06a8b72684c0f7a9c4e7a4ef8a32c89
4
- data.tar.gz: f1e726704611a3bc9ade5d3466fd64c4a5c9faddf5aa8338f3179ec4da2381e5
3
+ metadata.gz: d3680162e91000d7fb369fcfa17710f557c9a12435c5bf00c6fb367a5045b970
4
+ data.tar.gz: 000760f640032e5a0b44e9ea7918908a32c2568cefeffc747fa4ea45c46949bf
5
5
  SHA512:
6
- metadata.gz: aa35a3efefa37fc64066a78899b9b03a2283e2458471425a2e7cc2bc0b4ff829e87beda744057866f661e37fc54f14d094ba0fc446ea155537042478109576a7
7
- data.tar.gz: d90069b7a1b3bea22c2d5e6dd990231f1bf24f85fd50081a92a54bcd5c271faa42f40c9c526a8434abd793e6f4c928b5cb28d4dad3fbaeb2bf3701f67fd4fd21
6
+ metadata.gz: f03892d33e150921d3ec4bdfe1038b017301825474597037585c45e0f780affdfd72ed5171c577cee007af69a8542d4172cb8266526a97895b9c3461b4b2abb3
7
+ data.tar.gz: e5d9a4259d5dcd8cac8a0cab28b6d85f3aedc46050c51057aa9d458b4c0b8597428bed1a6c57473f3836500b1696cfb1f4a4c1aeac868c2771bb443dc59f10fa
data/CHANGELOG.md CHANGED
@@ -2,13 +2,17 @@
2
2
 
3
3
  ## master
4
4
 
5
+ ## 0.7.5 (2025-05-09) 🎇
6
+
7
+ - Ensure `result.value` is true or false. ([@palkan][])
8
+
5
9
  ## 0.7.4 (2025-03-12)
6
10
 
7
- - Let authorize! return the policy record ([@sedubois][])
11
+ - Let authorize! return the policy record. ([@sedubois][])
8
12
 
9
- - Enable `allowance_to` as a helper method by default ([@stephannv][])
13
+ - Enable `allowance_to` as a helper method by default. ([@stephannv][])
10
14
 
11
- - Allow the `:through` option of `authorize` to be passed a proc ([@brendon][])
15
+ - Allow the `:through` option of `authorize` to be passed a Proc. ([@brendon][])
12
16
 
13
17
  ## 0.7.3 (2024-12-18)
14
18
 
@@ -61,7 +61,7 @@ module ActionPolicy
61
61
 
62
62
  def policy_for_cache_key(record:, with: nil, namespace: nil, context: authorization_context, **__kwrest__)
63
63
  record_key = record._policy_cache_key(use_object_id: true)
64
- context_key = context.values.map { _1._policy_cache_key(use_object_id: true) }.join(".")
64
+ context_key = context.values.map { it = _1;it._policy_cache_key(use_object_id: true) }.join(".")
65
65
 
66
66
  "#{namespace}/#{with}/#{context_key}/#{record_key}"
67
67
  end
@@ -29,7 +29,7 @@ module ActionPolicy # :nodoc:
29
29
  [
30
30
  cache_namespace,
31
31
  *parts
32
- ].map { _1._policy_cache_key }.join("/")
32
+ ].map { it = _1;it._policy_cache_key }.join("/")
33
33
  end
34
34
 
35
35
  def rule_cache_key(rule)
@@ -16,7 +16,7 @@ module ActionPolicy
16
16
 
17
17
  # Populate the final value
18
18
  def load(value)
19
- @value = value
19
+ @value = !!value
20
20
  end
21
21
 
22
22
  def success?() ; @value == true; end
@@ -125,7 +125,7 @@ module ActionPolicy
125
125
  def pre_check(*names, **options)
126
126
  names.each do |name|
127
127
  # do not allow pre-check override
128
- check = pre_checks.find { _1.name == name }
128
+ check = pre_checks.find { it = _1;it.name == name }
129
129
  raise "Pre-check already defined: #{name}" unless check.nil?
130
130
 
131
131
  pre_checks << Check.new(self, name, **options)
@@ -134,14 +134,14 @@ module ActionPolicy
134
134
 
135
135
  def skip_pre_check(*names, **options)
136
136
  names.each do |name|
137
- check = pre_checks.find { _1.name == name }
137
+ check = pre_checks.find { it = _1;it.name == name }
138
138
  raise "Pre-check not found: #{name}" if check.nil?
139
139
 
140
140
  # when no options provided we remove this check completely
141
141
  next pre_checks.delete(check) if options.empty?
142
142
 
143
143
  # otherwise duplicate and apply skip options
144
- pre_checks[pre_checks.index(check)] = check.dup.tap { _1.skip!(**options) }
144
+ pre_checks[pre_checks.index(check)] = check.dup.tap { it = _1;it.skip!(**options) }
145
145
  end
146
146
  end
147
147
 
@@ -51,7 +51,7 @@ module ActionPolicy
51
51
 
52
52
  @actual_calls = ActionPolicy::Testing::AuthorizeTracker.calls
53
53
 
54
- actual_calls.any? { _1.matches?(policy, rule, target, context) }
54
+ actual_calls.any? { it = _1;it.matches?(policy, rule, target, context) }
55
55
  end
56
56
 
57
57
  def does_not_match?(*__rest__)
@@ -78,7 +78,7 @@ module ActionPolicy
78
78
 
79
79
  def formatted_calls
80
80
  actual_calls.map do
81
- " - #{_1.inspect}"
81
+ it = _1;" - #{it.inspect}"
82
82
  end.join("\n")
83
83
  end
84
84
 
@@ -61,7 +61,7 @@ module ActionPolicy
61
61
 
62
62
  @actual_scopes = ActionPolicy::Testing::AuthorizeTracker.scopings
63
63
 
64
- matching_scopes = actual_scopes.select { _1.matches?(policy, type, name, scope_options, context) }
64
+ matching_scopes = actual_scopes.select { it = _1;it.matches?(policy, type, name, scope_options, context) }
65
65
 
66
66
  return false if matching_scopes.empty?
67
67
 
@@ -114,7 +114,7 @@ module ActionPolicy
114
114
 
115
115
  def formatted_scopings
116
116
  actual_scopes.map do
117
- " - #{_1.inspect}"
117
+ it = _1;" - #{it.inspect}"
118
118
  end.join("\n")
119
119
  end
120
120
  end
@@ -119,7 +119,7 @@ module ActionPolicy
119
119
 
120
120
  # Some lines should not be evaled
121
121
  def ignore_exp?(exp)
122
- PrettyPrint.ignore_expressions.any? { exp.match?(_1) }
122
+ PrettyPrint.ignore_expressions.any? { it = _1;exp.match?(it) }
123
123
  end
124
124
  end
125
125
 
@@ -61,7 +61,7 @@ module ActionPolicy
61
61
 
62
62
  def policy_for_cache_key(record:, with: nil, namespace: nil, context: authorization_context, **__kwrest__)
63
63
  record_key = record._policy_cache_key(use_object_id: true)
64
- context_key = context.values.map { _1._policy_cache_key(use_object_id: true) }.join(".")
64
+ context_key = context.values.map { it = _1;it._policy_cache_key(use_object_id: true) }.join(".")
65
65
 
66
66
  "#{namespace}/#{with}/#{context_key}/#{record_key}"
67
67
  end
@@ -61,7 +61,7 @@ module ActionPolicy
61
61
 
62
62
  def policy_for_cache_key(record:, with: nil, namespace: nil, context: authorization_context, **__kwrest__)
63
63
  record_key = record._policy_cache_key(use_object_id: true)
64
- context_key = context.values.map { _1._policy_cache_key(use_object_id: true) }.join(".")
64
+ context_key = context.values.map { it = _1;it._policy_cache_key(use_object_id: true) }.join(".")
65
65
 
66
66
  "#{namespace}/#{with}/#{context_key}/#{record_key}"
67
67
  end
@@ -51,7 +51,7 @@ module ActionPolicy
51
51
 
52
52
  @actual_calls = ActionPolicy::Testing::AuthorizeTracker.calls
53
53
 
54
- actual_calls.any? { _1.matches?(policy, rule, target, context) }
54
+ actual_calls.any? { it = _1;it.matches?(policy, rule, target, context) }
55
55
  end
56
56
 
57
57
  def does_not_match?(*__rest__)
@@ -78,7 +78,7 @@ module ActionPolicy
78
78
 
79
79
  def formatted_calls
80
80
  actual_calls.map do
81
- " - #{_1.inspect}"
81
+ it = _1;" - #{it.inspect}"
82
82
  end.join("\n")
83
83
  end
84
84
 
@@ -61,7 +61,7 @@ module ActionPolicy
61
61
 
62
62
  @actual_scopes = ActionPolicy::Testing::AuthorizeTracker.scopings
63
63
 
64
- matching_scopes = actual_scopes.select { _1.matches?(policy, type, name, scope_options, context) }
64
+ matching_scopes = actual_scopes.select { it = _1;it.matches?(policy, type, name, scope_options, context) }
65
65
 
66
66
  return false if matching_scopes.empty?
67
67
 
@@ -114,7 +114,7 @@ module ActionPolicy
114
114
 
115
115
  def formatted_scopings
116
116
  actual_scopes.map do
117
- " - #{_1.inspect}"
117
+ it = _1;" - #{it.inspect}"
118
118
  end.join("\n")
119
119
  end
120
120
  end
@@ -0,0 +1,70 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActionPolicy
4
+ module Behaviours
5
+ # Adds `policy_for` method
6
+ module PolicyFor
7
+ require "action_policy/ext/policy_cache_key"
8
+ using ActionPolicy::Ext::PolicyCacheKey
9
+
10
+ # Returns policy instance for the record.
11
+ def policy_for(record:, with: nil, namespace: authorization_namespace, context: nil, allow_nil: false, default: default_authorization_policy_class, strict_namespace: authorization_strict_namespace)
12
+ context = context ? build_authorization_context.merge(context) : authorization_context
13
+
14
+ policy_class = with || ::ActionPolicy.lookup(
15
+ record,
16
+ namespace:, context:, allow_nil:, default:, strict_namespace:
17
+ )
18
+ policy_class&.new(record, **context)
19
+ end
20
+
21
+ def authorization_context = @authorization_context ||= build_authorization_context
22
+
23
+ def build_authorization_context
24
+ Kernel.raise NotImplementedError, "Please, define `build_authorization_context` method!"
25
+ end
26
+
27
+ def authorization_namespace
28
+ # override to provide specific authorization namespace
29
+ end
30
+
31
+ def default_authorization_policy_class
32
+ # override to provide a policy class use when no policy found
33
+ end
34
+
35
+ def authorization_strict_namespace
36
+ # override to provide strict namespace lookup option
37
+ end
38
+
39
+ # Override this method to provide implicit authorization target
40
+ # that would be used in case `record` is not specified in
41
+ # `authorize!` and `allowed_to?` call.
42
+ #
43
+ # It is also used to infer a policy for scoping (in `authorized_scope` method).
44
+ def implicit_authorization_target
45
+ # no-op
46
+ end
47
+
48
+ # Return implicit authorization target or raises an exception if it's nil
49
+ def implicit_authorization_target!
50
+ implicit_authorization_target || Kernel.raise(
51
+ NotFound,
52
+ [
53
+ self,
54
+ "Couldn't find implicit authorization target " \
55
+ "for #{self.class}. " \
56
+ "Please, provide policy class explicitly using `with` option or " \
57
+ "define the `implicit_authorization_target` method."
58
+ ]
59
+ )
60
+ end
61
+
62
+ def policy_for_cache_key(record:, with: nil, namespace: nil, context: authorization_context, **)
63
+ record_key = record._policy_cache_key(use_object_id: true)
64
+ context_key = context.values.map { it = _1;it._policy_cache_key(use_object_id: true) }.join(".")
65
+
66
+ "#{namespace}/#{with}/#{context_key}/#{record_key}"
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActionPolicy
4
+ module I18n # :nodoc:
5
+ DEFAULT_UNAUTHORIZED_MESSAGE = "You are not authorized to perform this action"
6
+
7
+ class << self
8
+ def full_message(policy_class, rule, details = nil)
9
+ candidates = candidates_for(policy_class, rule)
10
+
11
+ options = {scope: :action_policy}
12
+ options.merge!(details) unless details.nil?
13
+
14
+ ::I18n.t(
15
+ candidates.shift,
16
+ default: candidates,
17
+ **options
18
+ )
19
+ end
20
+
21
+ private
22
+
23
+ def candidates_for(policy_class, rule)
24
+ policy_hierarchy = policy_class.ancestors.select { it = _1;it.respond_to?(:identifier) }
25
+ [
26
+ *policy_hierarchy.map { |klass| :"policy.#{klass.identifier}.#{rule}" },
27
+ :"policy.#{rule}",
28
+ :unauthorized,
29
+ DEFAULT_UNAUTHORIZED_MESSAGE
30
+ ]
31
+ end
32
+ end
33
+
34
+ ActionPolicy::Policy::FailureReasons.prepend(Module.new do
35
+ def full_messages
36
+ reasons.flat_map do |policy_klass, rules|
37
+ rules.flat_map do |rule|
38
+ if rule.is_a?(::Hash)
39
+ rule.map do |key, details|
40
+ ActionPolicy::I18n.full_message(policy_klass, key, details)
41
+ end
42
+ else
43
+ ActionPolicy::I18n.full_message(policy_klass, rule)
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end)
49
+
50
+ ActionPolicy::Policy::ExecutionResult.prepend(Module.new do
51
+ def message
52
+ ActionPolicy::I18n.full_message(policy, rule, details)
53
+ end
54
+ end)
55
+ end
56
+ end
@@ -0,0 +1,101 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "action_policy/version"
4
+
5
+ module ActionPolicy # :nodoc:
6
+ using RubyNext
7
+
8
+ # By default cache namespace (or prefix) contains major and minor version of the gem
9
+ CACHE_NAMESPACE = "acp:#{ActionPolicy::VERSION.split(".").take(2).join(".")}"
10
+
11
+ require "action_policy/ext/policy_cache_key"
12
+
13
+ using ActionPolicy::Ext::PolicyCacheKey
14
+
15
+ module Policy
16
+ # Provides long-lived cache through ActionPolicy.cache_store.
17
+ #
18
+ # NOTE: if cache_store is nil then we silently skip all the caching.
19
+ module Cache
20
+ class << self
21
+ def included(base)
22
+ base.extend ClassMethods
23
+ end
24
+ end
25
+
26
+ def cache_namespace() = ActionPolicy::CACHE_NAMESPACE
27
+
28
+ def cache_key(*parts)
29
+ [
30
+ cache_namespace,
31
+ *parts
32
+ ].map { it = _1;it._policy_cache_key }.join("/")
33
+ end
34
+
35
+ def rule_cache_key(rule)
36
+ cache_key(
37
+ context_cache_key,
38
+ record,
39
+ self.class,
40
+ rule
41
+ )
42
+ end
43
+
44
+ def context_cache_key
45
+ authorization_context.map { _2._policy_cache_key.to_s }.join("/")
46
+ end
47
+
48
+ def apply_with_cache(rule)
49
+ options = self.class.cached_rules.fetch(rule)
50
+ key = rule_cache_key(rule)
51
+
52
+ ActionPolicy.cache_store.then do |store|
53
+ result = store.read(key)
54
+ unless result.nil?
55
+ result.cached!
56
+ next result
57
+ end
58
+ yield.tap do |result|
59
+ store.write(key, result, options)
60
+ end
61
+ end
62
+ end
63
+
64
+ def apply_r(rule)
65
+ return super if ActionPolicy.cache_store.nil? ||
66
+ !self.class.cached_rules.key?(rule)
67
+
68
+ apply_with_cache(rule) { super }
69
+ end
70
+
71
+ def cache(*parts, **options)
72
+ key = cache_key(*parts)
73
+ ActionPolicy.cache_store.then do |store|
74
+ res = store.read(key)
75
+ next res unless res.nil?
76
+ res = yield
77
+ store.write(key, res, options)
78
+ res
79
+ end
80
+ end
81
+
82
+ module ClassMethods # :nodoc:
83
+ def cache(*rules, **options)
84
+ rules.each do |rule|
85
+ cached_rules[rule] = options
86
+ end
87
+ end
88
+
89
+ def cached_rules
90
+ return @cached_rules if instance_variable_defined?(:@cached_rules)
91
+
92
+ @cached_rules = if superclass.respond_to?(:cached_rules)
93
+ superclass.cached_rules.dup
94
+ else
95
+ {}
96
+ end
97
+ end
98
+ end
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,160 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActionPolicy
4
+ module Policy
5
+ # Adds callback-style checks to policies to
6
+ # extract common checks from rules.
7
+ #
8
+ # class ApplicationPolicy < ActionPolicy::Base
9
+ # authorize :user
10
+ # pre_check :allow_admins
11
+ #
12
+ # private
13
+ # # Allow every action for admins
14
+ # def allow_admins
15
+ # allow! if user.admin?
16
+ # end
17
+ # end
18
+ #
19
+ # You can specify conditional pre-checks (through `except` / `only`) options
20
+ # and skip already defined pre-checks if necessary.
21
+ #
22
+ # class UserPolicy < ApplicationPolicy
23
+ # skip_pre_check :allow_admins, only: :destroy?
24
+ #
25
+ # def destroy?
26
+ # user.admin? && !record.admin?
27
+ # end
28
+ # end
29
+ module PreCheck
30
+ # Single pre-check instance.
31
+ #
32
+ # Implements filtering logic.
33
+ class Check
34
+ attr_reader :name, :policy_class
35
+
36
+ def initialize(policy, name, except: nil, only: nil)
37
+ if !except.nil? && !only.nil?
38
+ raise ArgumentError,
39
+ "Only one of `except` and `only` may be specified for pre-check"
40
+ end
41
+
42
+ @policy_class = policy
43
+ @name = name
44
+ @blacklist = Array(except) unless except.nil?
45
+ @whitelist = Array(only) unless only.nil?
46
+
47
+ rebuild_filter
48
+ end
49
+
50
+ def applicable?(rule)
51
+ return true if filter.nil?
52
+ filter.call(rule)
53
+ end
54
+
55
+ def call(policy) = policy.send(name)
56
+
57
+ def skip!(except: nil, only: nil)
58
+ if !except.nil? && !only.nil?
59
+ raise ArgumentError,
60
+ "Only one of `except` and `only` may be specified when skipping pre-check"
61
+ end
62
+
63
+ if except.nil? && only.nil?
64
+ raise ArgumentError,
65
+ "At least one of `except` and `only` must be specified when skipping pre-check"
66
+ end
67
+
68
+ if except
69
+ @whitelist = Array(except)
70
+ @whitelist -= blacklist if blacklist
71
+ @blacklist = nil
72
+ else
73
+ # only
74
+ @blacklist += Array(only) if blacklist
75
+ @whitelist -= Array(only) if whitelist
76
+ @blacklist = Array(only) if filter.nil?
77
+ end
78
+
79
+ rebuild_filter
80
+ end
81
+
82
+ def dup
83
+ self.class.new(
84
+ policy_class,
85
+ name,
86
+ except: blacklist&.dup,
87
+ only: whitelist&.dup
88
+ )
89
+ end
90
+
91
+ private
92
+
93
+ attr_reader :whitelist, :blacklist, :filter
94
+
95
+ def rebuild_filter
96
+ @filter =
97
+ if whitelist
98
+ proc { |rule| whitelist.include?(rule) }
99
+ elsif blacklist
100
+ proc { |rule| !blacklist.include?(rule) }
101
+ end
102
+ end
103
+ end
104
+
105
+ class << self
106
+ def included(base)
107
+ base.extend ClassMethods
108
+ end
109
+ end
110
+
111
+ def run_pre_checks(rule)
112
+ self.class.pre_checks.each do |check|
113
+ next unless check.applicable?(rule)
114
+ check.call(self)
115
+ end
116
+
117
+ yield if block_given?
118
+ end
119
+
120
+ def __apply__(rule)
121
+ run_pre_checks(rule) { super }
122
+ end
123
+
124
+ module ClassMethods # :nodoc:
125
+ def pre_check(*names, **options)
126
+ names.each do |name|
127
+ # do not allow pre-check override
128
+ check = pre_checks.find { it = _1;it.name == name }
129
+ raise "Pre-check already defined: #{name}" unless check.nil?
130
+
131
+ pre_checks << Check.new(self, name, **options)
132
+ end
133
+ end
134
+
135
+ def skip_pre_check(*names, **options)
136
+ names.each do |name|
137
+ check = pre_checks.find { it = _1;it.name == name }
138
+ raise "Pre-check not found: #{name}" if check.nil?
139
+
140
+ # when no options provided we remove this check completely
141
+ next pre_checks.delete(check) if options.empty?
142
+
143
+ # otherwise duplicate and apply skip options
144
+ pre_checks[pre_checks.index(check)] = check.dup.tap { it = _1;it.skip!(**options) }
145
+ end
146
+ end
147
+
148
+ def pre_checks
149
+ return @pre_checks if instance_variable_defined?(:@pre_checks)
150
+
151
+ @pre_checks = if superclass.respond_to?(:pre_checks)
152
+ superclass.pre_checks.dup
153
+ else
154
+ []
155
+ end
156
+ end
157
+ end
158
+ end
159
+ end
160
+ end
@@ -0,0 +1,96 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "action_policy/testing"
4
+
5
+ module ActionPolicy
6
+ module RSpec
7
+ # Authorization matcher `be_authorized_to`.
8
+ #
9
+ # Verifies that a block of code has been authorized using specific policy.
10
+ #
11
+ # Example:
12
+ #
13
+ # # in controller/request specs
14
+ # subject { patch :update, id: product.id }
15
+ #
16
+ # it "is authorized" do
17
+ # expect { subject }
18
+ # .to be_authorized_to(:manage?, product)
19
+ # .with(ProductPolicy)
20
+ # end
21
+ #
22
+ class BeAuthorizedTo < ::RSpec::Matchers::BuiltIn::BaseMatcher
23
+ attr_reader :rule, :target, :policy, :actual_calls, :context
24
+
25
+ def initialize(rule, target)
26
+ @rule = rule
27
+ @target = target
28
+ end
29
+
30
+ def with(policy)
31
+ @policy = policy
32
+ self
33
+ end
34
+
35
+ def with_context(context)
36
+ @context = context
37
+ self
38
+ end
39
+
40
+ def match(_expected, actual)
41
+ raise "This matcher only supports block expectations" unless actual.is_a?(Proc)
42
+
43
+ @policy ||= ::ActionPolicy.lookup(target)
44
+ @context ||= nil
45
+
46
+ begin
47
+ ActionPolicy::Testing::AuthorizeTracker.tracking { actual.call }
48
+ rescue ActionPolicy::Unauthorized
49
+ # we don't want to care about authorization result
50
+ end
51
+
52
+ @actual_calls = ActionPolicy::Testing::AuthorizeTracker.calls
53
+
54
+ actual_calls.any? { it = _1;it.matches?(policy, rule, target, context) }
55
+ end
56
+
57
+ def does_not_match?(*)
58
+ raise "This matcher doesn't support negation"
59
+ end
60
+
61
+ def supports_block_expectations?() = true
62
+
63
+ def failure_message
64
+ "expected #{formatted_record} " \
65
+ "to be authorized with #{policy}##{rule}, " \
66
+ "#{context ? "and context #{context.inspect}, " : ""}" \
67
+ "but #{actual_calls_message}"
68
+ end
69
+
70
+ def actual_calls_message
71
+ if actual_calls.empty?
72
+ "no authorization calls have been made"
73
+ else
74
+ "the following calls were encountered:\n" \
75
+ "#{formatted_calls}"
76
+ end
77
+ end
78
+
79
+ def formatted_calls
80
+ actual_calls.map do
81
+ it = _1;" - #{it.inspect}"
82
+ end.join("\n")
83
+ end
84
+
85
+ def formatted_record(record = target) = ::RSpec::Support::ObjectFormatter.format(record)
86
+ end
87
+ end
88
+ end
89
+
90
+ RSpec.configure do |config|
91
+ config.include(Module.new do
92
+ def be_authorized_to(rule, target)
93
+ ActionPolicy::RSpec::BeAuthorizedTo.new(rule, target)
94
+ end
95
+ end)
96
+ end
@@ -0,0 +1,130 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "action_policy/testing"
4
+
5
+ module ActionPolicy
6
+ module RSpec
7
+ # Implements `have_authorized_scope` matcher.
8
+ #
9
+ # Verifies that a block of code applies authorization scoping using specific policy.
10
+ #
11
+ # Example:
12
+ #
13
+ # # in controller/request specs
14
+ # subject { get :index }
15
+ #
16
+ # it "has authorized scope" do
17
+ # expect { subject }
18
+ # .to have_authorized_scope(:active_record_relation)
19
+ # .with(ProductPolicy)
20
+ # end
21
+ #
22
+ class HaveAuthorizedScope < ::RSpec::Matchers::BuiltIn::BaseMatcher
23
+ attr_reader :type, :name, :policy, :scope_options, :actual_scopes,
24
+ :target_expectations, :context
25
+
26
+ def initialize(type)
27
+ @type = type
28
+ @name = :default
29
+ @scope_options = nil
30
+ end
31
+
32
+ def with(policy)
33
+ @policy = policy
34
+ self
35
+ end
36
+
37
+ def as(name)
38
+ @name = name
39
+ self
40
+ end
41
+
42
+ def with_scope_options(scope_options)
43
+ @scope_options = scope_options
44
+ self
45
+ end
46
+
47
+ def with_target(&block)
48
+ @target_expectations = block
49
+ self
50
+ end
51
+
52
+ def with_context(context)
53
+ @context = context
54
+ self
55
+ end
56
+
57
+ def match(_expected, actual)
58
+ raise "This matcher only supports block expectations" unless actual.is_a?(Proc)
59
+
60
+ ActionPolicy::Testing::AuthorizeTracker.tracking { actual.call }
61
+
62
+ @actual_scopes = ActionPolicy::Testing::AuthorizeTracker.scopings
63
+
64
+ matching_scopes = actual_scopes.select { it = _1;it.matches?(policy, type, name, scope_options, context) }
65
+
66
+ return false if matching_scopes.empty?
67
+
68
+ return true unless target_expectations
69
+
70
+ if matching_scopes.size > 1
71
+ raise "Too many matching scopings (#{matching_scopes.size}), " \
72
+ "you can run `.with_target` only when there is the only one match"
73
+ end
74
+
75
+ target_expectations.call(matching_scopes.first.target)
76
+ true
77
+ end
78
+
79
+ def does_not_match?(*)
80
+ raise "This matcher doesn't support negation"
81
+ end
82
+
83
+ def supports_block_expectations?() = true
84
+
85
+ def failure_message
86
+ "expected a scoping named :#{name} for type :#{type} " \
87
+ "#{scope_options_message} " \
88
+ "#{context ? "and context #{context.inspect} " : ""}" \
89
+ "from #{policy} to have been applied, " \
90
+ "but #{actual_scopes_message}"
91
+ end
92
+
93
+ def scope_options_message
94
+ if scope_options
95
+ if defined?(::RSpec::Matchers::Composable) &&
96
+ scope_options.is_a?(::RSpec::Matchers::Composable)
97
+ "with scope options #{scope_options.description}"
98
+ else
99
+ "with scope options #{scope_options}"
100
+ end
101
+ else
102
+ "without scope options"
103
+ end
104
+ end
105
+
106
+ def actual_scopes_message
107
+ if actual_scopes.empty?
108
+ "no scopings have been made"
109
+ else
110
+ "the following scopings were encountered:\n" \
111
+ "#{formatted_scopings}"
112
+ end
113
+ end
114
+
115
+ def formatted_scopings
116
+ actual_scopes.map do
117
+ it = _1;" - #{it.inspect}"
118
+ end.join("\n")
119
+ end
120
+ end
121
+ end
122
+ end
123
+
124
+ RSpec.configure do |config|
125
+ config.include(Module.new do
126
+ def have_authorized_scope(type)
127
+ ActionPolicy::RSpec::HaveAuthorizedScope.new(type)
128
+ end
129
+ end)
130
+ end
@@ -0,0 +1,155 @@
1
+ # frozen_string_literal: true
2
+
3
+ old_verbose = $VERBOSE
4
+
5
+ begin
6
+ require "method_source"
7
+ # Ignore parse warnings when patch
8
+ # Ruby version mismatches
9
+ $VERBOSE = nil
10
+ require "prism"
11
+ rescue LoadError
12
+ # do nothing
13
+ ensure
14
+ $VERBOSE = old_verbose
15
+ end
16
+
17
+ module ActionPolicy
18
+ using RubyNext
19
+
20
+ # Takes the object and a method name,
21
+ # and returns the "annotated" source code for the method:
22
+ # code is split into parts by logical operators and each
23
+ # part is evaluated separately.
24
+ #
25
+ # Example:
26
+ #
27
+ # class MyClass
28
+ # def access?
29
+ # admin? && access_feed?
30
+ # end
31
+ # end
32
+ #
33
+ # puts PrettyPrint.format_method(MyClass.new, :access?)
34
+ #
35
+ # #=> MyClass#access?
36
+ # #=> ↳ admin? #=> false
37
+ # #=> AND
38
+ # #=> access_feed? #=> true
39
+ module PrettyPrint
40
+ TRUE = "\e[32mtrue\e[0m"
41
+ FALSE = "\e[31mfalse\e[0m"
42
+
43
+ class Visitor
44
+ attr_reader :lines, :object, :source
45
+ attr_accessor :indent
46
+
47
+ def initialize(object)
48
+ @object = object
49
+ end
50
+
51
+ def collect(ast)
52
+ @lines = []
53
+ @indent = 0
54
+ @source = ast.source.source
55
+ ast = ast.value.child_nodes[0].child_nodes[0].body
56
+
57
+ visit_node(ast)
58
+
59
+ lines.join("\n")
60
+ end
61
+
62
+ def visit_node(ast)
63
+ if respond_to?("visit_#{ast.type}")
64
+ send("visit_#{ast.type}", ast)
65
+ else
66
+ visit_missing ast
67
+ end
68
+ end
69
+
70
+ def expression_with_result(sexp)
71
+ expression = source[sexp.location.start_offset...sexp.location.end_offset]
72
+ "#{expression} #=> #{PrettyPrint.colorize(eval_exp(expression))}"
73
+ end
74
+
75
+ def eval_exp(exp)
76
+ return "<skipped>" if ignore_exp?(exp)
77
+ object.instance_eval(exp)
78
+ rescue => e
79
+ "Failed: #{e.message}"
80
+ end
81
+
82
+ def visit_and_node(ast)
83
+ visit_node(ast.left)
84
+ lines << indented("AND")
85
+ visit_node(ast.right)
86
+ end
87
+
88
+ def visit_or_node(ast)
89
+ visit_node(ast.left)
90
+ lines << indented("OR")
91
+ visit_node(ast.right)
92
+ end
93
+
94
+ def visit_statements_node(ast)
95
+ ast.child_nodes.each do |node|
96
+ visit_node(node)
97
+ # restore indent after each expression
98
+ self.indent -= 2
99
+ end
100
+ end
101
+
102
+ def visit_parentheses_node(ast)
103
+ lines << indented("(")
104
+ self.indent += 2
105
+ visit_node(ast.child_nodes[0])
106
+ lines << indented(")")
107
+ end
108
+
109
+ def visit_missing(ast)
110
+ lines << indented(expression_with_result(ast))
111
+ end
112
+
113
+ def indented(str)
114
+ "#{indent.zero? ? "↳ " : ""}#{" " * indent}#{str}".tap do
115
+ # increase indent after the first expression
116
+ self.indent += 2 if indent.zero?
117
+ end
118
+ end
119
+
120
+ # Some lines should not be evaled
121
+ def ignore_exp?(exp)
122
+ PrettyPrint.ignore_expressions.any? { it = _1;exp.match?(it) }
123
+ end
124
+ end
125
+
126
+ class << self
127
+ attr_accessor :ignore_expressions
128
+
129
+ if defined?(::Prism) && defined?(::MethodSource)
130
+ def available?() = true
131
+
132
+ def print_method(object, method_name)
133
+ ast = Prism.parse(object.method(method_name).source)
134
+
135
+ Visitor.new(object).collect(ast)
136
+ end
137
+ else
138
+ def available?() = false
139
+
140
+ def print_method(_, _) = ""
141
+ end
142
+
143
+ def colorize(val)
144
+ return val unless $stdout.isatty
145
+ return TRUE if val.eql?(true) # rubocop:disable Lint/DeprecatedConstants
146
+ return FALSE if val.eql?(false) # rubocop:disable Lint/DeprecatedConstants
147
+ val
148
+ end
149
+ end
150
+
151
+ self.ignore_expressions = [
152
+ /^\s*binding\.(pry|irb)\s*$/s
153
+ ]
154
+ end
155
+ end
@@ -61,7 +61,7 @@ module ActionPolicy
61
61
 
62
62
  def policy_for_cache_key(record:, with: nil, namespace: nil, context: authorization_context, **)
63
63
  record_key = record._policy_cache_key(use_object_id: true)
64
- context_key = context.values.map { _1._policy_cache_key(use_object_id: true) }.join(".")
64
+ context_key = context.values.map { it._policy_cache_key(use_object_id: true) }.join(".")
65
65
 
66
66
  "#{namespace}/#{with}/#{context_key}/#{record_key}"
67
67
  end
@@ -21,7 +21,7 @@ module ActionPolicy
21
21
  private
22
22
 
23
23
  def candidates_for(policy_class, rule)
24
- policy_hierarchy = policy_class.ancestors.select { _1.respond_to?(:identifier) }
24
+ policy_hierarchy = policy_class.ancestors.select { it.respond_to?(:identifier) }
25
25
  [
26
26
  *policy_hierarchy.map { |klass| :"policy.#{klass.identifier}.#{rule}" },
27
27
  :"policy.#{rule}",
@@ -29,7 +29,7 @@ module ActionPolicy # :nodoc:
29
29
  [
30
30
  cache_namespace,
31
31
  *parts
32
- ].map { _1._policy_cache_key }.join("/")
32
+ ].map { it._policy_cache_key }.join("/")
33
33
  end
34
34
 
35
35
  def rule_cache_key(rule)
@@ -16,7 +16,7 @@ module ActionPolicy
16
16
 
17
17
  # Populate the final value
18
18
  def load(value)
19
- @value = value
19
+ @value = !!value
20
20
  end
21
21
 
22
22
  def success?() = @value == true
@@ -125,7 +125,7 @@ module ActionPolicy
125
125
  def pre_check(*names, **options)
126
126
  names.each do |name|
127
127
  # do not allow pre-check override
128
- check = pre_checks.find { _1.name == name }
128
+ check = pre_checks.find { it.name == name }
129
129
  raise "Pre-check already defined: #{name}" unless check.nil?
130
130
 
131
131
  pre_checks << Check.new(self, name, **options)
@@ -134,14 +134,14 @@ module ActionPolicy
134
134
 
135
135
  def skip_pre_check(*names, **options)
136
136
  names.each do |name|
137
- check = pre_checks.find { _1.name == name }
137
+ check = pre_checks.find { it.name == name }
138
138
  raise "Pre-check not found: #{name}" if check.nil?
139
139
 
140
140
  # when no options provided we remove this check completely
141
141
  next pre_checks.delete(check) if options.empty?
142
142
 
143
143
  # otherwise duplicate and apply skip options
144
- pre_checks[pre_checks.index(check)] = check.dup.tap { _1.skip!(**options) }
144
+ pre_checks[pre_checks.index(check)] = check.dup.tap { it.skip!(**options) }
145
145
  end
146
146
  end
147
147
 
@@ -51,7 +51,7 @@ module ActionPolicy
51
51
 
52
52
  @actual_calls = ActionPolicy::Testing::AuthorizeTracker.calls
53
53
 
54
- actual_calls.any? { _1.matches?(policy, rule, target, context) }
54
+ actual_calls.any? { it.matches?(policy, rule, target, context) }
55
55
  end
56
56
 
57
57
  def does_not_match?(*)
@@ -78,7 +78,7 @@ module ActionPolicy
78
78
 
79
79
  def formatted_calls
80
80
  actual_calls.map do
81
- " - #{_1.inspect}"
81
+ " - #{it.inspect}"
82
82
  end.join("\n")
83
83
  end
84
84
 
@@ -61,7 +61,7 @@ module ActionPolicy
61
61
 
62
62
  @actual_scopes = ActionPolicy::Testing::AuthorizeTracker.scopings
63
63
 
64
- matching_scopes = actual_scopes.select { _1.matches?(policy, type, name, scope_options, context) }
64
+ matching_scopes = actual_scopes.select { it.matches?(policy, type, name, scope_options, context) }
65
65
 
66
66
  return false if matching_scopes.empty?
67
67
 
@@ -114,7 +114,7 @@ module ActionPolicy
114
114
 
115
115
  def formatted_scopings
116
116
  actual_scopes.map do
117
- " - #{_1.inspect}"
117
+ " - #{it.inspect}"
118
118
  end.join("\n")
119
119
  end
120
120
  end
@@ -119,7 +119,7 @@ module ActionPolicy
119
119
 
120
120
  # Some lines should not be evaled
121
121
  def ignore_exp?(exp)
122
- PrettyPrint.ignore_expressions.any? { exp.match?(_1) }
122
+ PrettyPrint.ignore_expressions.any? { exp.match?(it) }
123
123
  end
124
124
  end
125
125
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActionPolicy
4
- VERSION = "0.7.4"
4
+ VERSION = "0.7.5"
5
5
  end
@@ -0,0 +1,170 @@
1
+ # frozen_string_literal: true
2
+
3
+ # From https://gist.github.com/skryukov/35539d57b51f38235faaace2c1a2c1a1
4
+
5
+ require "active_support/inflector"
6
+
7
+ module RubyLsp
8
+ module ActionPolicy
9
+ class Addon < ::RubyLsp::Addon
10
+ def name
11
+ "ActionPolicy"
12
+ end
13
+
14
+ def activate(global_state, outgoing_queue)
15
+ require "action_policy"
16
+ warn "[ActionPolicy] Activating Ruby LSP addon v#{::ActionPolicy::VERSION}"
17
+ end
18
+
19
+ def deactivate
20
+ end
21
+
22
+ def create_definition_listener(response_builder, node_context, uri, dispatcher)
23
+ Definition.new(response_builder, node_context, uri, dispatcher)
24
+ end
25
+ end
26
+
27
+ class Definition
28
+ include Requests::Support::Common
29
+ include ActiveSupport::Inflector
30
+
31
+ POLICY_SUPERCLASSES = ["ApplicationPolicy", "ActionPolicy::Base"].freeze
32
+
33
+ def initialize(response_builder, uri, node_context, dispatcher)
34
+ @response_builder = response_builder
35
+ @node_context = node_context
36
+ @uri = uri
37
+ @path = uri.to_standardized_path
38
+ @policy_rules_cache = {}
39
+
40
+ dispatcher.register(self, :on_symbol_node_enter)
41
+ end
42
+
43
+ def on_symbol_node_enter(node)
44
+ return unless in_authorize_call?
45
+
46
+ target = @node_context.call_node
47
+ # authorization target is the first argument (if explicit)
48
+ policy_class = find_policy_class(target.arguments&.child_node&.first)
49
+ return unless policy_class
50
+
51
+ policy_path = find_policy_file(policy_class)
52
+ return unless policy_path
53
+
54
+ ensure_policy_rules_cached(policy_path)
55
+ add_definition(policy_path, node.value)
56
+ end
57
+
58
+ private
59
+
60
+ def in_authorize_call?
61
+ call = @node_context.call_node
62
+ call.is_a?(Prism::CallNode) && call.message == "authorize!"
63
+ end
64
+
65
+ def find_policy_class(target)
66
+ content = File.read(@path)
67
+ document = Prism.parse(content)
68
+ class_node = find_containing_class(document.value)
69
+ return derive_policy_from_target(target) unless class_node
70
+
71
+ class_name = class_node.constant_path.slice
72
+ return unless class_name.end_with?("Controller", "Channel")
73
+
74
+ resource_name = class_name
75
+ .delete_suffix("Controller")
76
+ .delete_suffix("Channel")
77
+ .singularize
78
+ "#{resource_name}Policy"
79
+ end
80
+
81
+ def find_containing_class(root)
82
+ return unless root.respond_to?(:statements)
83
+
84
+ root.statements.body.find do |node|
85
+ node.is_a?(Prism::ClassNode) &&
86
+ node.constant_path.slice.end_with?("Controller", "Channel")
87
+ end
88
+ end
89
+
90
+ def derive_policy_from_target(target)
91
+ target_name = case target
92
+ when Prism::InstanceVariableReadNode
93
+ target.name.to_s[1..].classify
94
+ when Prism::ConstantReadNode
95
+ target.name.to_s
96
+ when NilClass
97
+ return
98
+ else
99
+ target.slice
100
+ end
101
+
102
+ target_name.end_with?("Policy") ? target_name : "#{target_name}Policy"
103
+ end
104
+
105
+ def find_policy_file(policy_class)
106
+ file_path = policy_class.gsub(/([a-z])([A-Z])/, "\\1_\\2").downcase
107
+ root_path = Dir.pwd
108
+
109
+ [
110
+ File.join(root_path, "app/policies/#{file_path}.rb"),
111
+ File.join(root_path, "app/policies/#{file_path}_policy.rb"),
112
+ *Dir.glob(File.join(root_path, "app/policies/**/#{file_path}.rb")),
113
+ *Dir.glob(File.join(root_path, "app/policies/**/#{file_path}_policy.rb"))
114
+ ].find { |path| File.exist?(path) }
115
+ end
116
+
117
+ def ensure_policy_rules_cached(policy_path)
118
+ return if @policy_rules_cache[policy_path]
119
+
120
+ content = File.read(policy_path)
121
+ document = Prism.parse(content)
122
+
123
+ document.value.statements.body.each do |stmt|
124
+ if stmt.is_a?(Prism::ClassNode)
125
+ @policy_rules_cache[policy_path] = extract_rules(stmt)
126
+ break
127
+ end
128
+ end
129
+ end
130
+
131
+ def extract_rules(node)
132
+ return {} unless node.body
133
+
134
+ rules = {}
135
+ private_section = false
136
+
137
+ node.body.child_nodes.each do |stmt|
138
+ case stmt
139
+ when Prism::CallNode
140
+ if stmt.message == "private"
141
+ private_section = true
142
+ elsif stmt.message == "alias_rule" && stmt.arguments&.arguments
143
+ stmt.arguments.arguments
144
+ .select { |arg| arg.is_a?(Prism::SymbolNode) }
145
+ .each { |arg| rules[arg.value] ||= stmt.location.start_line }
146
+ end
147
+ when Prism::DefNode
148
+ next if private_section
149
+ rules[stmt.name.to_s] = stmt.location.start_line
150
+ end
151
+ end
152
+ rules
153
+ end
154
+
155
+ def add_definition(policy_path, action)
156
+ rules = @policy_rules_cache[policy_path]
157
+ line_number = rules&.[](action.to_s)
158
+ return unless line_number
159
+
160
+ @response_builder << Interface::Location.new(
161
+ uri: URI::Generic.from_path(path: policy_path).to_s,
162
+ range: Interface::Range.new(
163
+ start: Interface::Position.new(line: line_number - 1, character: 0),
164
+ end: Interface::Position.new(line: line_number - 1, character: 0)
165
+ )
166
+ )
167
+ end
168
+ end
169
+ end
170
+ 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
4
+ version: 0.7.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vladimir Dementyev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2025-03-12 00:00:00.000000000 Z
11
+ date: 2025-05-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ruby-next-core
@@ -162,6 +162,13 @@ files:
162
162
  - lib/.rbnext/3.2/action_policy/rspec/be_authorized_to.rb
163
163
  - lib/.rbnext/3.2/action_policy/rspec/have_authorized_scope.rb
164
164
  - lib/.rbnext/3.2/action_policy/utils/suggest_message.rb
165
+ - lib/.rbnext/3.4/action_policy/behaviours/policy_for.rb
166
+ - lib/.rbnext/3.4/action_policy/i18n.rb
167
+ - lib/.rbnext/3.4/action_policy/policy/cache.rb
168
+ - lib/.rbnext/3.4/action_policy/policy/pre_check.rb
169
+ - lib/.rbnext/3.4/action_policy/rspec/be_authorized_to.rb
170
+ - lib/.rbnext/3.4/action_policy/rspec/have_authorized_scope.rb
171
+ - lib/.rbnext/3.4/action_policy/utils/pretty_print.rb
165
172
  - lib/action_policy.rb
166
173
  - lib/action_policy/authorizer.rb
167
174
  - lib/action_policy/base.rb
@@ -218,6 +225,7 @@ files:
218
225
  - lib/generators/rspec/templates/policy_spec.rb.tt
219
226
  - lib/generators/test_unit/policy_generator.rb
220
227
  - lib/generators/test_unit/templates/policy_test.rb.tt
228
+ - lib/ruby_lsp/action_policy/addon.rb
221
229
  homepage: https://github.com/palkan/action_policy
222
230
  licenses:
223
231
  - MIT