role_core 0.0.14 → 0.0.15
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/README.md +110 -59
- data/lib/role_core/concerns/models/role.rb +1 -1
- data/lib/role_core/permission_set.rb +3 -3
- data/lib/role_core/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 293e6cf06bd28270f0d19f2d20f57ea8387849067b324fcfea3fb627cb11c841
|
4
|
+
data.tar.gz: d1e32d432228cc32598e0309474179d7eaea3fc9734be71fc5f3717dd27b5ebb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8c6d27326c402fe05183bd06840c84621093905efb98beafc9969c1d8847e9268f26e990ffaec62436ae9bc8316d10afe4080dac7eda9abe6e374de6edc1042e
|
7
|
+
data.tar.gz: 74385a5cce46f0202ea498916fa7471added4ad6185b13f19d2dcd7eac1d2810c9d159a0f535bf691d80fe6c5ffdc0dfcd06d59260e582c48c824396c346a4aa
|
data/README.md
CHANGED
@@ -3,12 +3,93 @@ RoleCore
|
|
3
3
|
|
4
4
|
RoleCore is a Rails engine which could provide essential industry of Role-based access control.
|
5
5
|
|
6
|
-
|
6
|
+
## Demo
|
7
|
+
|
8
|
+
The dummy app shows a simple multiple roles with CanCanCan integration including a management UI.
|
9
|
+
|
10
|
+
<img width="550" alt="RoleCore dummy preview" src="https://user-images.githubusercontent.com/5518/37262401-e6c9d604-25dd-11e8-849d-7f7d923d5f18.png">
|
11
|
+
|
12
|
+
Clone the repository.
|
13
|
+
|
14
|
+
```sh
|
15
|
+
$ git clone https://github.com/rails-engine/role_core.git
|
16
|
+
```
|
17
|
+
|
18
|
+
Change directory
|
19
|
+
|
20
|
+
```sh
|
21
|
+
$ cd role_core
|
22
|
+
```
|
23
|
+
|
24
|
+
Run bundler
|
25
|
+
|
26
|
+
```sh
|
27
|
+
$ bundle install
|
28
|
+
```
|
7
29
|
|
8
|
-
|
30
|
+
Preparing database
|
9
31
|
|
10
|
-
|
11
|
-
|
32
|
+
```sh
|
33
|
+
$ bin/rails db:migrate
|
34
|
+
```
|
35
|
+
|
36
|
+
Start the Rails server
|
37
|
+
|
38
|
+
```sh
|
39
|
+
$ bin/rails s
|
40
|
+
```
|
41
|
+
|
42
|
+
Open your browser, and visit `http://localhost:3000`
|
43
|
+
|
44
|
+
## What's does the RoleCore do
|
45
|
+
|
46
|
+
### The role model
|
47
|
+
|
48
|
+
The essence of RBAC is the role, despite your application, there are many possibilities: single-role, multi-roles, extendable-role and the role may associate to different kinds of resources (e.g: users and groups)
|
49
|
+
|
50
|
+
RoleCore provides a essential definition of Role,
|
51
|
+
you have to add association to adapt to your application,
|
52
|
+
for example:
|
53
|
+
|
54
|
+
- single-role: adding `one-to-many` association between Role and User
|
55
|
+
- multi-roles: adding `many-to-many` association between Role and User
|
56
|
+
- extendable-role: adding a self-association to Role
|
57
|
+
- polymorphic-asscociated-role: consider using polymorphic association technique
|
58
|
+
|
59
|
+
Although it's not out-of-box, but it will give you fully flexibility to suit your needs.
|
60
|
+
|
61
|
+
### Permissions definition
|
62
|
+
|
63
|
+
RoleCore provides a DSL (which inspired by [Redmine](https://github.com/redmine/redmine/blob/master/lib/redmine.rb#L76-L186)) that allows you define permissions for your application.
|
64
|
+
|
65
|
+
Empowered by virtual model technique,
|
66
|
+
these permissions your defined can be persisted through serialization,
|
67
|
+
and can be used with OO-style, for example: `role.permissions.project.create?`
|
68
|
+
|
69
|
+
There also support permission groups, and groups support nesting.
|
70
|
+
|
71
|
+
I18n is supported too.
|
72
|
+
|
73
|
+
In fact, the essence of permissions is Hash, keys are permissions, and values are booleans. so computing of permissions with many roles, can be understood as computing of Hashes.
|
74
|
+
|
75
|
+
### Management UI
|
76
|
+
|
77
|
+
Building a management UI is difficult,
|
78
|
+
but virtual model technique will translates permissions to a virtual model's (a class that conforms to ActiveModel) attributes,
|
79
|
+
and groups will translates to nested virtual models,
|
80
|
+
that means you can use all Rails view helpers including the mighty form builder,
|
81
|
+
and can benefit to Strong Parameter.
|
82
|
+
|
83
|
+
The dummy app shows that rendering a permission list [only about 20 lines](https://github.com/rails-engine/role_core/blob/master/test/dummy/app/views/roles/_permissions.html.erb).
|
84
|
+
|
85
|
+
If your application is API-only, you can simply dumping the role's permissions to JSON, and can still be benefit to StrongParameter.
|
86
|
+
|
87
|
+
### Checking permission
|
88
|
+
|
89
|
+
RoleCore **DOES NOT** handle the authentication or authorization directly,
|
90
|
+
you have to integrate with CanCanCan, Pundit or other solutions by yourself.
|
91
|
+
|
92
|
+
RoleCore can be working with CanCanCan, Pundit easily and happily.
|
12
93
|
|
13
94
|
## Installation
|
14
95
|
|
@@ -48,7 +129,7 @@ Run model generator
|
|
48
129
|
$ bin/rails g role_core:model
|
49
130
|
```
|
50
131
|
|
51
|
-
## Getting
|
132
|
+
## Getting start
|
52
133
|
|
53
134
|
### Define permissions
|
54
135
|
|
@@ -57,11 +138,15 @@ checking it to know how to define permissions.
|
|
57
138
|
|
58
139
|
In addition, there also includes a directive about how to integrate with CanCanCan.
|
59
140
|
|
60
|
-
|
141
|
+
#### I18n
|
61
142
|
|
62
|
-
|
143
|
+
Check `config/locales/role_core.en.yml`
|
63
144
|
|
64
|
-
|
145
|
+
### Hook application
|
146
|
+
|
147
|
+
In order to obtain maximum customizability, you need to hooking up role(s) to your user model by yourself.
|
148
|
+
|
149
|
+
#### User who has single role
|
65
150
|
|
66
151
|
##### Create `one-to-many` relationship between Role and User
|
67
152
|
|
@@ -77,7 +162,7 @@ Then do migrate
|
|
77
162
|
$ bin/rails db:migrate
|
78
163
|
```
|
79
164
|
|
80
|
-
Declare `a User belongs to a Role`
|
165
|
+
Declare `a User belongs to a Role` association
|
81
166
|
|
82
167
|
```ruby
|
83
168
|
class User < ApplicationRecord
|
@@ -87,7 +172,7 @@ class User < ApplicationRecord
|
|
87
172
|
end
|
88
173
|
```
|
89
174
|
|
90
|
-
Declare `a Role has many Users`
|
175
|
+
Declare `a Role has many Users` association
|
91
176
|
|
92
177
|
```ruby
|
93
178
|
class Role < RoleCore::Role
|
@@ -95,9 +180,9 @@ class Role < RoleCore::Role
|
|
95
180
|
end
|
96
181
|
```
|
97
182
|
|
98
|
-
#####
|
183
|
+
##### Checking permission
|
99
184
|
|
100
|
-
|
185
|
+
Permissions you've defined will translate to a virtual model (a Class which implemented ActiveModel interface),
|
101
186
|
`permission` would be an attribute, `group` would be a nested virtual model (like ActiveRecord's `has_one` association).
|
102
187
|
|
103
188
|
So you can simply check permission like:
|
@@ -128,7 +213,7 @@ user.permissions.project.read?
|
|
128
213
|
|
129
214
|
_Keep in mind: fetching `role` will made a SQL query, you may need eager loading to avoid N+1 problem in some cases._
|
130
215
|
|
131
|
-
####
|
216
|
+
#### User who has multiple roles
|
132
217
|
|
133
218
|
##### Create `many-to-many` relationship between Role and User
|
134
219
|
|
@@ -144,7 +229,7 @@ Then do migrate
|
|
144
229
|
$ bin/rails db:migrate
|
145
230
|
```
|
146
231
|
|
147
|
-
Declare `a User has many Roles through RoleAssignments`
|
232
|
+
Declare `a User has many Roles through RoleAssignments` association
|
148
233
|
|
149
234
|
```ruby
|
150
235
|
class User < ApplicationRecord
|
@@ -155,7 +240,7 @@ class User < ApplicationRecord
|
|
155
240
|
end
|
156
241
|
```
|
157
242
|
|
158
|
-
Declare `a Role has many Users through RoleAssignments`
|
243
|
+
Declare `a Role has many Users through RoleAssignments` association
|
159
244
|
|
160
245
|
```ruby
|
161
246
|
class Role < RoleCore::Role
|
@@ -166,7 +251,7 @@ end
|
|
166
251
|
|
167
252
|
##### Check permission
|
168
253
|
|
169
|
-
|
254
|
+
Permissions you've defined will translate to a virtual model (a Class which implemented ActiveModel interface),
|
170
255
|
`permission` would be an attribute, `group` would be a nested virtual model (like ActiveRecord's `has_one` association).
|
171
256
|
|
172
257
|
So you can simply check permission like:
|
@@ -211,63 +296,27 @@ Open your User model:
|
|
211
296
|
Add a delegate to User model:
|
212
297
|
|
213
298
|
```ruby
|
214
|
-
delegate :
|
299
|
+
delegate :computed_permissions, to: :role
|
215
300
|
```
|
216
301
|
|
217
302
|
- For a user who has multiple roles:
|
218
303
|
|
219
|
-
Add a `
|
304
|
+
Add a `computed_permissions` public method to User model:
|
220
305
|
|
221
306
|
```ruby
|
222
|
-
def
|
223
|
-
roles.map(&:
|
307
|
+
def computed_permissions
|
308
|
+
roles.map(&:computed_permissions).reduce(RoleCore::ComputedPermissions.new, &:concat)
|
224
309
|
end
|
225
310
|
```
|
226
311
|
|
227
|
-
Open `app/models/ability.rb`, add `user.
|
312
|
+
Open `app/models/ability.rb`, add `user.computed_permissions.call(self, user)` to `initialize` method.
|
228
313
|
|
229
|
-
You can check
|
314
|
+
You can check dummy app for better understanding.
|
230
315
|
|
231
316
|
### Management UI
|
232
317
|
|
233
318
|
See [RolesController in dummy app](https://github.com/rails-engine/role_core/blob/master/test/dummy/app/controllers/roles_controller.rb)
|
234
|
-
and relates [view](https://github.com/rails-engine/role_core/blob/master/test/dummy/app/views/roles/_form.html.erb).
|
235
|
-
|
236
|
-
## Demo
|
237
|
-
|
238
|
-
The dummy app shows a simple multiple roles with CanCanCan integration includes management UI.
|
239
|
-
|
240
|
-
Clone the repository.
|
241
|
-
|
242
|
-
```sh
|
243
|
-
$ git clone https://github.com/rails-engine/role_core.git
|
244
|
-
```
|
245
|
-
|
246
|
-
Change directory
|
247
|
-
|
248
|
-
```sh
|
249
|
-
$ cd role_core
|
250
|
-
```
|
251
|
-
|
252
|
-
Run bundler
|
253
|
-
|
254
|
-
```sh
|
255
|
-
$ bundle install
|
256
|
-
```
|
257
|
-
|
258
|
-
Preparing database
|
259
|
-
|
260
|
-
```sh
|
261
|
-
$ bin/rails db:migrate
|
262
|
-
```
|
263
|
-
|
264
|
-
Start the Rails server
|
265
|
-
|
266
|
-
```sh
|
267
|
-
$ bin/rails s
|
268
|
-
```
|
269
|
-
|
270
|
-
Open your browser, and visit `http://localhost:3000`
|
319
|
+
and relates [view](https://github.com/rails-engine/role_core/blob/master/test/dummy/app/views/roles/_form.html.erb) for details.
|
271
320
|
|
272
321
|
## Contributing
|
273
322
|
|
@@ -286,3 +335,5 @@ Please write unit test with your code if necessary.
|
|
286
335
|
## License
|
287
336
|
|
288
337
|
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
338
|
+
|
339
|
+
|
@@ -6,10 +6,10 @@ module RoleCore
|
|
6
6
|
attributes.select { |_, v| v }.keys
|
7
7
|
end
|
8
8
|
|
9
|
-
def
|
9
|
+
def computed_permissions(include_nesting: true)
|
10
10
|
permissions = self.class.registered_permissions.slice(*permitted_permission_names).values
|
11
|
-
if
|
12
|
-
permissions.concat nested_attributes.values.map(&:
|
11
|
+
if include_nesting && nested_attributes.any?
|
12
|
+
permissions.concat nested_attributes.values.map(&:computed_permissions).flatten!
|
13
13
|
end
|
14
14
|
|
15
15
|
ComputedPermissions.new(permissions)
|
data/lib/role_core/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: role_core
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.15
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- jasl
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-04-
|
11
|
+
date: 2018-04-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|