consent 0.6.0 → 2.0.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 +4 -4
- data/.gitignore +14 -5
- data/.rubocop.yml +9 -0
- data/.rubocop_todo.yml +9 -11
- data/Gemfile +5 -1
- data/Rakefile +9 -3
- data/app/models/concerns/consent/authorizable.rb +94 -0
- data/app/models/consent/application_record.rb +7 -0
- data/app/models/consent/history.rb +20 -0
- data/app/models/consent/permission.rb +71 -0
- data/config.ru +9 -0
- data/consent.gemspec +24 -18
- data/db/migrate/20211104225614_create_nitro_auth_authorization_permissions.rb +19 -0
- data/db/migrate/20220420135558_create_nitro_auth_authorization_histories.rb +15 -0
- data/doc/dependency_decisions.yml +3 -0
- data/docs/CHANGELOG.md +23 -0
- data/docs/README.md +355 -0
- data/lib/consent/ability.rb +148 -6
- data/lib/consent/action.rb +8 -5
- data/lib/consent/dsl.rb +3 -1
- data/lib/consent/{railtie.rb → engine.rb} +11 -8
- data/lib/consent/model_additions.rb +64 -0
- data/lib/consent/permission_migration.rb +139 -0
- data/lib/consent/reloader.rb +6 -5
- data/lib/consent/rspec/consent_action.rb +65 -0
- data/lib/consent/rspec/consent_view.rb +76 -0
- data/lib/consent/rspec.rb +13 -73
- data/lib/consent/subject.rb +0 -9
- data/lib/consent/subject_coder.rb +29 -0
- data/lib/consent/symbol_adapter.rb +18 -0
- data/lib/consent/version.rb +1 -1
- data/lib/consent.rb +34 -32
- data/lib/generators/consent/permissions_generator.rb +5 -5
- data/mkdocs.yml +5 -0
- data/renovate.json +18 -0
- metadata +113 -33
- data/.rspec +0 -2
- data/.ruby-version +0 -1
- data/.travis.yml +0 -18
- data/LICENSE +0 -21
- data/README.md +0 -245
- data/TODO.md +0 -1
- data/lib/consent/permission.rb +0 -35
- data/lib/consent/permissions.rb +0 -41
metadata
CHANGED
|
@@ -1,71 +1,113 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: consent
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 2.0.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Carlos Palhares
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date:
|
|
11
|
+
date: 2022-09-02 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
|
-
name:
|
|
14
|
+
name: cancancan
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - '='
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: 3.2.1
|
|
20
|
+
type: :runtime
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - '='
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: 3.2.1
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: activerecord
|
|
15
29
|
requirement: !ruby/object:Gem::Requirement
|
|
16
30
|
requirements:
|
|
17
31
|
- - ">="
|
|
18
32
|
- !ruby/object:Gem::Version
|
|
19
|
-
version:
|
|
33
|
+
version: '5'
|
|
20
34
|
type: :development
|
|
21
35
|
prerelease: false
|
|
22
36
|
version_requirements: !ruby/object:Gem::Requirement
|
|
23
37
|
requirements:
|
|
24
38
|
- - ">="
|
|
25
39
|
- !ruby/object:Gem::Version
|
|
26
|
-
version:
|
|
40
|
+
version: '5'
|
|
27
41
|
- !ruby/object:Gem::Dependency
|
|
28
42
|
name: bundler
|
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|
|
30
44
|
requirements:
|
|
31
|
-
- - "
|
|
45
|
+
- - "~>"
|
|
32
46
|
- !ruby/object:Gem::Version
|
|
33
|
-
version: 1
|
|
47
|
+
version: '2.1'
|
|
34
48
|
type: :development
|
|
35
49
|
prerelease: false
|
|
36
50
|
version_requirements: !ruby/object:Gem::Requirement
|
|
37
51
|
requirements:
|
|
38
|
-
- - "
|
|
52
|
+
- - "~>"
|
|
39
53
|
- !ruby/object:Gem::Version
|
|
40
|
-
version: 1
|
|
54
|
+
version: '2.1'
|
|
41
55
|
- !ruby/object:Gem::Dependency
|
|
42
|
-
name:
|
|
56
|
+
name: combustion
|
|
43
57
|
requirement: !ruby/object:Gem::Requirement
|
|
44
58
|
requirements:
|
|
45
59
|
- - "~>"
|
|
46
60
|
- !ruby/object:Gem::Version
|
|
47
|
-
version: 1.
|
|
61
|
+
version: '1.3'
|
|
48
62
|
type: :development
|
|
49
63
|
prerelease: false
|
|
50
64
|
version_requirements: !ruby/object:Gem::Requirement
|
|
51
65
|
requirements:
|
|
52
66
|
- - "~>"
|
|
53
67
|
- !ruby/object:Gem::Version
|
|
54
|
-
version: 1.
|
|
68
|
+
version: '1.3'
|
|
69
|
+
- !ruby/object:Gem::Dependency
|
|
70
|
+
name: license_finder
|
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
|
72
|
+
requirements:
|
|
73
|
+
- - ">="
|
|
74
|
+
- !ruby/object:Gem::Version
|
|
75
|
+
version: '7.0'
|
|
76
|
+
type: :development
|
|
77
|
+
prerelease: false
|
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
79
|
+
requirements:
|
|
80
|
+
- - ">="
|
|
81
|
+
- !ruby/object:Gem::Version
|
|
82
|
+
version: '7.0'
|
|
83
|
+
- !ruby/object:Gem::Dependency
|
|
84
|
+
name: pry-byebug
|
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
|
86
|
+
requirements:
|
|
87
|
+
- - '='
|
|
88
|
+
- !ruby/object:Gem::Version
|
|
89
|
+
version: 3.9.0
|
|
90
|
+
type: :development
|
|
91
|
+
prerelease: false
|
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
93
|
+
requirements:
|
|
94
|
+
- - '='
|
|
95
|
+
- !ruby/object:Gem::Version
|
|
96
|
+
version: 3.9.0
|
|
55
97
|
- !ruby/object:Gem::Dependency
|
|
56
98
|
name: rake
|
|
57
99
|
requirement: !ruby/object:Gem::Requirement
|
|
58
100
|
requirements:
|
|
59
101
|
- - "~>"
|
|
60
102
|
- !ruby/object:Gem::Version
|
|
61
|
-
version: '
|
|
103
|
+
version: '13'
|
|
62
104
|
type: :development
|
|
63
105
|
prerelease: false
|
|
64
106
|
version_requirements: !ruby/object:Gem::Requirement
|
|
65
107
|
requirements:
|
|
66
108
|
- - "~>"
|
|
67
109
|
- !ruby/object:Gem::Version
|
|
68
|
-
version: '
|
|
110
|
+
version: '13'
|
|
69
111
|
- !ruby/object:Gem::Dependency
|
|
70
112
|
name: rspec
|
|
71
113
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -81,20 +123,48 @@ dependencies:
|
|
|
81
123
|
- !ruby/object:Gem::Version
|
|
82
124
|
version: '3.0'
|
|
83
125
|
- !ruby/object:Gem::Dependency
|
|
84
|
-
name:
|
|
126
|
+
name: rspec-rails
|
|
85
127
|
requirement: !ruby/object:Gem::Requirement
|
|
86
128
|
requirements:
|
|
87
129
|
- - "~>"
|
|
88
130
|
- !ruby/object:Gem::Version
|
|
89
|
-
version:
|
|
131
|
+
version: 5.1.2
|
|
90
132
|
type: :development
|
|
91
133
|
prerelease: false
|
|
92
134
|
version_requirements: !ruby/object:Gem::Requirement
|
|
93
135
|
requirements:
|
|
94
136
|
- - "~>"
|
|
95
137
|
- !ruby/object:Gem::Version
|
|
96
|
-
version:
|
|
97
|
-
|
|
138
|
+
version: 5.1.2
|
|
139
|
+
- !ruby/object:Gem::Dependency
|
|
140
|
+
name: rubocop-powerhome
|
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
|
142
|
+
requirements:
|
|
143
|
+
- - '='
|
|
144
|
+
- !ruby/object:Gem::Version
|
|
145
|
+
version: 0.5.0
|
|
146
|
+
type: :development
|
|
147
|
+
prerelease: false
|
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
149
|
+
requirements:
|
|
150
|
+
- - '='
|
|
151
|
+
- !ruby/object:Gem::Version
|
|
152
|
+
version: 0.5.0
|
|
153
|
+
- !ruby/object:Gem::Dependency
|
|
154
|
+
name: sqlite3
|
|
155
|
+
requirement: !ruby/object:Gem::Requirement
|
|
156
|
+
requirements:
|
|
157
|
+
- - "~>"
|
|
158
|
+
- !ruby/object:Gem::Version
|
|
159
|
+
version: 1.4.2
|
|
160
|
+
type: :development
|
|
161
|
+
prerelease: false
|
|
162
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
163
|
+
requirements:
|
|
164
|
+
- - "~>"
|
|
165
|
+
- !ruby/object:Gem::Version
|
|
166
|
+
version: 1.4.2
|
|
167
|
+
description: Consent permission based authorization
|
|
98
168
|
email:
|
|
99
169
|
- chjunior@gmail.com
|
|
100
170
|
executables: []
|
|
@@ -102,38 +172,49 @@ extensions: []
|
|
|
102
172
|
extra_rdoc_files: []
|
|
103
173
|
files:
|
|
104
174
|
- ".gitignore"
|
|
105
|
-
- ".rspec"
|
|
106
175
|
- ".rubocop.yml"
|
|
107
176
|
- ".rubocop_todo.yml"
|
|
108
|
-
- ".ruby-version"
|
|
109
|
-
- ".travis.yml"
|
|
110
177
|
- Gemfile
|
|
111
|
-
- LICENSE
|
|
112
|
-
- README.md
|
|
113
178
|
- Rakefile
|
|
114
|
-
-
|
|
179
|
+
- app/models/concerns/consent/authorizable.rb
|
|
180
|
+
- app/models/consent/application_record.rb
|
|
181
|
+
- app/models/consent/history.rb
|
|
182
|
+
- app/models/consent/permission.rb
|
|
115
183
|
- bin/console
|
|
116
184
|
- bin/setup
|
|
185
|
+
- config.ru
|
|
117
186
|
- consent.gemspec
|
|
187
|
+
- db/migrate/20211104225614_create_nitro_auth_authorization_permissions.rb
|
|
188
|
+
- db/migrate/20220420135558_create_nitro_auth_authorization_histories.rb
|
|
189
|
+
- doc/dependency_decisions.yml
|
|
190
|
+
- docs/CHANGELOG.md
|
|
191
|
+
- docs/README.md
|
|
118
192
|
- lib/consent.rb
|
|
119
193
|
- lib/consent/ability.rb
|
|
120
194
|
- lib/consent/action.rb
|
|
121
195
|
- lib/consent/dsl.rb
|
|
122
|
-
- lib/consent/
|
|
123
|
-
- lib/consent/
|
|
124
|
-
- lib/consent/
|
|
196
|
+
- lib/consent/engine.rb
|
|
197
|
+
- lib/consent/model_additions.rb
|
|
198
|
+
- lib/consent/permission_migration.rb
|
|
125
199
|
- lib/consent/reloader.rb
|
|
126
200
|
- lib/consent/rspec.rb
|
|
201
|
+
- lib/consent/rspec/consent_action.rb
|
|
202
|
+
- lib/consent/rspec/consent_view.rb
|
|
127
203
|
- lib/consent/subject.rb
|
|
204
|
+
- lib/consent/subject_coder.rb
|
|
205
|
+
- lib/consent/symbol_adapter.rb
|
|
128
206
|
- lib/consent/version.rb
|
|
129
207
|
- lib/consent/view.rb
|
|
130
208
|
- lib/generators/consent/permissions_generator.rb
|
|
131
209
|
- lib/generators/consent/templates/permissions.rb.erb
|
|
132
210
|
- lib/generators/consent/templates/permissions_spec.rb.erb
|
|
133
|
-
|
|
211
|
+
- mkdocs.yml
|
|
212
|
+
- renovate.json
|
|
213
|
+
homepage: https://github.com/powerhome/power-tools
|
|
134
214
|
licenses:
|
|
135
215
|
- MIT
|
|
136
|
-
metadata:
|
|
216
|
+
metadata:
|
|
217
|
+
rubygems_mfa_required: 'true'
|
|
137
218
|
post_install_message:
|
|
138
219
|
rdoc_options: []
|
|
139
220
|
require_paths:
|
|
@@ -142,16 +223,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
142
223
|
requirements:
|
|
143
224
|
- - ">="
|
|
144
225
|
- !ruby/object:Gem::Version
|
|
145
|
-
version: '
|
|
226
|
+
version: '2.7'
|
|
146
227
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
147
228
|
requirements:
|
|
148
229
|
- - ">="
|
|
149
230
|
- !ruby/object:Gem::Version
|
|
150
231
|
version: '0'
|
|
151
232
|
requirements: []
|
|
152
|
-
|
|
153
|
-
rubygems_version: 2.7.3
|
|
233
|
+
rubygems_version: 3.3.7
|
|
154
234
|
signing_key:
|
|
155
235
|
specification_version: 4
|
|
156
|
-
summary: Consent
|
|
236
|
+
summary: Consent permission based authorization
|
|
157
237
|
test_files: []
|
data/.rspec
DELETED
data/.ruby-version
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
2.5.0
|
data/.travis.yml
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
sudo: false
|
|
2
|
-
language: ruby
|
|
3
|
-
rvm:
|
|
4
|
-
- 2.2.2
|
|
5
|
-
- 2.5.0
|
|
6
|
-
before_install: gem install bundler -v 1.17.3
|
|
7
|
-
script:
|
|
8
|
-
- bundle exec rubocop
|
|
9
|
-
- bundle exec rspec
|
|
10
|
-
deploy:
|
|
11
|
-
provider: rubygems
|
|
12
|
-
api_key:
|
|
13
|
-
secure: aCipdg6IqKaxQmWQXFlqI2JqGOrCAxzYlutOn6nCSk9VCzmlRjDoEp1M99ASvmF5CP3KQgfRq8bxfLql8jss8tymK0u2ExvKaUglK0zb2KoLQDGDGuHw3RxCTnaxPBuO4/2PuS+nso6IcdqUaGOzh8FA7ePKPBmxl3kyNtArEDV88Eyx6tQYs1/1w153bAeBk57nTiu8CPS+dIWg+guQADTLPZ61fH4xKjPWXnr65pFF7YtU5YdPBHFkqL60X384OZj1c7ZTGvFj70+36oo617HigSg8HCE+E0R1N4JQ8/5xo2yHBdrtgsAqYyYWRgm1C6qu+T6yKMuokdoSd0Ji3Rigz6VRrOJfjmQQkwMnno9fCY3vND62zOb9Ow+MCNuQYSgNHc+YPURKmT05wadG0FpZdmo6hstPusleuG89NxnB/s2YjR0QWQK05MW1uhFdru1S2gBd1fMH1GLoThhdfGG1UJSkKOipUJyRupFaB9zimJO3HwaTP0Q+wP8MGZDAqbyzQ3bJSZaQmJC6loC2EtxxnOQxUIxaySLtNEU2LOr9IijExj1ldXkz8X1fIWwRr6BPnIeChFnuST+L9UDYtqk1WgQTUs+tOAxUjepAWhtIUL5h3Jg2NZd9RxPXq8IngDWZJwT8yA3E9wloQx5JzTIMas5wHwCQJuwjjk5ibss=
|
|
14
|
-
gem: consent
|
|
15
|
-
on:
|
|
16
|
-
tags: true
|
|
17
|
-
repo: powerhome/consent
|
|
18
|
-
ruby: 2.5.0
|
data/LICENSE
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright Power Home Remodeling Group, LLC
|
|
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 all
|
|
13
|
-
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 THE
|
|
21
|
-
SOFTWARE.
|
data/README.md
DELETED
|
@@ -1,245 +0,0 @@
|
|
|
1
|
-
# Consent [](https://travis-ci.org/powerhome/consent)
|
|
2
|
-
|
|
3
|
-
## Installation
|
|
4
|
-
|
|
5
|
-
Add this line to your application's Gemfile:
|
|
6
|
-
|
|
7
|
-
```ruby
|
|
8
|
-
gem 'consent'
|
|
9
|
-
```
|
|
10
|
-
|
|
11
|
-
And then execute:
|
|
12
|
-
|
|
13
|
-
$ bundle
|
|
14
|
-
|
|
15
|
-
Or install it yourself as:
|
|
16
|
-
|
|
17
|
-
$ gem install consent
|
|
18
|
-
|
|
19
|
-
## What is Consent
|
|
20
|
-
|
|
21
|
-
Consent makes defining permissions easier by providing a clean, concise DSL for authorization so that all abilities do not have to be in your `Ability`
|
|
22
|
-
class.
|
|
23
|
-
|
|
24
|
-
Consent takes application permissions and models them so that permissions are organized and can be defined granularly. It does so using the
|
|
25
|
-
following models:
|
|
26
|
-
|
|
27
|
-
* View: A collection of objects limited by a given condition.
|
|
28
|
-
* Action: An action performed on top of the objects limited by the view. For example, one user could only `:view` something, while another could `:manage` it.
|
|
29
|
-
* Subject: Holds the scope of the actions.
|
|
30
|
-
* Permission: What is given to the user. Combines a subject, an action and
|
|
31
|
-
a view.
|
|
32
|
-
|
|
33
|
-
## What Consent Is Not
|
|
34
|
-
|
|
35
|
-
Consent isn't a tool to enforce permissions -- it is intended to be used with CanCanCan and is only to make permissions more easily readable and definable.
|
|
36
|
-
|
|
37
|
-
## Subject
|
|
38
|
-
|
|
39
|
-
The subject is the central point of a group of actions and views. It will typically
|
|
40
|
-
be an `ActiveRecord` class, a `:symbol`, or any Plain Old Ruby Object.
|
|
41
|
-
|
|
42
|
-
You define a subject with the following DSL:
|
|
43
|
-
|
|
44
|
-
```ruby
|
|
45
|
-
Consent.define Project, 'Our Projects' do
|
|
46
|
-
#in this case, Project is the subject
|
|
47
|
-
# and `Our Projects` is the description that makes it clear to users
|
|
48
|
-
# what the subject is acting upon.
|
|
49
|
-
…
|
|
50
|
-
end
|
|
51
|
-
```
|
|
52
|
-
|
|
53
|
-
The scope is the action that's being performed on the subject. It can be anything, but will typically be an ActiveRecord class, a `:symbol`, or a PORO.
|
|
54
|
-
|
|
55
|
-
For instance:
|
|
56
|
-
|
|
57
|
-
```ruby
|
|
58
|
-
Consent.define :features, 'Beta Features' do
|
|
59
|
-
# whatever you put inside this method defines the scope
|
|
60
|
-
end
|
|
61
|
-
```
|
|
62
|
-
|
|
63
|
-
## Views
|
|
64
|
-
|
|
65
|
-
Views are the rules that limit the access to actions. For instance,
|
|
66
|
-
a user may see a `Project` from his department, but not from others. That rule
|
|
67
|
-
could be enforced with a `:department` view, defined like this:
|
|
68
|
-
|
|
69
|
-
### Hash Conditions
|
|
70
|
-
|
|
71
|
-
This is probably the most commonly used and is useful, for example,
|
|
72
|
-
when the view can be defined using a where condition in an ActiveRecord context.
|
|
73
|
-
It follows a match condition and will return all objects that meet the criteria
|
|
74
|
-
and is based off a boolean:
|
|
75
|
-
|
|
76
|
-
```ruby
|
|
77
|
-
Consent.define Project, 'Projects' do
|
|
78
|
-
view :department, "User's department only" do |user|
|
|
79
|
-
{ department_id: user.id }
|
|
80
|
-
end
|
|
81
|
-
end
|
|
82
|
-
```
|
|
83
|
-
|
|
84
|
-
Although hash conditions (matching object's attributes) are recommended,
|
|
85
|
-
the constraints can be anything you want. Since Consent does not enforce the
|
|
86
|
-
rules, those rules are directly given to CanCan. Following [CanCan rules](https://github.com/CanCanCommunity/cancancan/wiki/Defining-Abilities%3A-Best-Practice)
|
|
87
|
-
for defining abilities is recommended.
|
|
88
|
-
|
|
89
|
-
### Object Conditions
|
|
90
|
-
|
|
91
|
-
If you're not matching for equal values, then you would need to use an object
|
|
92
|
-
condition, which matches data based off a range.
|
|
93
|
-
|
|
94
|
-
If you already have an object and want to check to see whether the user has
|
|
95
|
-
permission to view that specific object, you would use object conditions.
|
|
96
|
-
|
|
97
|
-
If your needs can't be satisfied by hash conditions, it is recommended that a
|
|
98
|
-
second condition is given for constraining object instances. For example, if you
|
|
99
|
-
want to restrict a view for smaller volume projects:
|
|
100
|
-
|
|
101
|
-
```ruby
|
|
102
|
-
Consent.define Project, 'Projects' do
|
|
103
|
-
view :small_volumes, "User's department only",
|
|
104
|
-
-> (user) {
|
|
105
|
-
['amount < ?', user.volume_limit]
|
|
106
|
-
end,
|
|
107
|
-
-> (user, project) {
|
|
108
|
-
project.amount < user.volume_limit
|
|
109
|
-
}
|
|
110
|
-
end
|
|
111
|
-
```
|
|
112
|
-
|
|
113
|
-
For object conditions, the latter argument will be the referred object, while the
|
|
114
|
-
former will be the context given to the [Permission](#permission) (also check
|
|
115
|
-
[CanCan integration](#cancan-integration)).
|
|
116
|
-
|
|
117
|
-
## Action
|
|
118
|
-
|
|
119
|
-
An action is anything you can perform on a given subject. In the example of
|
|
120
|
-
Features this would look like the following using Consent's DSL:
|
|
121
|
-
|
|
122
|
-
```ruby
|
|
123
|
-
Consent.define :features, 'Beta Features' do
|
|
124
|
-
action :beta_chat, 'Beta Chat App'
|
|
125
|
-
end
|
|
126
|
-
```
|
|
127
|
-
|
|
128
|
-
To associate different views to the same action:
|
|
129
|
-
|
|
130
|
-
```ruby
|
|
131
|
-
Consent.define Project, 'Projects' do
|
|
132
|
-
# returns conditions that can be used as a matcher for objects so the matcher
|
|
133
|
-
# can return true or false (hash version)
|
|
134
|
-
view :department, "User's department only" do |user|
|
|
135
|
-
{ department_id: user.id }
|
|
136
|
-
end
|
|
137
|
-
view :future_projects, "User's department only",
|
|
138
|
-
# returns a condition to be applied to a collection of objects
|
|
139
|
-
-> (_) {
|
|
140
|
-
['starts_at > ?', Date.today]
|
|
141
|
-
end,
|
|
142
|
-
# returns true/false based on a condition -- to use this, you must pass in
|
|
143
|
-
# an instance of an object in order to check the permission
|
|
144
|
-
-> (user, project) {
|
|
145
|
-
project.starts_at > Date.today
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
action :read, 'Read projects', views: [:department, :future_projects]
|
|
149
|
-
end
|
|
150
|
-
```
|
|
151
|
-
|
|
152
|
-
If you have a set of actions with the same set of views, you can use a
|
|
153
|
-
`with_defaults` block to simplify the writing:
|
|
154
|
-
|
|
155
|
-
```ruby
|
|
156
|
-
with_defaults views: [:department, :small_volumes] do
|
|
157
|
-
action :read, 'Read projects'
|
|
158
|
-
action :approve, 'Approve projects'
|
|
159
|
-
end
|
|
160
|
-
```
|
|
161
|
-
|
|
162
|
-
## Permission
|
|
163
|
-
|
|
164
|
-
A permission is what is consented to the user. It is the *permission* to perform
|
|
165
|
-
an *action* on a limited *view* of the *subject*. It marries the three concepts
|
|
166
|
-
to consent an access to the user.
|
|
167
|
-
|
|
168
|
-
A permission is not specified by the user, it is calculated from a permissions
|
|
169
|
-
hash owned by a `User`, or a `Role` on an application.
|
|
170
|
-
|
|
171
|
-
The permissions hash looks like the following:
|
|
172
|
-
|
|
173
|
-
```ruby
|
|
174
|
-
{
|
|
175
|
-
project: {
|
|
176
|
-
read: 'department',
|
|
177
|
-
approve: 'small_volumes'
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
```
|
|
181
|
-
|
|
182
|
-
In other words:
|
|
183
|
-
|
|
184
|
-
```ruby
|
|
185
|
-
{
|
|
186
|
-
<subject>: {
|
|
187
|
-
<action>: <view>
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
```
|
|
191
|
-
|
|
192
|
-
### Full Access
|
|
193
|
-
|
|
194
|
-
Full (unrestricted by views) access is granted when view is `'1'`, `true` or
|
|
195
|
-
`'true'`. For instance:
|
|
196
|
-
|
|
197
|
-
In other words:
|
|
198
|
-
|
|
199
|
-
```ruby
|
|
200
|
-
{
|
|
201
|
-
projects: {
|
|
202
|
-
approve: true
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
```
|
|
206
|
-
|
|
207
|
-
## CanCan Integration
|
|
208
|
-
|
|
209
|
-
Consent provides a CanCan ability (Consent::Ability) to integrate your
|
|
210
|
-
permissions with frameworks like Rails. To use it with Rails check out the
|
|
211
|
-
example at [Ability for Other Users](https://github.com/CanCanCommunity/cancancan/wiki/Ability-for-Other-Users)
|
|
212
|
-
on CanCanCan's wiki.
|
|
213
|
-
|
|
214
|
-
In the ability you define the scope of the permissions. This is typically an
|
|
215
|
-
user:
|
|
216
|
-
|
|
217
|
-
```ruby
|
|
218
|
-
Consent::Ability.new(user.permissions, user)
|
|
219
|
-
```
|
|
220
|
-
|
|
221
|
-
The first parameter given to the ability is the permissions hash, seen at
|
|
222
|
-
[Permission](#permission). The following parameters are the permission context.
|
|
223
|
-
These parameters are given directly to the condition blocks defined by the views
|
|
224
|
-
in the exact same order, so it's up to you to define what your context is.
|
|
225
|
-
|
|
226
|
-
## Rails Integration
|
|
227
|
-
|
|
228
|
-
Consent is integrated into Rails with `Consent::Railtie`. To define where
|
|
229
|
-
your permission files will be, use `config.consent.path`. This defaults to
|
|
230
|
-
`app/permissions/` to conform to Rails' standards.
|
|
231
|
-
|
|
232
|
-
## Development
|
|
233
|
-
|
|
234
|
-
After checking out the repo, run `bin/setup` to install dependencies. Then, run
|
|
235
|
-
`rake spec` to run the tests. You can also run `bin/console` for an interactive
|
|
236
|
-
prompt that will allow you to experiment.
|
|
237
|
-
|
|
238
|
-
To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
239
|
-
release a new version, update the version number in `version.rb`, and then run
|
|
240
|
-
`bundle exec rake release`, which will create a git tag for the version, push
|
|
241
|
-
git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
|
242
|
-
|
|
243
|
-
## Contributing
|
|
244
|
-
|
|
245
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/powerhome/consent.
|
data/TODO.md
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
* DSL validate Consent state
|
data/lib/consent/permission.rb
DELETED
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
module Consent
|
|
4
|
-
class Permission # :nodoc:
|
|
5
|
-
def initialize(subject, action, view = nil)
|
|
6
|
-
@subject = subject
|
|
7
|
-
@action = action
|
|
8
|
-
@view = view
|
|
9
|
-
end
|
|
10
|
-
|
|
11
|
-
def subject_key
|
|
12
|
-
@subject.key
|
|
13
|
-
end
|
|
14
|
-
|
|
15
|
-
def action_key
|
|
16
|
-
@action.key
|
|
17
|
-
end
|
|
18
|
-
|
|
19
|
-
# Disables Sytle/SafeNavigation to keep this code
|
|
20
|
-
# compatible with ruby < 2.3
|
|
21
|
-
# rubocop:disable Style/SafeNavigation
|
|
22
|
-
def view_key
|
|
23
|
-
@view && @view.key
|
|
24
|
-
end
|
|
25
|
-
|
|
26
|
-
def conditions(*args)
|
|
27
|
-
@view && @view.conditions(*args)
|
|
28
|
-
end
|
|
29
|
-
|
|
30
|
-
def object_conditions(*args)
|
|
31
|
-
@view && @view.object_conditions(*args)
|
|
32
|
-
end
|
|
33
|
-
# rubocop:enable Style/SafeNavigation
|
|
34
|
-
end
|
|
35
|
-
end
|
data/lib/consent/permissions.rb
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
module Consent
|
|
4
|
-
class Permissions # :nodoc:
|
|
5
|
-
include Enumerable
|
|
6
|
-
|
|
7
|
-
def initialize(permissions)
|
|
8
|
-
@permissions = permissions
|
|
9
|
-
end
|
|
10
|
-
|
|
11
|
-
def each(&block)
|
|
12
|
-
Consent.subjects.each do |subject|
|
|
13
|
-
subject.actions.map do |action|
|
|
14
|
-
map_permission subject, action
|
|
15
|
-
end.compact.each(&block)
|
|
16
|
-
end
|
|
17
|
-
end
|
|
18
|
-
|
|
19
|
-
private
|
|
20
|
-
|
|
21
|
-
def map_permission(subject, action)
|
|
22
|
-
subject_key = subject.permission_key
|
|
23
|
-
actions = @permissions[subject_key] || @permissions[subject_key.to_s]
|
|
24
|
-
view = actions && (actions[action.key] || actions[action.key.to_s])
|
|
25
|
-
full(subject, action, view) || partial(subject, action, view)
|
|
26
|
-
end
|
|
27
|
-
|
|
28
|
-
def full(subject, action, view_key)
|
|
29
|
-
return unless Consent::FULL_ACCESS.include?(view_key.to_s.strip)
|
|
30
|
-
|
|
31
|
-
Permission.new(subject, action)
|
|
32
|
-
end
|
|
33
|
-
|
|
34
|
-
def partial(subject, action, view_key)
|
|
35
|
-
view = subject.view_for(action, view_key.to_s.to_sym)
|
|
36
|
-
return if view.nil?
|
|
37
|
-
|
|
38
|
-
Permission.new(subject, action, view)
|
|
39
|
-
end
|
|
40
|
-
end
|
|
41
|
-
end
|