graphql-pundit 0.5.1 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1 +1 @@
1
- 2.4.2
1
+ 2.5
@@ -0,0 +1,7 @@
1
+ linters:
2
+ rubocop:
3
+ fixer: true
4
+ shellcheck:
5
+ shell: bash
6
+ fixers:
7
+ enable: true
@@ -4,9 +4,9 @@ dist: trusty
4
4
  language: ruby
5
5
 
6
6
  rvm:
7
- - 2.2.7
8
- - 2.3.5
9
- - 2.4.2
7
+ - 2.2.9
8
+ - 2.3.6
9
+ - 2.4.3
10
10
  - 2.5.0
11
11
 
12
12
  notifications:
@@ -15,7 +15,7 @@ notifications:
15
15
  matrix:
16
16
  fast_finish: true
17
17
  allow_failures:
18
- - rvm: 2.5.0
18
+ - rvm: 2.2.9
19
19
 
20
20
  script:
21
21
  - bundle exec rspec --format progress
data/README.md CHANGED
@@ -2,7 +2,6 @@
2
2
  [![Build Status](https://travis-ci.org/ontohub/graphql-pundit.svg?branch=master)](https://travis-ci.org/ontohub/graphql-pundit)
3
3
  [![Coverage Status](https://codecov.io/gh/ontohub/graphql-pundit/branch/master/graph/badge.svg)](https://codecov.io/gh/ontohub/graphql-pundit)
4
4
  [![Code Climate](https://codeclimate.com/github/ontohub/graphql-pundit/badges/gpa.svg)](https://codeclimate.com/github/ontohub/graphql-pundit)
5
- [![Dependency Status](https://gemnasium.com/badges/github.com/ontohub/graphql-pundit.svg)](https://gemnasium.com/github.com/ontohub/graphql-pundit)
6
5
  [![GitHub issues](https://img.shields.io/github/issues/ontohub/graphql-pundit.svg?maxAge=2592000)](https://waffle.io/ontohub/ontohub-backend?source=ontohub%2Fgraphql-pundit)
7
6
 
8
7
  # GraphQL::Pundit
@@ -105,29 +104,27 @@ If the lambda returns a falsy value or raises a `Pundit::UnauthorizedError` the
105
104
 
106
105
  ### Scopes
107
106
 
108
- Pundit scopes are supported by using `scope` in the field definition
107
+ Pundit scopes are supported by using `before_scope` and `after_scope` in the field definition
109
108
 
110
109
  ```ruby
111
110
  field :posts
112
- scope
111
+ after_scope
113
112
  resolve ...
114
113
  end
115
114
  ```
116
115
 
117
- By default, this will use the Scope definied in the `PostPolicy`. If you do not want to define a scope inside of the policy, you can also pass a lambda to `scope`. The return value will be passed to `resolve` as first argument.
116
+ Passing no arguments to `after_scope` and `before_scope` will infer the policy to use from the value it is passed: `before_scope` is run before `resolve` and will receive the parent object, `after_scope` will be run after `resolve` and receives the output of `resolve`. You can also pass a proc or a policy class to both `_scope`s:
118
117
 
119
118
  ```ruby
120
119
  field :posts
121
- scope ->(_root, _args, ctx) { Post.where(owner: ctx[:current_user]) }
120
+ before_scope ->(_root, _args, ctx) { Post.where(owner: ctx[:current_user]) }
122
121
  resolve ->(posts, args, ctx) { ... }
123
122
  end
124
123
  ```
125
124
 
126
- In case you only want to specify the Policy class containing the Scope explicitly, you can pass the Policy class explicitly:
127
-
128
125
  ```ruby
129
126
  field :posts
130
- scope PostablePolicy
127
+ after_scope PostablePolicy
131
128
  resolve ...
132
129
  end
133
130
  ```
@@ -1,4 +1,3 @@
1
- # coding: utf-8
2
1
  # frozen_string_literal: true
3
2
 
4
3
  lib = File.expand_path('../lib', __FILE__)
@@ -28,13 +27,13 @@ Gem::Specification.new do |spec|
28
27
 
29
28
  spec.add_development_dependency 'bundler', '~> 1.14'
30
29
  spec.add_development_dependency 'codecov', '~> 0.1.10'
31
- spec.add_development_dependency 'fuubar', '~> 2.2.0'
30
+ spec.add_development_dependency 'fuubar', '~> 2.3.0'
32
31
  spec.add_development_dependency 'pry', '~> 0.11.0'
33
32
  spec.add_development_dependency 'pry-byebug', '~> 3.5.0'
34
33
  spec.add_development_dependency 'pry-rescue', '~> 1.4.4'
35
34
  spec.add_development_dependency 'pry-stack_explorer', '~> 0.4.9.2'
36
35
  spec.add_development_dependency 'rake', '~> 12.0'
37
36
  spec.add_development_dependency 'rspec', '~> 3.6'
38
- spec.add_development_dependency 'rubocop', '~> 0.51.0'
37
+ spec.add_development_dependency 'rubocop', '~> 0.52.1'
39
38
  spec.add_development_dependency 'simplecov', '~> 0.15.1'
40
39
  end
@@ -30,13 +30,22 @@ module GraphQL
30
30
 
31
31
  # Defines `scope` helper
32
32
  class ScopeHelper
33
+ def initialize(before_or_after, deprecated: false)
34
+ @before_or_after = before_or_after
35
+ @deprecated = deprecated
36
+ end
37
+
33
38
  def call(defn, proc = :infer_scope)
34
- Define::InstanceDefinable::AssignMetadataKey.new(:scope).
35
- call(defn, proc)
39
+ opts = {proc: proc, deprecated: @deprecated}
40
+ Define::InstanceDefinable::AssignMetadataKey.
41
+ new(:"#{@before_or_after}_scope").
42
+ call(defn, opts)
36
43
  end
37
44
  end
38
45
 
39
46
  Field.accepts_definitions(authorize: AuthorizationHelper.new(false),
40
47
  authorize!: AuthorizationHelper.new(true),
41
- scope: ScopeHelper.new)
48
+ after_scope: ScopeHelper.new(:after),
49
+ before_scope: ScopeHelper.new(:before),
50
+ scope: ScopeHelper.new(:before, deprecated: true))
42
51
  end
@@ -2,7 +2,8 @@
2
2
 
3
3
  require 'pundit'
4
4
  require 'graphql-pundit/instrumenters/authorization'
5
- require 'graphql-pundit/instrumenters/scope'
5
+ require 'graphql-pundit/instrumenters/before_scope'
6
+ require 'graphql-pundit/instrumenters/after_scope'
6
7
 
7
8
  module GraphQL
8
9
  module Pundit
@@ -10,18 +11,23 @@ module GraphQL
10
11
  class Instrumenter
11
12
  attr_reader :current_user,
12
13
  :authorization_instrumenter,
13
- :scope_instrumenter
14
+ :before_scope_instrumenter,
15
+ :after_scope_instrumenter
14
16
 
15
17
  def initialize(current_user = :current_user)
16
18
  @current_user = current_user
17
- @authorization_instrumenter = Instrumenters::Authorization.
18
- new(current_user)
19
- @scope_instrumenter = Instrumenters::Scope.new(current_user)
19
+ @authorization_instrumenter =
20
+ Instrumenters::Authorization.new(current_user)
21
+ @before_scope_instrumenter =
22
+ Instrumenters::BeforeScope.new(current_user)
23
+ @after_scope_instrumenter = Instrumenters::AfterScope.new(current_user)
20
24
  end
21
25
 
22
26
  def instrument(type, field)
23
- scoped_field = scope_instrumenter.instrument(type, field)
24
- authorization_instrumenter.instrument(type, scoped_field)
27
+ before_scoped_field = before_scope_instrumenter.instrument(type, field)
28
+ after_scoped_field = after_scope_instrumenter.
29
+ instrument(type, before_scoped_field)
30
+ authorization_instrumenter.instrument(type, after_scoped_field)
25
31
  end
26
32
  end
27
33
  end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'pundit'
4
+ require_relative 'scope'
5
+
6
+ module GraphQL
7
+ module Pundit
8
+ module Instrumenters
9
+ # Instrumenter that supplies `after_scope`
10
+ class AfterScope < Scope
11
+ SCOPE_KEY = :after_scope
12
+
13
+ # Applies the scoping to the passed object
14
+ class ScopeResolver < ScopeResolver
15
+ def call(root, arguments, context)
16
+ resolver_result = old_resolver.call(root, arguments, context)
17
+ scope_proc = new_scope(scope)
18
+ scope_proc.call(resolver_result, arguments, context)
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'pundit'
4
+ require_relative 'scope'
5
+
6
+ module GraphQL
7
+ module Pundit
8
+ module Instrumenters
9
+ # Instrumenter that supplies `before_scope`
10
+ class BeforeScope < Scope
11
+ SCOPE_KEY = :before_scope
12
+
13
+ # Applies the scoping to the passed object
14
+ class ScopeResolver < ScopeResolver
15
+ def call(root, arguments, context)
16
+ if field.metadata[:before_scope][:deprecated]
17
+ Kernel.warn <<~DEPRECATION_WARNING
18
+ Using `scope` is deprecated and might be removed in the future.
19
+ Please use `before_scope` or `after_scope` instead.
20
+ DEPRECATION_WARNING
21
+ end
22
+ scope_proc = new_scope(scope)
23
+ resolver_result = scope_proc.call(root, arguments, context)
24
+ old_resolver.call(resolver_result, arguments, context)
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -5,15 +5,16 @@ require 'pundit'
5
5
  module GraphQL
6
6
  module Pundit
7
7
  module Instrumenters
8
- # Instrumenter that supplies `scope`
8
+ # Base instrumenter for `before_scope` and `after_scope`
9
9
  class Scope
10
10
  # Applies the scoping to the passed object
11
11
  class ScopeResolver
12
- attr_reader :current_user, :scope, :old_resolver
12
+ attr_reader :current_user, :scope, :old_resolver, :field
13
13
 
14
- def initialize(current_user, scope, old_resolver)
14
+ def initialize(current_user, scope, old_resolver, field)
15
15
  @current_user = current_user
16
16
  @old_resolver = old_resolver
17
+ @field = field
17
18
 
18
19
  unless valid_value?(scope)
19
20
  raise ArgumentError, 'Invalid value passed to `scope`'
@@ -22,12 +23,6 @@ module GraphQL
22
23
  @scope = scope
23
24
  end
24
25
 
25
- def call(root, arguments, context)
26
- scope_proc = new_scope(scope)
27
- new_scope = scope_proc.call(root, arguments, context)
28
- old_resolver.call(new_scope, arguments, context)
29
- end
30
-
31
26
  private
32
27
 
33
28
  def new_scope(scope)
@@ -73,12 +68,18 @@ module GraphQL
73
68
  @current_user = current_user
74
69
  end
75
70
 
71
+ # rubocop:disable Metrics/MethodLength
76
72
  def instrument(_type, field)
77
- scope = field.metadata[:scope]
78
- return field unless scope
73
+ # rubocop:enable Metrics/MethodLength
74
+ scope_metadata = field.metadata[self.class::SCOPE_KEY]
75
+ return field unless scope_metadata
76
+ scope = scope_metadata[:proc]
79
77
 
80
78
  old_resolver = field.resolve_proc
81
- resolver = ScopeResolver.new(current_user, scope, old_resolver)
79
+ resolver = self.class::ScopeResolver.new(current_user,
80
+ scope,
81
+ old_resolver,
82
+ field)
82
83
 
83
84
  field.redefine do
84
85
  resolve resolver
@@ -2,6 +2,6 @@
2
2
 
3
3
  module GraphQL
4
4
  module Pundit
5
- VERSION = '0.5.1'
5
+ VERSION = '0.6.0'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: graphql-pundit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ontohub Core Developers
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-11-27 00:00:00.000000000 Z
11
+ date: 2018-03-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: graphql
@@ -78,14 +78,14 @@ dependencies:
78
78
  requirements:
79
79
  - - "~>"
80
80
  - !ruby/object:Gem::Version
81
- version: 2.2.0
81
+ version: 2.3.0
82
82
  type: :development
83
83
  prerelease: false
84
84
  version_requirements: !ruby/object:Gem::Requirement
85
85
  requirements:
86
86
  - - "~>"
87
87
  - !ruby/object:Gem::Version
88
- version: 2.2.0
88
+ version: 2.3.0
89
89
  - !ruby/object:Gem::Dependency
90
90
  name: pry
91
91
  requirement: !ruby/object:Gem::Requirement
@@ -176,14 +176,14 @@ dependencies:
176
176
  requirements:
177
177
  - - "~>"
178
178
  - !ruby/object:Gem::Version
179
- version: 0.51.0
179
+ version: 0.52.1
180
180
  type: :development
181
181
  prerelease: false
182
182
  version_requirements: !ruby/object:Gem::Requirement
183
183
  requirements:
184
184
  - - "~>"
185
185
  - !ruby/object:Gem::Version
186
- version: 0.51.0
186
+ version: 0.52.1
187
187
  - !ruby/object:Gem::Dependency
188
188
  name: simplecov
189
189
  requirement: !ruby/object:Gem::Requirement
@@ -210,10 +210,8 @@ files:
210
210
  - ".hound.yml"
211
211
  - ".rspec"
212
212
  - ".rubocop.yml"
213
- - ".rubocop_disabled.yml"
214
- - ".rubocop_enabled.yml"
215
- - ".rubocop_modified.yml"
216
213
  - ".ruby-version"
214
+ - ".stickler.yml"
217
215
  - ".travis.yml"
218
216
  - Gemfile
219
217
  - LICENSE.txt
@@ -224,7 +222,9 @@ files:
224
222
  - graphql-pundit.gemspec
225
223
  - lib/graphql-pundit.rb
226
224
  - lib/graphql-pundit/instrumenter.rb
225
+ - lib/graphql-pundit/instrumenters/after_scope.rb
227
226
  - lib/graphql-pundit/instrumenters/authorization.rb
227
+ - lib/graphql-pundit/instrumenters/before_scope.rb
228
228
  - lib/graphql-pundit/instrumenters/scope.rb
229
229
  - lib/graphql-pundit/version.rb
230
230
  homepage: https://github.com/ontohub/graphql-pundit
@@ -247,7 +247,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
247
247
  version: '0'
248
248
  requirements: []
249
249
  rubyforge_project:
250
- rubygems_version: 2.6.13
250
+ rubygems_version: 2.6.14
251
251
  signing_key:
252
252
  specification_version: 4
253
253
  summary: Pundit authorization support for graphql
@@ -1,119 +0,0 @@
1
- # These are all the cops that are disabled in the default configuration.
2
-
3
- Layout/FirstArrayElementLineBreak:
4
- Description: >-
5
- Checks for a line break before the first element in a
6
- multi-line array.
7
- Enabled: false
8
-
9
- Layout/FirstHashElementLineBreak:
10
- Description: >-
11
- Checks for a line break before the first element in a
12
- multi-line hash.
13
- Enabled: false
14
-
15
- Layout/FirstMethodArgumentLineBreak:
16
- Description: >-
17
- Checks for a line break before the first argument in a
18
- multi-line method call.
19
- Enabled: false
20
-
21
- Layout/FirstMethodParameterLineBreak:
22
- Description: >-
23
- Checks for a line break before the first parameter in a
24
- multi-line method parameter definition.
25
- Enabled: false
26
-
27
- Layout/MultilineAssignmentLayout:
28
- Description: 'Check for a newline after the assignment operator in multi-line assignments.'
29
- StyleGuide: '#indent-conditional-assignment'
30
- Enabled: false
31
-
32
- # By default, the rails cops are not run. Override in project or home
33
- # directory .rubocop.yml files, or by giving the -R/--rails option.
34
- Rails:
35
- Enabled: false
36
-
37
- Rails/SaveBang:
38
- Description: 'Identifies possible cases where Active Record save! or related should be used.'
39
- StyleGuide: 'https://github.com/bbatsov/rails-style-guide#save-bang'
40
- Enabled: false
41
-
42
- Style/AutoResourceCleanup:
43
- Description: 'Suggests the usage of an auto resource cleanup version of a method (if available).'
44
- Enabled: false
45
-
46
- Style/CollectionMethods:
47
- Description: 'Preferred collection methods.'
48
- StyleGuide: '#map-find-select-reduce-size'
49
- Enabled: false
50
-
51
- Style/Copyright:
52
- Description: 'Include a copyright notice in each file before any code.'
53
- Enabled: false
54
-
55
- Style/DocumentationMethod:
56
- Description: 'Public methods.'
57
- Enabled: false
58
- Exclude:
59
- - 'spec/**/*'
60
- - 'test/**/*'
61
-
62
- Style/Encoding:
63
- Description: 'Use UTF-8 as the source file encoding.'
64
- StyleGuide: '#utf-8'
65
- Enabled: false
66
-
67
- Style/ImplicitRuntimeError:
68
- Description: >-
69
- Use `raise` or `fail` with an explicit exception class and
70
- message, rather than just a message.
71
- Enabled: false
72
-
73
- Style/InlineComment:
74
- Description: 'Avoid trailing inline comments.'
75
- Enabled: false
76
-
77
- Style/MethodCallWithArgsParentheses:
78
- Description: 'Use parentheses for method calls with arguments.'
79
- StyleGuide: '#method-invocation-parens'
80
- Enabled: false
81
-
82
- Style/MethodCalledOnDoEndBlock:
83
- Description: 'Avoid chaining a method call on a do...end block.'
84
- StyleGuide: '#single-line-blocks'
85
- Enabled: false
86
-
87
- Style/MissingElse:
88
- Description: >-
89
- Require if/case expressions to have an else branches.
90
- If enabled, it is recommended that
91
- Style/UnlessElse and Style/EmptyElse be enabled.
92
- This will conflict with Style/EmptyElse if
93
- Style/EmptyElse is configured to style "both"
94
- Enabled: false
95
- EnforcedStyle: both
96
- SupportedStyles:
97
- # if - warn when an if expression is missing an else branch
98
- # case - warn when a case expression is missing an else branch
99
- # both - warn when an if or case expression is missing an else branch
100
- - if
101
- - case
102
- - both
103
-
104
- Style/OptionHash:
105
- Description: "Don't use option hashes when you can use keyword arguments."
106
- Enabled: false
107
-
108
- Style/Send:
109
- Description: 'Prefer `Object#__send__` or `Object#public_send` to `send`, as `send` may overlap with existing methods.'
110
- StyleGuide: '#prefer-public-send'
111
- Enabled: false
112
-
113
- Style/StringMethods:
114
- Description: 'Checks if configured preferred methods are used over non-preferred.'
115
- Enabled: false
116
-
117
- Style/SingleLineBlockParams:
118
- Description: 'Enforces the names of some block params.'
119
- Enabled: false