the_role 1.5.0 → 1.5.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/README.md +223 -1
  2. data/lib/the_role/version.rb +1 -1
  3. data/the_role.gemspec +2 -2
  4. metadata +11 -11
data/README.md CHANGED
@@ -69,15 +69,237 @@ current_user.has_role?(:facebook, :like)
69
69
  These sections and the rules are not associated with real controllers and actions.
70
70
  And you can use them as well as other access rules.
71
71
 
72
- ### Who is the administrator?
72
+ ### Install and use
73
+
74
+ ``` ruby
75
+ gem 'the_role'
76
+ ```
77
+
78
+ ``` ruby
79
+ bundle install
80
+ ```
81
+
82
+ Add **role_id:integer** to User Model Migration
83
+
84
+
85
+ ``` ruby
86
+ rake the_role_engine:install:migrations
87
+ >> Copied migration 20111028145956_create_roles.rb from the_role_engine
88
+ ```
89
+
90
+ ``` ruby
91
+ rails g model role --migration=false
92
+ ```
93
+
94
+ ``` ruby
95
+ rake db:create && rake db:migrate
96
+ ```
97
+
98
+ Creating roles for test (**not required**)
99
+
100
+ ``` ruby
101
+ rake db:roles:test
102
+ >> Administrator, Moderator of pages, User, Demo
103
+ ```
104
+
105
+ Define aliases method for correctly work TheRole's controllers
106
+
107
+ **authenticate_user!** or any other method from your auth system
108
+
109
+ **access_denied** or any other method for processing access denied situation
110
+
111
+ **Example for Devise2**
112
+
113
+ ``` ruby
114
+ class ApplicationController < ActionController::Base
115
+ protect_from_forgery
116
+
117
+ def access_denied
118
+ render :text => 'access_denied: requires an role' and return
119
+ end
120
+
121
+ # define aliases for correctly work of TheRole admin panel
122
+ # *authenticate_user!* - method from Devise2
123
+ # *access_denied* - define it before alias_method
124
+ # before_filter :role_object_finder, :only => [:edit, :update, :rebuild, :destroy]
125
+ alias_method :role_login_required, :authenticate_user!
126
+ alias_method :role_access_denied, :access_denied
127
+
128
+ end
129
+ ```
130
+
131
+ Using with any controller
132
+
133
+ ``` ruby
134
+ class PagesController < ApplicationController
135
+ # Devise2 and TheRole before_filters
136
+ before_filter :role_login_required, :except => [:index, :show]
137
+ before_filter :role_require, :except => [:index, :show]
138
+
139
+ before_filter :find_page, :only => [:edit, :update, :destroy]
140
+ before_filter :owner_require, :only => [:edit, :update, :destroy]
141
+
142
+ end
143
+ ```
144
+
145
+ ### WARNING! IT'S IMPORTANT
146
+
147
+ When you checking **owner_require** - you should before this to define variable **@object_for_ownership_checking** in finder method.
148
+
149
+ For example:
150
+
151
+ ``` ruby
152
+ class PagesController < ApplicationController
153
+ before_filter :find_page, :only => [:edit, :update, :destroy]
154
+ before_filter :owner_require, :only => [:edit, :update, :destroy]
155
+
156
+ private
157
+
158
+ def find_page
159
+ @page = Page.find params[:id]
160
+ @object_for_ownership_checking = @page
161
+ end
162
+ end
163
+ ```
164
+
165
+ ### Who is the Administrator?
73
166
 
74
167
  Administrator - a user who can access any section and the rules of your application.
75
168
  The administrator is the owner of any objects in your application.
76
169
  Administrator - a user in the role-hash of which there is a section **system** and rule **administrator**.
77
170
 
78
171
 
172
+ ``` ruby
173
+ admin_role_fragment = {
174
+ :system => {
175
+ :administrator => true
176
+ }
177
+ }
178
+ ```
179
+
180
+ ### Who is the Moderator?
181
+
182
+ Moderator - a user who can access any actions of sections.
183
+ Moderator is the owner of any objects of this class.
184
+ Moderator - user which has in a section **moderator** rule with name of real or virtual section (controller).
79
185
 
186
+ There is role hash of Moderator of Pages (controller) and Twitter (virtual section)
187
+
188
+ ``` ruby
189
+ moderator_role_fragment = {
190
+ :moderator => {
191
+ :pages => true,
192
+ :blogs => false,
193
+ :twitter => true
194
+ }
195
+ }
196
+ ```
80
197
 
198
+ ### User methods
81
199
 
200
+ Has a user an access to **action** of **section**?
82
201
 
