ktopping_acl9 0.12.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.
data/README.textile ADDED
@@ -0,0 +1,903 @@
1
+ h1. Introduction
2
+
3
+ Acl9 is yet another solution for role-based authorization in Rails. It consists of two
4
+ subsystems which can be used separately.
5
+
6
+ *Role control subsystem* allows you to set and query user roles for various objects.
7
+
8
+ *Access control subsystem* allows you to specify different role-based access rules
9
+ inside controllers.
10
+
11
+ A bunch of access rules is translated into a complex
12
+ boolean expression. Then it's turned into a lambda or a method and can
13
+ be used with @before_filter@. Thus you can block unprivileged access to certain
14
+ actions of your controller.
15
+
16
+ An example:
17
+
18
+ <pre><code>
19
+ class VerySecretController < ApplicationController
20
+ access_control do
21
+ allow :superadmin
22
+ allow :owner, :of => :secret
23
+
24
+ action :index do
25
+ allow anonymous, logged_in
26
+ end
27
+
28
+ allow logged_in, :to => :show
29
+ allow :manager, :of => :secret, :except => [:delete, :destroy]
30
+ deny :thiefs
31
+ end
32
+
33
+ def index
34
+ # ...
35
+ end
36
+
37
+ # ...
38
+ end
39
+ </code></pre>
40
+
41
+ h1. Contacts
42
+
43
+ Acl9 is hosted "on the GitHub":http://github.com/be9/acl9.
44
+
45
+ You may find tutorials and additional docs on the "wiki page":http://wiki.github.com/be9/acl9.
46
+
47
+ Rdocs are available "here":http://rdoc.info/projects/be9/acl9.
48
+
49
+ If you have questions, please post to the
50
+ "acl9-discuss group":http://groups.google.com/group/acl9-discuss
51
+
52
+ h1. Installation
53
+
54
+ Acl9 can be installed as a gem from "gemcutter":http://gemcutter.org.
55
+
56
+ Add the following line to your @config/environment.rb@:
57
+
58
+ <pre><code>
59
+ config.gem "acl9", :source => "http://gemcutter.org", :lib => "acl9"
60
+ </pre></code>
61
+
62
+ Then run @rake gems:install@ (with possible @rake gems:unpack@ thereafter) and you're done!
63
+
64
+ Alternatively you can install Acl9 as a plugin:
65
+
66
+ <pre><code>
67
+ script/plugin install git://github.com/be9/acl9.git
68
+ </pre></code>
69
+
70
+ h1. Basics
71
+
72
+ h2. Authorization is not authentication!
73
+
74
+ Both words start with "auth" but have different meaning!
75
+
76
+ *Authentication* is basically a mapping of credentials (username, password) or
77
+ OpenID to specific user account in the system.
78
+
79
+ *Authorization* is an _authenticated_ user's permission to perform some
80
+ specific action somewhere in the system.
81
+
82
+ Acl9 is a authorization solution, so you will need to implement authentication
83
+ by other means. I recommend "Authlogic":http://github.com/binarylogic/authlogic
84
+ for that purpose, as it's simple, clean and at the same time very configurable.
85
+
86
+ h2. Roles
87
+
88
+ Role is an abstraction. You could directly assign permissions to user accounts in
89
+ your system, but you'd not want to! Way more manageable solution is to assign permissions
90
+ to roles and roles further to users.
91
+
92
+ For example, you can have role called _admin_ which has all available permissions. Now
93
+ you may assign this role to several trusted accounts on your system.
94
+
95
+ Acl9 also supports the notion of _object roles_, that is, roles with limited scope.
96
+
97
+ Imagine we are building a magazine site and want to develop a permission system. So, what roles
98
+ and permissions are there?
99
+
100
+ _Journalists_ should be able to create articles in their section and edit their own articles.
101
+
102
+ _Section editors_ should be able to edit and delete all articles in their sections and
103
+ change the _published_ flag.
104
+
105
+ _Editor-in-chief_ should be able to change everything.
106
+
107
+ We clearly see that journalists and section editors are tied to a specific section, whereas
108
+ editor-in-chief is a role with global scope.
109
+
110
+ h2. Role interface
111
+
112
+ All permission checks in Acl9 are boiled down to calls of a single method:
113
+
114
+ @subject.has_role?(role, object)@
115
+
116
+ That should be read as "Does _subject_ have _role_ on _object_?".
117
+
118
+ Subject is an instance of a @User@, or @Account@, or whatever model you use for
119
+ authentication. Object is an instance of any class (including subject class!)
120
+ or @nil@ (in which case it's a global role).
121
+
122
+ Acl9 builtin role control subsystem provides @has_role?@ method for you, but you can
123
+ also implemented it by hand (see _Coming up with your own role implementation_ below).
124
+
125
+ h1. Acl9 role control subsystem
126
+
127
+ Role control subsystem has been lifted from
128
+ "Rails authorization plugin":http://github.com/DocSavage/rails-authorization-plugin,
129
+ but undergone some modifications.
130
+
131
+ It's based on two tables in the database. First, role table, which stores pairs @[role_name, object]@
132
+ where object is a polymorphic model instance or a class. Second, join table, which joins users and roles.
133
+
134
+ To use this subsystem, you should define a @Role@ model.
135
+
136
+ h2. Role model
137
+
138
+ <pre><code>
139
+ class Role < ActiveRecord::Base
140
+ acts_as_authorization_role
141
+ end
142
+ </code></pre>
143
+
144
+ The structure of @roles@ table is as follows:
145
+
146
+ <pre><code>
147
+ create_table "roles", :force => true do |t|
148
+ t.string :name, :limit => 40
149
+ t.string :authorizable_type, :limit => 40
150
+ t.integer :authorizable_id
151
+ t.timestamps
152
+ end
153
+ </code></pre>
154
+
155
+ Note that you will almost never use the @Role@ class directly.
156
+
157
+ h2. Subject model
158
+
159
+ <pre><code>
160
+ class User < ActiveRecord::Base
161
+ acts_as_authorization_subject
162
+ end
163
+ </code></pre>
164
+
165
+ You won't need any specific columns in the @users@ table, but
166
+ there should be a join table:
167
+
168
+ <pre><code>
169
+ create_table "roles_users", :id => false, :force => true do |t|
170
+ t.references :user
171
+ t.references :role
172
+ t.timestamps
173
+ end
174
+ </code></pre>
175
+
176
+ h2. Object model
177
+
178
+ Place @acts_as_authorization_object@ call inside any model you want to act
179
+ as such.
180
+
181
+ <pre><code>
182
+ class Foo < ActiveRecord::Base
183
+ acts_as_authorization_object
184
+ end
185
+
186
+ class Bar < ActiveRecord::Base
187
+ acts_as_authorization_object
188
+ end
189
+ </code></pre>
190
+
191
+ h2. Interface
192
+
193
+ h3. Subject model
194
+
195
+ A call of @acts_as_authorization_subject@ defines following methods on the model:
196
+
197
+ @subject.has_role?(role, object = nil)@. Returns @true@ of @false@ (has or has not).
198
+
199
+ @subject.has_role!(role, object = nil)@. Assigns a @role@ for the @object@ to the @subject@.
200
+ Does nothing is subject already has such a role.
201
+
202
+ @subject.has_no_role!(role, object = nil)@. Unassigns a role from the @subject@.
203
+
204
+ @subject.has_roles_for?(object)@. Does the @subject@ has any roles for @object@? (@true@ of @false@)
205
+
206
+ @subject.has_role_for?(object)@. Same as @has_roles_for?@.
207
+
208
+ @subject.roles_for(object)@. Returns an array of @Role@ instances, corresponding to @subject@ 's roles on
209
+ @object@. E.g. @subject.roles_for(object).map(&:name).sort@ will give you role names in alphabetical order.
210
+
211
+ @subject.has_no_roles_for!(object)@. Unassign any @subject@ 's roles for a given @object@.
212
+
213
+ @subject.has_no_roles!@. Unassign all roles from @subject@.
214
+
215
+ h3. Object model
216
+
217
+ A call of @acts_as_authorization_object@ defines following methods on the model:
218
+
219
+ @object.accepts_role?(role_name, subject)@. An alias for @subject.has_role?(role_name, object)@.
220
+
221
+ @object.accepts_role!(role_name, subject)@. An alias for @subject.has_role!(role_name, object)@.
222
+
223
+ @object.accepts_no_role!(role_name, subject)@. An alias for @subject.has_no_role!(role_name, object)@.
224
+
225
+ @object.accepts_roles_by?(subject)@. An alias for @subject.has_roles_for?(object)@.
226
+
227
+ @object.accepts_role_by?(subject)@. Same as @accepts_roles_by?@.
228
+
229
+ @object.accepts_roles_by(subject)@. An alias for @subject.roles_for(object)@.
230
+
231
+ h2. Custom class names
232
+
233
+ You may want to deviate from default @User@ and @Role@ class names. That can easily be done with
234
+ arguments to @acts_as_...@.
235
+
236
+ Say, you have @Account@ and @AccountRole@:
237
+
238
+ <pre><code>
239
+ class Account < ActiveRecord::Base
240
+ acts_as_authorization_subject :role_class_name => 'AccountRole'
241
+ end
242
+
243
+ class AccountRole < ActiveRecord::Base
244
+ acts_as_authorization_role :subject_class_name => 'Account'
245
+ end
246
+
247
+ class FooBar < ActiveRecord::Base
248
+ acts_as_authorization_object :role_class_name => 'AccountRole', :subject_class_name => 'Account'
249
+ end
250
+ </code></pre>
251
+
252
+ Or... since Acl9 defaults can be changed in a special hash, you can put the following snippet:
253
+
254
+ <pre><code>
255
+ Acl9::config.merge!({
256
+ :default_role_class_name => 'AccountRole',
257
+ :default_subject_class_name => 'Account',
258
+ })
259
+ </code></pre>
260
+
261
+ ... into @config/initializers/acl9.rb@ and get rid of that clunky arguments:
262
+
263
+ <pre><code>
264
+ class Account < ActiveRecord::Base
265
+ acts_as_authorization_subject
266
+ end
267
+
268
+ class AccountRole < ActiveRecord::Base
269
+ acts_as_authorization_role
270
+ end
271
+
272
+ class FooBar < ActiveRecord::Base
273
+ acts_as_authorization_object
274
+ end
275
+ </code></pre>
276
+
277
+ Note that you'll need to change your database structure appropriately:
278
+
279
+ <pre><code>
280
+ create_table "account_roles", :force => true do |t|
281
+ t.string :name, :limit => 40
282
+ t.string :authorizable_type, :limit => 40
283
+ t.integer :authorizable_id
284
+ t.timestamps
285
+ end
286
+
287
+ create_table "account_roles_accounts", :id => false, :force => true do |t|
288
+ t.references :account
289
+ t.references :account_role
290
+ t.timestamps
291
+ end
292
+ </code></pre>
293
+
294
+ h2. Examples
295
+
296
+ <pre><code>
297
+ user = User.create!
298
+ user.has_role? 'admin' # => false
299
+
300
+ user.has_role! :admin
301
+
302
+ user.has_role? :admin # => true
303
+ </code></pre>
304
+
305
+ @user@ now has global role _admin_. Note that you can specify role name either
306
+ as a string or as a symbol.
307
+
308
+ <pre><code>
309
+ foo = Foo.create!
310
+
311
+ user.has_role? 'admin', foo # => false
312
+
313
+ user.has_role! :manager, foo
314
+
315
+ user.has_role? :manager, foo # => true
316
+ foo.accepts_role? :manager, user # => true
317
+
318
+ user.has_roles_for? foo # => true
319
+ </code></pre>
320
+
321
+ You can see here that global and object roles are distinguished from each other. User
322
+ with global role _admin_ isn't automatically admin of @foo@.
323
+
324
+ However,
325
+
326
+ <pre><code>
327
+ user.has_role? :manager # => true
328
+ </code></pre>
329
+
330
+ That is, if you have an object role, it means that you have a global role with the same name too!
331
+ In other words, you are _manager_ if you manage at least one @foo@ (or a @bar@...).
332
+
333
+ <pre><code>
334
+ bar = Bar.create!
335
+
336
+ user.has_role! :manager, bar
337
+ user.has_no_role! :manager, foo
338
+
339
+ user.has_role? :manager, foo # => false
340
+ user.has_role? :manager # => true
341
+ </code></pre>
342
+
343
+ Our @user@ is no more manager of @foo@, but has become a manager of @bar@.
344
+
345
+ <pre><code>
346
+ user.has_no_roles!
347
+
348
+ user.has_role? :manager # => false
349
+ user.has_role? :admin # => false
350
+ user.roles # => []
351
+ </code></pre>
352
+
353
+ At this time @user@ has no roles in the system.
354
+
355
+ h2. Coming up with your own role implementation
356
+
357
+ The described role system with its 2 tables (not counting the @users@ table!)
358
+ might be an overkill for many cases. If all you want is global roles without
359
+ any scope, you'd better off implementing it by hand.
360
+
361
+ The access control subsystem of Acl9 uses only @subject.has_role?@ method, so
362
+ there's no need to implement anything else except for own convenience.
363
+
364
+ For example, if each your user can have only one global role, just add @role@
365
+ column to your @User@ class:
366
+
367
+ <pre><code>
368
+ class User < ActiveRecord::Base
369
+ def has_role?(role_name, obj=nil)
370
+ self.role == role_name
371
+ end
372
+
373
+ def has_role!(role_name, obj=nil)
374
+ self.role = role_name
375
+ save!
376
+ end
377
+ end
378
+ </code></pre>
379
+
380
+ If you need to assign multiple roles to your users, you can use @serialize@
381
+ with role array or a special solution like
382
+ "preference_fu":http://github.com/brennandunn/preference_fu.
383
+
384
+ h1. Access control subsystem
385
+
386
+ By means of access control subsystem you can protect actions of your controller
387
+ from unauthorized access. Acl9 provides a nice DSL for writing access rules.
388
+
389
+ h2. Allow and deny
390
+
391
+ Access control is mostly about allowing and denying. So there are two
392
+ basic methods: @allow@ and @deny@. They have the same syntax:
393
+
394
+ <pre><code>
395
+ allow ROLE_LIST, OPTIONS
396
+ deny ROLE_LIST, OPTIONS
397
+ </code></pre>
398
+
399
+ h3. Specifying roles
400
+
401
+ ROLE_LIST is a list of roles (at least 1 role should be there). So,
402
+
403
+ <pre><code>
404
+ allow :manager, :admin
405
+ deny :banned
406
+ </code></pre>
407
+ will match holders of global role _manager_ *and* holders of global role _admin_ as allowed.
408
+ On the contrary, holders of _banned_ role will match as denied.
409
+
410
+ Basically this snippet is equivalent to
411
+
412
+ <pre><code>
413
+ allow :manager
414
+ allow :admin
415
+ deny :banned
416
+ </code></pre>
417
+ which means that roles in argument list are OR'ed for a match, and not AND'ed.
418
+
419
+ Also note that:
420
+ * You may use both strings and :symbols to specify roles (the latter get converted into strings).
421
+ * Role names are singularized before check.
422
+
423
+ Thus the snippet above can also be written as
424
+
425
+ <pre><code>
426
+ allow :managers, :admins
427
+ deny 'banned'
428
+ </code></pre>
429
+ or even
430
+
431
+ <pre><code>
432
+ allow *%w(managers admins)
433
+ deny 'banned'
434
+ </code></pre>
435
+
436
+ h3. Object and class roles
437
+
438
+ Examples in the previous section were all about global roles. Let's see how we can
439
+ use object and class roles in the ACL block.
440
+
441
+ <pre><code>
442
+ allow :responsible, :for => Widget
443
+ allow :possessor, :of => :foo
444
+ deny :angry, :at => :me
445
+ allow :interested, :in => Future
446
+ deny :short, :on => :time
447
+ deny :hated, :by => :us
448
+ </code></pre>
449
+
450
+ To specify an object you use one of the 6 preposition options:
451
+ * :of
452
+ * :at
453
+ * :on
454
+ * :by
455
+ * :for
456
+ * :in
457
+
458
+ They all have the same meaning, use one that makes better English out of your rule.
459
+
460
+ Now, each of these prepositions may point to a Class or a :symbol. In the former case we get
461
+ a class role. E.g. @allow :responsible, :for => Widget@ becomes @subject.has_role?('responsible', Widget)@.
462
+
463
+ Symbol is trickier, it means that the appropriate instance variable of the controller is taken
464
+ as an object.
465
+
466
+ @allow :possessor, :of => :foo@ is translated into
467
+ <code>subject.has_role?('possessor', controller.instance_variable_get('@foo'))</code>.
468
+
469
+ Checking against an instance variable has sense when you have another _before filter_ which is executed
470
+ *before* the one generated by @access_control@, like this:
471
+
472
+ <pre><code>
473
+ class MoorblesController < ApplicationController
474
+ before_filter :load_moorble, :only => [:edit, :update, :destroy]
475
+
476
+ access_control do
477
+ allow :creator, :of => :moorble
478
+
479
+ # ...
480
+ end
481
+
482
+ # ...
483
+
484
+ private
485
+
486
+ def load_moorble
487
+ @moorble = Moorble.find(params[:id])
488
+ end
489
+ end
490
+ </code></pre>
491
+
492
+ Note that the object option is applied to all of the roles you specify in the argument list.
493
+ As such,
494
+
495
+ <pre><code>
496
+ allow :devil, :son, :of => God
497
+ </code></pre>
498
+ is equivalent to
499
+
500
+ <pre><code>
501
+ allow :devil, :of => God
502
+ allow :son, :of => God
503
+ </code></pre>
504
+
505
+ but *NOT*
506
+
507
+ <pre><code>
508
+ allow :devil
509
+ allow :son, :of => God
510
+ </code></pre>
511
+
512
+ h3. Pseudo-roles
513
+
514
+ There are three pseudo-roles in the ACL: @all@, @anonymous@ and @logged_in@.
515
+
516
+ @allow all@ will always match (as well as @deny all@).
517
+
518
+ @allow anonymous@ and @deny anonymous@ will match when user is anonymous, i.e. subject is @nil@.
519
+ You may also use a shorter notation: @allow nil@ (@deny nil@).
520
+
521
+ @logged_in@ is direct opposite of @anonymous@, so @allow logged_in@ will match if the user is logged in
522
+ (subject is not @nil@).
523
+
524
+ No role checks are done in either case.
525
+
526
+ h3. Limiting action scope
527
+
528
+ By default rules apply to all actions of the controller. There are two options that
529
+ narrow the scope of the @deny@ or @allow@ rule: @:to@ and @:except@.
530
+
531
+ <pre><code>
532
+ allow :owner, :of => :site, :to => [:delete, :destroy]
533
+ deny anonymous, :except => [:index, :show]
534
+ </code></pre>
535
+
536
+ For the first rule to match not only the current user should be an _owner_ of the _site_, but also
537
+ current action should be _delete_ or _destroy_.
538
+
539
+ In the second rule anonymous user access is denied for all actions, except _index_ and _show_.
540
+
541
+ You may not specify both @:to@ and @:except@.
542
+
543
+ Note that you can use actions block instead of @:to@ (see _Actions block_
544
+ below). You can also use @:only@ and @:except@ options in the
545
+ @access_control@ call which will serve as options of the @before_filter@ and thus
546
+ limit the scope of the whole ACL.
547
+
548
+ h3. Rule conditions
549
+
550
+ You may create conditional rules using @:if@ and @:unless@ options.
551
+
552
+ <pre><code>
553
+ allow :owner, :of => :site, :to => [:delete, :destroy], :if => :chance_to_delete
554
+ </code></pre>
555
+
556
+ Controller's @:chance_to_delete@ method will be called here. The rule will match if the action
557
+ is 'delete' or 'destroy' AND if the method returned @true@.
558
+
559
+ @:unless@ has the opposite meaning and should return @false@ for a rule to match.
560
+
561
+ Both options can be specified in the same rule.
562
+
563
+ <pre><code>
564
+ allow :visitor, :to => [:index, :show], :if => :right_phase_of_the_moon?, :unless => :suspicious?
565
+ </code></pre>
566
+
567
+ @right_phase_of_the_moon?@ should return @true@ AND @suspicious?@ should return @false@ for a poor visitor to
568
+ see a page.
569
+
570
+ Currently only controller methods are supported (specify them as :symbols). Lambdas are *not* supported.
571
+
572
+ h2. Rule matching order
573
+
574
+ Rule matching system is similar to that of Apache web server. There are two modes: _default allow_
575
+ (corresponding to @Order Deny,Allow@ in Apache) and _default deny_ (@Order Allow,Deny@ in Apache).
576
+
577
+ h3. Setting modes
578
+
579
+ Mode is set with a @default@ call.
580
+
581
+ @default :allow@ will set _default allow_ mode.
582
+
583
+ @default :deny@ will set _default deny_ mode. Note that this is the default mode, i.e. it will be on
584
+ if you don't do a @default@ call at all.
585
+
586
+ h3. Matching algorithm
587
+
588
+ First of all, regardless of the mode, all @allow@ matches are OR'ed together and all @deny@ matches
589
+ are OR'ed as well.
590
+
591
+ We'll express this in the following manner:
592
+
593
+ <pre><code>
594
+ ALLOWED = (allow rule 1 matches?) OR ((allow rule 2 matches?) OR ...
595
+ NOT_DENIED = NOT ((deny rule 1 matches?) OR (deny rule 2 matches?) OR ...)
596
+ </code></pre>
597
+
598
+ So, ALLOWED is @true@ when either of the @allow@ rules matches, and NOT_DENIED is @true@ when none
599
+ of the @deny@ rules matches.
600
+
601
+ Let's denote the final result of algorithm as ALLOWANCE. If it's @true@, access is allowed, if @false@ - denied.
602
+
603
+ In the case of _default allow_:
604
+ <pre><code>
605
+ ALLOWANCE = ALLOWED OR NOT_DENIED
606
+ </code></pre>
607
+
608
+ In the case of _default deny_:
609
+ <pre><code>
610
+ ALLOWANCE = ALLOWED AND NOT_DENIED
611
+ </code></pre>
612
+
613
+ Same result as a table:
614
+
615
+ |_. Rule matches |_. Default allow mode |_. Default deny mode |
616
+ | None of the @allow@ and @deny@ rules matched. | Access is allowed. | Access is denied. |
617
+ | Some of the @allow@ rules matched, none of the @deny@ rules matched. | Access is allowed. | Access is allowed. |
618
+ | None of the @allow@ rules matched, some of the @deny@ rules matched. | Access is denied. | Access is denied. |
619
+ | Some of the @allow@ rules matched, some of the @deny@ rules matched. | Access is allowed. | Access is denied. |
620
+
621
+ Apparently _default deny_ mode is more strict, and that's because it's on by default.
622
+
623
+ h2. Actions block
624
+
625
+ You may group rules with the help of the @actions@ block.
626
+
627
+ An example from the imaginary @PostsController@:
628
+
629
+ <pre><code>
630
+ allow :admin
631
+
632
+ actions :index, :show do
633
+ allow all
634
+ end
635
+
636
+ actions :new, :create do
637
+ allow :managers, :of => Post
638
+ end
639
+
640
+ actions :edit, :update do
641
+ allow :owner, :of => :post
642
+ end
643
+
644
+ action :destroy do
645
+ allow :owner, :of => :post
646
+ end
647
+ </code></pre>
648
+
649
+ This is equivalent to:
650
+
651
+ <pre><code>
652
+ allow :admin
653
+
654
+ allow all, :to => [:index, :show]
655
+ allow :managers, :of => Post, :to => [:new, :create]
656
+ allow :owner, :of => :post, :to => [:edit, :update]
657
+ allow :owner, :of => :post, :to => :destroy
658
+ </code></pre>
659
+
660
+ Note that only @allow@ and @deny@ calls are available inside @actions@ block, and these may not have
661
+ @:to@/@:except@ options.
662
+
663
+ @action@ is just a synonym for @actions@.
664
+
665
+ h2. access_control method
666
+
667
+ By calling @access_control@ in your controller you can get your ACL block translated into...
668
+
669
+ # a lambda, installed with @before_filter@ and raising @Acl9::AccessDenied@ exception on occasion.
670
+ # a method, installed with @before_filter@ and raising @Acl9::AccessDenied@ exception on occasion.
671
+ # a method, returning @true@ or @false@, whether access is allowed or denied.
672
+
673
+ First case is by default. You can catch the exception with @rescue_from@ call and do something
674
+ you like: make a redirect, or render "Access denied" template, or whatever.
675
+
676
+ Second case is obtained with specifying method name as an argument to
677
+ @access_control@ (or using @:as_method@ option, see below) and may be helpful
678
+ if you want to use @skip_before_filter@ somewhere in the derived controller.
679
+
680
+ Third case will take place if you supply @:filter => false@ along with method
681
+ name. You'll get an ordinary method which you can call anywhere you want.
682
+
683
+ h3. :subject_method
684
+
685
+ Acl9 obtains the subject instance by calling specific method of the controller. By default it's
686
+ @:current_user@, but you may change it.
687
+
688
+ <pre><code>
689
+ class MyController < ApplicationController
690
+ access_control :subject_method => :current_account do
691
+ allow :nifty
692
+ # ...
693
+ end
694
+
695
+ # ...
696
+ end
697
+ </code></pre>
698
+
699
+ Subject method can also be changed globally. Place the following into @config/initializers/acl9.rb@:
700
+
701
+ <pre><code>
702
+ Acl9::config[:default_subject_method] = :current_account
703
+ </code></pre>
704
+
705
+ h3. :debug
706
+
707
+ @:debug => true@ will output the filtering expression into the debug log. If
708
+ Acl9 does something strange, you may look at it as the last resort.
709
+
710
+ h3. :as_method
711
+
712
+ In the case
713
+
714
+ <pre><code>
715
+ class NiftyController < ApplicationController
716
+ access_control :as_method => :acl do
717
+ allow :nifty
718
+ # ...
719
+ end
720
+
721
+ # ...
722
+ end
723
+ </code></pre>
724
+
725
+ access control checks will be added as @acl@ method onto MyController, with @before_filter :acl@ call thereafter.
726
+
727
+ Instead of using @:as_method@ you may specify the name of the method as a positional argument
728
+ to @access_control@:
729
+
730
+ <pre><code>
731
+ class MyController < ApplicationController
732
+ access_control :acl do
733
+ # ...
734
+ end
735
+
736
+ # ...
737
+ end
738
+ </code></pre>
739
+
740
+
741
+ h3. :filter
742
+
743
+ If you set @:filter@ to @false@ (it's @true@ by default) and also use
744
+ @:as_method@ (or method name as 1st argument to @access_control@, you'll get a
745
+ method which won't raise @Acl9::AccessDenied@ exception, but rather return
746
+ @true@ or @false@ (access allowed/denied).
747
+
748
+ <pre><code>
749
+ class SecretController < ApplicationController
750
+ access_control :secret_access?, :filter => false do
751
+ allow :james_bond
752
+ # ...
753
+ end
754
+
755
+ def index
756
+ if secret_access?
757
+ _secret_index
758
+ else
759
+ _ordinary_index
760
+ end
761
+ end
762
+
763
+ # ...
764
+
765
+ private
766
+
767
+ def _secret_index
768
+ # ...
769
+ end
770
+
771
+ def _ordinary_index
772
+ # ...
773
+ end
774
+ end
775
+ </code></pre>
776
+
777
+ The generated method can receive an objects hash as an argument. In this example,
778
+
779
+ <pre><code>
780
+ class LolController < ApplicationController
781
+ access_control :lolcats?, :filter => false do
782
+ allow :cats, :by => :lol
783
+ # ...
784
+ end
785
+ end
786
+ </code></pre>
787
+
788
+ you may not only call @lolcats?@ with no arguments, which will basically return
789
+
790
+ <pre><code>
791
+ current_user.has_role?('cats', @lol)
792
+ </code></pre>
793
+
794
+ but also as @lolcats?(:lol => Lol.find(params[:lol]))@. The hash will be looked into first,
795
+ even if you have an instance variable @lol@.
796
+
797
+ h3. :helper
798
+
799
+ Sometimes you want to have a boolean method (like @:filter => false@) accessible
800
+ in your views. Acl9 can call @helper_method@ for you:
801
+
802
+ <pre><code>
803
+ class LolController < ApplicationController
804
+ access_control :helper => :lolcats? do
805
+ allow :cats, :by => :lol
806
+ # ...
807
+ end
808
+ end
809
+ </code></pre>
810
+
811
+ That's equivalent to
812
+
813
+ <pre><code>
814
+ class LolController < ApplicationController
815
+ access_control :lolcats?, :filter => false do
816
+ allow :cats, :by => :lol
817
+ # ...
818
+ end
819
+
820
+ helper_method :lolcats?
821
+ end
822
+ </code></pre>
823
+
824
+ h3. Other options
825
+
826
+ Other options will be passed to @before_filter@. As such, you may use @:only@ and @:except@ to narrow
827
+ the action scope of the whole ACL block.
828
+
829
+ <pre><code>
830
+ class OmgController < ApplicationController
831
+ access_control :only => [:index, :show] do
832
+ allow all
833
+ deny :banned
834
+ end
835
+
836
+ # ...
837
+ end
838
+ </code></pre>
839
+
840
+ is basically equivalent to
841
+
842
+ <pre><code>
843
+ class OmgController < ApplicationController
844
+ access_control do
845
+ actions :index, :show do
846
+ allow all
847
+ deny :banned
848
+ end
849
+
850
+ allow all, :except => [:index, :show]
851
+ end
852
+
853
+ # ...
854
+ end
855
+ </code></pre>
856
+
857
+ h2. access_control in your helpers
858
+
859
+ Apart from using @:helper@ option for @access_control@ call inside controller, there's a
860
+ way to generate helper methods directly, like this:
861
+
862
+ <pre><code>
863
+ module SettingsHelper
864
+ include Acl9Helpers
865
+
866
+ access_control :show_settings? do
867
+ allow :admin
868
+ allow :settings_manager
869
+ end
870
+ end
871
+ </code></pre>
872
+
873
+ Here we mix in @Acl9Helpers@ module which brings in @access_control@ method and call it,
874
+ obtaining @show_settings?@ method.
875
+
876
+ An imaginary view:
877
+
878
+ <pre><code>
879
+ <% if show_settings? %>
880
+ <%= link_to 'Settings', settings_path %>
881
+ <% end %>
882
+ </code></pre>
883
+
884
+ h2. show_to in your views
885
+
886
+ @show_to@ is predefined helper for your views:
887
+
888
+ <pre><code>
889
+ <% show_to :admin, :supervisor do %>
890
+ <%= link_to 'destroy', destroy_path %>
891
+ <% end %>
892
+ </code></pre>
893
+
894
+ or even
895
+
896
+ <pre><code>
897
+ <% show_to :prince, :of => :persia do %>
898
+ <%= link_to 'Princess', princess_path %>
899
+ <% end %>
900
+ </code></pre>
901
+
902
+
903
+ Copyright (c) 2009, 2010 Oleg Dashevskii, released under the MIT license.