ii_policy 2.0.0 → 2.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/ci.yml +11 -3
- data/CHANGELOG.md +8 -0
- data/README.md +3 -1
- data/gemfiles/rails70.gemfile +5 -0
- data/ii_policy.gemspec +1 -1
- data/lib/ii_policy/base.rb +3 -1
- data/lib/ii_policy/callbacks.rb +20 -1
- data/lib/ii_policy/coactors.rb +0 -7
- data/lib/ii_policy/config.rb +2 -1
- data/lib/ii_policy/context.rb +1 -1
- data/lib/ii_policy/contextualizer.rb +14 -0
- data/lib/ii_policy/controller.rb +1 -1
- data/lib/ii_policy/core.rb +26 -11
- data/lib/ii_policy/instrumentation.rb +5 -0
- data/lib/ii_policy/log_subscriber.rb +11 -2
- data/lib/ii_policy/railtie.rb +4 -0
- data/lib/ii_policy/version.rb +1 -1
- metadata +7 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: df4defb25b2a7c7225aa220ed7494e10d40d0313bedad29bed9d4f45f8a87285
|
4
|
+
data.tar.gz: c381b03ef5c094101c6f8a2fa75af0b602c2115d75b58f8dd2631a6a7c959722
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 55f7d820cbd95e1e4294977c464539c4cf4eca1455a62e7ef38c9f7a3eee655b11207ca749fb9044c20058929f5083e3fb5199df126a0f013883a83d5597b415
|
7
|
+
data.tar.gz: 43ad5e24e33295aa08e3960639d58c1ea7c94ac82a5c15f1a21994042a8e7d7974f47ffe96afbb307f2f3b270b571ac495d8c980eab865c4a65226269d3ddaaf
|
data/.github/workflows/ci.yml
CHANGED
@@ -4,21 +4,29 @@ on: [push, pull_request]
|
|
4
4
|
|
5
5
|
jobs:
|
6
6
|
test:
|
7
|
-
runs-on: ubuntu-
|
7
|
+
runs-on: ubuntu-20.04
|
8
8
|
strategy:
|
9
9
|
fail-fast: false
|
10
10
|
matrix:
|
11
|
-
ruby: [2.3, 2.4, 2.5, 2.6, 2.7, 3.0]
|
12
|
-
gemfile: ['rails50', 'rails51', 'rails52', 'rails60', 'rails61']
|
11
|
+
ruby: [2.3, 2.4, 2.5, 2.6, 2.7, '3.0']
|
12
|
+
gemfile: ['rails50', 'rails51', 'rails52', 'rails60', 'rails61', 'rails70']
|
13
13
|
exclude:
|
14
14
|
- ruby: 2.3
|
15
15
|
gemfile: rails60
|
16
16
|
- ruby: 2.3
|
17
17
|
gemfile: rails61
|
18
|
+
- ruby: 2.3
|
19
|
+
gemfile: rails70
|
18
20
|
- ruby: 2.4
|
19
21
|
gemfile: rails60
|
20
22
|
- ruby: 2.4
|
21
23
|
gemfile: rails61
|
24
|
+
- ruby: 2.4
|
25
|
+
gemfile: rails70
|
26
|
+
- ruby: 2.5
|
27
|
+
gemfile: rails70
|
28
|
+
- ruby: 2.6
|
29
|
+
gemfile: rails70
|
22
30
|
- ruby: 3.0
|
23
31
|
gemfile: rails50
|
24
32
|
- ruby: 3.0
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -262,7 +262,9 @@ IIPolicy::LogSubscriber.attach_to :ii_policy
|
|
262
262
|
This subscriber will write logs in debug mode as the following example:
|
263
263
|
|
264
264
|
```
|
265
|
-
|
265
|
+
Calling ItemPolicy#index? with #<IIPolicy::Context ...>
|
266
|
+
...
|
267
|
+
Called ItemPolicy#index? and return true (Duration: 0.1ms, Allocations: 9)
|
266
268
|
```
|
267
269
|
|
268
270
|
## Contributing
|
data/ii_policy.gemspec
CHANGED
@@ -18,7 +18,7 @@ Gem::Specification.new do |spec|
|
|
18
18
|
spec.require_paths = ["lib"]
|
19
19
|
|
20
20
|
spec.add_dependency "activesupport", ">= 5.0"
|
21
|
-
spec.add_dependency "coactive", ">= 0.
|
21
|
+
spec.add_dependency "coactive", ">= 0.2"
|
22
22
|
|
23
23
|
spec.add_development_dependency "rails", ">= 5.0"
|
24
24
|
spec.add_development_dependency "sqlite3"
|
data/lib/ii_policy/base.rb
CHANGED
@@ -1,10 +1,11 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require_relative 'context'
|
4
3
|
require_relative 'core'
|
5
4
|
require_relative 'callbacks'
|
6
5
|
require_relative 'instrumentation'
|
7
6
|
require_relative 'lookup'
|
7
|
+
require_relative 'context'
|
8
|
+
require_relative 'contextualizer'
|
8
9
|
require_relative 'coactors'
|
9
10
|
|
10
11
|
module IIPolicy
|
@@ -13,6 +14,7 @@ module IIPolicy
|
|
13
14
|
include Callbacks
|
14
15
|
include Instrumentation
|
15
16
|
include Lookup
|
17
|
+
include Contextualizer
|
16
18
|
include Coactors
|
17
19
|
end
|
18
20
|
end
|
data/lib/ii_policy/callbacks.rb
CHANGED
@@ -6,16 +6,35 @@ module IIPolicy
|
|
6
6
|
include ActiveSupport::Callbacks
|
7
7
|
|
8
8
|
included do
|
9
|
+
define_callbacks :all
|
9
10
|
define_callbacks :call
|
10
11
|
end
|
11
12
|
|
13
|
+
def call_all(action)
|
14
|
+
run_callbacks :all do
|
15
|
+
super
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
12
19
|
def call(action)
|
13
|
-
run_callbacks
|
20
|
+
run_callbacks :call do
|
14
21
|
super
|
15
22
|
end
|
16
23
|
end
|
17
24
|
|
18
25
|
class_methods do
|
26
|
+
def before_all(*args, &block)
|
27
|
+
set_callback(:all, :before, *args, &block)
|
28
|
+
end
|
29
|
+
|
30
|
+
def after_all(*args, &block)
|
31
|
+
set_callback(:all, :after, *args, &block)
|
32
|
+
end
|
33
|
+
|
34
|
+
def around_all(*args, &block)
|
35
|
+
set_callback(:all, :around, *args, &block)
|
36
|
+
end
|
37
|
+
|
19
38
|
def before_call(*args, &block)
|
20
39
|
set_callback(:call, :before, *args, &block)
|
21
40
|
end
|
data/lib/ii_policy/coactors.rb
CHANGED
data/lib/ii_policy/config.rb
CHANGED
data/lib/ii_policy/context.rb
CHANGED
data/lib/ii_policy/controller.rb
CHANGED
@@ -20,7 +20,7 @@ module IIPolicy
|
|
20
20
|
|
21
21
|
def authorize(item, context = {})
|
22
22
|
instance = policy(item, context)
|
23
|
-
raise IIPolicy::AuthorizationError.new('Not Authorized') unless instance.
|
23
|
+
raise IIPolicy::AuthorizationError.new('Not Authorized') unless instance.allowed("#{action_name}?")
|
24
24
|
instance
|
25
25
|
end
|
26
26
|
end
|
data/lib/ii_policy/core.rb
CHANGED
@@ -3,19 +3,35 @@
|
|
3
3
|
module IIPolicy
|
4
4
|
module Core
|
5
5
|
extend ActiveSupport::Concern
|
6
|
+
include Coactive::Initializer
|
6
7
|
|
7
8
|
included do
|
8
|
-
|
9
|
+
self.context_class = IIPolicy::Context
|
10
|
+
context :user, :item
|
11
|
+
attr_reader :_result
|
9
12
|
end
|
10
13
|
|
11
|
-
def initialize(
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
14
|
+
def initialize(args = {})
|
15
|
+
super
|
16
|
+
end
|
17
|
+
|
18
|
+
def call_all(action)
|
19
|
+
planned = case IIPolicy.config.traversal
|
20
|
+
when :preorder
|
21
|
+
[self] + coactors
|
22
|
+
when :postorder
|
23
|
+
coactors + [self]
|
24
|
+
when :inorder
|
25
|
+
planned = coactors.in_groups(2, false)
|
26
|
+
planned[0] + [self] + planned[1]
|
16
27
|
end
|
17
|
-
|
18
|
-
|
28
|
+
|
29
|
+
planned.each do |policy|
|
30
|
+
result = policy == self ? call(action) : policy.new(@context).call_all(action)
|
31
|
+
return false unless result
|
32
|
+
end
|
33
|
+
|
34
|
+
return true
|
19
35
|
end
|
20
36
|
|
21
37
|
def call(action)
|
@@ -27,12 +43,11 @@ module IIPolicy
|
|
27
43
|
end
|
28
44
|
|
29
45
|
def allowed(action)
|
30
|
-
|
46
|
+
call_all(action)
|
31
47
|
end
|
32
48
|
|
33
49
|
def policy(item)
|
34
|
-
context = @context.dup
|
35
|
-
context.item = item
|
50
|
+
context = self.class.context_class.new(@context.to_h.dup.merge(item: item))
|
36
51
|
self.class.lookup(item).new(context)
|
37
52
|
end
|
38
53
|
end
|
@@ -4,6 +4,11 @@ module IIPolicy
|
|
4
4
|
module Instrumentation
|
5
5
|
extend ActiveSupport::Concern
|
6
6
|
|
7
|
+
def call_all(action)
|
8
|
+
ActiveSupport::Notifications.instrument 'calling.ii_policy', policy: self, action: action
|
9
|
+
super
|
10
|
+
end
|
11
|
+
|
7
12
|
def call(action)
|
8
13
|
ActiveSupport::Notifications.instrument 'call.ii_policy', policy: self, action: action do
|
9
14
|
super
|
@@ -2,15 +2,24 @@
|
|
2
2
|
|
3
3
|
module IIPolicy
|
4
4
|
class LogSubscriber < ActiveSupport::LogSubscriber
|
5
|
+
def calling(event)
|
6
|
+
debug do
|
7
|
+
policy = event.payload[:policy]
|
8
|
+
action = event.payload[:action]
|
9
|
+
"Calling #{policy.class}##{action} with #{policy.context}"
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
5
13
|
def call(event)
|
6
14
|
debug do
|
7
15
|
policy = event.payload[:policy]
|
8
16
|
action = event.payload[:action]
|
9
|
-
|
10
|
-
"Called #{policy.class}##{action}#{item} and return #{policy._result} (#{additional_log(event)})"
|
17
|
+
"Called #{policy.class}##{action} and return #{policy._result} (#{additional_log(event)})"
|
11
18
|
end
|
12
19
|
end
|
13
20
|
|
21
|
+
private
|
22
|
+
|
14
23
|
def additional_log(event)
|
15
24
|
additions = ["Duration: %.1fms" % event.duration]
|
16
25
|
additions << "Allocations: %d" % event.allocations if event.respond_to?(:allocations)
|
data/lib/ii_policy/railtie.rb
CHANGED
data/lib/ii_policy/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ii_policy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yoshikazu Kaneta
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-01-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -30,14 +30,14 @@ dependencies:
|
|
30
30
|
requirements:
|
31
31
|
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '0.
|
33
|
+
version: '0.2'
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '0.
|
40
|
+
version: '0.2'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rails
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -130,6 +130,7 @@ files:
|
|
130
130
|
- gemfiles/rails52.gemfile
|
131
131
|
- gemfiles/rails60.gemfile
|
132
132
|
- gemfiles/rails61.gemfile
|
133
|
+
- gemfiles/rails70.gemfile
|
133
134
|
- ii_policy.gemspec
|
134
135
|
- lib/ii_policy.rb
|
135
136
|
- lib/ii_policy/base.rb
|
@@ -137,6 +138,7 @@ files:
|
|
137
138
|
- lib/ii_policy/coactors.rb
|
138
139
|
- lib/ii_policy/config.rb
|
139
140
|
- lib/ii_policy/context.rb
|
141
|
+
- lib/ii_policy/contextualizer.rb
|
140
142
|
- lib/ii_policy/controller.rb
|
141
143
|
- lib/ii_policy/core.rb
|
142
144
|
- lib/ii_policy/errors.rb
|
@@ -164,7 +166,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
164
166
|
- !ruby/object:Gem::Version
|
165
167
|
version: '0'
|
166
168
|
requirements: []
|
167
|
-
rubygems_version: 3.
|
169
|
+
rubygems_version: 3.1.6
|
168
170
|
signing_key:
|
169
171
|
specification_version: 4
|
170
172
|
summary: A base policy to support management of authorization logic
|