graphql_pundit3 0.1.0
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.
- checksums.yaml +7 -0
- data/.codeclimate.yml +16 -0
- data/.rspec +3 -0
- data/.rubocop.yml +95 -0
- data/.ruby-version +1 -0
- data/.stickler.yml +7 -0
- data/CHANGELOG.md +5 -0
- data/Gemfile +12 -0
- data/Gemfile.lock +84 -0
- data/LICENSE.txt +21 -0
- data/README.md +159 -0
- data/Rakefile +12 -0
- data/graphql_pundit3.gemspec +46 -0
- data/lib/graphql_pundit/authorization_extension.rb +75 -0
- data/lib/graphql_pundit/common.rb +18 -0
- data/lib/graphql_pundit/version.rb +5 -0
- data/lib/graphql_pundit3.rb +13 -0
- data/sig/graphql_pundit.rbs +4 -0
- metadata +182 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 4025b3cadce1e11c4cfe03d09f51b3db2f20ee0755cdcedfdf32ead8c7b0d4e5
|
|
4
|
+
data.tar.gz: 5ebd6a459eabec18c9b2ded02cd4a2f1ad963b0d49c174103741b425041f73be
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: b3982a9237239fc192e0b90e5fac8010192426fc07428076dc3086bbef19573b22a49d6ce8889197a5b583ef524cc477f38e8f4515452ac75bf4a7caaca1d087
|
|
7
|
+
data.tar.gz: '0835406b1506f30f49d5b697049b242f01b98348e438f2faa41520f5b33aede052c8d75390c09c0d40a14b501d4794c6c7adb73d787bd20b08a0b2bcd2ed5df8'
|
data/.codeclimate.yml
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
AllCops:
|
|
2
|
+
TargetRubyVersion: 3
|
|
3
|
+
NewCops: enable
|
|
4
|
+
|
|
5
|
+
CacheRootDirectory: /tmp
|
|
6
|
+
AllowSymlinksInCacheRootDirectory: true
|
|
7
|
+
|
|
8
|
+
Exclude:
|
|
9
|
+
- 'apidoc/**/*'
|
|
10
|
+
- 'bin/bundle'
|
|
11
|
+
- 'bin/rails'
|
|
12
|
+
- 'bin/rake'
|
|
13
|
+
- 'bin/setup'
|
|
14
|
+
- 'bin/spring'
|
|
15
|
+
- 'bin/update'
|
|
16
|
+
- 'config/deploy/*.rb'
|
|
17
|
+
- 'config/mixins/applications/*.rb'
|
|
18
|
+
- 'data/**/*'
|
|
19
|
+
- 'db/schema.rb'
|
|
20
|
+
- 'node_modules/**/*'
|
|
21
|
+
- 'spec/dummy/**/*'
|
|
22
|
+
- 'vendor/**/*'
|
|
23
|
+
- 'repositories/**/*'
|
|
24
|
+
- 'repos/**/*'
|
|
25
|
+
- 'tmp/**/*'
|
|
26
|
+
|
|
27
|
+
Layout/ParameterAlignment:
|
|
28
|
+
Enabled: false
|
|
29
|
+
|
|
30
|
+
Layout/MultilineMethodCallIndentation:
|
|
31
|
+
EnforcedStyle: indented
|
|
32
|
+
|
|
33
|
+
Metrics/BlockLength:
|
|
34
|
+
Exclude:
|
|
35
|
+
- 'app/graphql/**/*'
|
|
36
|
+
- 'config/routes.rb'
|
|
37
|
+
- 'config/environments/**/*'
|
|
38
|
+
- 'config/initializers/devise.rb'
|
|
39
|
+
- 'lib/tasks/**/*.rake'
|
|
40
|
+
- 'spec/**/*'
|
|
41
|
+
- 'db/migrate/*'
|
|
42
|
+
- '*.gemspec'
|
|
43
|
+
|
|
44
|
+
Layout/LineLength:
|
|
45
|
+
Exclude:
|
|
46
|
+
- 'app/graphql/**/*_enum.rb'
|
|
47
|
+
- 'config/initializers/devise.rb'
|
|
48
|
+
|
|
49
|
+
Naming/FileName:
|
|
50
|
+
Exclude:
|
|
51
|
+
- config/deploy/*.rb
|
|
52
|
+
- config/mixins/applications/*.rb
|
|
53
|
+
- lib/git-shell.rb
|
|
54
|
+
- lib/graphql-pundit.rb
|
|
55
|
+
- lib/hets-agent.rb
|
|
56
|
+
- lib/ontohub-models.rb
|
|
57
|
+
- spec/lib/git-shell_spec.rb
|
|
58
|
+
|
|
59
|
+
Style/OptionalBooleanParameter:
|
|
60
|
+
Enabled: false
|
|
61
|
+
|
|
62
|
+
Naming/MethodParameterName:
|
|
63
|
+
Exclude:
|
|
64
|
+
- 'spec/**/*'
|
|
65
|
+
|
|
66
|
+
Style/Documentation:
|
|
67
|
+
Enabled: false
|
|
68
|
+
|
|
69
|
+
Style/DoubleNegation:
|
|
70
|
+
Enabled: false
|
|
71
|
+
|
|
72
|
+
Style/FormatStringToken:
|
|
73
|
+
Enabled: false
|
|
74
|
+
|
|
75
|
+
Style/NumericLiterals:
|
|
76
|
+
Enabled: false
|
|
77
|
+
|
|
78
|
+
Style/PercentLiteralDelimiters:
|
|
79
|
+
PreferredDelimiters:
|
|
80
|
+
default: ()
|
|
81
|
+
'%i': '()'
|
|
82
|
+
'%I': '()'
|
|
83
|
+
'%r': '{}'
|
|
84
|
+
'%w': '()'
|
|
85
|
+
'%W': '()'
|
|
86
|
+
|
|
87
|
+
Style/SymbolArray:
|
|
88
|
+
Exclude:
|
|
89
|
+
- 'db/migrate/**'
|
|
90
|
+
|
|
91
|
+
Style/TrailingCommaInArrayLiteral:
|
|
92
|
+
EnforcedStyleForMultiline: comma
|
|
93
|
+
|
|
94
|
+
Style/TrailingCommaInHashLiteral:
|
|
95
|
+
EnforcedStyleForMultiline: comma
|
data/.ruby-version
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
3.1.0
|
data/.stickler.yml
ADDED
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
PATH
|
|
2
|
+
remote: .
|
|
3
|
+
specs:
|
|
4
|
+
graphql_pundit3 (0.1.0)
|
|
5
|
+
graphql (>= 1.8, < 3)
|
|
6
|
+
pundit (~> 2.1.0)
|
|
7
|
+
|
|
8
|
+
GEM
|
|
9
|
+
remote: https://rubygems.org/
|
|
10
|
+
specs:
|
|
11
|
+
activesupport (7.0.3)
|
|
12
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
|
13
|
+
i18n (>= 1.6, < 2)
|
|
14
|
+
minitest (>= 5.1)
|
|
15
|
+
tzinfo (~> 2.0)
|
|
16
|
+
ast (2.4.2)
|
|
17
|
+
codecov (0.1.21)
|
|
18
|
+
json
|
|
19
|
+
simplecov
|
|
20
|
+
concurrent-ruby (1.1.10)
|
|
21
|
+
diff-lcs (1.5.0)
|
|
22
|
+
docile (1.4.0)
|
|
23
|
+
graphql (2.0.11)
|
|
24
|
+
i18n (1.11.0)
|
|
25
|
+
concurrent-ruby (~> 1.0)
|
|
26
|
+
json (2.6.2)
|
|
27
|
+
minitest (5.16.2)
|
|
28
|
+
parallel (1.22.1)
|
|
29
|
+
parser (3.1.2.0)
|
|
30
|
+
ast (~> 2.4.1)
|
|
31
|
+
pundit (2.1.1)
|
|
32
|
+
activesupport (>= 3.0.0)
|
|
33
|
+
rainbow (3.1.1)
|
|
34
|
+
rake (13.0.6)
|
|
35
|
+
regexp_parser (2.5.0)
|
|
36
|
+
rexml (3.2.5)
|
|
37
|
+
rspec (3.11.0)
|
|
38
|
+
rspec-core (~> 3.11.0)
|
|
39
|
+
rspec-expectations (~> 3.11.0)
|
|
40
|
+
rspec-mocks (~> 3.11.0)
|
|
41
|
+
rspec-core (3.11.0)
|
|
42
|
+
rspec-support (~> 3.11.0)
|
|
43
|
+
rspec-expectations (3.11.0)
|
|
44
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
45
|
+
rspec-support (~> 3.11.0)
|
|
46
|
+
rspec-mocks (3.11.1)
|
|
47
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
48
|
+
rspec-support (~> 3.11.0)
|
|
49
|
+
rspec-support (3.11.0)
|
|
50
|
+
rubocop (1.31.2)
|
|
51
|
+
json (~> 2.3)
|
|
52
|
+
parallel (~> 1.10)
|
|
53
|
+
parser (>= 3.1.0.0)
|
|
54
|
+
rainbow (>= 2.2.2, < 4.0)
|
|
55
|
+
regexp_parser (>= 1.8, < 3.0)
|
|
56
|
+
rexml (>= 3.2.5, < 4.0)
|
|
57
|
+
rubocop-ast (>= 1.18.0, < 2.0)
|
|
58
|
+
ruby-progressbar (~> 1.7)
|
|
59
|
+
unicode-display_width (>= 1.4.0, < 3.0)
|
|
60
|
+
rubocop-ast (1.19.1)
|
|
61
|
+
parser (>= 3.1.1.0)
|
|
62
|
+
ruby-progressbar (1.11.0)
|
|
63
|
+
simplecov (0.18.5)
|
|
64
|
+
docile (~> 1.1)
|
|
65
|
+
simplecov-html (~> 0.11)
|
|
66
|
+
simplecov-html (0.12.3)
|
|
67
|
+
tzinfo (2.0.4)
|
|
68
|
+
concurrent-ruby (~> 1.0)
|
|
69
|
+
unicode-display_width (2.2.0)
|
|
70
|
+
|
|
71
|
+
PLATFORMS
|
|
72
|
+
x86_64-darwin-21
|
|
73
|
+
|
|
74
|
+
DEPENDENCIES
|
|
75
|
+
bundler (~> 2.0)
|
|
76
|
+
codecov (~> 0.1.10)
|
|
77
|
+
graphql_pundit3!
|
|
78
|
+
rake (~> 13.0)
|
|
79
|
+
rspec (~> 3.0)
|
|
80
|
+
rubocop (~> 1.21)
|
|
81
|
+
simplecov (~> 0.18.5)
|
|
82
|
+
|
|
83
|
+
BUNDLED WITH
|
|
84
|
+
2.3.12
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Lubomir Vnenk
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
[](https://travis-ci.com/lubosch/graphql_pundit)
|
|
2
|
+
[](https://codecov.io/gh/lubosch/graphql_pundit)
|
|
3
|
+
|
|
4
|
+
# GraphQL::Pundit
|
|
5
|
+
|
|
6
|
+
Use field authorize api with pundit
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
Add this line to your application's Gemfile:
|
|
11
|
+
|
|
12
|
+
```ruby
|
|
13
|
+
gem 'graphql_pundit'
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
And then execute:
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
$ bundle
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Upgrading notes
|
|
23
|
+
|
|
24
|
+
# If you are coming from ontobot
|
|
25
|
+
|
|
26
|
+
Add this to your BaseField:
|
|
27
|
+
|
|
28
|
+
```
|
|
29
|
+
def initialize(*args, raise_unauthorized: false, authorize: nil, record: nil, policy: nil, **kwargs, &block)
|
|
30
|
+
super(*args, **kwargs, &block)
|
|
31
|
+
@raise_unauthorized = raise_unauthorized
|
|
32
|
+
@authorize = authorize
|
|
33
|
+
@record = record
|
|
34
|
+
@policy = policy
|
|
35
|
+
extension(GraphqlPundit::AuthorizationExtension, current_user: :current_user)
|
|
36
|
+
end
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## Usage
|
|
40
|
+
|
|
41
|
+
```ruby
|
|
42
|
+
class Car < BaseObject
|
|
43
|
+
field :trunk, CarContent, null: true,
|
|
44
|
+
authorize: true
|
|
45
|
+
end
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
The above example shows the most basic usage of this gem. The example would
|
|
49
|
+
use `CarPolicy#trunk?` for authorizing access to the field, passing in the
|
|
50
|
+
parent object (in this case probably a `Car` model).
|
|
51
|
+
|
|
52
|
+
##### Options
|
|
53
|
+
|
|
54
|
+
Two styles of declaring fields is supported:
|
|
55
|
+
|
|
56
|
+
1. the inline style, passing all the options as a hash to the field method
|
|
57
|
+
2. the block style
|
|
58
|
+
|
|
59
|
+
Both styles are presented below side by side.
|
|
60
|
+
|
|
61
|
+
###### `authorize`
|
|
62
|
+
|
|
63
|
+
To use authorization on a field, you **must** pass either the `authorize`
|
|
64
|
+
option. Both options will cause the field to return `nil` if the
|
|
65
|
+
access is unauthorized. Use `raise_unauthorized: true` to also add an error message (e.g.
|
|
66
|
+
for usage with mutations).
|
|
67
|
+
|
|
68
|
+
`authorize` can be passed three different things:
|
|
69
|
+
|
|
70
|
+
```ruby
|
|
71
|
+
class User < BaseObject
|
|
72
|
+
# will use the `UserPolicy#display_name?` method
|
|
73
|
+
field :display_name, ..., authorize: true
|
|
74
|
+
|
|
75
|
+
# will use the passed lambda instead of a policy method
|
|
76
|
+
field :password_hash, ..., authorize: ->(obj, args, ctx) { ... }
|
|
77
|
+
|
|
78
|
+
# will use the `UserPolicy#personal_info?` method
|
|
79
|
+
field :email, ..., authorize: :personal_info
|
|
80
|
+
end
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
- `true` will trigger the inference mechanism, meaning that the method that will be called on the policy class will be inferred from the (snake_case) field name.
|
|
84
|
+
- a lambda function that will be called with the parent object, the arguments of the field and the context object; if the lambda returns a truthy value, authorization succeeds; otherwise (including thrown exceptions), authorization fails
|
|
85
|
+
- a string or a symbol that corresponds to the policy method that should be called **minus the "?"**
|
|
86
|
+
|
|
87
|
+
###### `policy`
|
|
88
|
+
|
|
89
|
+
`policy` is an optional argument that can also be passed three different values:
|
|
90
|
+
|
|
91
|
+
```ruby
|
|
92
|
+
class User < BaseObject
|
|
93
|
+
# will use the `UserPolicy#display_name?` method (default inference)
|
|
94
|
+
field :display_name, ..., authorize: true, policy: nil
|
|
95
|
+
|
|
96
|
+
# will use OtherUserPolicy#password_hash?
|
|
97
|
+
field :password_hash, ...,
|
|
98
|
+
authorize: true,
|
|
99
|
+
policy: ->(obj, args, ctx) { OtherUserPolicy }
|
|
100
|
+
|
|
101
|
+
# will use MemberPolicy#email?
|
|
102
|
+
field :email, ..., authorize: true, policy: MemberPolicy
|
|
103
|
+
end
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
- `nil` is the default behavior and results in inferring the policy class from the record (see below)
|
|
107
|
+
- a lambda function that will be called with the parent object, the arguments of the field and the context object; the return value of this function will be used as the policy class
|
|
108
|
+
- an actual policy class
|
|
109
|
+
|
|
110
|
+
###### `record`
|
|
111
|
+
|
|
112
|
+
`record` can be used to pass a different value to the policy. Like `policy`,
|
|
113
|
+
this argument also can receive three different values:
|
|
114
|
+
|
|
115
|
+
```ruby
|
|
116
|
+
class User < BaseObject
|
|
117
|
+
# will use the parent object
|
|
118
|
+
field :display_name, ..., authorize: true, record: nil
|
|
119
|
+
|
|
120
|
+
# will use the current user as the record
|
|
121
|
+
field :password_hash, ...,
|
|
122
|
+
authorize: true,
|
|
123
|
+
record: ->(obj, args, ctx) { ctx[:current_user] }
|
|
124
|
+
|
|
125
|
+
# will use AccountPolicy#email? with the first account as the record (the policy was inferred from the record class)
|
|
126
|
+
field :email, ..., authorize: true, record: Account.first
|
|
127
|
+
end
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
- `nil` is again used for the inference; in this case, the parent object is used
|
|
131
|
+
- a lambda function, again called with the parent object, the field arguments and the context object; the result will be used as the record
|
|
132
|
+
- any other value that will be used as the record
|
|
133
|
+
|
|
134
|
+
Using `record` can be helpful for e.g. mutations, where you need a value to
|
|
135
|
+
initialize the policy with, but for mutations there is no parent object.
|
|
136
|
+
|
|
137
|
+
#### Current user
|
|
138
|
+
|
|
139
|
+
By default, `ctx[:current_user]` will be used as the user to authorize. To change that behavior, pass a symbol to `GraphQL::Pundit::Instrumenter`.
|
|
140
|
+
|
|
141
|
+
```ruby
|
|
142
|
+
GraphQL::Pundit::Instrumenter.new(:me) # will use ctx[:me]
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
## Development
|
|
146
|
+
|
|
147
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
148
|
+
|
|
149
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
|
150
|
+
|
|
151
|
+
## Contributing
|
|
152
|
+
|
|
153
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/lubosch/graphql-pundit.
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
## License
|
|
157
|
+
|
|
158
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
|
159
|
+
|
data/Rakefile
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'lib/graphql_pundit/version'
|
|
4
|
+
|
|
5
|
+
Gem::Specification.new do |spec|
|
|
6
|
+
spec.name = 'graphql_pundit3'
|
|
7
|
+
spec.version = GraphqlPundit::VERSION
|
|
8
|
+
spec.authors = ['Lubomir Vnenk']
|
|
9
|
+
spec.email = ['lubomir.vnenk@zoho.com']
|
|
10
|
+
|
|
11
|
+
spec.summary = 'Pundit authorization support for new graphql interpreter'
|
|
12
|
+
spec.description = spec.summary
|
|
13
|
+
spec.homepage = 'https://github.com/lubosch/graphql_pundit'
|
|
14
|
+
spec.license = 'MIT'
|
|
15
|
+
spec.required_ruby_version = '>= 3.0'
|
|
16
|
+
|
|
17
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
|
18
|
+
spec.metadata['source_code_uri'] = 'https://github.com/lubosch/graphql_pundit/blob/master/README.md'
|
|
19
|
+
spec.metadata['changelog_uri'] = 'https://github.com/lubosch/graphql_pundit/blob/master/CHANGELOG.md'
|
|
20
|
+
|
|
21
|
+
# Specify which files should be added to the gem when it is released.
|
|
22
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
|
23
|
+
spec.files = Dir.chdir(__dir__) do
|
|
24
|
+
`git ls-files -z`.split("\x0").reject do |f|
|
|
25
|
+
(f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
spec.bindir = 'exe'
|
|
29
|
+
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
|
30
|
+
spec.require_paths = ['lib']
|
|
31
|
+
|
|
32
|
+
# Uncomment to register a new dependency of your gem
|
|
33
|
+
spec.add_dependency 'graphql', '>= 1.8', '< 3'
|
|
34
|
+
spec.add_dependency 'pundit', '~> 2.1.0'
|
|
35
|
+
|
|
36
|
+
spec.add_development_dependency 'bundler', '~> 2.0'
|
|
37
|
+
spec.add_development_dependency 'codecov', '~> 0.1.10'
|
|
38
|
+
spec.add_development_dependency 'rake', '~> 13.0'
|
|
39
|
+
spec.add_development_dependency 'rspec', '~> 3.6'
|
|
40
|
+
spec.add_development_dependency 'rubocop', '>= 0.83.0'
|
|
41
|
+
spec.add_development_dependency 'simplecov', '~> 0.18.5'
|
|
42
|
+
|
|
43
|
+
# For more information and examples about making a new gem, check out our
|
|
44
|
+
# guide at: https://bundler.io/guides/creating_gem.html
|
|
45
|
+
spec.metadata['rubygems_mfa_required'] = 'true'
|
|
46
|
+
end
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module GraphqlPundit
|
|
4
|
+
# Authorization methods to be included in the used Field class
|
|
5
|
+
class AuthorizationExtension < GraphQL::Schema::FieldExtension
|
|
6
|
+
include GraphqlPundit::Common
|
|
7
|
+
|
|
8
|
+
attr_reader :authorize, :policy, :record, :raise_unauthorized
|
|
9
|
+
|
|
10
|
+
def initialize(field:, options:)
|
|
11
|
+
super
|
|
12
|
+
@current_user = options[:current_user] || :current_user
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def apply
|
|
16
|
+
@raise_unauthorized = field.raise_unauthorized
|
|
17
|
+
@authorize = field.authorize
|
|
18
|
+
@record = field.record
|
|
19
|
+
@policy = field.policy
|
|
20
|
+
@method_sym = field.method_sym
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def resolve(object:, arguments:, context:, **_rest)
|
|
24
|
+
# yield the current time as `memo`
|
|
25
|
+
raise ::Pundit::NotAuthorizedError unless do_authorize(object, arguments, context)
|
|
26
|
+
|
|
27
|
+
yield(object, arguments)
|
|
28
|
+
rescue ::Pundit::NotAuthorizedError
|
|
29
|
+
raise GraphQL::ExecutionError, "You're not authorized to do this" if @raise_unauthorized
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
private
|
|
33
|
+
|
|
34
|
+
def do_authorize(root, arguments, context)
|
|
35
|
+
return true unless @authorize
|
|
36
|
+
return @authorize.call(root, arguments, context) if callable?(@authorize)
|
|
37
|
+
|
|
38
|
+
query = infer_query(@authorize)
|
|
39
|
+
record = infer_record(@record, root, arguments, context)
|
|
40
|
+
policy = infer_policy(@policy, record, arguments, context)
|
|
41
|
+
|
|
42
|
+
policy.new(context[@current_user], record).public_send(query)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def infer_query(auth_value)
|
|
46
|
+
# authorize can be callable, true (for inference) or a policy query
|
|
47
|
+
query = auth_value.equal?(true) ? @method_sym : auth_value
|
|
48
|
+
"#{query}?"
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def infer_record(record, root, arguments, context)
|
|
52
|
+
# record can be callable, nil (for inference) or just any other value
|
|
53
|
+
if callable?(record)
|
|
54
|
+
record.call(root, arguments, context)
|
|
55
|
+
elsif record.equal?(nil)
|
|
56
|
+
root
|
|
57
|
+
else
|
|
58
|
+
record
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def infer_policy(policy, record, arguments, context)
|
|
63
|
+
# policy can be callable, nil (for inference) or a policy class
|
|
64
|
+
if callable?(policy)
|
|
65
|
+
policy.call(record, arguments, context)
|
|
66
|
+
elsif policy.equal?(nil)
|
|
67
|
+
infer_from = model?(record) ? record.model : record
|
|
68
|
+
infer_from = object?(record) ? record.object : infer_from
|
|
69
|
+
::Pundit::PolicyFinder.new(infer_from).policy!
|
|
70
|
+
else
|
|
71
|
+
policy
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
end
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module GraphqlPundit
|
|
4
|
+
# Common methods used for authorization and scopes
|
|
5
|
+
module Common
|
|
6
|
+
def callable?(thing)
|
|
7
|
+
thing.respond_to?(:call)
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def model?(thing)
|
|
11
|
+
thing.respond_to?(:model)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def object?(thing)
|
|
15
|
+
thing.respond_to?(:object)
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'graphql'
|
|
4
|
+
require 'pundit'
|
|
5
|
+
|
|
6
|
+
require_relative 'graphql_pundit/version'
|
|
7
|
+
require_relative 'graphql_pundit/common'
|
|
8
|
+
require_relative 'graphql_pundit/authorization_extension'
|
|
9
|
+
|
|
10
|
+
module GraphqlPundit
|
|
11
|
+
class Error < StandardError; end
|
|
12
|
+
# Your code goes here...
|
|
13
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: graphql_pundit3
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Lubomir Vnenk
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2022-07-12 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: graphql
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - ">="
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '1.8'
|
|
20
|
+
- - "<"
|
|
21
|
+
- !ruby/object:Gem::Version
|
|
22
|
+
version: '3'
|
|
23
|
+
type: :runtime
|
|
24
|
+
prerelease: false
|
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
26
|
+
requirements:
|
|
27
|
+
- - ">="
|
|
28
|
+
- !ruby/object:Gem::Version
|
|
29
|
+
version: '1.8'
|
|
30
|
+
- - "<"
|
|
31
|
+
- !ruby/object:Gem::Version
|
|
32
|
+
version: '3'
|
|
33
|
+
- !ruby/object:Gem::Dependency
|
|
34
|
+
name: pundit
|
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
|
36
|
+
requirements:
|
|
37
|
+
- - "~>"
|
|
38
|
+
- !ruby/object:Gem::Version
|
|
39
|
+
version: 2.1.0
|
|
40
|
+
type: :runtime
|
|
41
|
+
prerelease: false
|
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
43
|
+
requirements:
|
|
44
|
+
- - "~>"
|
|
45
|
+
- !ruby/object:Gem::Version
|
|
46
|
+
version: 2.1.0
|
|
47
|
+
- !ruby/object:Gem::Dependency
|
|
48
|
+
name: bundler
|
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
|
50
|
+
requirements:
|
|
51
|
+
- - "~>"
|
|
52
|
+
- !ruby/object:Gem::Version
|
|
53
|
+
version: '2.0'
|
|
54
|
+
type: :development
|
|
55
|
+
prerelease: false
|
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
57
|
+
requirements:
|
|
58
|
+
- - "~>"
|
|
59
|
+
- !ruby/object:Gem::Version
|
|
60
|
+
version: '2.0'
|
|
61
|
+
- !ruby/object:Gem::Dependency
|
|
62
|
+
name: codecov
|
|
63
|
+
requirement: !ruby/object:Gem::Requirement
|
|
64
|
+
requirements:
|
|
65
|
+
- - "~>"
|
|
66
|
+
- !ruby/object:Gem::Version
|
|
67
|
+
version: 0.1.10
|
|
68
|
+
type: :development
|
|
69
|
+
prerelease: false
|
|
70
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
71
|
+
requirements:
|
|
72
|
+
- - "~>"
|
|
73
|
+
- !ruby/object:Gem::Version
|
|
74
|
+
version: 0.1.10
|
|
75
|
+
- !ruby/object:Gem::Dependency
|
|
76
|
+
name: rake
|
|
77
|
+
requirement: !ruby/object:Gem::Requirement
|
|
78
|
+
requirements:
|
|
79
|
+
- - "~>"
|
|
80
|
+
- !ruby/object:Gem::Version
|
|
81
|
+
version: '13.0'
|
|
82
|
+
type: :development
|
|
83
|
+
prerelease: false
|
|
84
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
85
|
+
requirements:
|
|
86
|
+
- - "~>"
|
|
87
|
+
- !ruby/object:Gem::Version
|
|
88
|
+
version: '13.0'
|
|
89
|
+
- !ruby/object:Gem::Dependency
|
|
90
|
+
name: rspec
|
|
91
|
+
requirement: !ruby/object:Gem::Requirement
|
|
92
|
+
requirements:
|
|
93
|
+
- - "~>"
|
|
94
|
+
- !ruby/object:Gem::Version
|
|
95
|
+
version: '3.6'
|
|
96
|
+
type: :development
|
|
97
|
+
prerelease: false
|
|
98
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
99
|
+
requirements:
|
|
100
|
+
- - "~>"
|
|
101
|
+
- !ruby/object:Gem::Version
|
|
102
|
+
version: '3.6'
|
|
103
|
+
- !ruby/object:Gem::Dependency
|
|
104
|
+
name: rubocop
|
|
105
|
+
requirement: !ruby/object:Gem::Requirement
|
|
106
|
+
requirements:
|
|
107
|
+
- - ">="
|
|
108
|
+
- !ruby/object:Gem::Version
|
|
109
|
+
version: 0.83.0
|
|
110
|
+
type: :development
|
|
111
|
+
prerelease: false
|
|
112
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
113
|
+
requirements:
|
|
114
|
+
- - ">="
|
|
115
|
+
- !ruby/object:Gem::Version
|
|
116
|
+
version: 0.83.0
|
|
117
|
+
- !ruby/object:Gem::Dependency
|
|
118
|
+
name: simplecov
|
|
119
|
+
requirement: !ruby/object:Gem::Requirement
|
|
120
|
+
requirements:
|
|
121
|
+
- - "~>"
|
|
122
|
+
- !ruby/object:Gem::Version
|
|
123
|
+
version: 0.18.5
|
|
124
|
+
type: :development
|
|
125
|
+
prerelease: false
|
|
126
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
127
|
+
requirements:
|
|
128
|
+
- - "~>"
|
|
129
|
+
- !ruby/object:Gem::Version
|
|
130
|
+
version: 0.18.5
|
|
131
|
+
description: Pundit authorization support for new graphql interpreter
|
|
132
|
+
email:
|
|
133
|
+
- lubomir.vnenk@zoho.com
|
|
134
|
+
executables: []
|
|
135
|
+
extensions: []
|
|
136
|
+
extra_rdoc_files: []
|
|
137
|
+
files:
|
|
138
|
+
- ".codeclimate.yml"
|
|
139
|
+
- ".rspec"
|
|
140
|
+
- ".rubocop.yml"
|
|
141
|
+
- ".ruby-version"
|
|
142
|
+
- ".stickler.yml"
|
|
143
|
+
- CHANGELOG.md
|
|
144
|
+
- Gemfile
|
|
145
|
+
- Gemfile.lock
|
|
146
|
+
- LICENSE.txt
|
|
147
|
+
- README.md
|
|
148
|
+
- Rakefile
|
|
149
|
+
- graphql_pundit3.gemspec
|
|
150
|
+
- lib/graphql_pundit/authorization_extension.rb
|
|
151
|
+
- lib/graphql_pundit/common.rb
|
|
152
|
+
- lib/graphql_pundit/version.rb
|
|
153
|
+
- lib/graphql_pundit3.rb
|
|
154
|
+
- sig/graphql_pundit.rbs
|
|
155
|
+
homepage: https://github.com/lubosch/graphql_pundit
|
|
156
|
+
licenses:
|
|
157
|
+
- MIT
|
|
158
|
+
metadata:
|
|
159
|
+
homepage_uri: https://github.com/lubosch/graphql_pundit
|
|
160
|
+
source_code_uri: https://github.com/lubosch/graphql_pundit/blob/master/README.md
|
|
161
|
+
changelog_uri: https://github.com/lubosch/graphql_pundit/blob/master/CHANGELOG.md
|
|
162
|
+
rubygems_mfa_required: 'true'
|
|
163
|
+
post_install_message:
|
|
164
|
+
rdoc_options: []
|
|
165
|
+
require_paths:
|
|
166
|
+
- lib
|
|
167
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
168
|
+
requirements:
|
|
169
|
+
- - ">="
|
|
170
|
+
- !ruby/object:Gem::Version
|
|
171
|
+
version: '3.0'
|
|
172
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
173
|
+
requirements:
|
|
174
|
+
- - ">="
|
|
175
|
+
- !ruby/object:Gem::Version
|
|
176
|
+
version: '0'
|
|
177
|
+
requirements: []
|
|
178
|
+
rubygems_version: 3.3.3
|
|
179
|
+
signing_key:
|
|
180
|
+
specification_version: 4
|
|
181
|
+
summary: Pundit authorization support for new graphql interpreter
|
|
182
|
+
test_files: []
|