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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 12f75fa515f57c1d7cb21138025c5fea4f8202ecfdde800abec38451c54c9604
4
- data.tar.gz: 5b52a7b58ed0eb07e3ff42165518e3be2b79f41846a6d34c92a0c4090c90b51a
3
+ metadata.gz: df4defb25b2a7c7225aa220ed7494e10d40d0313bedad29bed9d4f45f8a87285
4
+ data.tar.gz: c381b03ef5c094101c6f8a2fa75af0b602c2115d75b58f8dd2631a6a7c959722
5
5
  SHA512:
6
- metadata.gz: '0933c5f001415a5c54ab3a89d3485d62579f0a7c0cb4a4519934dc68a17843eed920feeb44356c7f4d0e04203fd8e28f8541211ce448aa7c468de51fda0a13d8'
7
- data.tar.gz: 73b0729f99bfd3ed83a13fa4ffda3ec091934b03b834f6a522667dacd822ffac4294ad11ea332cc40f5de53dabd58cd61e6726dcc1a62619fa80aaf9c131e67d
6
+ metadata.gz: 55f7d820cbd95e1e4294977c464539c4cf4eca1455a62e7ef38c9f7a3eee655b11207ca749fb9044c20058929f5083e3fb5199df126a0f013883a83d5597b415
7
+ data.tar.gz: 43ad5e24e33295aa08e3960639d58c1ea7c94ac82a5c15f1a21994042a8e7d7974f47ffe96afbb307f2f3b270b571ac495d8c980eab865c4a65226269d3ddaaf
@@ -4,21 +4,29 @@ on: [push, pull_request]
4
4
 
5
5
  jobs:
6
6
  test:
7
- runs-on: ubuntu-18.04
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
@@ -1,5 +1,13 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 2.1.0
4
+
5
+ * Add traversal config.
6
+ * Add callbacks for `call_all`.
7
+ * Add calling instrumentation.
8
+ * Clear class cache when reloaded.
9
+ * Bump coactive version to 0.2.
10
+
3
11
  ## 2.0.0
4
12
 
5
13
  * Replace chain feature with coactive.
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
- Called ItemPolicy#index? for Item#1 and return true (Duration: 0.1ms, Allocations: 9)
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
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem "rails", "~> 7.0.0"
4
+
5
+ gemspec path: "../"
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.1"
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"
@@ -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
@@ -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(:call) do
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
@@ -18,12 +18,5 @@ module IIPolicy
18
18
  alias_method :chain, :coact
19
19
  end
20
20
  end
21
-
22
- def call(action)
23
- coactors.each do |policy|
24
- return false unless policy.new(@context).call(action)
25
- end
26
- super
27
- end
28
21
  end
29
22
  end
@@ -5,7 +5,8 @@ module IIPolicy
5
5
  class_attribute :data
6
6
 
7
7
  self.data = {
8
- lookup_cache: true
8
+ lookup_cache: true,
9
+ traversal: :postorder
9
10
  }
10
11
 
11
12
  data.keys.each do |key|
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module IIPolicy
4
- class Context < OpenStruct
4
+ class Context < Coactive::Context
5
5
  end
6
6
  end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module IIPolicy
4
+ module Contextualizer
5
+ extend ActiveSupport::Concern
6
+ include Coactive::Contextualizer
7
+
8
+ def call(action)
9
+ contextualize do
10
+ super
11
+ end
12
+ end
13
+ end
14
+ end
@@ -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.call("#{action_name}?")
23
+ raise IIPolicy::AuthorizationError.new('Not Authorized') unless instance.allowed("#{action_name}?")
24
24
  instance
25
25
  end
26
26
  end
@@ -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
- attr_reader :context, :user, :item, :_result
9
+ self.context_class = IIPolicy::Context
10
+ context :user, :item
11
+ attr_reader :_result
9
12
  end
10
13
 
11
- def initialize(context = {})
12
- @context = if context.is_a?(IIPolicy::Context)
13
- context
14
- else
15
- IIPolicy::Context.new(context)
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
- @item = @context.item
18
- @user = @context.user
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
- call(action)
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
- item = " for #{policy.item.class}##{policy.item.id}" if policy.item
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)
@@ -9,5 +9,9 @@ module IIPolicy
9
9
  ActiveSupport.on_load :action_view do
10
10
  ActionView::Base.send :include, IIPolicy::Helper
11
11
  end
12
+
13
+ ActiveSupport::Reloader.to_prepare do
14
+ IIPolicy::Lookup.cache.clear
15
+ end
12
16
  end
13
17
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module IIPolicy
4
- VERSION = '2.0.0'
4
+ VERSION = '2.1.0'
5
5
  end
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.0.0
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: 2021-11-29 00:00:00.000000000 Z
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.1'
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.1'
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.0.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