codeprimate-cancan 1.6.5

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.
Files changed (39) hide show
  1. data/CHANGELOG.rdoc +291 -0
  2. data/Gemfile +20 -0
  3. data/LICENSE +20 -0
  4. data/README.rdoc +111 -0
  5. data/Rakefile +18 -0
  6. data/init.rb +1 -0
  7. data/lib/cancan.rb +13 -0
  8. data/lib/cancan/ability.rb +298 -0
  9. data/lib/cancan/controller_additions.rb +389 -0
  10. data/lib/cancan/controller_resource.rb +222 -0
  11. data/lib/cancan/exceptions.rb +50 -0
  12. data/lib/cancan/inherited_resource.rb +19 -0
  13. data/lib/cancan/matchers.rb +14 -0
  14. data/lib/cancan/model_adapters/abstract_adapter.rb +56 -0
  15. data/lib/cancan/model_adapters/active_record_adapter.rb +165 -0
  16. data/lib/cancan/model_adapters/data_mapper_adapter.rb +34 -0
  17. data/lib/cancan/model_adapters/default_adapter.rb +7 -0
  18. data/lib/cancan/model_adapters/mongoid_adapter.rb +53 -0
  19. data/lib/cancan/model_additions.rb +31 -0
  20. data/lib/cancan/rule.rb +142 -0
  21. data/lib/generators/cancan/ability/USAGE +4 -0
  22. data/lib/generators/cancan/ability/ability_generator.rb +11 -0
  23. data/lib/generators/cancan/ability/templates/ability.rb +28 -0
  24. data/spec/README.rdoc +28 -0
  25. data/spec/cancan/ability_spec.rb +419 -0
  26. data/spec/cancan/controller_additions_spec.rb +137 -0
  27. data/spec/cancan/controller_resource_spec.rb +412 -0
  28. data/spec/cancan/exceptions_spec.rb +58 -0
  29. data/spec/cancan/inherited_resource_spec.rb +42 -0
  30. data/spec/cancan/matchers_spec.rb +33 -0
  31. data/spec/cancan/model_adapters/active_record_adapter_spec.rb +278 -0
  32. data/spec/cancan/model_adapters/data_mapper_adapter_spec.rb +119 -0
  33. data/spec/cancan/model_adapters/default_adapter_spec.rb +7 -0
  34. data/spec/cancan/model_adapters/mongoid_adapter_spec.rb +216 -0
  35. data/spec/cancan/rule_spec.rb +39 -0
  36. data/spec/matchers.rb +13 -0
  37. data/spec/spec.opts +2 -0
  38. data/spec/spec_helper.rb +41 -0
  39. metadata +167 -0
