action_policy 0.4.4 → 0.5.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +211 -175
- data/README.md +5 -4
- data/config/rubocop-rspec.yml +17 -0
- data/lib/.rbnext/2.7/action_policy/behaviours/policy_for.rb +62 -0
- data/lib/.rbnext/2.7/action_policy/i18n.rb +56 -0
- data/lib/.rbnext/2.7/action_policy/policy/cache.rb +101 -0
- data/lib/.rbnext/2.7/action_policy/policy/pre_check.rb +162 -0
- data/lib/.rbnext/2.7/action_policy/rspec/be_authorized_to.rb +89 -0
- data/lib/.rbnext/2.7/action_policy/rspec/have_authorized_scope.rb +124 -0
- data/lib/.rbnext/2.7/action_policy/utils/pretty_print.rb +159 -0
- data/lib/.rbnext/3.0/action_policy/behaviour.rb +115 -0
- data/lib/.rbnext/3.0/action_policy/behaviours/policy_for.rb +62 -0
- data/lib/.rbnext/3.0/action_policy/behaviours/scoping.rb +35 -0
- data/lib/.rbnext/3.0/action_policy/behaviours/thread_memoized.rb +59 -0
- data/lib/.rbnext/3.0/action_policy/ext/policy_cache_key.rb +72 -0
- data/lib/.rbnext/3.0/action_policy/policy/aliases.rb +69 -0
- data/lib/.rbnext/3.0/action_policy/policy/authorization.rb +87 -0
- data/lib/.rbnext/3.0/action_policy/policy/cache.rb +101 -0
- data/lib/.rbnext/3.0/action_policy/policy/core.rb +161 -0
- data/lib/.rbnext/3.0/action_policy/policy/defaults.rb +31 -0
- data/lib/.rbnext/3.0/action_policy/policy/execution_result.rb +37 -0
- data/lib/.rbnext/3.0/action_policy/policy/pre_check.rb +162 -0
- data/lib/.rbnext/3.0/action_policy/policy/reasons.rb +212 -0
- data/lib/.rbnext/3.0/action_policy/policy/scoping.rb +160 -0
- data/lib/.rbnext/3.0/action_policy/rspec/be_authorized_to.rb +89 -0
- data/lib/.rbnext/3.0/action_policy/rspec/have_authorized_scope.rb +124 -0
- data/lib/.rbnext/3.0/action_policy/utils/pretty_print.rb +159 -0
- data/lib/.rbnext/3.0/action_policy/utils/suggest_message.rb +19 -0
- data/lib/action_policy.rb +7 -1
- data/lib/action_policy/behaviour.rb +22 -16
- data/lib/action_policy/behaviours/policy_for.rb +10 -3
- data/lib/action_policy/behaviours/scoping.rb +2 -1
- data/lib/action_policy/behaviours/thread_memoized.rb +1 -3
- data/lib/action_policy/ext/module_namespace.rb +1 -6
- data/lib/action_policy/ext/policy_cache_key.rb +10 -30
- data/lib/action_policy/i18n.rb +1 -1
- data/lib/action_policy/lookup_chain.rb +26 -28
- data/lib/action_policy/policy/aliases.rb +7 -12
- data/lib/action_policy/policy/authorization.rb +8 -7
- data/lib/action_policy/policy/cache.rb +11 -17
- data/lib/action_policy/policy/core.rb +25 -12
- data/lib/action_policy/policy/defaults.rb +3 -9
- data/lib/action_policy/policy/execution_result.rb +3 -9
- data/lib/action_policy/policy/pre_check.rb +19 -58
- data/lib/action_policy/policy/reasons.rb +31 -19
- data/lib/action_policy/policy/scoping.rb +5 -6
- data/lib/action_policy/rails/controller.rb +6 -1
- data/lib/action_policy/rails/policy/instrumentation.rb +1 -1
- data/lib/action_policy/rspec/be_authorized_to.rb +5 -9
- data/lib/action_policy/rspec/dsl.rb +1 -1
- data/lib/action_policy/rspec/have_authorized_scope.rb +5 -7
- data/lib/action_policy/utils/pretty_print.rb +21 -24
- data/lib/action_policy/utils/suggest_message.rb +1 -3
- data/lib/action_policy/version.rb +1 -1
- data/lib/generators/action_policy/install/templates/{application_policy.rb → application_policy.rb.tt} +0 -0
- data/lib/generators/action_policy/policy/policy_generator.rb +4 -1
- data/lib/generators/action_policy/policy/templates/{policy.rb → policy.rb.tt} +0 -0
- data/lib/generators/rspec/templates/{policy_spec.rb → policy_spec.rb.tt} +0 -0
- data/lib/generators/test_unit/templates/{policy_test.rb → policy_test.rb.tt} +0 -0
- metadata +55 -119
- data/.gitattributes +0 -2
- data/.github/ISSUE_TEMPLATE.md +0 -21
- data/.github/PULL_REQUEST_TEMPLATE.md +0 -29
- data/.github/bug_report_template.rb +0 -175
- data/.gitignore +0 -15
- data/.rubocop.yml +0 -54
- data/.tidelift.yml +0 -6
- data/.travis.yml +0 -31
- data/Gemfile +0 -22
- data/Rakefile +0 -27
- data/action_policy.gemspec +0 -44
- data/benchmarks/namespaced_lookup_cache.rb +0 -74
- data/benchmarks/pre_checks.rb +0 -73
- data/bin/console +0 -14
- data/bin/setup +0 -8
- data/docs/.nojekyll +0 -0
- data/docs/CNAME +0 -1
- data/docs/README.md +0 -79
- data/docs/_sidebar.md +0 -27
- data/docs/aliases.md +0 -122
- data/docs/assets/docsify-search.js +0 -364
- data/docs/assets/docsify.min.js +0 -3
- data/docs/assets/fonts/FiraCode-Medium.woff +0 -0
- data/docs/assets/fonts/FiraCode-Regular.woff +0 -0
- data/docs/assets/images/banner.png +0 -0
- data/docs/assets/images/cache.png +0 -0
- data/docs/assets/images/cache.svg +0 -70
- data/docs/assets/images/layer.png +0 -0
- data/docs/assets/images/layer.svg +0 -35
- data/docs/assets/prism-ruby.min.js +0 -1
- data/docs/assets/styles.css +0 -347
- data/docs/assets/vue.min.css +0 -1
- data/docs/authorization_context.md +0 -92
- data/docs/behaviour.md +0 -113
- data/docs/caching.md +0 -291
- data/docs/controller_action_aliases.md +0 -109
- data/docs/custom_lookup_chain.md +0 -48
- data/docs/custom_policy.md +0 -53
- data/docs/debugging.md +0 -55
- data/docs/decorators.md +0 -27
- data/docs/favicon.ico +0 -0
- data/docs/graphql.md +0 -302
- data/docs/i18n.md +0 -44
- data/docs/index.html +0 -43
- data/docs/instrumentation.md +0 -84
- data/docs/lookup_chain.md +0 -22
- data/docs/namespaces.md +0 -77
- data/docs/non_rails.md +0 -28
- data/docs/pre_checks.md +0 -57
- data/docs/pundit_migration.md +0 -80
- data/docs/quick_start.md +0 -118
- data/docs/rails.md +0 -120
- data/docs/reasons.md +0 -120
- data/docs/scoping.md +0 -255
- data/docs/testing.md +0 -390
- data/docs/writing_policies.md +0 -107
- data/gemfiles/jruby.gemfile +0 -8
- data/gemfiles/rails42.gemfile +0 -9
- data/gemfiles/rails6.gemfile +0 -8
- data/gemfiles/railsmaster.gemfile +0 -6
- data/lib/action_policy/ext/string_match.rb +0 -14
- data/lib/action_policy/ext/yield_self_then.rb +0 -25
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 911b1f7b929d458ae0bb95eb22fa11ff2a17925d9c797b626664bdad82b7abbc
|
4
|
+
data.tar.gz: 69bc6ead609db7cbcdc3d31455e7e17b88bf9037a51a8b1722e414a7e50c41e4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e3cf8e4bd9347f052a34cee11fad004030ee80d9281c6bbbb9fe99b8f6665b0c611661d6ce3be8578345ef3171ad35d2c38fff8936bdd47efef48bb137a5a415
|
7
|
+
data.tar.gz: 63c4444667971ee445b60e2d9ffee89402021151ac730f00063f2066984c1ca75c15ae40b45fbce60ce9c4201826116bb417f6e62dbfd91f96a3f3974e26a458
|
data/CHANGELOG.md
CHANGED
@@ -2,13 +2,48 @@
|
|
2
2
|
|
3
3
|
## master
|
4
4
|
|
5
|
+
## 0.5.4 (2020-12-09)
|
6
|
+
|
7
|
+
- Add support for RSpec aliases detection when linting policy specs with `rubocop-rspec` 2.0 ([@pirj][])
|
8
|
+
|
9
|
+
- Fix `strict_namespace: true` lookup option not finding policies in global namespace ([@Be-ngt-oH][])
|
10
|
+
|
11
|
+
## 0.5.0 (2020-09-29)
|
12
|
+
|
13
|
+
- Move `deny!` / `allow!` to core. ([@palkan][])
|
14
|
+
|
15
|
+
Now you can call `deny!` and `allow!` in policy rules to fail- or pass-fast.
|
16
|
+
|
17
|
+
**BREAKING.** Pre-check name is no longer added automatically to failure reasons. You should specify the reason
|
18
|
+
explicitly: `deny!(:my_reason)`.
|
19
|
+
|
20
|
+
- Add `Result#all_details` to return all collected details in a single hash. ([@palkan][])
|
21
|
+
|
22
|
+
- Add `default` option to lookup and `default_authorization_policy_class` callback to behaviour. ([@palkan][])
|
23
|
+
|
24
|
+
- Add `skip_verify_authorized!` to Rails controllers integration. ([@palkan][])
|
25
|
+
|
26
|
+
This method allows you to skip the `verify_authorized` callback dynamically.
|
27
|
+
|
28
|
+
- **Drop Ruby 2.4 support**. ([@palkan][])
|
29
|
+
|
30
|
+
- Add `allowance_to` method to authorization behaviour. ([@palkan][])
|
31
|
+
|
32
|
+
This method is similar to `allowed_to?` but returns an authorization result object.
|
33
|
+
|
34
|
+
- Support aliases in `allowed_to?` / `check?` calls within policies. ([@palkan][])
|
35
|
+
|
36
|
+
## 0.4.5 (2020-07-29)
|
37
|
+
|
38
|
+
- Add strict_namespace option to lookup chain. (@rainerborene)
|
39
|
+
|
5
40
|
## 0.4.4 (2020-07-07)
|
6
41
|
|
7
42
|
- Fix symbol lookup with namespaces. ([@palkan][])
|
8
43
|
|
9
44
|
Fixes [#122](https://github.com/palkan/action_policy/issues/122).
|
10
45
|
|
11
|
-
- Separated `#classify`-based and `#camelize`-based symbol lookups. ([Be-ngt-oH][])
|
46
|
+
- Separated `#classify`-based and `#camelize`-based symbol lookups. ([@Be-ngt-oH][])
|
12
47
|
|
13
48
|
Only affects Rails apps. Now lookup for `:users` tries to find `UsersPolicy` first (camelize),
|
14
49
|
and only then search for `UserPolicy` (classify).
|
@@ -23,12 +58,12 @@ See [PR#118](https://github.com/palkan/action_policy/pull/118).
|
|
23
58
|
|
24
59
|
- Add `#cache(*parts, **options) { ... }` method. ([@palkan][])
|
25
60
|
|
26
|
-
|
27
|
-
|
61
|
+
Allows you to cache anything in policy classes using the Action Policy
|
62
|
+
cache key generation mechanism.
|
28
63
|
|
29
64
|
- Handle versioned Rails cache keys. ([@palkan][])
|
30
65
|
|
31
|
-
|
66
|
+
Use `#cache_with_version` as a cache key if defined.
|
32
67
|
|
33
68
|
## 0.4.2 (2019-12-13)
|
34
69
|
|
@@ -38,274 +73,275 @@ See [PR#118](https://github.com/palkan/action_policy/pull/118).
|
|
38
73
|
|
39
74
|
- Add `action_policy.init` instrumentation event. ([@palkan][])
|
40
75
|
|
41
|
-
|
76
|
+
Triggered every time a new policy object is initialized.
|
42
77
|
|
43
78
|
- Fix policy memoization with explicit context. ([@palkan][])
|
44
79
|
|
45
|
-
|
46
|
-
|
80
|
+
Explicit context (`authorize! context: {}`) wasn't considered during
|
81
|
+
policies memoization. Not this is fixed.
|
47
82
|
|
48
83
|
- Support composed matchers for authorization target testing. ([@palkan][])
|
49
84
|
|
50
|
-
|
85
|
+
Now you can write tests like this:
|
51
86
|
|
52
|
-
|
53
|
-
|
54
|
-
|
87
|
+
```ruby
|
88
|
+
expect { subject }.to be_authorized_to(:show?, an_instance_of(User))
|
89
|
+
```
|
55
90
|
|
56
91
|
## 0.3.4 (2019-11-27)
|
57
92
|
|
58
93
|
- Fix Rails generators. ([@palkan][])
|
59
94
|
|
60
|
-
|
61
|
-
|
95
|
+
Only invoke install generator if `application_policy.rb` is missing.
|
96
|
+
Fix hooking into test frameworks.
|
62
97
|
|
63
98
|
## 0.3.3 (2019-11-27)
|
64
99
|
|
65
100
|
- Improve pretty print functionality. ([@palkan][])
|
66
101
|
|
67
|
-
|
68
|
-
|
102
|
+
Colorize true/false values.
|
103
|
+
Handle multiline expressions and debug statements (i.e., `binding.pry`).
|
69
104
|
|
70
105
|
- Add Rails generators. ([@nicolas-brousse][])
|
71
106
|
|
72
|
-
|
107
|
+
Adds `action_policy:install` and `action_policy:policy MODEL` Rails generators.
|
73
108
|
|
74
109
|
- Optional authorization target. ([@somenugget][])
|
75
110
|
|
76
|
-
|
111
|
+
Allows making authorization context optional:
|
77
112
|
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
113
|
+
```ruby
|
114
|
+
class OptionalRolePolicy < ActionPolicy::Base
|
115
|
+
authorize :role, optional: true
|
116
|
+
end
|
82
117
|
|
83
|
-
|
84
|
-
|
85
|
-
|
118
|
+
policy = OptionalRolePolicy.new
|
119
|
+
policy.role #=> nil
|
120
|
+
```
|
86
121
|
|
87
122
|
## 0.3.2 (2019-05-26) 👶
|
88
123
|
|
89
124
|
- Fixed thread-safety issues with scoping configs. ([@palkan][])
|
90
125
|
|
91
|
-
|
126
|
+
Fixes [#75](https://github.com/palkan/action_policy/issues/75).
|
92
127
|
|
93
128
|
## 0.3.1 (2019-05-30)
|
94
129
|
|
95
130
|
- Fixed bug with missing implicit target and hash like scoping data. ([@palkan][])
|
96
131
|
|
97
|
-
|
132
|
+
Fixes [#70](https://github.com/palkan/action_policy/issues/70).
|
98
133
|
|
99
134
|
## 0.3.0 (2019-04-02)
|
100
135
|
|
101
136
|
- Added ActiveSupport-based instrumentation. ([@palkan][])
|
102
137
|
|
103
|
-
|
138
|
+
See [PR#4](https://github.com/palkan/action_policy/pull/4)
|
104
139
|
|
105
140
|
- Allow passing authorization context explicitly. ([@palkan][])
|
106
141
|
|
107
|
-
|
142
|
+
Closes [#3](https://github.com/palkan/action_policy/issues/3).
|
108
143
|
|
109
|
-
|
110
|
-
|
144
|
+
Now it's possible to override implicit authorization context
|
145
|
+
via `context` option:
|
111
146
|
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
147
|
+
```ruby
|
148
|
+
authorize! target, to: :show?, context: {user: another_user}
|
149
|
+
authorized_scope User.all, context: {user: another_user}
|
150
|
+
```
|
116
151
|
|
117
152
|
- Renamed `#authorized` to `#authorized_scope`. ([@palkan][])
|
118
153
|
|
119
|
-
|
154
|
+
**NOTE:** `#authorized` alias is also available.
|
120
155
|
|
121
156
|
- Added `Policy#pp(rule)` method to print annotated rule source code. ([@palkan][])
|
122
157
|
|
123
|
-
|
158
|
+
Example (debugging):
|
124
159
|
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
)
|
160
|
+
```ruby
|
161
|
+
def edit?
|
162
|
+
binding.pry # rubocop:disable Lint/Debugger
|
163
|
+
(user.name == "John") && (admin? || access_feed?)
|
164
|
+
end
|
165
|
+
```
|
166
|
+
|
167
|
+
```sh
|
168
|
+
pry> pp :edit?
|
169
|
+
MyPolicy#edit?
|
170
|
+
↳ (
|
171
|
+
user.name == "John" #=> false
|
172
|
+
)
|
173
|
+
AND
|
174
|
+
(
|
175
|
+
admin? #=> false
|
176
|
+
OR
|
177
|
+
access_feed? #=> true
|
144
178
|
)
|
145
|
-
|
179
|
+
)
|
180
|
+
```
|
146
181
|
|
147
|
-
|
182
|
+
See [PR#63](https://github.com/palkan/action_policy/pull/63)
|
148
183
|
|
149
184
|
- Added ability to provide additional failure reasons details. ([@palkan][])
|
150
185
|
|
151
|
-
|
186
|
+
Example:
|
152
187
|
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
end
|
188
|
+
```ruby
|
189
|
+
class ApplicantPolicy < ApplicationPolicy
|
190
|
+
def show?
|
191
|
+
allowed_to?(:show?, object.stage)
|
158
192
|
end
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
193
|
+
end
|
194
|
+
|
195
|
+
class StagePolicy < ApplicationPolicy
|
196
|
+
def show?
|
197
|
+
# Add stage title to the failure reason (if any)
|
198
|
+
# (could be used by client to show more descriptive message)
|
199
|
+
details[:title] = record.title
|
200
|
+
# then perform the checks
|
201
|
+
user.stages.where(id: record.id).exists?
|
168
202
|
end
|
203
|
+
end
|
169
204
|
|
170
|
-
|
171
|
-
|
172
|
-
|
205
|
+
# when accessing the reasons
|
206
|
+
p ex.result.reasons.details #=> { stage: [{show?: {title: "Onboarding"}] }
|
207
|
+
```
|
173
208
|
|
174
|
-
|
209
|
+
See https://github.com/palkan/action_policy/pull/58
|
175
210
|
|
176
211
|
- Ruby 2.4+ is required. ([@palkan][])
|
177
212
|
|
178
213
|
- Added RSpec DSL for writing policy specs. ([@palkan])
|
179
214
|
|
180
|
-
|
181
|
-
|
215
|
+
The goal of this DSL is to reduce the boilerplate when writing
|
216
|
+
policies specs.
|
182
217
|
|
183
|
-
|
218
|
+
Example:
|
184
219
|
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
220
|
+
```ruby
|
221
|
+
describe PostPolicy do
|
222
|
+
let(:user) { build_stubbed :user }
|
223
|
+
let(:record) { build_stubbed :post, draft: false }
|
189
224
|
|
190
|
-
|
225
|
+
let(:context) { {user: user} }
|
191
226
|
|
192
|
-
|
193
|
-
|
227
|
+
describe_rule :show? do
|
228
|
+
succeed "when post is published"
|
194
229
|
|
195
|
-
|
196
|
-
|
230
|
+
failed "when post is draft" do
|
231
|
+
before { post.draft = false }
|
197
232
|
|
198
|
-
|
199
|
-
|
200
|
-
end
|
233
|
+
succeed "when user is a manager" do
|
234
|
+
before { user.role = "manager" }
|
201
235
|
end
|
202
236
|
end
|
203
237
|
end
|
204
|
-
|
238
|
+
end
|
239
|
+
```
|
205
240
|
|
206
241
|
- Added I18n support ([@DmitryTsepelev][])
|
207
242
|
|
208
|
-
|
243
|
+
Example:
|
209
244
|
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
end
|
245
|
+
```ruby
|
246
|
+
class ApplicationController < ActionController::Base
|
247
|
+
rescue_from ActionPolicy::Unauthorized do |ex|
|
248
|
+
p ex.result.message #=> "You do not have access to the stage"
|
249
|
+
p ex.result.reasons.full_messages #=> ["You do not have access to the stage"]
|
216
250
|
end
|
217
|
-
|
251
|
+
end
|
252
|
+
```
|
218
253
|
|
219
254
|
- Added scope options to scopes. ([@korolvs][])
|
220
255
|
|
221
|
-
|
256
|
+
See [#47](https://github.com/palkan/action_policy/pull/47).
|
222
257
|
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
258
|
+
Example:
|
259
|
+
|
260
|
+
```ruby
|
261
|
+
# users_controller.rb
|
262
|
+
class UsersController < ApplicationController
|
263
|
+
def index
|
264
|
+
@user = authorized(User.all, scope_options: {with_deleted: true})
|
230
265
|
end
|
266
|
+
end
|
231
267
|
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
end
|
268
|
+
# user_policy.rb
|
269
|
+
describe UserPolicy < Application do
|
270
|
+
relation_scope do |relation, with_deleted: false|
|
271
|
+
rel = some_logic(relation)
|
272
|
+
with_deleted ? rel.with_deleted : rel
|
238
273
|
end
|
239
|
-
|
274
|
+
end
|
275
|
+
```
|
240
276
|
|
241
277
|
- Added Symbol lookup to the lookup chain ([@DmitryTsepelev][])
|
242
278
|
|
243
|
-
|
279
|
+
For instance, lookup will implicitly use `AdminPolicy` in a following case:
|
244
280
|
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
281
|
+
```ruby
|
282
|
+
# admin_controller.rb
|
283
|
+
class AdminController < ApplicationController
|
284
|
+
authorize! :admin, to: :update_settings
|
285
|
+
end
|
286
|
+
```
|
251
287
|
|
252
288
|
- Added testing for scopes. ([@palkan][])
|
253
289
|
|
254
|
-
|
290
|
+
Example:
|
255
291
|
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
end
|
292
|
+
```ruby
|
293
|
+
# users_controller.rb
|
294
|
+
class UsersController < ApplicationController
|
295
|
+
def index
|
296
|
+
@user = authorized(User.all)
|
262
297
|
end
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
298
|
+
end
|
299
|
+
|
300
|
+
# users_controller_spec.rb
|
301
|
+
describe UsersController do
|
302
|
+
subject { get :index }
|
303
|
+
it "has authorized scope" do
|
304
|
+
expect { subject }.to have_authorized_scope(:active_record_relation)
|
305
|
+
.with(PostPolicy)
|
271
306
|
end
|
272
|
-
|
307
|
+
end
|
308
|
+
```
|
273
309
|
|
274
310
|
- Added scoping support. ([@palkan][])
|
275
311
|
|
276
|
-
|
312
|
+
See [#5](https://github.com/palkan/action_policy/issues/5).
|
277
313
|
|
278
|
-
|
314
|
+
By "scoping" we mean an ability to use policies to _scope data_.
|
279
315
|
|
280
|
-
|
281
|
-
|
316
|
+
For example, when you want to _scope_ Active Record collections depending
|
317
|
+
on the current user permissions:
|
282
318
|
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
end
|
319
|
+
```ruby
|
320
|
+
class PostsController < ApplicationController
|
321
|
+
def index
|
322
|
+
@posts = authorized(Post.all)
|
288
323
|
end
|
324
|
+
end
|
289
325
|
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
end
|
326
|
+
class PostPolicy < ApplicationPolicy
|
327
|
+
relation_scope do |relation|
|
328
|
+
next relation if user.admin?
|
329
|
+
relation.where(user: user)
|
295
330
|
end
|
296
|
-
|
331
|
+
end
|
332
|
+
```
|
297
333
|
|
298
|
-
|
334
|
+
Action Policy provides a flexible mechanism to apply scopes to anything you want.
|
299
335
|
|
300
|
-
|
336
|
+
Read more in [docs](https://actionpolicy.evilmartians.io/).
|
301
337
|
|
302
338
|
- Added `#implicit_authorization_target`. ([@palkan][]).
|
303
339
|
|
304
|
-
|
340
|
+
See [#35](https://github.com/palkan/action_policy/issues/35).
|
305
341
|
|
306
|
-
|
342
|
+
Implicit authorization target (defined by `implicit_authorization_target`) is used when no target specified for `authorize!` call.
|
307
343
|
|
308
|
-
|
344
|
+
For example, for Rails controllers integration it's just `controller_name.classify.safe_constantize`.
|
309
345
|
|
310
346
|
- Consider `record#policy_name` when looking up for a policy class. ([@palkan][])
|
311
347
|
|
@@ -319,21 +355,21 @@ See [PR#118](https://github.com/palkan/action_policy/pull/118).
|
|
319
355
|
|
320
356
|
- Add ability to disable per-thread cache and disable it in test env by default. ([@palkan][])
|
321
357
|
|
322
|
-
|
358
|
+
You can control per-thread cache by setting:
|
323
359
|
|
324
|
-
|
325
|
-
|
326
|
-
|
360
|
+
```ruby
|
361
|
+
ActionPolicy::PerThreadCache.enabled = true # or false
|
362
|
+
```
|
327
363
|
|
328
364
|
## 0.2.3 (2018-07-03)
|
329
365
|
|
330
366
|
- [Fix [#16](https://github.com/palkan/action_policy/issues/16)] Add ability to disable namespace resolution cache. ([@palkan][])
|
331
367
|
|
332
|
-
|
368
|
+
We cache namespaced policy resolution for better performance (it could affect performance when we look up a policy from a deeply nested module context).
|
333
369
|
|
334
|
-
|
370
|
+
It could be disabled by setting `ActionPolicy::LookupChain.namespace_cache_enabled = false`. It's enabled by default unless `RACK_ENV` env var is specified and is not equal to `"production"` (e.g. when `RACK_ENV=test` the cache is disabled).
|
335
371
|
|
336
|
-
|
372
|
+
When using Rails it's enabled only in production mode but could be configured through setting the `config.action_policy.namespace_cache_enabled` parameter.
|
337
373
|
|
338
374
|
- [Fix [#18](https://github.com/palkan/action_policy/issues/18)] Clarify documentation around, and fix the way `resolve_rule` resolves rules and rule aliases when subclasses are involved. ([@brendon][])
|
339
375
|
|
@@ -341,11 +377,10 @@ See [PR#118](https://github.com/palkan/action_policy/pull/118).
|
|
341
377
|
|
342
378
|
- [Fix [#29](https://github.com/palkan/action_policy/issues/29)] Fix loading cache middleware. ([@palkan][])
|
343
379
|
|
344
|
-
|
345
380
|
- Use `send` instead of `public_send` to get the `authorization_context` so that contexts such as
|
346
381
|
`current_user` can be `private` in the controller. ([@brendon][])
|
347
382
|
|
348
|
-
- Fix railtie
|
383
|
+
- Fix railtie initialization for Rails < 5. ([@brendon][])
|
349
384
|
|
350
385
|
## 0.2.1 (yanked)
|
351
386
|
|
@@ -355,18 +390,18 @@ See [PR#118](https://github.com/palkan/action_policy/pull/118).
|
|
355
390
|
|
356
391
|
- Add `reasons.details`. ([@palkan][])
|
357
392
|
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
|
393
|
+
```ruby
|
394
|
+
rescue_from ActionPolicy::Unauthorized do |ex|
|
395
|
+
ex.result.reasons.details #=> { stage: [:show?] }
|
396
|
+
end
|
397
|
+
```
|
363
398
|
|
364
399
|
- Add `ExecutionResult`. ([@palkan][])
|
365
400
|
|
366
|
-
|
367
|
-
|
401
|
+
ExecutionResult contains all the rule application artifacts: the result (`true` / `false`),
|
402
|
+
failures reasons.
|
368
403
|
|
369
|
-
|
404
|
+
This value is now stored in a cache (if any) instead of just the call result (`true` / `false`).
|
370
405
|
|
371
406
|
- Add `Policy.identifier`. ([@palkan][])
|
372
407
|
|
@@ -400,3 +435,4 @@ See [PR#118](https://github.com/palkan/action_policy/pull/118).
|
|
400
435
|
[@nicolas-brousse]: https://github.com/nicolas-brousse
|
401
436
|
[@somenugget]: https://github.com/somenugget
|
402
437
|
[@Be-ngt-oH]: https://github.com/Be-ngt-oH
|
438
|
+
[@pirj]: https://github.com/pirj
|