kitty_policy 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/.gitignore +11 -0
- data/.rspec +3 -0
- data/.rubocop.yml +67 -0
- data/.ruby-version +1 -0
- data/.travis.yml +11 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +57 -0
- data/LICENSE.txt +21 -0
- data/README.md +243 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/kitty_policy.gemspec +38 -0
- data/lib/kitty_policy.rb +6 -0
- data/lib/kitty_policy/access_denied.rb +15 -0
- data/lib/kitty_policy/dsl.rb +35 -0
- data/lib/kitty_policy/graphql/can_resolver.rb +34 -0
- data/lib/kitty_policy/graphql/field_authorization.rb +46 -0
- data/lib/kitty_policy/helper.rb +33 -0
- data/lib/kitty_policy/rspec.rb +29 -0
- data/lib/kitty_policy/version.rb +5 -0
- metadata +167 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: b8f0d1d608e02d9582d87caa5eaaa996b44330dc9ac80d5127714ab83999edb1
|
|
4
|
+
data.tar.gz: c3eae5bde27d4f36e68f27b5f9b67018df1a30b89cdf2f046bc6b15579c2db34
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: a956131b3414441be7638c127844f8f2a92c1a523e20ef19a086a48b9846d234738f6d4efd7aeaa697b441674774daa019c793bf7933fa974be435a7044cc6d6
|
|
7
|
+
data.tar.gz: d013ea28cfd0fa1796d12857d8ae33e9bb0015cef1da23d1254ac9462e8931ac12dbba33a41d54c619ea897d24a7b1f134d9db9a6dd115d4cca78bed633aa66a
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
require:
|
|
2
|
+
- rubocop-rspec
|
|
3
|
+
|
|
4
|
+
AllCops:
|
|
5
|
+
TargetRubyVersion: 2.6
|
|
6
|
+
Exclude:
|
|
7
|
+
- bin/**/*
|
|
8
|
+
- db/**/*
|
|
9
|
+
- vendor/**/*
|
|
10
|
+
- node_modules/**/*
|
|
11
|
+
|
|
12
|
+
Style/PercentLiteralDelimiters:
|
|
13
|
+
PreferredDelimiters:
|
|
14
|
+
'%i': '()'
|
|
15
|
+
'%w': '()'
|
|
16
|
+
'%r': '()'
|
|
17
|
+
|
|
18
|
+
LineLength:
|
|
19
|
+
Max: 140
|
|
20
|
+
|
|
21
|
+
Metrics/MethodLength:
|
|
22
|
+
Max: 20
|
|
23
|
+
|
|
24
|
+
Metrics/AbcSize:
|
|
25
|
+
Max: 40
|
|
26
|
+
|
|
27
|
+
Documentation:
|
|
28
|
+
Enabled: false
|
|
29
|
+
|
|
30
|
+
Style/ModuleFunction:
|
|
31
|
+
Enabled: false
|
|
32
|
+
|
|
33
|
+
Style/EachWithObject:
|
|
34
|
+
Enabled: false
|
|
35
|
+
|
|
36
|
+
Style/CollectionMethods:
|
|
37
|
+
Enabled: false
|
|
38
|
+
|
|
39
|
+
Style/ClassAndModuleChildren:
|
|
40
|
+
Enabled: false
|
|
41
|
+
|
|
42
|
+
Style/TrailingCommaInArrayLiteral:
|
|
43
|
+
EnforcedStyleForMultiline: comma
|
|
44
|
+
|
|
45
|
+
Style/TrailingCommaInHashLiteral:
|
|
46
|
+
EnforcedStyleForMultiline: comma
|
|
47
|
+
|
|
48
|
+
Style/TrailingCommaInArguments:
|
|
49
|
+
EnforcedStyleForMultiline: comma
|
|
50
|
+
|
|
51
|
+
Style/DoubleNegation:
|
|
52
|
+
Enabled: false
|
|
53
|
+
|
|
54
|
+
BlockLength:
|
|
55
|
+
Enabled: false
|
|
56
|
+
|
|
57
|
+
RSpec/MultipleExpectations:
|
|
58
|
+
Enabled: false
|
|
59
|
+
|
|
60
|
+
RSpec/ExampleLength:
|
|
61
|
+
Enabled: false
|
|
62
|
+
|
|
63
|
+
Capybara/FeatureMethods:
|
|
64
|
+
Enabled: false
|
|
65
|
+
|
|
66
|
+
Lint/AmbiguousBlockAssociation:
|
|
67
|
+
Enabled: false
|
data/.ruby-version
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
2.6.2
|
data/.travis.yml
ADDED
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
|
2
|
+
|
|
3
|
+
## Our Pledge
|
|
4
|
+
|
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
|
10
|
+
orientation.
|
|
11
|
+
|
|
12
|
+
## Our Standards
|
|
13
|
+
|
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
|
15
|
+
include:
|
|
16
|
+
|
|
17
|
+
* Using welcoming and inclusive language
|
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
|
19
|
+
* Gracefully accepting constructive criticism
|
|
20
|
+
* Focusing on what is best for the community
|
|
21
|
+
* Showing empathy towards other community members
|
|
22
|
+
|
|
23
|
+
Examples of unacceptable behavior by participants include:
|
|
24
|
+
|
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
|
26
|
+
advances
|
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
|
28
|
+
* Public or private harassment
|
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
|
30
|
+
address, without explicit permission
|
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
|
32
|
+
professional setting
|
|
33
|
+
|
|
34
|
+
## Our Responsibilities
|
|
35
|
+
|
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
|
38
|
+
response to any instances of unacceptable behavior.
|
|
39
|
+
|
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
|
44
|
+
threatening, offensive, or harmful.
|
|
45
|
+
|
|
46
|
+
## Scope
|
|
47
|
+
|
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
|
49
|
+
when an individual is representing the project or its community. Examples of
|
|
50
|
+
representing a project or community include using an official project e-mail
|
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
|
53
|
+
further defined and clarified by project maintainers.
|
|
54
|
+
|
|
55
|
+
## Enforcement
|
|
56
|
+
|
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
|
58
|
+
reported by contacting the project team at rstankov@gmail.com. All
|
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
|
63
|
+
|
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
|
66
|
+
members of the project's leadership.
|
|
67
|
+
|
|
68
|
+
## Attribution
|
|
69
|
+
|
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
|
72
|
+
|
|
73
|
+
[homepage]: http://contributor-covenant.org
|
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
PATH
|
|
2
|
+
remote: .
|
|
3
|
+
specs:
|
|
4
|
+
kitty_policy (0.1.0)
|
|
5
|
+
|
|
6
|
+
GEM
|
|
7
|
+
remote: https://rubygems.org/
|
|
8
|
+
specs:
|
|
9
|
+
ast (2.4.0)
|
|
10
|
+
diff-lcs (1.3)
|
|
11
|
+
graphql (1.9.7)
|
|
12
|
+
jaro_winkler (1.5.3)
|
|
13
|
+
parallel (1.17.0)
|
|
14
|
+
parser (2.6.3.0)
|
|
15
|
+
ast (~> 2.4.0)
|
|
16
|
+
rainbow (3.0.0)
|
|
17
|
+
rake (10.5.0)
|
|
18
|
+
rspec (3.8.0)
|
|
19
|
+
rspec-core (~> 3.8.0)
|
|
20
|
+
rspec-expectations (~> 3.8.0)
|
|
21
|
+
rspec-mocks (~> 3.8.0)
|
|
22
|
+
rspec-core (3.8.1)
|
|
23
|
+
rspec-support (~> 3.8.0)
|
|
24
|
+
rspec-expectations (3.8.4)
|
|
25
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
26
|
+
rspec-support (~> 3.8.0)
|
|
27
|
+
rspec-mocks (3.8.1)
|
|
28
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
29
|
+
rspec-support (~> 3.8.0)
|
|
30
|
+
rspec-support (3.8.2)
|
|
31
|
+
rubocop (0.72.0)
|
|
32
|
+
jaro_winkler (~> 1.5.1)
|
|
33
|
+
parallel (~> 1.10)
|
|
34
|
+
parser (>= 2.6)
|
|
35
|
+
rainbow (>= 2.2.2, < 4.0)
|
|
36
|
+
ruby-progressbar (~> 1.7)
|
|
37
|
+
unicode-display_width (>= 1.4.0, < 1.7)
|
|
38
|
+
rubocop-rspec (1.33.0)
|
|
39
|
+
rubocop (>= 0.60.0)
|
|
40
|
+
ruby-progressbar (1.10.1)
|
|
41
|
+
unicode-display_width (1.6.0)
|
|
42
|
+
|
|
43
|
+
PLATFORMS
|
|
44
|
+
ruby
|
|
45
|
+
|
|
46
|
+
DEPENDENCIES
|
|
47
|
+
bundler (~> 2.0)
|
|
48
|
+
graphql (~> 1.8)
|
|
49
|
+
kitty_policy!
|
|
50
|
+
rake (~> 10.0)
|
|
51
|
+
rspec (~> 3.8)
|
|
52
|
+
rspec-mocks (~> 3.8)
|
|
53
|
+
rubocop (= 0.72.0)
|
|
54
|
+
rubocop-rspec (= 1.33.0)
|
|
55
|
+
|
|
56
|
+
BUNDLED WITH
|
|
57
|
+
2.0.1
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2019 Radoslav Stankov
|
|
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,243 @@
|
|
|
1
|
+
# KittyPolicy
|
|
2
|
+
|
|
3
|
+
**NOT RELEASED YET**
|
|
4
|
+
|
|
5
|
+
[](https://codeclimate.com/github/producthunt/kitty-policy)
|
|
6
|
+
[](http://travis-ci.org/producthunt/kitty-policy)
|
|
7
|
+
|
|
8
|
+
Minimalistic authorization library extracted from [Product Hunt](https://www.producthunt.com/).
|
|
9
|
+
|
|
10
|
+
Features:
|
|
11
|
+
|
|
12
|
+
* small DSL for defining abilities
|
|
13
|
+
* not class initializations when performing abilities check
|
|
14
|
+
* integrations with [GraphQL gem](https://rubygems.org/gems/graphql).
|
|
15
|
+
|
|
16
|
+
## Installation
|
|
17
|
+
|
|
18
|
+
Add this line to your application's Gemfile:
|
|
19
|
+
|
|
20
|
+
```ruby
|
|
21
|
+
gem 'kitty_policy'
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
And then execute:
|
|
25
|
+
|
|
26
|
+
$ bundle
|
|
27
|
+
|
|
28
|
+
Or install it yourself as:
|
|
29
|
+
|
|
30
|
+
$ gem install kitty_policy
|
|
31
|
+
|
|
32
|
+
## Usage
|
|
33
|
+
|
|
34
|
+
### Step 1 - Define policy object
|
|
35
|
+
|
|
36
|
+
```ruby
|
|
37
|
+
module ApplicationPolicy
|
|
38
|
+
extend KittyPolicy::DSL
|
|
39
|
+
|
|
40
|
+
# generates a method named `can_moderate?`
|
|
41
|
+
# example: no subject, just action
|
|
42
|
+
can :moderate do |user|
|
|
43
|
+
user.admin?
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# generates a method named `can_start_trial?`
|
|
47
|
+
# example: `allow_guest` access
|
|
48
|
+
can :start_trial, allow_guest: true do |user, _subscription|
|
|
49
|
+
!user || user.trial_used?
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
# generates a method named `can_create_chat_room?`
|
|
53
|
+
# example: subject as symbol
|
|
54
|
+
can :create, :chat_room do |user|
|
|
55
|
+
user.admin?
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
# generates a method named `can_create_post?`
|
|
59
|
+
# example: subject as class, instance not used
|
|
60
|
+
can :create, Post do |user|
|
|
61
|
+
user.can_post?
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
# generates a method named `can_edit_post?`
|
|
65
|
+
# example: subject as class, passing subject instance
|
|
66
|
+
can :edit, Post do |user, post|
|
|
67
|
+
user.admin? || user == post.author
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
# generates a method named `can_manage_account?`
|
|
71
|
+
# example: using a private helper method
|
|
72
|
+
can :manage, Account do |user, account|
|
|
73
|
+
user.admin? || member?(user, account)
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
private
|
|
77
|
+
|
|
78
|
+
# you can extract private helper methods
|
|
79
|
+
def member?(user, account)
|
|
80
|
+
# ...
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
`can` is just a convince helper to create methods on a module:
|
|
86
|
+
|
|
87
|
+
```
|
|
88
|
+
ApplicationPolicy.can_moderate?
|
|
89
|
+
ApplicationPolicy.can_start_trial?
|
|
90
|
+
ApplicationPolicy.can_create_post?
|
|
91
|
+
ApplicationPolicy.can_edit_post?
|
|
92
|
+
ApplicationPolicy.can_manage_account?
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
### Step 2 - Use policy object
|
|
96
|
+
|
|
97
|
+
```ruby
|
|
98
|
+
# answers if user can perform certain action
|
|
99
|
+
ApplicationPolicy.can?(user, :create, Post)
|
|
100
|
+
ApplicationPolicy.can?(user, :create, Post.new)
|
|
101
|
+
ApplicationPolicy.can?(user, :create, post)
|
|
102
|
+
ApplicationPolicy.can?(user, :start_trial)
|
|
103
|
+
|
|
104
|
+
# raises `KittyPolicy::AccessDenied` when user can't perform certain action
|
|
105
|
+
ApplicationPolicy.authorize!(user, :create, Post)
|
|
106
|
+
ApplicationPolicy.authorize!(user, :create, Post.new)
|
|
107
|
+
ApplicationPolicy.authorize!(user, :create, post)
|
|
108
|
+
ApplicationPolicy.authorize!(user, :start_trial)
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
### (Optional Step) - Group policies into separate files
|
|
112
|
+
|
|
113
|
+
You can split your logic into multiple policy files:
|
|
114
|
+
|
|
115
|
+
```ruby
|
|
116
|
+
module Posts::Policy
|
|
117
|
+
extend KittyPolicy::DSL
|
|
118
|
+
|
|
119
|
+
# ... define abilities
|
|
120
|
+
end
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
Then you can group them together.
|
|
124
|
+
|
|
125
|
+
```ruby
|
|
126
|
+
module ApplicationPolicy
|
|
127
|
+
extend Posts::Policy
|
|
128
|
+
extend Ship::Policy
|
|
129
|
+
end
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
### Testing with RSpec
|
|
133
|
+
|
|
134
|
+
```ruby
|
|
135
|
+
require 'spec_helper'
|
|
136
|
+
require 'kitty_policy/rspec'
|
|
137
|
+
|
|
138
|
+
describe ApplicationPolicy do
|
|
139
|
+
include KittyPolicy::RSpec
|
|
140
|
+
|
|
141
|
+
describe 'can_moderate?' do
|
|
142
|
+
it 'returns true for admin' do
|
|
143
|
+
expect(User.new(admin: true)).to be_able_to :moderate
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
it 'returns false for everyone else' do
|
|
147
|
+
expect(User.new(admin: false)).not_to be_able_to :moderate
|
|
148
|
+
end
|
|
149
|
+
end
|
|
150
|
+
end
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
### Integration with GraphQL
|
|
154
|
+
|
|
155
|
+
#### Field level authorization
|
|
156
|
+
|
|
157
|
+
```ruby
|
|
158
|
+
# Manually import graphql plugin
|
|
159
|
+
require 'kitty_policy/graphql/field_authorization'
|
|
160
|
+
|
|
161
|
+
class ProductHuntSchema < GraphQL::Schema
|
|
162
|
+
# setup authorization per field
|
|
163
|
+
instrument :field, KittyPolicy::GraphQL::FieldAuthorization.new(
|
|
164
|
+
policy: ApplicationPolicy, # required
|
|
165
|
+
current_user_key: :current_user, # optional, default: :current_user
|
|
166
|
+
)
|
|
167
|
+
|
|
168
|
+
# ...
|
|
169
|
+
end
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
```ruby
|
|
173
|
+
module Types
|
|
174
|
+
class PostType < BaseObject
|
|
175
|
+
# Same as:
|
|
176
|
+
# if ApplicationPolicy.can?(context[:current_user], :edit, object)
|
|
177
|
+
# return metrics
|
|
178
|
+
# else
|
|
179
|
+
# return []
|
|
180
|
+
# end
|
|
181
|
+
field :metrics, [MetricType], null: false, authorize: :edit, fallback: []
|
|
182
|
+
|
|
183
|
+
# Same as:
|
|
184
|
+
# if ApplicationPolicy.can?(context[:current_user], :moderate, object)
|
|
185
|
+
# return moderation_changes_count
|
|
186
|
+
# else
|
|
187
|
+
# return 0
|
|
188
|
+
# end
|
|
189
|
+
field :moderation_changes_count, Integer, null: false, authorize: :moderate, fallback: 0
|
|
190
|
+
end
|
|
191
|
+
end
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
#### Can resolver
|
|
195
|
+
|
|
196
|
+
Exposes if current user can perform certain action.
|
|
197
|
+
|
|
198
|
+
```ruby
|
|
199
|
+
# Manually import graphql plugin
|
|
200
|
+
require 'kitty_policy/graphql/can_resolver'
|
|
201
|
+
|
|
202
|
+
module Resolvers
|
|
203
|
+
Can = KittyPolicy::GraphQL::CanResolver.new(
|
|
204
|
+
policy: ApplicationPolicy, # required
|
|
205
|
+
current_user_key: :current_user, # optional, default: :current_user
|
|
206
|
+
base_resolver: BaseResolver, # optional, default: ::GraphQL::Schema::Resolver,
|
|
207
|
+
)
|
|
208
|
+
end
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
```ruby
|
|
212
|
+
module Types
|
|
213
|
+
class PostType < BaseObject
|
|
214
|
+
# ...
|
|
215
|
+
|
|
216
|
+
field :can_edit, resolver: Resolvers::Can.perform(:edit) # -> ApplicationPolicy.can?(edit, post)
|
|
217
|
+
field :can_moderate, resolver: Resolvers::Can.perform(:moderate) { :site } # -> ApplicationPolicy.can?(:moderate, :site)
|
|
218
|
+
end
|
|
219
|
+
end
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
## Development
|
|
223
|
+
|
|
224
|
+
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.
|
|
225
|
+
|
|
226
|
+
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).
|
|
227
|
+
|
|
228
|
+
## Contributing
|
|
229
|
+
|
|
230
|
+
1. Fork it
|
|
231
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
|
232
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
|
233
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
|
234
|
+
5. Run the tests (`rake`)
|
|
235
|
+
6. Create new Pull Request
|
|
236
|
+
|
|
237
|
+
## License
|
|
238
|
+
|
|
239
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
|
240
|
+
|
|
241
|
+
## Code of Conduct
|
|
242
|
+
|
|
243
|
+
Everyone interacting in the KittyPolicy project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/producthunt/kitty-policy/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
|
|
3
|
+
require "bundler/setup"
|
|
4
|
+
require "kitty_policy"
|
|
5
|
+
|
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
|
8
|
+
|
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
|
10
|
+
# require "pry"
|
|
11
|
+
# Pry.start
|
|
12
|
+
|
|
13
|
+
require "irb"
|
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
lib = File.expand_path('lib', __dir__)
|
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
5
|
+
require 'kitty_policy/version'
|
|
6
|
+
|
|
7
|
+
Gem::Specification.new do |spec|
|
|
8
|
+
spec.name = 'kitty_policy'
|
|
9
|
+
spec.version = KittyPolicy::VERSION
|
|
10
|
+
spec.authors = ['Radoslav Stankov']
|
|
11
|
+
spec.email = ['rstankov@gmail.com']
|
|
12
|
+
|
|
13
|
+
spec.summary = 'General purpose authorization library'
|
|
14
|
+
spec.description = 'Can be used for Rails and GraphQL applications'
|
|
15
|
+
spec.homepage = 'https://github.com/producthunt/kitty-policy'
|
|
16
|
+
spec.license = 'MIT'
|
|
17
|
+
|
|
18
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
|
19
|
+
spec.metadata['source_code_uri'] = 'https://github.com/producthunt/kitty-policy'
|
|
20
|
+
spec.metadata['changelog_uri'] = 'https://github.com/producthunt/kitty-policy/blob/master/CHANGELOG.md'
|
|
21
|
+
|
|
22
|
+
# Specify which files should be added to the gem when it is released.
|
|
23
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
|
24
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
|
25
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
|
26
|
+
end
|
|
27
|
+
spec.bindir = 'exe'
|
|
28
|
+
spec.executables = spec.files.grep(%r(^exe/)) { |f| File.basename(f) }
|
|
29
|
+
spec.require_paths = ['lib']
|
|
30
|
+
|
|
31
|
+
spec.add_development_dependency 'bundler', '~> 2.0'
|
|
32
|
+
spec.add_development_dependency 'graphql', '~> 1.8'
|
|
33
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
|
34
|
+
spec.add_development_dependency 'rspec', '~> 3.8'
|
|
35
|
+
spec.add_development_dependency 'rspec-mocks', '~> 3.8'
|
|
36
|
+
spec.add_development_dependency 'rubocop', '0.72.0'
|
|
37
|
+
spec.add_development_dependency 'rubocop-rspec', '1.33.0'
|
|
38
|
+
end
|
data/lib/kitty_policy.rb
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module KittyPolicy
|
|
4
|
+
class AccessDenied < StandardError
|
|
5
|
+
attr_reader :user, :action, :subject
|
|
6
|
+
|
|
7
|
+
def initialize(user = nil, action = nil, subject = nil)
|
|
8
|
+
@user = user
|
|
9
|
+
@action = action
|
|
10
|
+
@subject = subject
|
|
11
|
+
|
|
12
|
+
super 'Not authorized'
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module KittyPolicy
|
|
4
|
+
module DSL
|
|
5
|
+
def self.extended(base)
|
|
6
|
+
base.extend(base)
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def can?(user, action, subject = :empty)
|
|
10
|
+
if subject == :empty
|
|
11
|
+
public_send Helper.method_name(action), user
|
|
12
|
+
else
|
|
13
|
+
public_send Helper.method_name(action, subject), user, subject
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def authorize!(*args)
|
|
18
|
+
raise AccessDenied.new(*args) unless can?(*args)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
private
|
|
22
|
+
|
|
23
|
+
DEFAULT_BLOCK = ->(user, *_rest) { !!user }
|
|
24
|
+
|
|
25
|
+
def can(abilities, subject = nil, allow_guest: false, &block)
|
|
26
|
+
block ||= DEFAULT_BLOCK
|
|
27
|
+
|
|
28
|
+
Array(abilities).each do |action|
|
|
29
|
+
define_method Helper.method_name(action, subject) do |*args|
|
|
30
|
+
(args[0] || allow_guest) && !!block.call(*args)
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module KittyPolicy
|
|
4
|
+
module GraphQL
|
|
5
|
+
class CanResolver
|
|
6
|
+
def initialize(policy:, base_resolver: ::GraphQL::Schema::Resolver, current_user_key: :current_user)
|
|
7
|
+
@base_resolver = base_resolver
|
|
8
|
+
@current_user_key = current_user_key
|
|
9
|
+
@policy = policy
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def perform(action, &extract_subject)
|
|
13
|
+
policy = @policy
|
|
14
|
+
current_user_key = @current_user_key
|
|
15
|
+
|
|
16
|
+
Class.new(@base_resolver) do
|
|
17
|
+
type ::GraphQL::BOOLEAN_TYPE, null: false
|
|
18
|
+
|
|
19
|
+
define_method(:resolve) do
|
|
20
|
+
policy.can?(
|
|
21
|
+
context[current_user_key],
|
|
22
|
+
action,
|
|
23
|
+
extract_subject ? extract_subject.call(object) : object,
|
|
24
|
+
)
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def field_options
|
|
30
|
+
raise "Can't use `#{self.class.name}` directly as resolver. Use `resolve: #{self.class.name}.perform(action)`"
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
if defined?(::GraphQL::Field)
|
|
4
|
+
::GraphQL::Field.accepts_definitions(
|
|
5
|
+
fallback: GraphQL::Define.assign_metadata_key(:fallback),
|
|
6
|
+
authorize: GraphQL::Define.assign_metadata_key(:authorize),
|
|
7
|
+
)
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
if defined?(::GraphQL::Schema::Field)
|
|
11
|
+
::GraphQL::Schema::Field.accepts_definition(:fallback)
|
|
12
|
+
::GraphQL::Schema::Field.accepts_definition(:authorize)
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
module KittyPolicy
|
|
16
|
+
module GraphQL
|
|
17
|
+
class FieldAuthorization
|
|
18
|
+
def initialize(policy:, current_user_key: :current_user)
|
|
19
|
+
@policy = policy
|
|
20
|
+
@current_user_key = current_user_key
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def instrument(_type, field)
|
|
24
|
+
return field unless field.metadata.key?(:authorize)
|
|
25
|
+
|
|
26
|
+
policy = @policy
|
|
27
|
+
current_user_key = @current_user_key
|
|
28
|
+
|
|
29
|
+
old_resolve = field.resolve_proc
|
|
30
|
+
new_resolve = lambda do |type_or_object, arguments, context|
|
|
31
|
+
object = type_or_object.is_a?(::GraphQL::Schema::Object) ? type_or_object.object : type_or_object
|
|
32
|
+
|
|
33
|
+
if policy.can?(context[current_user_key], field.metadata[:authorize], object)
|
|
34
|
+
old_resolve.call(type_or_object, arguments, context)
|
|
35
|
+
else
|
|
36
|
+
field.metadata[:fallback]
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
field.redefine do
|
|
41
|
+
resolve new_resolve
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module KittyPolicy
|
|
4
|
+
module Helper
|
|
5
|
+
extend self
|
|
6
|
+
|
|
7
|
+
def method_name(action, subject = nil)
|
|
8
|
+
if subject
|
|
9
|
+
"can_#{action}_#{underscore(subject_to_string(subject)).tr('/', '_')}?"
|
|
10
|
+
else
|
|
11
|
+
"can_#{action}?"
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
private
|
|
16
|
+
|
|
17
|
+
def subject_to_string(subject)
|
|
18
|
+
case subject
|
|
19
|
+
when Class, Symbol then subject.to_s
|
|
20
|
+
when String then subject.gsub(/[^\w]/, '')
|
|
21
|
+
else subject.class.to_s
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def underscore(text)
|
|
26
|
+
text.tr('::', '_')
|
|
27
|
+
.gsub(/([A-Z]+)([A-Z][a-z])/) { "#{Regexp.last_match[1]}_#{Regexp.last_match[2]}" }
|
|
28
|
+
.gsub(/([a-z\d])([A-Z])/) { "#{Regexp.last_match[1]}_#{Regexp.last_match[2]}" }
|
|
29
|
+
.tr('-', '_')
|
|
30
|
+
.downcase
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module KittyPolicy
|
|
4
|
+
module RSpec
|
|
5
|
+
class ToBeAbleTo
|
|
6
|
+
def initialize(policy, action, subject)
|
|
7
|
+
@policy = policy
|
|
8
|
+
@action = action
|
|
9
|
+
@subject = subject
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def matches?(user)
|
|
13
|
+
@policy.can?(user, @action, @subject)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def failure_message
|
|
17
|
+
"Expected user to be able to #{@action.inspect} #{@subject.inspect unless @subject == :empty}, but isn't"
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def failure_message_when_negated
|
|
21
|
+
"Expected user not to be able to #{@action.inspect} #{@subject.inspect unless @subject == :empty}, but is"
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def be_able_to(action, subject = :empty)
|
|
26
|
+
ToBeAbleTo.new(described_class, action, subject)
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: kitty_policy
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Radoslav Stankov
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2019-07-28 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: bundler
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - "~>"
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '2.0'
|
|
20
|
+
type: :development
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - "~>"
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '2.0'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: graphql
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - "~>"
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '1.8'
|
|
34
|
+
type: :development
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - "~>"
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '1.8'
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
name: rake
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - "~>"
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: '10.0'
|
|
48
|
+
type: :development
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - "~>"
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: '10.0'
|
|
55
|
+
- !ruby/object:Gem::Dependency
|
|
56
|
+
name: rspec
|
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
|
58
|
+
requirements:
|
|
59
|
+
- - "~>"
|
|
60
|
+
- !ruby/object:Gem::Version
|
|
61
|
+
version: '3.8'
|
|
62
|
+
type: :development
|
|
63
|
+
prerelease: false
|
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
65
|
+
requirements:
|
|
66
|
+
- - "~>"
|
|
67
|
+
- !ruby/object:Gem::Version
|
|
68
|
+
version: '3.8'
|
|
69
|
+
- !ruby/object:Gem::Dependency
|
|
70
|
+
name: rspec-mocks
|
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
|
72
|
+
requirements:
|
|
73
|
+
- - "~>"
|
|
74
|
+
- !ruby/object:Gem::Version
|
|
75
|
+
version: '3.8'
|
|
76
|
+
type: :development
|
|
77
|
+
prerelease: false
|
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
79
|
+
requirements:
|
|
80
|
+
- - "~>"
|
|
81
|
+
- !ruby/object:Gem::Version
|
|
82
|
+
version: '3.8'
|
|
83
|
+
- !ruby/object:Gem::Dependency
|
|
84
|
+
name: rubocop
|
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
|
86
|
+
requirements:
|
|
87
|
+
- - '='
|
|
88
|
+
- !ruby/object:Gem::Version
|
|
89
|
+
version: 0.72.0
|
|
90
|
+
type: :development
|
|
91
|
+
prerelease: false
|
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
93
|
+
requirements:
|
|
94
|
+
- - '='
|
|
95
|
+
- !ruby/object:Gem::Version
|
|
96
|
+
version: 0.72.0
|
|
97
|
+
- !ruby/object:Gem::Dependency
|
|
98
|
+
name: rubocop-rspec
|
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
|
100
|
+
requirements:
|
|
101
|
+
- - '='
|
|
102
|
+
- !ruby/object:Gem::Version
|
|
103
|
+
version: 1.33.0
|
|
104
|
+
type: :development
|
|
105
|
+
prerelease: false
|
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
107
|
+
requirements:
|
|
108
|
+
- - '='
|
|
109
|
+
- !ruby/object:Gem::Version
|
|
110
|
+
version: 1.33.0
|
|
111
|
+
description: Can be used for Rails and GraphQL applications
|
|
112
|
+
email:
|
|
113
|
+
- rstankov@gmail.com
|
|
114
|
+
executables: []
|
|
115
|
+
extensions: []
|
|
116
|
+
extra_rdoc_files: []
|
|
117
|
+
files:
|
|
118
|
+
- ".gitignore"
|
|
119
|
+
- ".rspec"
|
|
120
|
+
- ".rubocop.yml"
|
|
121
|
+
- ".ruby-version"
|
|
122
|
+
- ".travis.yml"
|
|
123
|
+
- CHANGELOG.md
|
|
124
|
+
- CODE_OF_CONDUCT.md
|
|
125
|
+
- Gemfile
|
|
126
|
+
- Gemfile.lock
|
|
127
|
+
- LICENSE.txt
|
|
128
|
+
- README.md
|
|
129
|
+
- Rakefile
|
|
130
|
+
- bin/console
|
|
131
|
+
- bin/setup
|
|
132
|
+
- kitty_policy.gemspec
|
|
133
|
+
- lib/kitty_policy.rb
|
|
134
|
+
- lib/kitty_policy/access_denied.rb
|
|
135
|
+
- lib/kitty_policy/dsl.rb
|
|
136
|
+
- lib/kitty_policy/graphql/can_resolver.rb
|
|
137
|
+
- lib/kitty_policy/graphql/field_authorization.rb
|
|
138
|
+
- lib/kitty_policy/helper.rb
|
|
139
|
+
- lib/kitty_policy/rspec.rb
|
|
140
|
+
- lib/kitty_policy/version.rb
|
|
141
|
+
homepage: https://github.com/producthunt/kitty-policy
|
|
142
|
+
licenses:
|
|
143
|
+
- MIT
|
|
144
|
+
metadata:
|
|
145
|
+
homepage_uri: https://github.com/producthunt/kitty-policy
|
|
146
|
+
source_code_uri: https://github.com/producthunt/kitty-policy
|
|
147
|
+
changelog_uri: https://github.com/producthunt/kitty-policy/blob/master/CHANGELOG.md
|
|
148
|
+
post_install_message:
|
|
149
|
+
rdoc_options: []
|
|
150
|
+
require_paths:
|
|
151
|
+
- lib
|
|
152
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
153
|
+
requirements:
|
|
154
|
+
- - ">="
|
|
155
|
+
- !ruby/object:Gem::Version
|
|
156
|
+
version: '0'
|
|
157
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
158
|
+
requirements:
|
|
159
|
+
- - ">="
|
|
160
|
+
- !ruby/object:Gem::Version
|
|
161
|
+
version: '0'
|
|
162
|
+
requirements: []
|
|
163
|
+
rubygems_version: 3.0.3
|
|
164
|
+
signing_key:
|
|
165
|
+
specification_version: 4
|
|
166
|
+
summary: General purpose authorization library
|
|
167
|
+
test_files: []
|