@@ -0,0 +1,291 @@
1
+ 1.6.5 (May 18, 2011)
2
+
3
+ * pass action and subject through AccessDenied exception when :through isn't found - issue #366
4
+
5
+ * many Mongoid adapter improvements (thanks rahearn, cardagin) - issues #363, #352, #343
6
+
7
+ * allow :through option to work with private controller methods - issue #360
8
+
9
+ * ensure Mongoid::Document is defined before loading Mongoid adapter - issue #359
10
+
11
+ * many DataMapper adapter improvements (thanks emmanuel) - issue #355
12
+
13
+ * handle checking nil attributes through associations (thanks thatothermitch) - issue #330
14
+
15
+ * improve scope merging - issue #328
16
+
17
+
18
+
19
+ 1.6.4 (March 29, 2011)
20
+
21
+ * Fixed mongoid 'or' error - see issue #322
22
+
23
+
24
+ 1.6.3 (March 25, 2011)
25
+
26
+ * Make sure ActiveRecord::Relation is defined before checking conditions against it so Rails 2 is supported again - see issue #312
27
+
28
+ * Return subject passed to authorize! - see issue #314
29
+
30
+
31
+ 1.6.2 (March 18, 2011)
32
+
33
+ * Fixed instance loading when :singleton option is used - see issue #310
34
+
35
+
36
+ 1.6.1 (March 15, 2011)
37
+
38
+ * Use Item.new instead of build_item for singleton resource so it doesn't effect database - see issue #304
39
+
40
+ * Made accessible_by action default to :index and parent action default to :show instead of :read - see issue #302
41
+
42
+ * Reverted Inherited Resources "collection" override since it doesn't seem to be working - see issue #305
43
+
44
+
45
+ 1.6.0 (March 11, 2011)
46
+
47
+ * Added MetaWhere support - see issue #194 and #261
48
+
49
+ * Allow Active Record scopes in Ability conditions - see issue #257
50
+
51
+ * Added :if and :unless options to check_authorization - see issue #284
52
+
53
+ * Several Inherited Resources fixes (thanks aq1018, tanordheim and stefanoverna)
54
+
55
+ * Pass action name to accessible_by call when loading a collection (thanks amw)
56
+
57
+ * Added :prepend option to load_and_authorize_resource to load before other filters - see issue #290
58
+
59
+ * Fixed spacing issue in I18n message for multi-word model names - see issue #292
60
+
61
+ * Load resource collection for any action which doesn't have an "id" parameter - see issue #296
62
+
63
+ * Raise an exception when trying to make a Ability condition with both a hash of conditions and a block - see issue #269
64
+
65
+
66
+ 1.5.1 (January 20, 2011)
67
+
68
+ * Fixing deeply nested conditions in Active Record adapter - see issue #246
69
+
70
+ * Improving Mongoid support for multiple can and cannot definitions (thanks stellard) - see issue #239
71
+
72
+
73
+ 1.5.0 (January 11, 2011)
74
+
75
+ * Added an Ability generator - see issue #170
76
+
77
+ * Added DataMapper support (thanks natemueller)
78
+
79
+ * Added Mongoid support (thanks bowsersenior)
80
+
81
+ * Added skip_load_and_authorize_resource methods to controller class - see issue #164
82
+
83
+ * Added support for uncountable resources in index action - see issue #193
84
+
85
+ * Cleaned up README and added spec/README
86
+
87
+ * Internal: renamed CanDefinition to Rule
88
+
89
+ * Internal: added a model adapter layer for easily supporting more ORMs
90
+
91
+ * Internal: added .rvmrc to auto-switch to 1.8.7 with gemset - see issue #231
92
+
93
+
94
+ 1.4.1 (November 12, 2010)
95
+
96
+ * Renaming skip_authorization to skip_authorization_check - see issue #169
97
+
98
+ * Adding :through_association option to load_resource (thanks hunterae) - see issue #171
99
+
100
+ * The :shallow option now works with the :singleton option (thanks nandalopes) - see issue #187
101
+
102
+ * Play nicely with quick_scopes gem (thanks ramontayag) - see issue #183
103
+
104
+ * Fix odd behavior when "cache_classes = false" (thanks mphalliday) - see issue #174
105
+
106
+
107
+ 1.4.0 (October 5, 2010)
108
+
109
+ * Adding Gemfile; to get specs running just +bundle+ and +rake+ - see issue #163
110
+
111
+ * Stop at 'cannot' definition when there are no conditions - see issue #161
112
+
113
+ * The :through option will now call a method with that name if instance variable doesn't exist - see issue #146
114
+
115
+ * Adding :shallow option to load_resource to bring back old behavior of fetching a child without a parent
116
+
117
+ * Raise AccessDenied error when loading a child and parent resource isn't found
118
+
119
+ * Abilities defined on a module will apply to anything that includes that module - see issue #150 and #152
120
+
121
+ * Abilities can be defined with a string of SQL in addition to a block so accessible_by works with a block - see issue #150
122
+
123
+ * Adding better support for InheritedResource - see issue #23
124
+
125
+ * Loading the collection instance variable (for index action) using accessible_by - see issue #137
126
+
127
+ * Adding action and subject variables to I18n unauthorized message - closes #142
128
+
129
+ * Adding check_authorization and skip_authorization controller class methods to ensure authorization is performed (thanks justinko) - see issue #135
130
+
131
+ * Setting initial attributes based on ability conditions in new/create actions - see issue #114
132
+
133
+ * Check parent attributes for nested association in index action - see issue #121
134
+
135
+ * Supporting nesting in can? method using hash - see issue #121
136
+
137
+ * Adding I18n support for Access Denied messages (thanks EppO) - see issue #103
138
+
139
+ * Passing no arguments to +can+ definition will pass action, class, and object to block - see issue #129
140
+
141
+ * Don't pass action to block in +can+ definition when using :+manage+ option - see issue #129
142
+
143
+ * No longer calling block in +can+ definition when checking on class - see issue #116
144
+
145
+
146
+ 1.3.4 (August 31, 2010)
147
+
148
+ * Don't stop at +cannot+ with hash conditions when checking class (thanks tamoya) - see issue #131
149
+
150
+
151
+ 1.3.3 (August 20, 2010)
152
+
153
+ * Switching to Rspec namespace to remove deprecation warning in Rspec 2 - see issue #119
154
+
155
+ * Pluralize nested associations for conditions in accessible_by (thanks mlooney) - see issue #123
156
+
157
+
158
+ 1.3.2 (August 7, 2010)
159
+
160
+ * Fixing slice error when passing in custom resource name - see issue #112
161
+
162
+
163
+ 1.3.1 (August 6, 2010)
164
+
165
+ * Fixing protected sanitize_sql error - see issue #111
166
+
167
+
168
+ 1.3.0 (August 6, 2010)
169
+
170
+ * Adding :find_by option to load_resource - see issue #19
171
+
172
+ * Adding :singleton option to load_resource - see issue #93
173
+
174
+ * Supporting multiple resources in :through option for polymorphic associations - see issue #73
175
+
176
+ * Supporting Single Table Inheritance for "can" comparisons - see issue #55
177
+
178
+ * Adding :instance_name option to load/authorize_resource - see issue #44
179
+
180
+ * Don't pass nil to "new" to keep MongoMapper happy - see issue #63
181
+
182
+ * Parent resources are now authorized with :read action.
183
+
184
+ * Changing :resource option in load/authorize_resource back to :class with ability to pass false
185
+
186
+ * Removing :nested option in favor of :through option with separate load/authorize call
187
+
188
+ * Moving internal logic from ResourceAuthorization to ControllerResource class
189
+
190
+ * Supporting multiple "can" and "cannot" calls with accessible_by (thanks funny-falcon) - see issue #71
191
+
192
+ * Supporting deeply nested aliases - see issue #98
193
+
194
+
195
+ 1.2.0 (July 16, 2010)
196
+
197
+ * Load nested parent resources on collection actions such as "index" (thanks dohzya)
198
+
199
+ * Adding :name option to load_and_authorize_resource if it does not match controller - see issue #65
200
+
201
+ * Fixing issue when using accessible_by with nil can conditions (thanks jrallison) - see issue #66
202
+
203
+ * Pluralize table name for belongs_to associations in can conditions hash (thanks logandk) - see issue #62
204
+
205
+ * Support has_many association or arrays in can conditions hash
206
+
207
+ * Adding joins clause to accessible_by when conditions are across associations
208
+
209
+
210
+ 1.1.1 (April 17, 2010)
211
+
212
+ * Fixing behavior in Rails 3 by properly initializing ResourceAuthorization
213
+
214
+
215
+ 1.1.0 (April 17, 2010)
216
+
217
+ * Supporting arrays, ranges, and nested hashes in ability conditions
218
+
219
+ * Removing "unauthorized!" method in favor of "authorize!" in controllers
220
+
221
+ * Adding action, subject and default_message abilities to AccessDenied exception - see issue #40
222
+
223
+ * Adding caching to current_ability controller method, if you're overriding this be sure to add caching too.
224
+
225
+ * Adding "accessible_by" method to Active Record for fetching records matching a specific ability
226
+
227
+ * Adding conditions behavior to Ability#can and fetch with Ability#conditions - see issue #53
228
+
229
+ * Renaming :class option to :resource for load_and_authorize_resource which now supports a symbol for non models - see issue #45
230
+
231
+ * Properly handle Admin::AbilitiesController in params[:controller] - see issue #46
232
+
233
+ * Adding be_able_to RSpec matcher (thanks dchelimsky), requires Ruby 1.8.7 or higher - see issue #54
234
+
235
+ * Support additional arguments to can? which get passed to the block - see issue #48
236
+
237
+
238
+ 1.0.2 (Dec 30, 2009)
239
+
240
+ * Adding clear_aliased_actions to Ability which removes previously defined actions including defaults - see issue #20
241
+
242
+ * Append aliased actions (don't overwrite them) - see issue #20
243
+
244
+ * Adding custom message argument to unauthorized! method (thanks tjwallace) - see issue #18
245
+
246
+
247
+ 1.0.1 (Dec 14, 2009)
248
+
249
+ * Adding :class option to load_resource so one can customize which class to use for the model - see issue #17
250
+
251
+ * Don't fetch parent of nested resource if *_id parameter is missing so it works with shallow nested routes - see issue #14
252
+
253
+
254
+ 1.0.0 (Dec 13, 2009)
255
+
256
+ * Don't set resource instance variable if it has been set already - see issue #13
257
+
258
+ * Allowing :nested option to accept an array for deep nesting
259
+
260
+ * Adding :nested option to load resource method - see issue #10
261
+
262
+ * Pass :only and :except options to before filters for load/authorize resource methods.
263
+
264
+ * Adding :collection and :new options to load_resource method so we can specify behavior of additional actions if needed.
265
+
266
+ * BACKWARDS INCOMPATIBLE: turning load and authorize resource methods into class methods which set up the before filter so they can accept additional arguments.
267
+
268
+
269
+ 0.2.1 (Nov 26, 2009)
270
+
271
+ * many internal refactorings - see issues #11 and #12
272
+
273
+ * adding "cannot" method to define which abilities cannot be done - see issue #7
274
+
275
+ * support custom objects (usually symbols) in can definition - see issue #8
276
+
277
+
278
+ 0.2.0 (Nov 17, 2009)
279
+
280
+ * fix behavior of load_and_authorize_resource for namespaced controllers - see issue #3
281
+
282
+ * support arrays being passed to "can" to specify multiple actions or classes - see issue #2
283
+
284
+ * adding "cannot?" method to ability, controller, and view which is inverse of "can?" - see issue #1
285
+
286
+ * BACKWARDS INCOMPATIBLE: use Ability#initialize instead of 'prepare' to set up abilities - see issue #4
287
+
288
+
289
+ 0.1.0 (Nov 16, 2009)
290
+
291
+ * initial release
data/Gemfile ADDED
@@ -0,0 +1,20 @@
1
+ source "http://rubygems.org"
2
+
3
+ case ENV["MODEL_ADAPTER"]
4
+ when nil, "active_record"
5
+ gem "sqlite3"
6
+ gem "activerecord", '~> 3.0.9', :require => "active_record"
7
+ gem "with_model"
8
+ gem "meta_where"
9
+ when "data_mapper"
10
+ gem "dm-core", "~> 1.0.2"
11
+ gem "dm-sqlite-adapter", "~> 1.0.2"
12
+ gem "dm-migrations", "~> 1.0.2"
13
+ when "mongoid"
14
+ gem "bson_ext", "~> 1.1"
15
+ gem "mongoid", "~> 2.0.0.beta.20"
16
+ else
17
+ raise "Unknown model adapter: #{ENV["MODEL_ADAPTER"]}"
18
+ end
19
+
20
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Ryan Bates
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,111 @@
1
+ = CanCan
2
+
3
+ Wiki[https://github.com/ryanb/cancan/wiki] | RDocs[http://rdoc.info/projects/ryanb/cancan] | Screencast[http://railscasts.com/episodes/192-authorization-with-cancan]
4
+
5
+ CanCan is an authorization library for Ruby on Rails which restricts what resources a given user is allowed to access. All permissions are defined in a single location (the +Ability+ class) and not duplicated across controllers, views, and database queries.
6
+
7
+
8
+ == Installation
9
+
10
+ In <b>Rails 3</b>, add this to your Gemfile and run the +bundle+ command.
11
+
12
+ gem "cancan"
13
+
14
+ In <b>Rails 2</b>, add this to your environment.rb file.
15
+
16
+ config.gem "cancan"
17
+
18
+ Alternatively, you can install it as a plugin.
19
+
20
+ rails plugin install git://github.com/ryanb/cancan.git
21
+
22
+
23
+ == Getting Started
24
+
25
+ CanCan expects a +current_user+ method to exist in the controller. First, set up some authentication (such as Authlogic[https://github.com/binarylogic/authlogic] or Devise[https://github.com/plataformatec/devise]). See {Changing Defaults}[https://github.com/ryanb/cancan/wiki/changing-defaults] if you need different behavior.
26
+
27
+
28
+ === 1. Define Abilities
29
+
30
+ User permissions are defined in an +Ability+ class. CanCan 1.5 includes a Rails 3 generator for creating this class.
31
+
32
+ rails g cancan:ability
33
+
34
+ See {Defining Abilities}[https://github.com/ryanb/cancan/wiki/defining-abilities] for details.
35
+
36
+
37
+ === 2. Check Abilities & Authorization
38
+
39
+ The current user's permissions can then be checked using the <tt>can?</tt> and <tt>cannot?</tt> methods in the view and controller.
40
+
41
+ <% if can? :update, @article %>
42
+ <%= link_to "Edit", edit_article_path(@article) %>
43
+ <% end %>
44
+
45
+ See {Checking Abilities}[https://github.com/ryanb/cancan/wiki/checking-abilities] for more information
46
+
47
+ The <tt>authorize!</tt> method in the controller will raise an exception if the user is not able to perform the given action.
48
+
49
+ def show
50
+ @article = Article.find(params[:id])
51
+ authorize! :read, @article
52
+ end
53
+
54
+ Setting this for every action can be tedious, therefore the +load_and_authorize_resource+ method is provided to automatically authorize all actions in a RESTful style resource controller. It will use a before filter to load the resource into an instance variable and authorize it for every action.
55
+
56
+ class ArticlesController < ApplicationController
57
+ load_and_authorize_resource
58
+
59
+ def show
60
+ # @article is already loaded and authorized
61
+ end
62
+ end
63
+
64
+ See {Authorizing Controller Actions}[https://github.com/ryanb/cancan/wiki/authorizing-controller-actions] for more information.
65
+
66
+
67
+ === 3. Handle Unauthorized Access
68
+
69
+ If the user authorization fails, a <tt>CanCan::AccessDenied</tt> exception will be raised. You can catch this and modify its behavior in the +ApplicationController+.
70
+
71
+ class ApplicationController < ActionController::Base
72
+ rescue_from CanCan::AccessDenied do |exception|
73
+ redirect_to root_url, :alert => exception.message
74
+ end
75
+ end
76
+
77
+ See {Exception Handling}[https://github.com/ryanb/cancan/wiki/exception-handling] for more information.
78
+
79
+
80
+ === 4. Lock It Down
81
+
82
+ If you want to ensure authorization happens on every action in your application, add +check_authorization+ to your ApplicationController.
83
+
84
+ class ApplicationController < ActionController::Base
85
+ check_authorization
86
+ end
87
+
88
+ This will raise an exception if authorization is not performed in an action. If you want to skip this add +skip_authorization_check+ to a controller subclass. See {Ensure Authorization}[https://github.com/ryanb/cancan/wiki/Ensure-Authorization] for more information.
89
+
90
+
91
+ == Wiki Docs
92
+
93
+ * {Upgrading to 1.6}[https://github.com/ryanb/cancan/wiki/Upgrading-to-1.6]
94
+ * {Defining Abilities}[https://github.com/ryanb/cancan/wiki/Defining-Abilities]
95
+ * {Checking Abilities}[https://github.com/ryanb/cancan/wiki/Checking-Abilities]
96
+ * {Authorizing Controller Actions}[https://github.com/ryanb/cancan/wiki/Authorizing-Controller-Actions]
97
+ * {Exception Handling}[https://github.com/ryanb/cancan/wiki/Exception-Handling]
98
+ * {Changing Defaults}[https://github.com/ryanb/cancan/wiki/Changing-Defaults]
99
+ * {See more}[https://github.com/ryanb/cancan/wiki]
100
+
101
+
102
+ == Questions or Problems?
103
+
104
+ If you have any issues with CanCan which you cannot find the solution to in the documentation[https://github.com/ryanb/cancan/wiki], please add an {issue on GitHub}[https://github.com/ryanb/cancan/issues] or fork the project and send a pull request.
105
+
106
+ To get the specs running you should call +bundle+ and then +rake+. See the {spec/README}[https://github.com/ryanb/cancan/blob/master/spec/README.rdoc] for more information.
107
+
108
+
109
+ == Special Thanks
110
+
111
+ CanCan was inspired by declarative_authorization[https://github.com/stffn/declarative_authorization/] and aegis[https://github.com/makandra/aegis]. Also many thanks to the CanCan contributors[https://github.com/ryanb/cancan/contributors]. See the CHANGELOG[https://github.com/ryanb/cancan/blob/master/CHANGELOG.rdoc] for the full list.
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'rspec/core/rake_task'
4
+
5
+ desc "Run RSpec"
6
+ RSpec::Core::RakeTask.new do |t|
7
+ t.verbose = false
8
+ end
9
+
10
+ desc "Run specs for all adapters"
11
+ task :spec_all do
12
+ %w[active_record data_mapper mongoid].each do |model_adapter|
13
+ puts "MODEL_ADAPTER = #{model_adapter}"
14
+ system "rake spec MODEL_ADAPTER=#{model_adapter}"
15
+ end
16
+ end
17
+
18
+ task :default => :spec