202
+ ``` ruby
203
+ current_user.has_role?(:pages, :show) => true | false
204
+ current_user.has_role?(:blogs, :new) => true | false
205
+ current_user.has_role?(:articles, :edit) => true | false
206
+ ```
207
+
208
+ Is it Moderator?
209
+
210
+ ``` ruby
211
+ current_user.moderator?(:pages) => true | false
212
+ current_user.moderator?(:blogs) => true | false
213
+ current_user.moderator?(:articles) => true | false
214
+ ```
215
+
216
+ Is it Administrator?
217
+
218
+ ``` ruby
219
+ current_user.admin? => true | false
220
+ ```
221
+
222
+ Is it **Owner** of object?
223
+
224
+ ``` ruby
225
+ current_user.owner?(@page) => true | false
226
+ current_user.owner?(@blog) => true | false
227
+ current_user.owner?(@article) => true | false
228
+ ```
229
+
230
+ ### Role methods
231
+
232
+ ``` ruby
233
+ # Find a Role by name
234
+ @role.find_by_name(:user)
235
+ ```
236
+
237
+ ``` ruby
238
+ # User Model like methods
239
+
240
+ @role.has?(:pages, :show) => true | false
241
+ @role.moderator?(:pages) => true | false
242
+ @role.admin? => true | false
243
+ ```
244
+
245
+ ## CRUD API
246
+
247
+ #### CREATE
248
+
249
+ ``` ruby
250
+ # Create a section of rules
251
+ @role.create_section(:pages)
252
+ ```
253
+
254
+ ``` ruby
255
+ # Create rule in section (false value by default)
256
+ @role.create_rule(:pages, :index)
257
+ ```
258
+
259
+ #### READ
260
+
261
+ ``` ruby
262
+ @role.to_hash => Hash
263
+
264
+ # YAML string
265
+ @role.to_yaml => String
266
+
267
+ # YAML string
268
+ @role.to_s => String
269
+ ```
270
+
271
+ #### UPDATE
272
+
273
+ ``` ruby
274
+ # Incoming hash is true-mask-hash
275
+ # All rules of Role will be reset to false
276
+ # Only rules from true-mask-hash will be set on true
277
+ new_role_hash = {
278
+ :pages => {
279
+ :index => true,
280
+ :show => true
281
+ }
282
+ }
283
+
284
+ @role.update_role(new_role_hash)
285
+ ```
286
+
287
+ ``` ruby
288
+ # set this rule on true
289
+ @role.rule_on(:pages, :index)
290
+ ```
291
+
292
+ ``` ruby
293
+ # set this rule on false
294
+ @role.rule_off(:pages, :index)
295
+ ```
296
+
297
+ ### DELETE
298
+
299
+ ``` ruby
300
+ # delete a section
301
+ @role.delete_section(:pages)
83
302
 
303
+ # delete rule in section
304
+ @role.delete_rule(:pages, :show)
305
+ ```
@@ -1,3 +1,3 @@
1
1
  module TheRole
2
- VERSION = "1.5.0"
2
+ VERSION = "1.5.1"
3
3
  end
data/the_role.gemspec CHANGED
@@ -8,8 +8,8 @@ Gem::Specification.new do |s|
8
8
  s.authors = ["Ilya N. Zykin"]
9
9
  s.email = ["zykin-ilya@ya.ru"]
10
10
  s.homepage = "https://github.com/the-teacher/the_role"
11
- s.summary = %q{TheRole - simple, but powerful role system}
12
- s.description = %q{TheRole - simple, but powerful role system for ROR applications}
11
+ s.summary = %q{TheRole, Role system with Web Interface, aka CanCan killer}
12
+ s.description = %q{TheRole, Role system with Web Interface, aka CanCan killer}
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: 1.5.0
4
+ version: 1.5.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-04-05 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: haml
16
- requirement: &70445940 !ruby/object:Gem::Requirement
16
+ requirement: &73560920 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70445940
24
+ version_requirements: *73560920
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: sass
27
- requirement: &70445730 !ruby/object:Gem::Requirement
27
+ requirement: &73560710 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70445730
35
+ version_requirements: *73560710
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: sass-rails
38
- requirement: &70445520 !ruby/object:Gem::Requirement
38
+ requirement: &73560500 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *70445520
46
+ version_requirements: *73560500
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: coffee-rails
49
- requirement: &70445310 !ruby/object:Gem::Requirement
49
+ requirement: &73560290 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,8 +54,8 @@ dependencies:
54
54
  version: '0'
55
55
  type: :runtime
56
56
  prerelease: false
57
- version_requirements: *70445310
58
- description: TheRole - simple, but powerful role system for ROR applications
57
+ version_requirements: *73560290
58
+ description: TheRole, Role system with Web Interface, aka CanCan killer
59
59
  email:
60
60
  - zykin-ilya@ya.ru
61
61
  executables: []
@@ -117,5 +117,5 @@ rubyforge_project: the_role
117
117
  rubygems_version: 1.8.15
118
118
  signing_key:
119
119
  specification_version: 3
120
- summary: TheRole - simple, but powerful role system
120
+ summary: TheRole, Role system with Web Interface, aka CanCan killer
121
121
  test_files: []