consent 1.0.1 → 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 -6
- 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 -20
- 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 +113 -4
- data/lib/consent/dsl.rb +1 -0
- 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 +7 -7
- data/lib/consent/rspec/consent_view.rb +8 -11
- data/lib/consent/rspec.rb +3 -3
- 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 +25 -13
- data/lib/generators/consent/permissions_generator.rb +5 -5
- data/mkdocs.yml +5 -0
- data/renovate.json +15 -2
- metadata +86 -34
- data/.rspec +0 -2
- data/.ruby-version +0 -1
- data/.travis.yml +0 -20
- data/LICENSE +0 -21
- data/README.md +0 -252
- data/TODO.md +0 -1
metadata
CHANGED
@@ -1,15 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: consent
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
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
|
+
- !ruby/object:Gem::Dependency
|
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
|
13
27
|
- !ruby/object:Gem::Dependency
|
14
28
|
name: activerecord
|
15
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -28,58 +42,72 @@ dependencies:
|
|
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'
|
55
69
|
- !ruby/object:Gem::Dependency
|
56
|
-
name:
|
70
|
+
name: license_finder
|
57
71
|
requirement: !ruby/object:Gem::Requirement
|
58
72
|
requirements:
|
59
|
-
- - "
|
73
|
+
- - ">="
|
60
74
|
- !ruby/object:Gem::Version
|
61
|
-
version: 0
|
75
|
+
version: '7.0'
|
62
76
|
type: :development
|
63
77
|
prerelease: false
|
64
78
|
version_requirements: !ruby/object:Gem::Requirement
|
65
79
|
requirements:
|
66
|
-
- - "
|
80
|
+
- - ">="
|
67
81
|
- !ruby/object:Gem::Version
|
68
|
-
version: 0
|
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
|
69
97
|
- !ruby/object:Gem::Dependency
|
70
98
|
name: rake
|
71
99
|
requirement: !ruby/object:Gem::Requirement
|
72
100
|
requirements:
|
73
|
-
- - "
|
101
|
+
- - "~>"
|
74
102
|
- !ruby/object:Gem::Version
|
75
|
-
version:
|
103
|
+
version: '13'
|
76
104
|
type: :development
|
77
105
|
prerelease: false
|
78
106
|
version_requirements: !ruby/object:Gem::Requirement
|
79
107
|
requirements:
|
80
|
-
- - "
|
108
|
+
- - "~>"
|
81
109
|
- !ruby/object:Gem::Version
|
82
|
-
version:
|
110
|
+
version: '13'
|
83
111
|
- !ruby/object:Gem::Dependency
|
84
112
|
name: rspec
|
85
113
|
requirement: !ruby/object:Gem::Requirement
|
@@ -95,19 +123,33 @@ dependencies:
|
|
95
123
|
- !ruby/object:Gem::Version
|
96
124
|
version: '3.0'
|
97
125
|
- !ruby/object:Gem::Dependency
|
98
|
-
name:
|
126
|
+
name: rspec-rails
|
99
127
|
requirement: !ruby/object:Gem::Requirement
|
100
128
|
requirements:
|
101
129
|
- - "~>"
|
102
130
|
- !ruby/object:Gem::Version
|
103
|
-
version:
|
131
|
+
version: 5.1.2
|
104
132
|
type: :development
|
105
133
|
prerelease: false
|
106
134
|
version_requirements: !ruby/object:Gem::Requirement
|
107
135
|
requirements:
|
108
136
|
- - "~>"
|
109
137
|
- !ruby/object:Gem::Version
|
110
|
-
version:
|
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
|
111
153
|
- !ruby/object:Gem::Dependency
|
112
154
|
name: sqlite3
|
113
155
|
requirement: !ruby/object:Gem::Requirement
|
@@ -122,7 +164,7 @@ dependencies:
|
|
122
164
|
- - "~>"
|
123
165
|
- !ruby/object:Gem::Version
|
124
166
|
version: 1.4.2
|
125
|
-
description: Consent
|
167
|
+
description: Consent permission based authorization
|
126
168
|
email:
|
127
169
|
- chjunior@gmail.com
|
128
170
|
executables: []
|
@@ -130,39 +172,49 @@ extensions: []
|
|
130
172
|
extra_rdoc_files: []
|
131
173
|
files:
|
132
174
|
- ".gitignore"
|
133
|
-
- ".rspec"
|
134
175
|
- ".rubocop.yml"
|
135
176
|
- ".rubocop_todo.yml"
|
136
|
-
- ".ruby-version"
|
137
|
-
- ".travis.yml"
|
138
177
|
- Gemfile
|
139
|
-
- LICENSE
|
140
|
-
- README.md
|
141
178
|
- Rakefile
|
142
|
-
-
|
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
|
143
183
|
- bin/console
|
144
184
|
- bin/setup
|
185
|
+
- config.ru
|
145
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
|
146
192
|
- lib/consent.rb
|
147
193
|
- lib/consent/ability.rb
|
148
194
|
- lib/consent/action.rb
|
149
195
|
- lib/consent/dsl.rb
|
150
|
-
- lib/consent/
|
196
|
+
- lib/consent/engine.rb
|
197
|
+
- lib/consent/model_additions.rb
|
198
|
+
- lib/consent/permission_migration.rb
|
151
199
|
- lib/consent/reloader.rb
|
152
200
|
- lib/consent/rspec.rb
|
153
201
|
- lib/consent/rspec/consent_action.rb
|
154
202
|
- lib/consent/rspec/consent_view.rb
|
155
203
|
- lib/consent/subject.rb
|
204
|
+
- lib/consent/subject_coder.rb
|
205
|
+
- lib/consent/symbol_adapter.rb
|
156
206
|
- lib/consent/version.rb
|
157
207
|
- lib/consent/view.rb
|
158
208
|
- lib/generators/consent/permissions_generator.rb
|
159
209
|
- lib/generators/consent/templates/permissions.rb.erb
|
160
210
|
- lib/generators/consent/templates/permissions_spec.rb.erb
|
211
|
+
- mkdocs.yml
|
161
212
|
- renovate.json
|
162
|
-
homepage:
|
213
|
+
homepage: https://github.com/powerhome/power-tools
|
163
214
|
licenses:
|
164
215
|
- MIT
|
165
|
-
metadata:
|
216
|
+
metadata:
|
217
|
+
rubygems_mfa_required: 'true'
|
166
218
|
post_install_message:
|
167
219
|
rdoc_options: []
|
168
220
|
require_paths:
|
@@ -171,15 +223,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
171
223
|
requirements:
|
172
224
|
- - ">="
|
173
225
|
- !ruby/object:Gem::Version
|
174
|
-
version: '
|
226
|
+
version: '2.7'
|
175
227
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
176
228
|
requirements:
|
177
229
|
- - ">="
|
178
230
|
- !ruby/object:Gem::Version
|
179
231
|
version: '0'
|
180
232
|
requirements: []
|
181
|
-
rubygems_version: 3.
|
233
|
+
rubygems_version: 3.3.7
|
182
234
|
signing_key:
|
183
235
|
specification_version: 4
|
184
|
-
summary: Consent
|
236
|
+
summary: Consent permission based authorization
|
185
237
|
test_files: []
|
data/.rspec
DELETED
data/.ruby-version
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
2.5.0
|
data/.travis.yml
DELETED
@@ -1,20 +0,0 @@
|
|
1
|
-
sudo: false
|
2
|
-
language: ruby
|
3
|
-
rvm:
|
4
|
-
- 2.5.8
|
5
|
-
- 2.6.6
|
6
|
-
- 2.7.2
|
7
|
-
- 3.0.0
|
8
|
-
before_install: gem install bundler -v 1.17.3
|
9
|
-
script:
|
10
|
-
- bundle exec rubocop
|
11
|
-
- bundle exec rspec
|
12
|
-
deploy:
|
13
|
-
provider: rubygems
|
14
|
-
api_key:
|
15
|
-
secure: aCipdg6IqKaxQmWQXFlqI2JqGOrCAxzYlutOn6nCSk9VCzmlRjDoEp1M99ASvmF5CP3KQgfRq8bxfLql8jss8tymK0u2ExvKaUglK0zb2KoLQDGDGuHw3RxCTnaxPBuO4/2PuS+nso6IcdqUaGOzh8FA7ePKPBmxl3kyNtArEDV88Eyx6tQYs1/1w153bAeBk57nTiu8CPS+dIWg+guQADTLPZ61fH4xKjPWXnr65pFF7YtU5YdPBHFkqL60X384OZj1c7ZTGvFj70+36oo617HigSg8HCE+E0R1N4JQ8/5xo2yHBdrtgsAqYyYWRgm1C6qu+T6yKMuokdoSd0Ji3Rigz6VRrOJfjmQQkwMnno9fCY3vND62zOb9Ow+MCNuQYSgNHc+YPURKmT05wadG0FpZdmo6hstPusleuG89NxnB/s2YjR0QWQK05MW1uhFdru1S2gBd1fMH1GLoThhdfGG1UJSkKOipUJyRupFaB9zimJO3HwaTP0Q+wP8MGZDAqbyzQ3bJSZaQmJC6loC2EtxxnOQxUIxaySLtNEU2LOr9IijExj1ldXkz8X1fIWwRr6BPnIeChFnuST+L9UDYtqk1WgQTUs+tOAxUjepAWhtIUL5h3Jg2NZd9RxPXq8IngDWZJwT8yA3E9wloQx5JzTIMas5wHwCQJuwjjk5ibss=
|
16
|
-
gem: consent
|
17
|
-
on:
|
18
|
-
tags: true
|
19
|
-
repo: powerhome/consent
|
20
|
-
ruby: 2.6.6
|
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,252 +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
|
22
|
-
so that all abilities do not have to be in your `Ability`
|
23
|
-
class.
|
24
|
-
|
25
|
-
Consent takes application permissions and models them so that permissions are organized and can
|
26
|
-
be defined granularly. It does so using the following models:
|
27
|
-
|
28
|
-
* View: A collection of objects limited by a given condition.
|
29
|
-
* 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.
|
30
|
-
* Subject: Holds the scope of the actions.
|
31
|
-
* Permission: The combination of a subject, an action, and a view (or full-access).
|
32
|
-
|
33
|
-
## What Consent Is Not
|
34
|
-
|
35
|
-
Consent isn't a tool to enforce permissions -- it supports CanCan(Can) for that goal.
|
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
|
54
|
-
typically be an ActiveRecord class, a `:symbol`, or a PORO.
|
55
|
-
|
56
|
-
For instance:
|
57
|
-
|
58
|
-
```ruby
|
59
|
-
Consent.define :features, 'Beta Features' do
|
60
|
-
# whatever you put inside this method defines the scope
|
61
|
-
end
|
62
|
-
```
|
63
|
-
|
64
|
-
## Views
|
65
|
-
|
66
|
-
Views are the rules that limit access to actions. For instance, a user may see a `Project`
|
67
|
-
from his department, but not from others. You can enforce it with a `:department` view,
|
68
|
-
as in the examples below:
|
69
|
-
|
70
|
-
### Hash Conditions
|
71
|
-
|
72
|
-
Probably the most commonly used. When the view can be defined using a `where` scope in
|
73
|
-
an ActiveRecord context. It follows a match condition and will return all objects that meet
|
74
|
-
the criteria:
|
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, the constraints can
|
85
|
-
be anything you want. Since Consent does not enforce the rules, those rules are directly given
|
86
|
-
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 condition.
|
92
|
-
|
93
|
-
If you already have an object and want to check to see whether the user has permission to view
|
94
|
-
that specific object, you would use object conditions.
|
95
|
-
|
96
|
-
If your needs can't be satisfied by hash conditions, it is recommended that a second condition
|
97
|
-
is given for constraining object instances. For example, if you want to restrict a view for smaller
|
98
|
-
volume projects:
|
99
|
-
|
100
|
-
```ruby
|
101
|
-
Consent.define Project, 'Projects' do
|
102
|
-
view :small_volumes, "User's department only",
|
103
|
-
-> (user) {
|
104
|
-
['amount < ?', user.volume_limit]
|
105
|
-
end,
|
106
|
-
-> (user, project) {
|
107
|
-
project.amount < user.volume_limit
|
108
|
-
}
|
109
|
-
end
|
110
|
-
```
|
111
|
-
|
112
|
-
For object conditions, the latter argument will be the referred object, while the
|
113
|
-
first will be the context given to the [Permission](#permission) (also check
|
114
|
-
[CanCan integration](#cancan-integration)).
|
115
|
-
|
116
|
-
## Action
|
117
|
-
|
118
|
-
An action is anything you can perform on a given subject. In the example of
|
119
|
-
Features this would look like the following using Consent's DSL:
|
120
|
-
|
121
|
-
```ruby
|
122
|
-
Consent.define :features, 'Beta Features' do
|
123
|
-
action :beta_chat, 'Beta Chat App'
|
124
|
-
end
|
125
|
-
```
|
126
|
-
|
127
|
-
To associate different views to the same action:
|
128
|
-
|
129
|
-
```ruby
|
130
|
-
Consent.define Project, 'Projects' do
|
131
|
-
# returns conditions that can be used as a matcher for objects so the matcher
|
132
|
-
# can return true or false (hash version)
|
133
|
-
view :department, "User's department only" do |user|
|
134
|
-
{ department_id: user.id }
|
135
|
-
end
|
136
|
-
view :future_projects, "User's department only",
|
137
|
-
# returns a condition to be applied to a collection of objects
|
138
|
-
-> (_) {
|
139
|
-
['starts_at > ?', Date.today]
|
140
|
-
end,
|
141
|
-
# returns true/false based on a condition -- to use this, you must pass in
|
142
|
-
# an instance of an object in order to check the permission
|
143
|
-
-> (user, project) {
|
144
|
-
project.starts_at > Date.today
|
145
|
-
}
|
146
|
-
|
147
|
-
action :read, 'Read projects', views: [:department, :future_projects]
|
148
|
-
end
|
149
|
-
```
|
150
|
-
|
151
|
-
If you have a set of actions with the same set of views, you can use a
|
152
|
-
`with_defaults` block to simplify the writing:
|
153
|
-
|
154
|
-
```ruby
|
155
|
-
with_defaults views: [:department, :small_volumes] do
|
156
|
-
action :read, 'Read projects'
|
157
|
-
action :approve, 'Approve projects'
|
158
|
-
end
|
159
|
-
```
|
160
|
-
|
161
|
-
## Permission
|
162
|
-
|
163
|
-
A permission is what is consented to the user. It consentment to perform
|
164
|
-
an *action* on a limited *view* of the *subject*. It marries the three concepts
|
165
|
-
to consent an access to the user.
|
166
|
-
|
167
|
-
## CanCan Integration
|
168
|
-
|
169
|
-
Consent provides a CanCan ability (Consent::Ability) to integrate your
|
170
|
-
permissions with frameworks like Rails. To use it with Rails check out the
|
171
|
-
example at [Ability for Other Users](https://github.com/CanCanCommunity/cancancan/wiki/Ability-for-Other-Users)
|
172
|
-
on CanCanCan's wiki.
|
173
|
-
|
174
|
-
In the ability you define the scope of the permissions. This is typically a
|
175
|
-
user:
|
176
|
-
|
177
|
-
```ruby
|
178
|
-
Consent::Ability.new(user)
|
179
|
-
```
|
180
|
-
|
181
|
-
You'd more commonly define a subclass of `Consent::Ability`, and consent access
|
182
|
-
to the user by calling `consent`:
|
183
|
-
|
184
|
-
```ruby
|
185
|
-
class MyAbility < Consent::Ability
|
186
|
-
def initialize(user)
|
187
|
-
super user
|
188
|
-
|
189
|
-
consent :read, Project, :department
|
190
|
-
end
|
191
|
-
end
|
192
|
-
```
|
193
|
-
|
194
|
-
You can also consent full access by not specifying the view:
|
195
|
-
|
196
|
-
```ruby
|
197
|
-
consent :read, Project
|
198
|
-
```
|
199
|
-
|
200
|
-
If you have a somehow manageable permission, you can consent them in batch in your ability:
|
201
|
-
|
202
|
-
```ruby
|
203
|
-
class MyAbility < Consent::Ability
|
204
|
-
def initialize(user)
|
205
|
-
super user
|
206
|
-
|
207
|
-
user.permissions.each do |permission|
|
208
|
-
consent permission.action, permission.subject, permission.view
|
209
|
-
end
|
210
|
-
end
|
211
|
-
end
|
212
|
-
```
|
213
|
-
|
214
|
-
Consenting the same permission multiple times is handled as a Union by CanCanCan:
|
215
|
-
|
216
|
-
```ruby
|
217
|
-
class MyAbility < Consent::Ability
|
218
|
-
def initialize(user)
|
219
|
-
super user
|
220
|
-
|
221
|
-
consent :read, Project, :department
|
222
|
-
consent :read, Project, :future_projects
|
223
|
-
end
|
224
|
-
end
|
225
|
-
|
226
|
-
user = User.new(department_id: 13)
|
227
|
-
ability = MyAbility.new(user)
|
228
|
-
|
229
|
-
Project.accessible_by(ability, :read).to_sql
|
230
|
-
=> SELECT * FROM projects WHERE ((department_id = 13) OR (starts_at > '2021-04-06'))
|
231
|
-
```
|
232
|
-
|
233
|
-
## Rails Integration
|
234
|
-
|
235
|
-
Consent is integrated into Rails with `Consent::Railtie`. To define where
|
236
|
-
your permission files will be, use `config.consent.path`. This defaults to
|
237
|
-
`#{Rails.root}/app/permissions/` to conform to Rails' standards.
|
238
|
-
|
239
|
-
## Development
|
240
|
-
|
241
|
-
After checking out the repo, run `bin/setup` to install dependencies. Then, run
|
242
|
-
`rake spec` to run the tests. You can also run `bin/console` for an interactive
|
243
|
-
prompt that will allow you to experiment.
|
244
|
-
|
245
|
-
To install this gem onto your local machine, run `bundle exec rake install`. To
|
246
|
-
release a new version, update the version number in `version.rb`, and then run
|
247
|
-
`bundle exec rake release`, which will create a git tag for the version, push
|
248
|
-
git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
249
|
-
|
250
|
-
## Contributing
|
251
|
-
|
252
|
-
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
|