the_role 2.0.1 → 2.0.2
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.
- data/README.md +215 -172
- data/app/assets/stylesheets/the_role/bootstrap_sass.css.scss +20 -0
- data/app/controllers/admin/role_sections_controller.rb +24 -19
- data/lib/generators/the_role/USAGE +10 -0
- data/lib/generators/the_role/templates/role.rb +3 -0
- data/lib/generators/the_role/templates/the_role.rb +2 -1
- data/lib/generators/the_role/the_role_generator.rb +38 -0
- data/lib/the_role/version.rb +1 -1
- data/the_role.gemspec +2 -2
- metadata +6 -5
- data/app/views/layouts/the_role.html.haml +0 -36
data/README.md
CHANGED
@@ -1,9 +1,11 @@
|
|
1
1
|
## TheRole - Authorization Gem for Ruby on Rails with administrative interface.
|
2
2
|
|
3
|
-
[
|
3
|
+
[](http://badge.fury.io/rb/the_role) | [](https://travis-ci.org/the-teacher/the_role) | [](https://codeclimate.com/github/the-teacher/the_role) | [ruby-toolbox](https://www.ruby-toolbox.com/categories/rails_authorization)
|
4
4
|
|
5
5
|
### Semantic, Flexible, Lightweight
|
6
6
|
|
7
|
+
### INTRO
|
8
|
+
|
7
9
|
<table>
|
8
10
|
<tr>
|
9
11
|
<th align="left">Bye bye CanCan, I got The Role!</th>
|
@@ -15,53 +17,75 @@
|
|
15
17
|
</tr>
|
16
18
|
</table>
|
17
19
|
|
18
|
-
###
|
20
|
+
### GUI
|
19
21
|
|
20
|
-
|
22
|
+
<table>
|
23
|
+
<tr>
|
24
|
+
<td>TheRole management web interface => localhost:3000/admin/roles</td>
|
25
|
+
</tr>
|
26
|
+
<tr>
|
27
|
+
<td><img src="https://github.com/the-teacher/the_role/raw/master/pic.png" alt="TheRole"></td>
|
28
|
+
</tr>
|
29
|
+
</table>
|
21
30
|
|
22
|
-
|
31
|
+
puts following yields into your layout:
|
23
32
|
|
33
|
+
```ruby
|
34
|
+
= yield :role_sidebar
|
35
|
+
= yield :role_main
|
24
36
|
```
|
25
|
-
gem "the_role", "~> 2.0.0"
|
26
|
-
```
|
27
|
-
|
28
|
-
**Rails 3**
|
29
37
|
|
30
|
-
|
38
|
+
### Rails 4 version
|
31
39
|
|
32
40
|
```
|
33
|
-
gem
|
41
|
+
gem 'the_role', '~> 2.0.0'
|
34
42
|
```
|
35
43
|
|
36
|
-
|
44
|
+
[Rails 3 version](http://rubygems.org/gems/the_role/versions/1.7.0) (not recommended for use)
|
37
45
|
|
38
|
-
|
46
|
+
### Instalation
|
39
47
|
|
40
|
-
|
48
|
+
* [INSTALL](#install)
|
49
|
+
* [INTEGRATION](#integration)
|
50
|
+
* [Assets and Bootstrap](#assets-and-bootstrap)
|
51
|
+
* [Configuration (optional)](#configuration)
|
41
52
|
|
42
|
-
|
53
|
+
### Understanding
|
43
54
|
|
44
|
-
|
55
|
+
* [TheRole instead CanCan?](#therole-instead-cancan)
|
56
|
+
* [What does it mean semantic?](#what-does-it-mean-semantic)
|
57
|
+
* [Virtual sections and rules](#virtual-sections-and-rules)
|
58
|
+
* [Using with Views](#using-with-views)
|
59
|
+
* [Who is Administrator?](#who-is-administrator)
|
60
|
+
* [Who is Moderator?](#who-is-moderator)
|
61
|
+
* [Who is Owner?](#who-is-owner)
|
45
62
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
<tr>
|
51
|
-
<td><img src="https://github.com/the-teacher/the_role/raw/master/pic.png" alt="TheRole"></td>
|
52
|
-
</tr>
|
53
|
-
</table>
|
63
|
+
### API
|
64
|
+
|
65
|
+
* [User](#user)
|
66
|
+
* [Role](#role)
|
54
67
|
|
55
68
|
## Install
|
56
69
|
|
57
|
-
```
|
70
|
+
```ruby
|
71
|
+
# Optional for UI.
|
72
|
+
# You can use any Bootstrap version (CSS, LESS, SCSS)
|
73
|
+
# You can find required components below
|
74
|
+
gem 'bootstrap-sass', '~> 2.3.1.0'
|
75
|
+
|
58
76
|
gem "the_role", "~> 2.0.0"
|
59
77
|
```
|
60
78
|
|
61
|
-
```
|
79
|
+
```ruby
|
62
80
|
bundle
|
63
81
|
```
|
64
82
|
|
83
|
+
install note
|
84
|
+
|
85
|
+
```
|
86
|
+
bundle exec rails g the_role --help
|
87
|
+
```
|
88
|
+
|
65
89
|
### Change User migration
|
66
90
|
|
67
91
|
Add **role_id:integer** field to your User Model
|
@@ -69,33 +93,25 @@ Add **role_id:integer** field to your User Model
|
|
69
93
|
```ruby
|
70
94
|
def self.up
|
71
95
|
create_table :users do |t|
|
72
|
-
t.string :login
|
73
|
-
t.string :email
|
74
|
-
t.string :crypted_password
|
75
|
-
t.string :salt
|
96
|
+
t.string :login
|
97
|
+
t.string :email
|
98
|
+
t.string :crypted_password
|
99
|
+
t.string :salt
|
76
100
|
|
77
101
|
# TheRole field
|
78
|
-
t.integer :role_id
|
102
|
+
t.integer :role_id
|
79
103
|
|
80
104
|
t.timestamps
|
81
105
|
end
|
82
106
|
end
|
83
107
|
```
|
84
108
|
|
85
|
-
### Role
|
109
|
+
### Create Role model
|
86
110
|
|
87
111
|
Generate Role model
|
88
112
|
|
89
|
-
``` ruby
|
90
|
-
rails g model role --migration=false
|
91
|
-
```
|
92
|
-
|
93
|
-
Change your Role model
|
94
|
-
|
95
113
|
```ruby
|
96
|
-
|
97
|
-
include RoleModel
|
98
|
-
end
|
114
|
+
bundle exec rails g the_role install
|
99
115
|
```
|
100
116
|
|
101
117
|
install TheRole migrations
|
@@ -104,44 +120,33 @@ install TheRole migrations
|
|
104
120
|
rake the_role_engine:install:migrations
|
105
121
|
```
|
106
122
|
|
107
|
-
|
123
|
+
Invoke migration
|
108
124
|
|
109
125
|
```ruby
|
110
|
-
rake db:
|
126
|
+
rake db:migrate
|
111
127
|
```
|
112
128
|
|
113
|
-
### Create Admin
|
129
|
+
### Create Admin
|
114
130
|
|
115
|
-
|
116
|
-
bin/rails c
|
117
|
-
```
|
118
|
-
|
119
|
-
``` ruby
|
120
|
-
role = Role.new
|
121
|
-
role.name = "admin"
|
122
|
-
role.title = "role for admin"
|
123
|
-
role.description = "this user can do anything"
|
124
|
-
role.save
|
131
|
+
Create admin role
|
125
132
|
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
role.admin? # => true
|
133
|
+
```
|
134
|
+
bundle exec rails g the_role admin
|
130
135
|
```
|
131
136
|
|
132
|
-
|
137
|
+
Makes any user as Admin
|
133
138
|
|
134
139
|
```
|
135
140
|
User.first.update( role: Role.with_name(:admin) )
|
136
141
|
```
|
137
142
|
|
138
|
-
|
143
|
+
## Integration
|
139
144
|
|
140
|
-
|
145
|
+
#### Change your ApplicationController
|
141
146
|
|
142
|
-
|
147
|
+
**include TheRoleController** in your Application controller
|
143
148
|
|
144
|
-
```
|
149
|
+
```ruby
|
145
150
|
class ApplicationController < ActionController::Base
|
146
151
|
include TheRoleController
|
147
152
|
|
@@ -152,27 +157,29 @@ class ApplicationController < ActionController::Base
|
|
152
157
|
return render(text: 'access_denied: requires an role')
|
153
158
|
end
|
154
159
|
|
155
|
-
# 1) LOGIN_REQUIRE => authenticate_user!
|
156
|
-
# 2) LOGIN_REQUIRE => require_login
|
160
|
+
# 1) LOGIN_REQUIRE => authenticate_user! for Devise
|
161
|
+
# 2) LOGIN_REQUIRE => require_login for Sorcery
|
162
|
+
# 3) LOGIN_REQUIRE => user_require_method for other Authentication solution
|
157
163
|
|
164
|
+
# Define aliases method for correctly work TheRole's controllers
|
158
165
|
alias_method :login_required, :LOGIN_REQUIRE
|
159
166
|
alias_method :role_access_denied, :access_denied
|
160
167
|
end
|
161
168
|
```
|
162
169
|
|
163
|
-
|
170
|
+
#### Using with any controller
|
164
171
|
|
165
|
-
```
|
172
|
+
```ruby
|
166
173
|
class PagesController < ApplicationController
|
167
|
-
|
168
|
-
|
174
|
+
before_action :login_required, except: [:index, :show]
|
175
|
+
before_action :role_required, except: [:index, :show]
|
169
176
|
|
170
|
-
|
171
|
-
|
177
|
+
before_action :set_page, only: [:edit, :update, :destroy]
|
178
|
+
before_action :owner_required, only: [:edit, :update, :destroy]
|
172
179
|
|
173
180
|
private
|
174
181
|
|
175
|
-
def
|
182
|
+
def set_page
|
176
183
|
@page = Page.find params[:id]
|
177
184
|
|
178
185
|
# TheRole: You should define OWNER CHECK OBJECT
|
@@ -183,20 +190,103 @@ class PagesController < ApplicationController
|
|
183
190
|
end
|
184
191
|
```
|
185
192
|
|
186
|
-
|
193
|
+
### Assets and Bootstrap
|
194
|
+
|
195
|
+
**application.css**
|
196
|
+
|
197
|
+
```
|
198
|
+
//= require the_role/bootstrap_sass
|
199
|
+
//= require the_role
|
200
|
+
```
|
201
|
+
|
202
|
+
If you not use **bootstrap-sass** gem you should to add following componetns from your bootstrap version instead **the_role/bootstrap_sass**:
|
203
|
+
|
204
|
+
```
|
205
|
+
bootstrap/variables
|
206
|
+
bootstrap/mixins
|
207
|
+
bootstrap/reset
|
208
|
+
|
209
|
+
bootstrap/scaffolding
|
210
|
+
bootstrap/grid
|
211
|
+
bootstrap/layouts
|
212
|
+
|
213
|
+
bootstrap/navs
|
214
|
+
bootstrap/wells
|
215
|
+
bootstrap/forms
|
216
|
+
bootstrap/close
|
217
|
+
bootstrap/tables
|
218
|
+
bootstrap/navbar
|
219
|
+
bootstrap/dropdowns
|
220
|
+
|
221
|
+
bootstrap/alerts
|
222
|
+
bootstrap/buttons
|
223
|
+
bootstrap/button-groups
|
224
|
+
```
|
225
|
+
|
226
|
+
### Configuration
|
187
227
|
|
188
228
|
config/initializers/the_role.rb
|
189
229
|
|
190
230
|
```ruby
|
191
231
|
TheRole.configure do |config|
|
192
|
-
config.layout
|
193
|
-
config.default_user_role =
|
232
|
+
config.layout = :application # default Layout for TheRole UI
|
233
|
+
config.default_user_role = nil # set default role (name)
|
194
234
|
end
|
195
235
|
```
|
196
236
|
|
197
|
-
## Understanding
|
237
|
+
## Understanding
|
238
|
+
|
239
|
+
#### TheRole instead CanCan?
|
198
240
|
|
199
|
-
|
241
|
+
TheRole in contrast to CanCan has simple and predefined way to find access state for current role. If you didn't want to create your own role scheme with CanCan Abilities - TheRole can be great solution for your.
|
242
|
+
|
243
|
+
You can manage roles with simple UI. TheRole's ACL structure inspired by Rails controllers, that is why it's so great for Rails application.
|
244
|
+
|
245
|
+
#### What does it mean semantic?
|
246
|
+
|
247
|
+
Semantic - the science of meaning. Human should fast to understand what is happening in a role system.
|
248
|
+
|
249
|
+
Look at next Role hash. If you can understand access rules - this authorization system is semantically.
|
250
|
+
|
251
|
+
```ruby
|
252
|
+
role = {
|
253
|
+
'pages' => {
|
254
|
+
'index' => true,
|
255
|
+
'show' => true,
|
256
|
+
'new' => false,
|
257
|
+
'edit' => false,
|
258
|
+
'update' => false,
|
259
|
+
'destroy' => false
|
260
|
+
},
|
261
|
+
'articles' => {
|
262
|
+
'index' => true,
|
263
|
+
'show' => true
|
264
|
+
},
|
265
|
+
'twitter' => {
|
266
|
+
'button' => true,
|
267
|
+
'follow' => false
|
268
|
+
}
|
269
|
+
}
|
270
|
+
```
|
271
|
+
|
272
|
+
#### Virtual sections and rules
|
273
|
+
|
274
|
+
Usually, we use real names of controllers and actions for names of sections and rules:
|
275
|
+
|
276
|
+
```ruby
|
277
|
+
@user.has_role?(:pages, :show)
|
278
|
+
```
|
279
|
+
|
280
|
+
But, also, you can use virtual names of sections, and virtual names of section's rules.
|
281
|
+
|
282
|
+
```ruby
|
283
|
+
@user.has_role?(:twitter, :button)
|
284
|
+
@user.has_role?(:facebook, :like)
|
285
|
+
```
|
286
|
+
|
287
|
+
And you can use them as well as other access rules.
|
288
|
+
|
289
|
+
#### Using with Views
|
200
290
|
|
201
291
|
```ruby
|
202
292
|
<% if @user.has_role?(:twitter, :button) %>
|
@@ -206,7 +296,7 @@ end
|
|
206
296
|
<% end %>
|
207
297
|
```
|
208
298
|
|
209
|
-
|
299
|
+
#### Who is Administrator?
|
210
300
|
|
211
301
|
Administrator it's a user who can access any section and the rules of your application.
|
212
302
|
|
@@ -215,7 +305,7 @@ Administrator is the owner of any objects in your application.
|
|
215
305
|
Administrator it's a user, which has virtual section **system** and rule **administrator** in the role-hash.
|
216
306
|
|
217
307
|
|
218
|
-
```
|
308
|
+
```ruby
|
219
309
|
admin_role_fragment = {
|
220
310
|
:system => {
|
221
311
|
:administrator => true
|
@@ -223,7 +313,7 @@ admin_role_fragment = {
|
|
223
313
|
}
|
224
314
|
```
|
225
315
|
|
226
|
-
|
316
|
+
#### Who is Moderator?
|
227
317
|
|
228
318
|
Moderator it's a user, which has access to any actions of some section(s).
|
229
319
|
|
@@ -233,7 +323,7 @@ Moderator it's a user, which has a virtual section **moderator**, with **section
|
|
233
323
|
|
234
324
|
There is Moderator of Pages (controller) and Twitter (virtual section)
|
235
325
|
|
236
|
-
```
|
326
|
+
```ruby
|
237
327
|
moderator_role_fragment = {
|
238
328
|
:moderator => {
|
239
329
|
:pages => true,
|
@@ -243,7 +333,7 @@ moderator_role_fragment = {
|
|
243
333
|
}
|
244
334
|
```
|
245
335
|
|
246
|
-
|
336
|
+
#### Who is Owner?
|
247
337
|
|
248
338
|
Administrator is owner of any object in system.
|
249
339
|
|
@@ -251,135 +341,96 @@ Moderator of pages is owner of any page.
|
|
251
341
|
|
252
342
|
User is owner of object, when **Object#user_id == User#id**.
|
253
343
|
|
254
|
-
## What does it mean semantic?
|
255
344
|
|
256
|
-
|
345
|
+
# API
|
257
346
|
|
258
|
-
|
347
|
+
## User
|
259
348
|
|
260
|
-
```
|
261
|
-
|
262
|
-
|
263
|
-
'index' => true,
|
264
|
-
'show' => true,
|
265
|
-
'new' => false,
|
266
|
-
'edit' => false,
|
267
|
-
'update' => false,
|
268
|
-
'destroy' => false
|
269
|
-
},
|
270
|
-
'articles' => {
|
271
|
-
'index' => true,
|
272
|
-
'show' => true
|
273
|
-
},
|
274
|
-
'twitter' => {
|
275
|
-
'button' => true,
|
276
|
-
'follow' => false
|
277
|
-
}
|
278
|
-
}
|
279
|
-
```
|
280
|
-
|
281
|
-
### Virtual sections and rules
|
282
|
-
|
283
|
-
Usually, we use real names of controllers and actions for names of sections and rules:
|
284
|
-
|
285
|
-
``` ruby
|
286
|
-
current_user.has_role?(:pages, :show)
|
287
|
-
```
|
288
|
-
|
289
|
-
But, also, you can use virtual names of sections, and virtual names of section's rules.
|
290
|
-
|
291
|
-
``` ruby
|
292
|
-
current_user.has_role?(:twitter, :button)
|
293
|
-
current_user.has_role?(:facebook, :like)
|
349
|
+
```ruby
|
350
|
+
# User's role
|
351
|
+
@user.role # => Role obj
|
294
352
|
```
|
295
353
|
|
296
|
-
|
297
|
-
|
298
|
-
# User Model methods
|
299
|
-
|
300
|
-
Has a user an access to **rule** of **section** (action of controller)?
|
354
|
+
Is it Administrator?
|
301
355
|
|
302
|
-
```
|
303
|
-
|
304
|
-
current_user.has_role?(:blogs, :new) => true | false
|
305
|
-
current_user.has_role?(:articles, :edit) => true | false
|
356
|
+
```ruby
|
357
|
+
@user.admin? => true | false
|
306
358
|
```
|
307
359
|
|
308
360
|
Is it Moderator?
|
309
361
|
|
310
|
-
```
|
311
|
-
|
312
|
-
|
313
|
-
|
362
|
+
```ruby
|
363
|
+
@user.moderator?(:pages) => true | false
|
364
|
+
@user.moderator?(:blogs) => true | false
|
365
|
+
@user.moderator?(:articles) => true | false
|
314
366
|
```
|
315
367
|
|
316
|
-
|
368
|
+
Has a user an access to **rule** of **section** (action of controller)?
|
317
369
|
|
318
|
-
```
|
319
|
-
|
370
|
+
```ruby
|
371
|
+
@user.has_role?(:pages, :show) => true | false
|
372
|
+
@user.has_role?(:blogs, :new) => true | false
|
373
|
+
@user.has_role?(:articles, :edit) => true | false
|
320
374
|
```
|
321
375
|
|
322
376
|
Is it **Owner** of object?
|
323
377
|
|
324
|
-
```
|
325
|
-
|
326
|
-
|
327
|
-
|
378
|
+
```ruby
|
379
|
+
@user.owner?(@page) => true | false
|
380
|
+
@user.owner?(@blog) => true | false
|
381
|
+
@user.owner?(@article) => true | false
|
328
382
|
```
|
329
383
|
|
330
|
-
|
331
|
-
|
332
|
-
``` ruby
|
333
|
-
# User's role
|
334
|
-
@role = current_user.role
|
335
|
-
```
|
384
|
+
## Role
|
336
385
|
|
337
|
-
```
|
386
|
+
```ruby
|
338
387
|
# Find a Role by name
|
339
|
-
@role = Role.
|
388
|
+
@role = Role.with_name(:user)
|
340
389
|
```
|
341
390
|
|
342
|
-
```
|
391
|
+
```ruby
|
343
392
|
@role.has?(:pages, :show) => true | false
|
344
393
|
@role.moderator?(:pages) => true | false
|
345
394
|
@role.admin? => true | false
|
346
395
|
```
|
347
396
|
|
348
|
-
# CRUD API (for console users)
|
349
|
-
|
350
397
|
#### CREATE
|
351
398
|
|
352
|
-
```
|
399
|
+
```ruby
|
353
400
|
# Create a section of rules
|
354
401
|
@role.create_section(:pages)
|
355
402
|
```
|
356
403
|
|
357
|
-
```
|
404
|
+
```ruby
|
358
405
|
# Create rule in section (false value by default)
|
359
406
|
@role.create_rule(:pages, :index)
|
360
407
|
```
|
361
408
|
|
362
409
|
#### READ
|
363
410
|
|
364
|
-
```
|
411
|
+
```ruby
|
365
412
|
@role.to_hash => Hash
|
366
413
|
|
367
414
|
# JSON string
|
368
415
|
@role.to_json => String
|
369
416
|
|
370
|
-
# JSON string
|
371
|
-
@role.to_s => String
|
372
|
-
|
373
417
|
# check method
|
374
418
|
@role.has_section?(:pages) => true | false
|
375
|
-
|
376
|
-
# check method
|
377
|
-
@role.has_rule?(:pages, :index) => true | false
|
378
419
|
```
|
379
420
|
|
380
421
|
#### UPDATE
|
381
422
|
|
382
|
-
```
|
423
|
+
```ruby
|
424
|
+
# set this rule on true
|
425
|
+
@role.rule_on(:pages, :index)
|
426
|
+
```
|
427
|
+
|
428
|
+
```ruby
|
429
|
+
# set this rule on false
|
430
|
+
@role.rule_off(:pages, :index)
|
431
|
+
```
|
432
|
+
|
433
|
+
```ruby
|
383
434
|
# Incoming hash is true-mask-hash
|
384
435
|
# All rules of Role will be reset to false
|
385
436
|
# Only rules from true-mask-hash will be set on true
|
@@ -393,19 +444,9 @@ new_role_hash = {
|
|
393
444
|
@role.update_role(new_role_hash)
|
394
445
|
```
|
395
446
|
|
396
|
-
|
397
|
-
# set this rule on true
|
398
|
-
@role.rule_on(:pages, :index)
|
399
|
-
```
|
400
|
-
|
401
|
-
``` ruby
|
402
|
-
# set this rule on false
|
403
|
-
@role.rule_off(:pages, :index)
|
404
|
-
```
|
447
|
+
#### DELETE
|
405
448
|
|
406
|
-
|
407
|
-
|
408
|
-
``` ruby
|
449
|
+
```ruby
|
409
450
|
# delete a section
|
410
451
|
@role.delete_section(:pages)
|
411
452
|
|
@@ -413,8 +454,10 @@ new_role_hash = {
|
|
413
454
|
@role.delete_rule(:pages, :show)
|
414
455
|
```
|
415
456
|
|
416
|
-
|
457
|
+
#### Changelog
|
417
458
|
|
459
|
+
* 2.0.2 - code cleanup, readme
|
460
|
+
* 2.0.1 - code cleanup
|
418
461
|
* 2.0.0 - Rails 4 ready, configurable, tests
|
419
462
|
* 1.7.0 - mass assignment for User#role_id, doc, locales, changes in test app
|
420
463
|
* 1.6.9 - assets precompile addon
|
@@ -0,0 +1,20 @@
|
|
1
|
+
@import "bootstrap/variables";
|
2
|
+
@import "bootstrap/mixins";
|
3
|
+
@import "bootstrap/reset";
|
4
|
+
|
5
|
+
@import "bootstrap/scaffolding";
|
6
|
+
@import "bootstrap/grid";
|
7
|
+
@import "bootstrap/layouts";
|
8
|
+
|
9
|
+
@import "bootstrap/navs";
|
10
|
+
@import "bootstrap/wells";
|
11
|
+
@import "bootstrap/forms";
|
12
|
+
@import "bootstrap/close";
|
13
|
+
@import "bootstrap/tables";
|
14
|
+
@import "bootstrap/navbar";
|
15
|
+
@import "bootstrap/dropdowns";
|
16
|
+
|
17
|
+
@import "bootstrap/alerts";
|
18
|
+
@import "bootstrap/buttons";
|
19
|
+
@import "bootstrap/button-groups";
|
20
|
+
|
@@ -5,8 +5,9 @@ class Admin::RoleSectionsController < ApplicationController
|
|
5
5
|
before_filter :login_required
|
6
6
|
before_filter :role_required
|
7
7
|
|
8
|
-
before_filter :
|
9
|
-
before_filter :
|
8
|
+
before_filter :section_rule_names, only: [:rule_on, :rule_off, :destroy_rule]
|
9
|
+
before_filter :role_find, only: [:create, :create_rule, :rule_on, :rule_off, :destroy, :destroy_rule]
|
10
|
+
before_filter :owner_required, only: [:create, :create_rule, :rule_on, :rule_off, :destroy, :destroy_rule]
|
10
11
|
|
11
12
|
def create
|
12
13
|
if @role.create_section params[:section_name]
|
@@ -14,6 +15,7 @@ class Admin::RoleSectionsController < ApplicationController
|
|
14
15
|
else
|
15
16
|
flash[:error] = t 'the_role.section_not_created'
|
16
17
|
end
|
18
|
+
|
17
19
|
redirect_to_edit
|
18
20
|
end
|
19
21
|
|
@@ -23,28 +25,37 @@ class Admin::RoleSectionsController < ApplicationController
|
|
23
25
|
else
|
24
26
|
flash[:error] = t 'the_role.section_rule_not_created'
|
25
27
|
end
|
28
|
+
|
26
29
|
redirect_to_edit
|
27
30
|
end
|
28
31
|
|
29
32
|
def rule_on
|
30
|
-
section_name
|
31
|
-
rule_name = params[:name]
|
32
|
-
if @role.rule_on section_name, rule_name
|
33
|
+
if @role.rule_on @section_name, @rule_name
|
33
34
|
flash[:notice] = t 'the_role.section_rule_on'
|
34
35
|
else
|
35
36
|
flash[:error] = t 'the_role.state_not_changed'
|
36
37
|
end
|
38
|
+
|
37
39
|
redirect_to_edit
|
38
40
|
end
|
39
41
|
|
40
42
|
def rule_off
|
41
|
-
section_name
|
42
|
-
rule_name = params[:name]
|
43
|
-
if @role.rule_off section_name, rule_name
|
43
|
+
if @role.rule_off @section_name, @rule_name
|
44
44
|
flash[:notice] = t 'the_role.section_rule_off'
|
45
45
|
else
|
46
46
|
flash[:error] = t 'the_role.state_not_changed'
|
47
47
|
end
|
48
|
+
|
49
|
+
redirect_to_edit
|
50
|
+
end
|
51
|
+
|
52
|
+
def destroy_rule
|
53
|
+
if @role.delete_rule @section_name, @rule_name
|
54
|
+
flash[:notice] = t 'the_role.section_rule_deleted'
|
55
|
+
else
|
56
|
+
flash[:error] = t 'the_role.section_rule_not_deleted'
|
57
|
+
end
|
58
|
+
|
48
59
|
redirect_to_edit
|
49
60
|
end
|
50
61
|
|
@@ -58,19 +69,13 @@ class Admin::RoleSectionsController < ApplicationController
|
|
58
69
|
redirect_to_edit
|
59
70
|
end
|
60
71
|
|
61
|
-
def destroy_rule
|
62
|
-
section_name = params[:id]
|
63
|
-
rule_name = params[:name]
|
64
|
-
if @role.delete_rule section_name, rule_name
|
65
|
-
flash[:notice] = t 'the_role.section_rule_deleted'
|
66
|
-
else
|
67
|
-
flash[:error] = t 'the_role.section_rule_not_deleted'
|
68
|
-
end
|
69
|
-
redirect_to_edit
|
70
|
-
end
|
71
|
-
|
72
72
|
protected
|
73
73
|
|
74
|
+
def section_rule_names
|
75
|
+
@section_name = params[:id]
|
76
|
+
@rule_name = params[:name]
|
77
|
+
end
|
78
|
+
|
74
79
|
def role_find
|
75
80
|
@role = Role.find params[:role_id]
|
76
81
|
|
@@ -10,6 +10,16 @@ This text:
|
|
10
10
|
|
11
11
|
Generators:
|
12
12
|
bundle exec rails g the_role install
|
13
|
+
- Install config file and models
|
14
|
+
|
15
|
+
bundle exec rails g the_role setup
|
16
|
+
- Install config file
|
17
|
+
|
18
|
+
bundle exec rails g the_role models
|
19
|
+
- Install models
|
20
|
+
|
21
|
+
bundle exec rails g the_role admin
|
22
|
+
- Create Admin Role
|
13
23
|
|
14
24
|
Migrations:
|
15
25
|
bundle exec rake the_role_engine:install:migrations
|
@@ -5,6 +5,13 @@ class TheRoleGenerator < Rails::Generators::NamedBase
|
|
5
5
|
def generate_controllers
|
6
6
|
if gen_name == 'install'
|
7
7
|
cp_setup
|
8
|
+
cp_models
|
9
|
+
elsif gen_name == 'setup'
|
10
|
+
cp_setup
|
11
|
+
elsif gen_name == 'models'
|
12
|
+
cp_models
|
13
|
+
elsif gen_name == 'admin'
|
14
|
+
create_admin_role
|
8
15
|
else
|
9
16
|
puts 'TheComments Generator - wrong Name'
|
10
17
|
puts 'Try to use install'
|
@@ -17,7 +24,38 @@ class TheRoleGenerator < Rails::Generators::NamedBase
|
|
17
24
|
name.to_s.downcase
|
18
25
|
end
|
19
26
|
|
27
|
+
def cp_models
|
28
|
+
copy_file 'role.rb', 'app/models/role.rb'
|
29
|
+
end
|
30
|
+
|
20
31
|
def cp_setup
|
21
32
|
copy_file 'the_role.rb', 'config/initializers/the_role.rb'
|
22
33
|
end
|
34
|
+
|
35
|
+
def create_admin_role
|
36
|
+
puts `clear`
|
37
|
+
puts '~'*40
|
38
|
+
puts 'TheRole'
|
39
|
+
puts '~'*40
|
40
|
+
|
41
|
+
unless Role.with_name(:admin)
|
42
|
+
role = Role.create(
|
43
|
+
name: :admin,
|
44
|
+
title: "role for admin",
|
45
|
+
description:"this user can do anything"
|
46
|
+
)
|
47
|
+
|
48
|
+
role.create_rule(:system, :administrator)
|
49
|
+
role.rule_on(:system, :administrator)
|
50
|
+
|
51
|
+
puts "Admin role created"
|
52
|
+
else
|
53
|
+
puts "Admin role exists"
|
54
|
+
end
|
55
|
+
|
56
|
+
puts "Now you can makes any user as Admin:"
|
57
|
+
puts "> bin/rails c"
|
58
|
+
puts "> User.first.update( role: Role.with_name(:admin) )"
|
59
|
+
puts '~'*40
|
60
|
+
end
|
23
61
|
end
|
data/lib/the_role/version.rb
CHANGED
data/the_role.gemspec
CHANGED
@@ -8,8 +8,8 @@ Gem::Specification.new do |s|
|
|
8
8
|
s.authors = ["Ilya N. Zykin [the-teacher]"]
|
9
9
|
s.email = ["zykin-ilya@ya.ru"]
|
10
10
|
s.homepage = "https://github.com/the-teacher/the_role"
|
11
|
-
s.summary = %q{Authorization
|
12
|
-
s.description = %q{Authorization
|
11
|
+
s.summary = %q{Authorization for Rails 4}
|
12
|
+
s.description = %q{Authorization for Rails 4 with Web Interface}
|
13
13
|
|
14
14
|
s.rubyforge_project = "the_role"
|
15
15
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: the_role
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0.
|
4
|
+
version: 2.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-05-
|
12
|
+
date: 2013-05-27 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: haml
|
@@ -27,7 +27,7 @@ dependencies:
|
|
27
27
|
- - ! '>='
|
28
28
|
- !ruby/object:Gem::Version
|
29
29
|
version: '0'
|
30
|
-
description: Authorization
|
30
|
+
description: Authorization for Rails 4 with Web Interface
|
31
31
|
email:
|
32
32
|
- zykin-ilya@ya.ru
|
33
33
|
executables: []
|
@@ -42,6 +42,7 @@ files:
|
|
42
42
|
- README.md
|
43
43
|
- Rakefile
|
44
44
|
- app/assets/stylesheets/the_role.css.scss
|
45
|
+
- app/assets/stylesheets/the_role/bootstrap_sass.css.scss
|
45
46
|
- app/controllers/admin/role_sections_controller.rb
|
46
47
|
- app/controllers/admin/roles_controller.rb
|
47
48
|
- app/controllers/the_role_controller.rb
|
@@ -54,7 +55,6 @@ files:
|
|
54
55
|
- app/views/admin/roles/edit.html.haml
|
55
56
|
- app/views/admin/roles/index.haml
|
56
57
|
- app/views/admin/roles/new.html.haml
|
57
|
-
- app/views/layouts/the_role.html.haml
|
58
58
|
- config/locales/en.yml
|
59
59
|
- config/locales/es.yml
|
60
60
|
- config/locales/ru.yml
|
@@ -62,6 +62,7 @@ files:
|
|
62
62
|
- config/routes.rb
|
63
63
|
- db/migrate/20111025025129_create_roles.rb
|
64
64
|
- lib/generators/the_role/USAGE
|
65
|
+
- lib/generators/the_role/templates/role.rb
|
65
66
|
- lib/generators/the_role/templates/the_role.rb
|
66
67
|
- lib/generators/the_role/the_role_generator.rb
|
67
68
|
- lib/tasks/roles.rake
|
@@ -96,5 +97,5 @@ rubyforge_project: the_role
|
|
96
97
|
rubygems_version: 1.8.25
|
97
98
|
signing_key:
|
98
99
|
specification_version: 3
|
99
|
-
summary: Authorization
|
100
|
+
summary: Authorization for Rails 4
|
100
101
|
test_files: []
|
@@ -1,36 +0,0 @@
|
|
1
|
-
!!! 5
|
2
|
-
%html
|
3
|
-
%head
|
4
|
-
%meta{ :charset => 'utf-8' }
|
5
|
-
%title Bye bye, CanCan, I got the Role!
|
6
|
-
= stylesheet_link_tag 'admin_the_role'
|
7
|
-
= javascript_include_tag 'admin_the_role'
|
8
|
-
= csrf_meta_tags
|
9
|
-
%body
|
10
|
-
.the_role
|
11
|
-
%h3
|
12
|
-
= link_to 'The Role', 'https://github.com/the-teacher/the_role'
|
13
|
-
\|
|
14
|
-
Role system for Rails Apps
|
15
|
-
\|
|
16
|
-
- u = current_user
|
17
|
-
User: #{u.try(:name) || u.try(:username) || u.try(:login)|| u.try(:email) || u.id}
|
18
|
-
\|
|
19
|
-
= I18n.locale
|
20
|
-
.crusty
|
21
|
-
.container
|
22
|
-
|
23
|
-
- if flash[:notice]
|
24
|
-
.alert.alert-success
|
25
|
-
%a.close{:href => '#', 'data-dismiss' => :alert } ×
|
26
|
-
= flash[:notice]
|
27
|
-
|
28
|
-
- if flash[:error]
|
29
|
-
.alert.alert-error
|
30
|
-
%a.close{:href => '#', 'data-dismiss' => :alert } ×
|
31
|
-
= flash[:error]
|
32
|
-
.row
|
33
|
-
.span3
|
34
|
-
.sidebar= yield :sidebar
|
35
|
-
.span9
|
36
|
-
.content= yield
|