Compare commits
39 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ff8c11cfc5 | ||
|
|
f1ba76b61b | ||
|
|
283f58ee16 | ||
|
|
f46696348e | ||
|
|
8903feee70 | ||
|
|
ecf2818a9e | ||
|
|
d9f3c8b0ae | ||
|
|
240c281061 | ||
|
|
ef5900c5b1 | ||
|
|
37f482e8d5 | ||
|
|
3c68a911d0 | ||
|
|
baeef0b9dd | ||
|
|
23a5888fe0 | ||
|
|
f2a1695636 | ||
|
|
6e1e96c85a | ||
|
|
cf49c5b9de | ||
|
|
35c4864de4 | ||
|
|
510cf509ee | ||
|
|
69f7a65914 | ||
|
|
f027b2ebb3 | ||
|
|
5d4138f0b2 | ||
|
|
3b9cdce09a | ||
|
|
a13f78d6f9 | ||
|
|
41f67ad6d0 | ||
|
|
7d3b4cdbc2 | ||
|
|
f99d506050 | ||
|
|
ef22de689b | ||
|
|
67416532f4 | ||
|
|
f919ac53bb | ||
|
|
021f33c9a0 | ||
|
|
e9f01300b6 | ||
|
|
f7480d1f5a | ||
|
|
ffa677b2b0 | ||
|
|
a75aee751b | ||
|
|
51fa61bbae | ||
|
|
cd217eb9cf | ||
|
|
94e031bf96 | ||
|
|
63634b4f5d | ||
|
|
a5f98824a0 |
@@ -1,3 +1,57 @@
|
|||||||
|
1.1.0 (April 17, 2010)
|
||||||
|
|
||||||
|
* Supporting arrays, ranges, and nested hashes in ability conditions
|
||||||
|
|
||||||
|
* Removing "unauthorized!" method in favor of "authorize!" in controllers
|
||||||
|
|
||||||
|
* Adding action, subject and default_message abilities to AccessDenied exception - see issue #40
|
||||||
|
|
||||||
|
* Adding caching to current_ability controller method, if you're overriding this be sure to add caching too.
|
||||||
|
|
||||||
|
* Adding "accessible_by" method to Active Record for fetching records matching a specific ability
|
||||||
|
|
||||||
|
* Adding conditions behavior to Ability#can and fetch with Ability#conditions - see issue #53
|
||||||
|
|
||||||
|
* Renaming :class option to :resource for load_and_authorize_resource which now supports a symbol for non models - see issue #45
|
||||||
|
|
||||||
|
* Properly handle Admin::AbilitiesController in params[:controller] - see issue #46
|
||||||
|
|
||||||
|
* Adding be_able_to RSpec matcher (thanks dchelimsky), requires Ruby 1.8.7 or higher - see issue #54
|
||||||
|
|
||||||
|
* Support additional arguments to can? which get passed to the block - see issue #48
|
||||||
|
|
||||||
|
|
||||||
|
1.0.2 (Dec 30, 2009)
|
||||||
|
|
||||||
|
* Adding clear_aliased_actions to Ability which removes previously defined actions including defaults - see issue #20
|
||||||
|
|
||||||
|
* Append aliased actions (don't overwrite them) - see issue #20
|
||||||
|
|
||||||
|
* Adding custom message argument to unauthorized! method (thanks tjwallace) - see issue #18
|
||||||
|
|
||||||
|
|
||||||
|
1.0.1 (Dec 14, 2009)
|
||||||
|
|
||||||
|
* Adding :class option to load_resource so one can customize which class to use for the model - see issue #17
|
||||||
|
|
||||||
|
* Don't fetch parent of nested resource if *_id parameter is missing so it works with shallow nested routes - see issue #14
|
||||||
|
|
||||||
|
|
||||||
|
1.0.0 (Dec 13, 2009)
|
||||||
|
|
||||||
|
* Don't set resource instance variable if it has been set already - see issue #13
|
||||||
|
|
||||||
|
* Allowing :nested option to accept an array for deep nesting
|
||||||
|
|
||||||
|
* Adding :nested option to load resource method - see issue #10
|
||||||
|
|
||||||
|
* Pass :only and :except options to before filters for load/authorize resource methods.
|
||||||
|
|
||||||
|
* Adding :collection and :new options to load_resource method so we can specify behavior of additional actions if needed.
|
||||||
|
|
||||||
|
* BACKWARDS INCOMPATIBLE: turning load and authorize resource methods into class methods which set up the before filter so they can accept additional arguments.
|
||||||
|
|
||||||
|
|
||||||
0.2.1 (Nov 26, 2009)
|
0.2.1 (Nov 26, 2009)
|
||||||
|
|
||||||
* many internal refactorings - see issues #11 and #12
|
* many internal refactorings - see issues #11 and #12
|
||||||
@@ -6,6 +60,7 @@
|
|||||||
|
|
||||||
* support custom objects (usually symbols) in can definition - see issue #8
|
* support custom objects (usually symbols) in can definition - see issue #8
|
||||||
|
|
||||||
|
|
||||||
0.2.0 (Nov 17, 2009)
|
0.2.0 (Nov 17, 2009)
|
||||||
|
|
||||||
* fix behavior of load_and_authorize_resource for namespaced controllers - see issue #3
|
* fix behavior of load_and_authorize_resource for namespaced controllers - see issue #3
|
||||||
|
|||||||
157
README.rdoc
157
README.rdoc
@@ -1,29 +1,26 @@
|
|||||||
= CanCan
|
= CanCan
|
||||||
|
|
||||||
This is a simple authorization solution for Ruby on Rails to restrict what a given user is allowed to access in the application. This is completely decoupled from any role based implementation allowing you to define user roles the way you want. All permissions are stored in a single location for convenience.
|
Wiki[http://wiki.github.com/ryanb/cancan] | RDocs[http://rdoc.info/projects/ryanb/cancan] | Screencast[http://railscasts.com/episodes/192-authorization-with-cancan] | Metrics[http://getcaliper.com/caliper/project?repo=git%3A%2F%2Fgithub.com%2Fryanb%2Fcancan.git]
|
||||||
|
|
||||||
This assumes you already have authentication (such as Authlogic[http://github.com/binarylogic/authlogic]) which provides a current_user model.
|
CanCan is an authorization solution for Ruby on Rails. This restricts what a given user is allowed to access throughout the application. It is completely decoupled from any role based implementation and focusses on keeping permission logic in a single location (the +Ability+ class) so it is not duplicated across controllers, views, and database queries.
|
||||||
|
|
||||||
|
This assumes you already have authentication (such as Authlogic[http://github.com/binarylogic/authlogic] or Devise[http://github.com/plataformatec/devise]). This will provide a +current_user+ method which CanCan relies on. See {Changing Defaults}[http://wiki.github.com/ryanb/cancan/changing-defaults] if you need different behavior.
|
||||||
|
|
||||||
See the RDocs[http://rdoc.info/projects/ryanb/cancan] and Wiki[http://wiki.github.com/ryanb/cancan] for additional documentation.
|
|
||||||
|
|
||||||
== Installation
|
== Installation
|
||||||
|
|
||||||
You can set it up as a gem in your environment.rb file.
|
CanCan is provided as a gem. Simply include it in your environment.rb or Gemfile.
|
||||||
|
|
||||||
config.gem "cancan", :source => "http://gemcutter.org"
|
config.gem "cancan"
|
||||||
|
|
||||||
And then install the gem.
|
Alternatively it can be installed as a plugin.
|
||||||
|
|
||||||
sudo rake gems:install
|
|
||||||
|
|
||||||
Alternatively you can install it as a Rails plugin.
|
|
||||||
|
|
||||||
script/plugin install git://github.com/ryanb/cancan.git
|
script/plugin install git://github.com/ryanb/cancan.git
|
||||||
|
|
||||||
|
|
||||||
== Setup
|
== Getting Started
|
||||||
|
|
||||||
First, define a class called Ability in "models/ability.rb".
|
First, define a class called +Ability+ in "models/ability.rb". It should look something like this.
|
||||||
|
|
||||||
class Ability
|
class Ability
|
||||||
include CanCan::Ability
|
include CanCan::Ability
|
||||||
@@ -39,144 +36,110 @@ First, define a class called Ability in "models/ability.rb".
|
|||||||
|
|
||||||
This is where all permissions will go. See the "Defining Abilities" section below for more information.
|
This is where all permissions will go. See the "Defining Abilities" section below for more information.
|
||||||
|
|
||||||
You can access the current permissions at any point using the "can?" and "cannot?" methods in the view.
|
The current user's permissions can be accessed using the "can?" and "cannot?" methods in the view and controller.
|
||||||
|
|
||||||
<% if can? :update, @article %>
|
<% if can? :update, @article %>
|
||||||
<%= link_to "Edit", edit_article_path(@article) %>
|
<%= link_to "Edit", edit_article_path(@article) %>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|
||||||
You can also use these methods in a controller along with the "unauthorized!" method to restrict access.
|
See {Checking Abilities}[http://wiki.github.com/ryanb/cancan/checking-abilities] for more information
|
||||||
|
|
||||||
|
The "authorize!" method in the controller will raise an exception if the user is not able to perform the given action.
|
||||||
|
|
||||||
def show
|
def show
|
||||||
@article = Article.find(params[:id])
|
@article = Article.find(params[:id])
|
||||||
unauthorized! if cannot? :read, @article
|
authorize! :read, @article
|
||||||
end
|
end
|
||||||
|
|
||||||
Setting this for every action can be tedious, therefore a before filter is also provided to automatically authorize all actions in a RESTful style resource controller.
|
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 set up a before filter which loads the resource into the instance variable and authorizes it for each action.
|
||||||
|
|
||||||
class ArticlesController < ApplicationController
|
class ArticlesController < ApplicationController
|
||||||
before_filter :load_and_authorize_resource
|
load_and_authorize_resource
|
||||||
|
|
||||||
def show
|
def show
|
||||||
# @article is already loaded
|
# @article is already loaded and authorized
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
If the user authorization fails, a CanCan::AccessDenied exception will be raised. You can catch this and modify its behavior in the ApplicationController.
|
See {Authorizing Controller Actions}[http://wiki.github.com/ryanb/cancan/authorizing-controller-actions] for more information
|
||||||
|
|
||||||
|
If the user authorization fails a CanCan::AccessDenied exception will be raised. You can catch this and modify its behavior in the +ApplicationController+.
|
||||||
|
|
||||||
class ApplicationController < ActionController::Base
|
class ApplicationController < ActionController::Base
|
||||||
rescue_from CanCan::AccessDenied, :with => :access_denied
|
rescue_from CanCan::AccessDenied do |exception|
|
||||||
|
flash[:error] = exception.message
|
||||||
protected
|
|
||||||
|
|
||||||
def access_denied
|
|
||||||
flash[:error] = "Sorry, you are not allowed to access that page."
|
|
||||||
redirect_to root_url
|
redirect_to root_url
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
See {Exception Handling}[http://wiki.github.com/ryanb/cancan/exception-handling] for more information.
|
||||||
|
|
||||||
|
|
||||||
== Defining Abilities
|
== Defining Abilities
|
||||||
|
|
||||||
As shown above, the Ability class is where all user permissions are defined. The user model is passed into the initialize method so you are free to modify the permissions based on the user's attributes. This way CanCan is completely decoupled with how you choose to handle roles.
|
As shown above, the +Ability+ class is where all user permissions are defined. The user model is passed into the initialize method so the permissions can be modified based on any user attributes. CanCan makes no assumptions about how roles are handled in your application. See {Role Based Authorization}[http://wiki.github.com/ryanb/cancan/role-based-authorization] for an example.
|
||||||
|
|
||||||
The "can" method accepts two arguments, the first one is the action you're setting the permission for, the second one is the class of object you're setting it on.
|
The +can+ method is used to define permissions and requires two arguments. The first one is the action you're setting the permission for, the second one is the class of object you're setting it on.
|
||||||
|
|
||||||
can :update, Article
|
can :update, Article
|
||||||
|
|
||||||
You can pass an array for either of these parameters to match any one.
|
You can pass an array for either of these parameters to match any one. In this case the user will have the ability to update or destroy both articles and comments.
|
||||||
|
|
||||||
can [:update, :destroy], [Article, Comment]
|
can [:update, :destroy], [Article, Comment]
|
||||||
|
|
||||||
In this case the user has the ability to update or destroy both articles and comments.
|
Use :+manage+ to represent any action and :+all+ to represent any class. Here are some examples.
|
||||||
|
|
||||||
You can pass a block to provide logic based on the article's attributes.
|
can :manage, Article # has permissions to do anything to articles
|
||||||
|
can :read, :all # has permission to read any model
|
||||||
|
can :manage, :all # has permission to do anything to any model
|
||||||
|
|
||||||
can :update, Article do |article|
|
You can pass a hash of conditions as the third argument to further restrict what the user is able to access. Here the user will only have permission to read active projects which he owns.
|
||||||
article && article.user == user
|
|
||||||
|
can :read, Project, :active => true, :user_id => user.id
|
||||||
|
|
||||||
|
See {Defining Abilities with Hashes}[http://wiki.github.com/ryanb/cancan/defining-abilities-with-hashes] for more information.
|
||||||
|
|
||||||
|
Blocks can also be used if you need more control.
|
||||||
|
|
||||||
|
can :update, Project do |project|
|
||||||
|
project && project.groups.include?(user.group)
|
||||||
end
|
end
|
||||||
|
|
||||||
If the block returns true then the user has that :update ability for that article, otherwise he will be denied access. It's possible for the passed in model to be nil if one isn't specified, so be sure to take that into consideration.
|
If the block returns true then the user has that :+update+ ability for that project, otherwise he will be denied access. See {Defining Abilities with Blocks}[http://wiki.github.com/ryanb/cancan/defining-abilities-with-blocks] for more information.
|
||||||
|
|
||||||
You can pass :all to reference every type of object. In this case the object type will be passed into the block as well (just in case object is nil).
|
|
||||||
|
|
||||||
can :read, :all do |object_class, object|
|
|
||||||
object_class != Order
|
|
||||||
end
|
|
||||||
|
|
||||||
Here the user has permission to read all objects except orders.
|
|
||||||
|
|
||||||
You can also pass :manage as the action which will match any action. In this case the action is passed to the block.
|
|
||||||
|
|
||||||
can :manage, Comment do |action, comment|
|
|
||||||
action != :destroy
|
|
||||||
end
|
|
||||||
|
|
||||||
Finally, the "cannot" method works similar to "can" but defines which abilities cannot be done.
|
|
||||||
|
|
||||||
can :read, :all
|
|
||||||
cannot :read, Product
|
|
||||||
|
|
||||||
|
|
||||||
== Checking Abilities
|
|
||||||
|
|
||||||
Use the "can?" method in the controller or view to check the user's permission for a given action and object.
|
|
||||||
|
|
||||||
can? :destroy, @project
|
|
||||||
|
|
||||||
You can also pass the class instead of an instance (if you don't have one handy).
|
|
||||||
|
|
||||||
<% if can? :create, Project %>
|
|
||||||
<%= link_to "New Project", new_project_path %>
|
|
||||||
<% end %>
|
|
||||||
|
|
||||||
The "cannot?" method is for convenience and performs the opposite check of "can?"
|
|
||||||
|
|
||||||
cannot? :destroy, @project
|
|
||||||
|
|
||||||
|
|
||||||
== Aliasing Actions
|
== Aliasing Actions
|
||||||
|
|
||||||
You can use the "alias_action" method to alias one or more actions into one.
|
You will usually be working with four actions when defining and checking permissions: :+read+, :+create+, :+update+, :+destroy+. These aren't the same as the 7 RESTful actions in Rails. CanCan adds some default aliases for mapping those actions.
|
||||||
|
|
||||||
alias_action :update, :destroy, :to => :modify
|
|
||||||
can :modify, Comment
|
|
||||||
can? :update, Comment # => true
|
|
||||||
|
|
||||||
The following aliases are added by default for conveniently mapping common controller actions.
|
|
||||||
|
|
||||||
alias_action :index, :show, :to => :read
|
alias_action :index, :show, :to => :read
|
||||||
alias_action :new, :to => :create
|
alias_action :new, :to => :create
|
||||||
alias_action :edit, :to => :update
|
alias_action :edit, :to => :update
|
||||||
|
|
||||||
|
Notice the +edit+ action is aliased to +update+. If the user is able to update a record he also has permission to edit it. You can define your own aliases in the +Ability+ class
|
||||||
|
|
||||||
== Assumptions & Configuring
|
alias_action :update, :destroy, :to => :modify
|
||||||
|
can :modify, Comment
|
||||||
|
can? :update, Comment # => true
|
||||||
|
|
||||||
CanCan makes two assumptions about your application.
|
See {Custom Actions}[http://wiki.github.com/ryanb/cancan/custom-actions] for information on adding other actions.
|
||||||
|
|
||||||
* You have an Ability class which defines the permissions.
|
|
||||||
* You have a current_user method in the controller which returns the current user model.
|
|
||||||
|
|
||||||
You can override these by overriding the "current_ability" method in your ApplicationController.
|
|
||||||
|
|
||||||
def current_ability
|
|
||||||
UserAbility.new(current_account) # instead of Ability.new(current_user)
|
|
||||||
end
|
|
||||||
|
|
||||||
That's it!
|
|
||||||
|
|
||||||
|
|
||||||
== Testing Abilities
|
== Fetching Records
|
||||||
|
|
||||||
It is very easy to test the Ability model since you can call "can?" directly on it as you would in the view or controller.
|
In the controller +index+ action you may want to fetch only the records which the user has permission to read. You can do this with the +accessible_by+ scope.
|
||||||
|
|
||||||
def test "user can only destroy projects which he owns"
|
@articles = Article.accessible_by(current_ability)
|
||||||
user = User.new
|
|
||||||
ability = Ability.new(user)
|
|
||||||
assert ability.can?(:destroy, Project.new(:user => user))
|
|
||||||
assert ability.cannot?(:destroy, Project.new)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
See {Fetching Records}[http://wiki.github.com/ryanb/cancan/fetching-records] for more information.
|
||||||
|
|
||||||
|
|
||||||
|
== Additional Docs
|
||||||
|
|
||||||
|
* {Upgrading to 1.1}[http://wiki.github.com/ryanb/cancan/upgrading-to-11]
|
||||||
|
* {Testing Abilities}[http://wiki.github.com/ryanb/cancan/testing-abilities]
|
||||||
|
* {Accessing Request Data}[http://wiki.github.com/ryanb/cancan/accessing-request-data]
|
||||||
|
* {See more}[http://wiki.github.com/ryanb/cancan/]
|
||||||
|
|
||||||
== Special Thanks
|
== Special Thanks
|
||||||
|
|
||||||
|
|||||||
2
Rakefile
2
Rakefile
@@ -9,3 +9,5 @@ Spec::Rake::SpecTask.new do |t|
|
|||||||
t.spec_files = spec_files
|
t.spec_files = spec_files
|
||||||
t.spec_opts = ["-c"]
|
t.spec_opts = ["-c"]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
task :default => :spec
|
||||||
@@ -1,22 +1,15 @@
|
|||||||
Gem::Specification.new do |s|
|
Gem::Specification.new do |s|
|
||||||
s.name = "cancan"
|
s.name = "cancan"
|
||||||
|
s.version = "1.1.0"
|
||||||
|
s.author = "Ryan Bates"
|
||||||
|
s.email = "ryan@railscasts.com"
|
||||||
|
s.homepage = "http://github.com/ryanb/cancan"
|
||||||
s.summary = "Simple authorization solution for Rails."
|
s.summary = "Simple authorization solution for Rails."
|
||||||
s.description = "Simple authorization solution for Rails which is completely decoupled from the user's roles. All permissions are stored in a single location for convenience."
|
s.description = "Simple authorization solution for Rails which is completely decoupled from the user's roles. All permissions are stored in a single location for convenience."
|
||||||
s.homepage = "http://github.com/ryanb/cancan"
|
|
||||||
|
|
||||||
s.version = "0.2.1"
|
s.files = Dir["{lib,spec}/**/*", "[A-Z]*", "init.rb"]
|
||||||
s.date = "2009-11-26"
|
s.require_path = "lib"
|
||||||
|
|
||||||
s.authors = ["Ryan Bates"]
|
s.rubyforge_project = s.name
|
||||||
s.email = "ryan@railscasts.com"
|
s.required_rubygems_version = ">= 1.3.4"
|
||||||
|
|
||||||
s.require_paths = ["lib"]
|
|
||||||
s.files = Dir["lib/**/*"] + Dir["spec/**/*"] + ["LICENSE", "README.rdoc", "Rakefile", "CHANGELOG.rdoc", "init.rb"]
|
|
||||||
s.extra_rdoc_files = ["README.rdoc", "CHANGELOG.rdoc", "LICENSE"]
|
|
||||||
|
|
||||||
s.has_rdoc = true
|
|
||||||
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "CanCan", "--main", "README.rdoc"]
|
|
||||||
|
|
||||||
s.rubygems_version = "1.3.4"
|
|
||||||
s.required_rubygems_version = Gem::Requirement.new(">= 1.2")
|
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,9 +1,6 @@
|
|||||||
module CanCan
|
require 'cancan/ability'
|
||||||
# This error is raised when a user isn't allowed to access a given
|
require 'cancan/controller_resource'
|
||||||
# controller action. See ControllerAdditions#unauthorized! for details.
|
require 'cancan/resource_authorization'
|
||||||
class AccessDenied < StandardError; end
|
require 'cancan/controller_additions'
|
||||||
end
|
require 'cancan/active_record_additions'
|
||||||
|
require 'cancan/exceptions'
|
||||||
require File.dirname(__FILE__) + '/cancan/ability'
|
|
||||||
require File.dirname(__FILE__) + '/cancan/resource_authorization'
|
|
||||||
require File.dirname(__FILE__) + '/cancan/controller_additions'
|
|
||||||
|
|||||||
@@ -16,8 +16,6 @@ module CanCan
|
|||||||
# end
|
# end
|
||||||
#
|
#
|
||||||
module Ability
|
module Ability
|
||||||
attr_accessor :user
|
|
||||||
|
|
||||||
# Use to check the user's permission for a given action and object.
|
# Use to check the user's permission for a given action and object.
|
||||||
#
|
#
|
||||||
# can? :destroy, @project
|
# can? :destroy, @project
|
||||||
@@ -26,6 +24,15 @@ module CanCan
|
|||||||
#
|
#
|
||||||
# can? :create, Project
|
# can? :create, Project
|
||||||
#
|
#
|
||||||
|
# Any additional arguments will be passed into the "can" block definition. This
|
||||||
|
# can be used to pass more information about the user's request for example.
|
||||||
|
#
|
||||||
|
# can? :create, Project, request.remote_ip
|
||||||
|
#
|
||||||
|
# can :create Project do |project, remote_ip|
|
||||||
|
# # ...
|
||||||
|
# end
|
||||||
|
#
|
||||||
# Not only can you use the can? method in the controller and view (see ControllerAdditions),
|
# Not only can you use the can? method in the controller and view (see ControllerAdditions),
|
||||||
# but you can also call it directly on an ability instance.
|
# but you can also call it directly on an ability instance.
|
||||||
#
|
#
|
||||||
@@ -40,15 +47,11 @@ module CanCan
|
|||||||
# assert ability.cannot?(:destroy, Project.new)
|
# assert ability.cannot?(:destroy, Project.new)
|
||||||
# end
|
# end
|
||||||
#
|
#
|
||||||
def can?(action, noun)
|
def can?(action, subject, *extra_args)
|
||||||
(@can_definitions || []).reverse.each do |base_behavior, defined_action, defined_noun, defined_block|
|
matching_can_definition(action, subject) do |base_behavior, defined_actions, defined_subjects, defined_conditions, defined_block|
|
||||||
defined_actions = expand_actions(defined_action)
|
result = can_perform_action?(action, subject, defined_actions, defined_subjects, defined_conditions, defined_block, extra_args)
|
||||||
defined_nouns = [defined_noun].flatten
|
|
||||||
if includes_action?(defined_actions, action) && includes_noun?(defined_nouns, noun)
|
|
||||||
result = can_perform_action?(action, noun, defined_actions, defined_nouns, defined_block)
|
|
||||||
return base_behavior ? result : !result
|
return base_behavior ? result : !result
|
||||||
end
|
end
|
||||||
end
|
|
||||||
false
|
false
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -71,16 +74,26 @@ module CanCan
|
|||||||
#
|
#
|
||||||
# In this case the user has the ability to update or destroy both articles and comments.
|
# In this case the user has the ability to update or destroy both articles and comments.
|
||||||
#
|
#
|
||||||
# You can pass a block to provide logic based on the article's attributes.
|
# You can pass a hash of conditions as the third argument.
|
||||||
#
|
#
|
||||||
# can :update, Article do |article|
|
# can :read, Project, :active => true, :user_id => user.id
|
||||||
# article && article.user == user
|
#
|
||||||
|
# Here the user can only see active projects which he owns. See ControllerAdditions#conditions for a way to
|
||||||
|
# use this in database queries.
|
||||||
|
#
|
||||||
|
# If the conditions hash does not give you enough control over defining abilities, you can use a block to
|
||||||
|
# write any Ruby code you want.
|
||||||
|
#
|
||||||
|
# can :update, Project do |project|
|
||||||
|
# project && project.groups.include?(user.group)
|
||||||
# end
|
# end
|
||||||
#
|
#
|
||||||
# If the block returns true then the user has that :update ability for that article, otherwise he
|
# If the block returns true then the user has that :update ability for that project, otherwise he
|
||||||
# will be denied access. It's possible for the passed in model to be nil if one isn't specified,
|
# will be denied access. It's possible for the passed in model to be nil if one isn't specified,
|
||||||
# so be sure to take that into consideration.
|
# so be sure to take that into consideration.
|
||||||
#
|
#
|
||||||
|
# The downside to using a block is that it cannot be used to generate conditions for database queries.
|
||||||
|
#
|
||||||
# You can pass :all to reference every type of object. In this case the object type will be passed
|
# You can pass :all to reference every type of object. In this case the object type will be passed
|
||||||
# into the block as well (just in case object is nil).
|
# into the block as well (just in case object is nil).
|
||||||
#
|
#
|
||||||
@@ -103,9 +116,9 @@ module CanCan
|
|||||||
# can :read, :stats
|
# can :read, :stats
|
||||||
# can? :read, :stats # => true
|
# can? :read, :stats # => true
|
||||||
#
|
#
|
||||||
def can(action, noun, &block)
|
def can(action, subject, conditions = nil, &block)
|
||||||
@can_definitions ||= []
|
@can_definitions ||= []
|
||||||
@can_definitions << [true, action, noun, block]
|
@can_definitions << [true, action, subject, conditions, block]
|
||||||
end
|
end
|
||||||
|
|
||||||
# Define an ability which cannot be done. Accepts the same arguments as "can".
|
# Define an ability which cannot be done. Accepts the same arguments as "can".
|
||||||
@@ -120,9 +133,9 @@ module CanCan
|
|||||||
# product.invisible?
|
# product.invisible?
|
||||||
# end
|
# end
|
||||||
#
|
#
|
||||||
def cannot(action, noun, &block)
|
def cannot(action, subject, conditions = nil, &block)
|
||||||
@can_definitions ||= []
|
@can_definitions ||= []
|
||||||
@can_definitions << [false, action, noun, block]
|
@can_definitions << [false, action, subject, conditions, block]
|
||||||
end
|
end
|
||||||
|
|
||||||
# Alias one or more actions into another one.
|
# Alias one or more actions into another one.
|
||||||
@@ -156,13 +169,49 @@ module CanCan
|
|||||||
# This way one can use params[:action] in the controller to determine the permission.
|
# This way one can use params[:action] in the controller to determine the permission.
|
||||||
def alias_action(*args)
|
def alias_action(*args)
|
||||||
target = args.pop[:to]
|
target = args.pop[:to]
|
||||||
aliased_actions[target] = args
|
aliased_actions[target] ||= []
|
||||||
|
aliased_actions[target] += args
|
||||||
|
end
|
||||||
|
|
||||||
|
# Returns a hash of aliased actions. The key is the target and the value is an array of actions aliasing the key.
|
||||||
|
def aliased_actions
|
||||||
|
@aliased_actions ||= default_alias_actions
|
||||||
|
end
|
||||||
|
|
||||||
|
# Removes previously aliased actions including the defaults.
|
||||||
|
def clear_aliased_actions
|
||||||
|
@aliased_actions = {}
|
||||||
|
end
|
||||||
|
|
||||||
|
# Returns a hash of conditions which match the given ability. This is useful if you need to generate a database
|
||||||
|
# query based on the current ability.
|
||||||
|
#
|
||||||
|
# can :read, Article, :visible => true
|
||||||
|
# conditions :read, Article # returns { :visible => true }
|
||||||
|
#
|
||||||
|
# Normally you will not call this method directly, but instead go through ActiveRecordAdditions#accessible_by method.
|
||||||
|
#
|
||||||
|
# If the ability is not defined then false is returned so be sure to take that into consideration.
|
||||||
|
# If the ability is defined using a block then this will raise an exception since a hash of conditions cannot be
|
||||||
|
# determined from that.
|
||||||
|
def conditions(action, subject)
|
||||||
|
matching_can_definition(action, subject) do |base_behavior, defined_actions, defined_subjects, defined_conditions, defined_block|
|
||||||
|
raise Error, "Cannot determine ability conditions from block for #{action.inspect} #{subject.inspect}" if defined_block
|
||||||
|
return defined_conditions || {}
|
||||||
|
end
|
||||||
|
false
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def aliased_actions
|
def matching_can_definition(action, subject, &block)
|
||||||
@aliased_actions ||= default_alias_actions
|
(@can_definitions || []).reverse.each do |base_behavior, defined_action, defined_subject, defined_conditions, defined_block|
|
||||||
|
defined_actions = expand_actions(defined_action)
|
||||||
|
defined_subjects = [defined_subject].flatten
|
||||||
|
if includes_action?(defined_actions, action) && includes_subject?(defined_subjects, subject)
|
||||||
|
return block.call(base_behavior, defined_actions, defined_subjects, defined_conditions, defined_block)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def default_alias_actions
|
def default_alias_actions
|
||||||
@@ -183,15 +232,35 @@ module CanCan
|
|||||||
end.flatten
|
end.flatten
|
||||||
end
|
end
|
||||||
|
|
||||||
def can_perform_action?(action, noun, defined_actions, defined_nouns, defined_block)
|
def can_perform_action?(action, subject, defined_actions, defined_subjects, defined_conditions, defined_block, extra_args)
|
||||||
if defined_block.nil?
|
if defined_block
|
||||||
true
|
|
||||||
else
|
|
||||||
block_args = []
|
block_args = []
|
||||||
block_args << action if defined_actions.include?(:manage)
|
block_args << action if defined_actions.include?(:manage)
|
||||||
block_args << (noun.class == Class ? noun : noun.class) if defined_nouns.include?(:all)
|
block_args << (subject.class == Class ? subject : subject.class) if defined_subjects.include?(:all)
|
||||||
block_args << (noun.class == Class ? nil : noun)
|
block_args << (subject.class == Class ? nil : subject)
|
||||||
return defined_block.call(*block_args)
|
block_args += extra_args
|
||||||
|
defined_block.call(*block_args)
|
||||||
|
elsif defined_conditions
|
||||||
|
if subject.class == Class
|
||||||
|
true
|
||||||
|
else
|
||||||
|
matches_conditions? subject, defined_conditions
|
||||||
|
end
|
||||||
|
else
|
||||||
|
true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def matches_conditions?(subject, defined_conditions)
|
||||||
|
defined_conditions.all? do |name, value|
|
||||||
|
attribute = subject.send(name)
|
||||||
|
if value.kind_of?(Hash)
|
||||||
|
matches_conditions? attribute, value
|
||||||
|
elsif value.kind_of?(Array) || value.kind_of?(Range)
|
||||||
|
value.include? attribute
|
||||||
|
else
|
||||||
|
attribute == value
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -199,8 +268,8 @@ module CanCan
|
|||||||
actions.include?(:manage) || actions.include?(action)
|
actions.include?(:manage) || actions.include?(action)
|
||||||
end
|
end
|
||||||
|
|
||||||
def includes_noun?(nouns, noun)
|
def includes_subject?(subjects, subject)
|
||||||
nouns.include?(:all) || nouns.include?(noun) || nouns.any? { |c| c.kind_of?(Class) && noun.kind_of?(c) }
|
subjects.include?(:all) || subjects.include?(subject) || subjects.any? { |c| c.kind_of?(Class) && subject.kind_of?(c) }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
42
lib/cancan/active_record_additions.rb
Normal file
42
lib/cancan/active_record_additions.rb
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
module CanCan
|
||||||
|
# This module is automatically included into all Active Record.
|
||||||
|
module ActiveRecordAdditions
|
||||||
|
module ClassMethods
|
||||||
|
# Returns a scope which fetches only the records that the passed ability
|
||||||
|
# can perform a given action on. The action defaults to :read. This
|
||||||
|
# is usually called from a controller and passed the +current_ability+.
|
||||||
|
#
|
||||||
|
# @articles = Article.accessible_by(current_ability)
|
||||||
|
#
|
||||||
|
# Here only the articles which the user is able to read will be returned.
|
||||||
|
# If the user does not have permission to read any articles then an empty
|
||||||
|
# result is returned. Since this is a scope it can be combined with any
|
||||||
|
# other scopes or pagination.
|
||||||
|
#
|
||||||
|
# An alternative action can optionally be passed as a second argument.
|
||||||
|
#
|
||||||
|
# @articles = Article.accessible_by(current_ability, :update)
|
||||||
|
#
|
||||||
|
# Here only the articles which the user can update are returned. This
|
||||||
|
# internally uses Ability#conditions method, see that for more information.
|
||||||
|
def accessible_by(ability, action = :read)
|
||||||
|
conditions = ability.conditions(action, self) || {:id => nil}
|
||||||
|
if respond_to? :where
|
||||||
|
where(conditions)
|
||||||
|
else
|
||||||
|
scoped(:conditions => conditions)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.included(base)
|
||||||
|
base.extend ClassMethods
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if defined? ActiveRecord
|
||||||
|
ActiveRecord::Base.class_eval do
|
||||||
|
include CanCan::ActiveRecordAdditions
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -3,48 +3,171 @@ module CanCan
|
|||||||
# This module is automatically included into all controllers.
|
# This module is automatically included into all controllers.
|
||||||
# It also makes the "can?" and "cannot?" methods available to all views.
|
# It also makes the "can?" and "cannot?" methods available to all views.
|
||||||
module ControllerAdditions
|
module ControllerAdditions
|
||||||
|
module ClassMethods
|
||||||
|
# Sets up a before filter which loads and authorizes the current resource. This performs both
|
||||||
|
# load_resource and authorize_resource and accepts the same arguments. See those methods for details.
|
||||||
|
#
|
||||||
|
# class BooksController < ApplicationController
|
||||||
|
# load_and_authorize_resource
|
||||||
|
# end
|
||||||
|
#
|
||||||
|
def load_and_authorize_resource(options = {})
|
||||||
|
ResourceAuthorization.add_before_filter(self, :load_and_authorize_resource, options)
|
||||||
|
end
|
||||||
|
|
||||||
|
# Sets up a before filter which loads the appropriate model resource into an instance variable.
|
||||||
|
# For example, given an ArticlesController it will load the current article into the @article
|
||||||
|
# instance variable. It does this by either calling Article.find(params[:id]) or
|
||||||
|
# Article.new(params[:article]) depending upon the action. It does nothing for the "index"
|
||||||
|
# action.
|
||||||
|
#
|
||||||
|
# Call this method directly on the controller class.
|
||||||
|
#
|
||||||
|
# class BooksController < ApplicationController
|
||||||
|
# load_resource
|
||||||
|
# end
|
||||||
|
#
|
||||||
|
# A resource is not loaded if the instance variable is already set. This makes it easy to override
|
||||||
|
# the behavior through a before_filter on certain actions.
|
||||||
|
#
|
||||||
|
# class BooksController < ApplicationController
|
||||||
|
# before_filter :find_book_by_permalink, :only => :show
|
||||||
|
# load_resource
|
||||||
|
#
|
||||||
|
# private
|
||||||
|
#
|
||||||
|
# def find_book_by_permalink
|
||||||
|
# @book = Book.find_by_permalink!(params[:id)
|
||||||
|
# end
|
||||||
|
# end
|
||||||
|
#
|
||||||
|
# See load_and_authorize_resource to automatically authorize the resource too.
|
||||||
|
#
|
||||||
|
# Options:
|
||||||
|
# [:+only+]
|
||||||
|
# Only applies before filter to given actions.
|
||||||
|
#
|
||||||
|
# [:+except+]
|
||||||
|
# Does not apply before filter to given actions.
|
||||||
|
#
|
||||||
|
# [:+nested+]
|
||||||
|
# Specify which resource this is nested under.
|
||||||
|
#
|
||||||
|
# load_resource :nested => :author
|
||||||
|
#
|
||||||
|
# Deep nesting can be defined in an array.
|
||||||
|
#
|
||||||
|
# load_resource :nested => [:publisher, :author]
|
||||||
|
#
|
||||||
|
# [:+resource+]
|
||||||
|
# The class to use for the model (string or constant).
|
||||||
|
#
|
||||||
|
# [:+collection+]
|
||||||
|
# Specify which actions are resource collection actions in addition to :+index+. This
|
||||||
|
# is usually not necessary because it will try to guess depending on if an :+id+
|
||||||
|
# is present in +params+.
|
||||||
|
#
|
||||||
|
# load_resource :collection => [:sort, :list]
|
||||||
|
#
|
||||||
|
# [:+new+]
|
||||||
|
# Specify which actions are new resource actions in addition to :+new+ and :+create+.
|
||||||
|
# Pass an action name into here if you would like to build a new resource instead of
|
||||||
|
# fetch one.
|
||||||
|
#
|
||||||
|
# load_resource :new => :build
|
||||||
|
#
|
||||||
|
def load_resource(options = {})
|
||||||
|
ResourceAuthorization.add_before_filter(self, :load_resource, options)
|
||||||
|
end
|
||||||
|
|
||||||
|
# Sets up a before filter which authorizes the current resource using the instance variable.
|
||||||
|
# For example, if you have an ArticlesController it will check the @article instance variable
|
||||||
|
# and ensure the user can perform the current action on it. Under the hood it is doing
|
||||||
|
# something like the following.
|
||||||
|
#
|
||||||
|
# authorize!(params[:action].to_sym, @article || Article)
|
||||||
|
#
|
||||||
|
# Call this method directly on the controller class.
|
||||||
|
#
|
||||||
|
# class BooksController < ApplicationController
|
||||||
|
# authorize_resource
|
||||||
|
# end
|
||||||
|
#
|
||||||
|
# See load_and_authorize_resource to automatically load the resource too.
|
||||||
|
#
|
||||||
|
# Options:
|
||||||
|
# [:+only+]
|
||||||
|
# Only applies before filter to given actions.
|
||||||
|
#
|
||||||
|
# [:+except+]
|
||||||
|
# Does not apply before filter to given actions.
|
||||||
|
#
|
||||||
|
# [:+resource+]
|
||||||
|
# The class to use for the model (string or constant). Alternatively pass a symbol
|
||||||
|
# to represent a resource which does not have a class.
|
||||||
|
#
|
||||||
|
def authorize_resource(options = {})
|
||||||
|
ResourceAuthorization.add_before_filter(self, :authorize_resource, options)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def self.included(base)
|
def self.included(base)
|
||||||
|
base.extend ClassMethods
|
||||||
base.helper_method :can?, :cannot?
|
base.helper_method :can?, :cannot?
|
||||||
end
|
end
|
||||||
|
|
||||||
# Raises the CanCan::AccessDenied exception. This is often used in a
|
# Raises a CanCan::AccessDenied exception if the current_ability cannot
|
||||||
# controller action to mark a request as unauthorized.
|
# perform the given action. This is usually called in a controller action or
|
||||||
|
# before filter to perform the authorization.
|
||||||
#
|
#
|
||||||
# def show
|
# def show
|
||||||
# @article = Article.find(params[:id])
|
# @article = Article.find(params[:id])
|
||||||
# unauthorized! if cannot? :read, @article
|
# authorize! :read, @article
|
||||||
# end
|
# end
|
||||||
#
|
#
|
||||||
# You can rescue from the exception in the controller to specify
|
# A :message option can be passed to specify a different message.
|
||||||
# the user experience.
|
#
|
||||||
|
# authorize! :read, @article, :message => "Not authorized to read #{@article.name}"
|
||||||
|
#
|
||||||
|
# You can rescue from the exception in the controller to customize how unauthorized
|
||||||
|
# access is displayed to the user.
|
||||||
#
|
#
|
||||||
# class ApplicationController < ActionController::Base
|
# class ApplicationController < ActionController::Base
|
||||||
# rescue_from CanCan::AccessDenied, :with => :access_denied
|
# rescue_from CanCan::AccessDenied do |exception|
|
||||||
#
|
# flash[:error] = exception.message
|
||||||
# protected
|
|
||||||
#
|
|
||||||
# def access_denied
|
|
||||||
# flash[:error] = "Sorry, you are not allowed to access that page."
|
|
||||||
# redirect_to root_url
|
# redirect_to root_url
|
||||||
# end
|
# end
|
||||||
# end
|
# end
|
||||||
#
|
#
|
||||||
# See the load_and_authorize_resource method to automatically add
|
# See the CanCan::AccessDenied exception for more details on working with the exception.
|
||||||
# the "unauthorized!" behavior to a RESTful controller's actions.
|
#
|
||||||
def unauthorized!
|
# See the load_and_authorize_resource method to automatically add the authorize! behavior
|
||||||
raise AccessDenied, "You are unable to access this page."
|
# to the default RESTful actions.
|
||||||
|
def authorize!(action, subject, *args)
|
||||||
|
message = nil
|
||||||
|
if args.last.kind_of?(Hash) && args.last.has_key?(:message)
|
||||||
|
message = args.pop[:message]
|
||||||
|
end
|
||||||
|
raise AccessDenied.new(message, action, subject) if cannot?(action, subject, *args)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Creates and returns the current user's ability. You generally do not invoke
|
def unauthorized!(message = nil)
|
||||||
# this method directly, instead you can override this method to change its
|
raise ImplementationRemoved, "The unauthorized! method has been removed from CanCan, use authorize! instead."
|
||||||
# behavior if the Ability class or current_user method are different.
|
end
|
||||||
|
|
||||||
|
# Creates and returns the current user's ability and caches it. If you
|
||||||
|
# want to override how the Ability is defined then this is the place.
|
||||||
|
# Just define the method in the controller to change behavior.
|
||||||
#
|
#
|
||||||
# def current_ability
|
# def current_ability
|
||||||
# UserAbility.new(current_account) # instead of Ability.new(current_user)
|
# # instead of Ability.new(current_user)
|
||||||
|
# @current_ability ||= UserAbility.new(current_account)
|
||||||
# end
|
# end
|
||||||
#
|
#
|
||||||
|
# Notice it is important to cache the ability object so it is not
|
||||||
|
# recreated every time.
|
||||||
def current_ability
|
def current_ability
|
||||||
::Ability.new(current_user)
|
@current_ability ||= ::Ability.new(current_user)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Use in the controller or view to check the user's permission for a given action
|
# Use in the controller or view to check the user's permission for a given action
|
||||||
@@ -60,7 +183,7 @@ module CanCan
|
|||||||
#
|
#
|
||||||
# This simply calls "can?" on the current_ability. See Ability#can?.
|
# This simply calls "can?" on the current_ability. See Ability#can?.
|
||||||
def can?(*args)
|
def can?(*args)
|
||||||
(@current_ability ||= current_ability).can?(*args)
|
current_ability.can?(*args)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Convenience method which works the same as "can?" but returns the opposite value.
|
# Convenience method which works the same as "can?" but returns the opposite value.
|
||||||
@@ -68,49 +191,7 @@ module CanCan
|
|||||||
# cannot? :destroy, @project
|
# cannot? :destroy, @project
|
||||||
#
|
#
|
||||||
def cannot?(*args)
|
def cannot?(*args)
|
||||||
(@current_ability ||= current_ability).cannot?(*args)
|
current_ability.cannot?(*args)
|
||||||
end
|
|
||||||
|
|
||||||
# This method loads the appropriate model resource into an instance variable. For example,
|
|
||||||
# given an ArticlesController it will load the current article into the @article instance
|
|
||||||
# variable. It does this by either calling Article.find(params[:id]) or
|
|
||||||
# Article.new(params[:article]) depending upon the action. It does nothing for the "index"
|
|
||||||
# action.
|
|
||||||
#
|
|
||||||
# You would often use this as a before filter in the controller. See
|
|
||||||
# load_and_authorize_resource to handle authorization too.
|
|
||||||
#
|
|
||||||
# before_filter :load_resource
|
|
||||||
#
|
|
||||||
def load_resource
|
|
||||||
ResourceAuthorization.new(self, params).load_resource
|
|
||||||
end
|
|
||||||
|
|
||||||
# Authorizes the resource in the current instance variable. For example,
|
|
||||||
# if you have an ArticlesController it will check the @article instance variable
|
|
||||||
# and ensure the user can perform the current action on it.
|
|
||||||
# Under the hood it is doing something like the following.
|
|
||||||
#
|
|
||||||
# unauthorized! if cannot?(params[:action].to_sym, @article || Article)
|
|
||||||
#
|
|
||||||
# You would often use this as a before filter in the controller.
|
|
||||||
#
|
|
||||||
# before_filter :authorize_resource
|
|
||||||
#
|
|
||||||
# See load_and_authorize_resource to automatically load the resource too.
|
|
||||||
def authorize_resource
|
|
||||||
ResourceAuthorization.new(self, params).authorize_resource
|
|
||||||
end
|
|
||||||
|
|
||||||
# Calls load_resource to load the current resource model into an instance variable.
|
|
||||||
# Then calls authorize_resource to ensure the current user is authorized to access the page.
|
|
||||||
# You would often use this as a before filter in the controller.
|
|
||||||
#
|
|
||||||
# before_filter :load_and_authorize_resource
|
|
||||||
#
|
|
||||||
def load_and_authorize_resource
|
|
||||||
load_resource
|
|
||||||
authorize_resource
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
47
lib/cancan/controller_resource.rb
Normal file
47
lib/cancan/controller_resource.rb
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
module CanCan
|
||||||
|
class ControllerResource # :nodoc:
|
||||||
|
def initialize(controller, name, parent = nil, options = {})
|
||||||
|
raise ImplementationRemoved, "The :class option has been renamed to :resource for specifying the class in CanCan." if options.has_key? :class
|
||||||
|
@controller = controller
|
||||||
|
@name = name
|
||||||
|
@parent = parent
|
||||||
|
@options = options
|
||||||
|
end
|
||||||
|
|
||||||
|
def model_class
|
||||||
|
if @options[:resource].nil?
|
||||||
|
@name.to_s.camelize.constantize
|
||||||
|
elsif @options[:resource].kind_of? String
|
||||||
|
@options[:resource].constantize
|
||||||
|
else
|
||||||
|
@options[:resource]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def find(id)
|
||||||
|
self.model_instance ||= base.find(id)
|
||||||
|
end
|
||||||
|
|
||||||
|
def build(attributes)
|
||||||
|
if base.kind_of? Class
|
||||||
|
self.model_instance ||= base.new(attributes)
|
||||||
|
else
|
||||||
|
self.model_instance ||= base.build(attributes)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def model_instance
|
||||||
|
@controller.instance_variable_get("@#{@name}")
|
||||||
|
end
|
||||||
|
|
||||||
|
def model_instance=(instance)
|
||||||
|
@controller.instance_variable_set("@#{@name}", instance)
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def base
|
||||||
|
@parent ? @parent.model_instance.send(@name.to_s.pluralize) : model_class
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
43
lib/cancan/exceptions.rb
Normal file
43
lib/cancan/exceptions.rb
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
module CanCan
|
||||||
|
# A general CanCan exception
|
||||||
|
class Error < StandardError; end
|
||||||
|
|
||||||
|
# Raised when removed code is called, an alternative solution is provided in message.
|
||||||
|
class ImplementationRemoved < Error; end
|
||||||
|
|
||||||
|
# This error is raised when a user isn't allowed to access a given controller action.
|
||||||
|
# This usually happens within a call to ControllerAdditions#authorize! but can be
|
||||||
|
# raised manually.
|
||||||
|
#
|
||||||
|
# raise CanCan::AccessDenied.new("Not authorized!", :read, Article)
|
||||||
|
#
|
||||||
|
# The passed message, action, and subject are optional and can later be retrieved when
|
||||||
|
# rescuing from the exception.
|
||||||
|
#
|
||||||
|
# exception.message # => "Not authorized!"
|
||||||
|
# exception.action # => :read
|
||||||
|
# exception.subject # => Article
|
||||||
|
#
|
||||||
|
# If the message is not specified (or is nil) it will default to "You are anot authorized
|
||||||
|
# to access this page." This default can be overridden by setting default_message.
|
||||||
|
#
|
||||||
|
# exception.default_message = "Default error message"
|
||||||
|
# exception.message # => "Default error message"
|
||||||
|
#
|
||||||
|
# See ControllerAdditions#authorized! for more information on rescuing from this exception.
|
||||||
|
class AccessDenied < Error
|
||||||
|
attr_reader :action, :subject
|
||||||
|
attr_writer :default_message
|
||||||
|
|
||||||
|
def initialize(message = nil, action = nil, subject = nil)
|
||||||
|
@message = message
|
||||||
|
@action = action
|
||||||
|
@subject = subject
|
||||||
|
@default_message = "You are not authorized to access this page."
|
||||||
|
end
|
||||||
|
|
||||||
|
def to_s
|
||||||
|
@message || @default_message
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
13
lib/cancan/matchers.rb
Normal file
13
lib/cancan/matchers.rb
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
Spec::Matchers.define :be_able_to do |*args|
|
||||||
|
match do |ability|
|
||||||
|
ability.can?(*args)
|
||||||
|
end
|
||||||
|
|
||||||
|
failure_message_for_should do |ability|
|
||||||
|
"expected to be able to #{args.map(&:inspect).join(" ")}"
|
||||||
|
end
|
||||||
|
|
||||||
|
failure_message_for_should_not do |ability|
|
||||||
|
"expected not to be able to #{args.map(&:inspect).join(" ")}"
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -2,9 +2,16 @@ module CanCan
|
|||||||
class ResourceAuthorization # :nodoc:
|
class ResourceAuthorization # :nodoc:
|
||||||
attr_reader :params
|
attr_reader :params
|
||||||
|
|
||||||
def initialize(controller, params)
|
def self.add_before_filter(controller_class, method, options = {})
|
||||||
|
controller_class.before_filter(options.slice(:only, :except)) do |controller|
|
||||||
|
new(controller, controller.params, options.except(:only, :except)).send(method)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def initialize(controller, params, options = {})
|
||||||
@controller = controller
|
@controller = controller
|
||||||
@params = params
|
@params = params
|
||||||
|
@options = options
|
||||||
end
|
end
|
||||||
|
|
||||||
def load_and_authorize_resource
|
def load_and_authorize_resource
|
||||||
@@ -13,29 +20,49 @@ module CanCan
|
|||||||
end
|
end
|
||||||
|
|
||||||
def load_resource
|
def load_resource
|
||||||
self.model_instance = params[:id] ? model_class.find(params[:id]) : model_class.new(params[model_name.to_sym]) unless params[:action] == "index"
|
unless collection_actions.include? params[:action].to_sym
|
||||||
|
if new_actions.include? params[:action].to_sym
|
||||||
|
resource.build(params[model_name.to_sym])
|
||||||
|
elsif params[:id]
|
||||||
|
resource.find(params[:id])
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def authorize_resource
|
def authorize_resource
|
||||||
@controller.unauthorized! if @controller.cannot?(params[:action].to_sym, model_instance || model_class)
|
@controller.authorize!(params[:action].to_sym, resource.model_instance || resource.model_class)
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
def resource
|
||||||
|
@resource ||= ControllerResource.new(@controller, model_name, parent_resource, @options)
|
||||||
|
end
|
||||||
|
|
||||||
|
def parent_resource
|
||||||
|
parent = nil
|
||||||
|
[@options[:nested]].flatten.compact.each do |name|
|
||||||
|
id = @params["#{name}_id".to_sym]
|
||||||
|
if id
|
||||||
|
parent = ControllerResource.new(@controller, name, parent)
|
||||||
|
parent.find(id)
|
||||||
|
else
|
||||||
|
parent = nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
parent
|
||||||
|
end
|
||||||
|
|
||||||
def model_name
|
def model_name
|
||||||
params[:controller].split('/').last.singularize
|
params[:controller].sub("Controller", "").underscore.split('/').last.singularize
|
||||||
end
|
end
|
||||||
|
|
||||||
def model_class
|
def collection_actions
|
||||||
model_name.camelcase.constantize
|
[:index] + [@options[:collection]].flatten
|
||||||
end
|
end
|
||||||
|
|
||||||
def model_instance
|
def new_actions
|
||||||
@controller.instance_variable_get("@#{model_name}")
|
[:new, :create] + [@options[:new]].flatten
|
||||||
end
|
|
||||||
|
|
||||||
def model_instance=(instance)
|
|
||||||
@controller.instance_variable_set("@#{model_name}", instance)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,10 +1,9 @@
|
|||||||
require File.dirname(__FILE__) + '/../spec_helper'
|
require "spec_helper"
|
||||||
|
|
||||||
describe CanCan::Ability do
|
describe CanCan::Ability do
|
||||||
before(:each) do
|
before(:each) do
|
||||||
@ability_class = Class.new
|
@ability = Object.new
|
||||||
@ability_class.send(:include, CanCan::Ability)
|
@ability.extend(CanCan::Ability)
|
||||||
@ability = @ability_class.new
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should be able to :read anything" do
|
it "should be able to :read anything" do
|
||||||
@@ -50,9 +49,7 @@ describe CanCan::Ability do
|
|||||||
|
|
||||||
it "should alias update or destroy actions to modify action" do
|
it "should alias update or destroy actions to modify action" do
|
||||||
@ability.alias_action :update, :destroy, :to => :modify
|
@ability.alias_action :update, :destroy, :to => :modify
|
||||||
@ability.can :modify, :all do |object_class, object|
|
@ability.can(:modify, :all) { :modify_called }
|
||||||
:modify_called
|
|
||||||
end
|
|
||||||
@ability.can?(:update, 123).should == :modify_called
|
@ability.can?(:update, 123).should == :modify_called
|
||||||
@ability.can?(:destroy, 123).should == :modify_called
|
@ability.can?(:destroy, 123).should == :modify_called
|
||||||
end
|
end
|
||||||
@@ -123,4 +120,72 @@ describe CanCan::Ability do
|
|||||||
@ability.can?(:read, 3).should be_true
|
@ability.can?(:read, 3).should be_true
|
||||||
@ability.can?(:read, 123).should be_false
|
@ability.can?(:read, 123).should be_false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "should append aliased actions" do
|
||||||
|
@ability.alias_action :update, :to => :modify
|
||||||
|
@ability.alias_action :destroy, :to => :modify
|
||||||
|
@ability.aliased_actions[:modify].should == [:update, :destroy]
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should clear aliased actions" do
|
||||||
|
@ability.alias_action :update, :to => :modify
|
||||||
|
@ability.clear_aliased_actions
|
||||||
|
@ability.aliased_actions[:modify].should be_nil
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should pass additional arguments to block from can?" do
|
||||||
|
@ability.can :read, Integer do |int, x|
|
||||||
|
int > x
|
||||||
|
end
|
||||||
|
@ability.can?(:read, 2, 1).should be_true
|
||||||
|
@ability.can?(:read, 2, 3).should be_false
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should use conditions as third parameter and determine abilities from it" do
|
||||||
|
@ability.can :read, Array, :first => 1, :last => 3
|
||||||
|
@ability.can?(:read, [1, 2, 3]).should be_true
|
||||||
|
@ability.can?(:read, [1, 2, 3, 4]).should be_false
|
||||||
|
@ability.can?(:read, Array).should be_true
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should allow an array of options in conditions hash" do
|
||||||
|
@ability.can :read, Array, :first => [1, 3, 5]
|
||||||
|
@ability.can?(:read, [1, 2, 3]).should be_true
|
||||||
|
@ability.can?(:read, [2, 3]).should be_false
|
||||||
|
@ability.can?(:read, [3, 4]).should be_true
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should allow a range of options in conditions hash" do
|
||||||
|
@ability.can :read, Array, :first => 1..3
|
||||||
|
@ability.can?(:read, [1, 2, 3]).should be_true
|
||||||
|
@ability.can?(:read, [3, 4]).should be_true
|
||||||
|
@ability.can?(:read, [4, 5]).should be_false
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should allow nested hashes in conditions hash" do
|
||||||
|
@ability.can :read, Array, :first => { :length => 5 }
|
||||||
|
@ability.can?(:read, ["foo", "bar"]).should be_false
|
||||||
|
@ability.can?(:read, ["test1", "foo"]).should be_true
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should return conditions for a given ability" do
|
||||||
|
@ability.can :read, Array, :first => 1, :last => 3
|
||||||
|
@ability.conditions(:show, Array).should == {:first => 1, :last => 3}
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should raise an exception when a block is used on condition" do
|
||||||
|
@ability.can :read, Array do |a|
|
||||||
|
true
|
||||||
|
end
|
||||||
|
lambda { @ability.conditions(:show, Array) }.should raise_error(CanCan::Error, "Cannot determine ability conditions from block for :show Array")
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should return an empty hash for conditions when there are no conditions" do
|
||||||
|
@ability.can :read, Array
|
||||||
|
@ability.conditions(:show, Array).should == {}
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should return false when performed on an action which isn't defined" do
|
||||||
|
@ability.conditions(:foo, Array).should == false
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
28
spec/cancan/active_record_additions_spec.rb
Normal file
28
spec/cancan/active_record_additions_spec.rb
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
require "spec_helper"
|
||||||
|
|
||||||
|
describe CanCan::ActiveRecordAdditions do
|
||||||
|
before(:each) do
|
||||||
|
@model_class = Class.new
|
||||||
|
stub(@model_class).scoped { :scoped_stub }
|
||||||
|
@model_class.send(:include, CanCan::ActiveRecordAdditions)
|
||||||
|
@ability = Object.new
|
||||||
|
@ability.extend(CanCan::Ability)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should call where(:id => nil) when no ability is defined so no records are found" do
|
||||||
|
stub(@model_class).where(:id => nil) { :no_where }
|
||||||
|
@model_class.accessible_by(@ability, :read).should == :no_where
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should call where with matching ability conditions" do
|
||||||
|
@ability.can :read, @model_class, :foo => 1
|
||||||
|
stub(@model_class).where(:foo => 1) { :found_records }
|
||||||
|
@model_class.accessible_by(@ability, :read).should == :found_records
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should default to :read ability and use scoped when where isn't available" do
|
||||||
|
@ability.can :read, @model_class, :foo => 1
|
||||||
|
stub(@model_class).scoped(:conditions => {:foo => 1}) { :found_records }
|
||||||
|
@model_class.accessible_by(@ability).should == :found_records
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -1,45 +1,72 @@
|
|||||||
require File.dirname(__FILE__) + '/../spec_helper'
|
require "spec_helper"
|
||||||
|
|
||||||
describe CanCan::ControllerAdditions do
|
describe CanCan::ControllerAdditions do
|
||||||
before(:each) do
|
before(:each) do
|
||||||
@controller_class = Class.new
|
@controller_class = Class.new
|
||||||
@controller = @controller_class.new
|
@controller = @controller_class.new
|
||||||
stub(@controller).params { {} }
|
stub(@controller).params { {} }
|
||||||
|
stub(@controller).current_user { :current_user }
|
||||||
mock(@controller_class).helper_method(:can?, :cannot?)
|
mock(@controller_class).helper_method(:can?, :cannot?)
|
||||||
@controller_class.send(:include, CanCan::ControllerAdditions)
|
@controller_class.send(:include, CanCan::ControllerAdditions)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should read from the cache with request uri as key and render that text" do
|
it "should raise ImplementationRemoved when attempting to call 'unauthorized!' on a controller" do
|
||||||
lambda {
|
lambda { @controller.unauthorized! }.should raise_error(CanCan::ImplementationRemoved)
|
||||||
@controller.unauthorized!
|
end
|
||||||
}.should raise_error(CanCan::AccessDenied)
|
|
||||||
|
it "should raise access denied exception if ability us unauthorized to perform a certain action" do
|
||||||
|
begin
|
||||||
|
@controller.authorize! :read, :foo, 1, 2, 3, :message => "Access denied!"
|
||||||
|
rescue CanCan::AccessDenied => e
|
||||||
|
e.message.should == "Access denied!"
|
||||||
|
e.action.should == :read
|
||||||
|
e.subject.should == :foo
|
||||||
|
else
|
||||||
|
fail "Expected CanCan::AccessDenied exception to be raised"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should not raise access denied exception if ability is authorized to perform an action" do
|
||||||
|
@controller.current_ability.can :read, :foo
|
||||||
|
lambda { @controller.authorize!(:read, :foo) }.should_not raise_error
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should raise access denied exception with default message if not specified" do
|
||||||
|
begin
|
||||||
|
@controller.authorize! :read, :foo
|
||||||
|
rescue CanCan::AccessDenied => e
|
||||||
|
e.default_message = "Access denied!"
|
||||||
|
e.message.should == "Access denied!"
|
||||||
|
else
|
||||||
|
fail "Expected CanCan::AccessDenied exception to be raised"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should have a current_ability method which generates an ability for the current user" do
|
it "should have a current_ability method which generates an ability for the current user" do
|
||||||
stub(@controller).current_user { :current_user }
|
|
||||||
@controller.current_ability.should be_kind_of(Ability)
|
@controller.current_ability.should be_kind_of(Ability)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should provide a can? and cannot? methods which go through the current ability" do
|
it "should provide a can? and cannot? methods which go through the current ability" do
|
||||||
stub(@controller).current_user { :current_user }
|
|
||||||
@controller.current_ability.should be_kind_of(Ability)
|
@controller.current_ability.should be_kind_of(Ability)
|
||||||
@controller.can?(:foo, :bar).should be_false
|
@controller.can?(:foo, :bar).should be_false
|
||||||
@controller.cannot?(:foo, :bar).should be_true
|
@controller.cannot?(:foo, :bar).should be_true
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should load resource" do
|
it "load_and_authorize_resource should setup a before filter which passes call to ResourceAuthorization" do
|
||||||
mock.instance_of(CanCan::ResourceAuthorization).load_resource
|
stub(CanCan::ResourceAuthorization).new(@controller, @controller.params, :foo => :bar).mock!.load_and_authorize_resource
|
||||||
@controller.load_resource
|
mock(@controller_class).before_filter({}) { |options, block| block.call(@controller) }
|
||||||
|
@controller_class.load_and_authorize_resource :foo => :bar
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should authorize resource" do
|
it "authorize_resource should setup a before filter which passes call to ResourceAuthorization" do
|
||||||
mock.instance_of(CanCan::ResourceAuthorization).authorize_resource
|
stub(CanCan::ResourceAuthorization).new(@controller, @controller.params, :foo => :bar).mock!.authorize_resource
|
||||||
@controller.authorize_resource
|
mock(@controller_class).before_filter(:except => :show) { |options, block| block.call(@controller) }
|
||||||
|
@controller_class.authorize_resource :foo => :bar, :except => :show
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should load and authorize resource in one call through controller" do
|
it "load_resource should setup a before filter which passes call to ResourceAuthorization" do
|
||||||
mock(@controller).load_resource
|
stub(CanCan::ResourceAuthorization).new(@controller, @controller.params, :foo => :bar).mock!.load_resource
|
||||||
mock(@controller).authorize_resource
|
mock(@controller_class).before_filter(:only => [:show, :index]) { |options, block| block.call(@controller) }
|
||||||
@controller.load_and_authorize_resource
|
@controller_class.load_resource :foo => :bar, :only => [:show, :index]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
59
spec/cancan/controller_resource_spec.rb
Normal file
59
spec/cancan/controller_resource_spec.rb
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
require "spec_helper"
|
||||||
|
|
||||||
|
describe CanCan::ControllerResource do
|
||||||
|
before(:each) do
|
||||||
|
@controller = Object.new
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should determine model class by constantizing give name" do
|
||||||
|
CanCan::ControllerResource.new(@controller, :ability).model_class.should == Ability
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should fetch model through model class and assign it to the instance" do
|
||||||
|
stub(Ability).find(123) { :some_ability }
|
||||||
|
CanCan::ControllerResource.new(@controller, :ability).find(123)
|
||||||
|
@controller.instance_variable_get(:@ability).should == :some_ability
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should fetch model through parent and assign it to the instance" do
|
||||||
|
parent = Object.new
|
||||||
|
stub(parent).model_instance.stub!.abilities.stub!.find(123) { :some_ability }
|
||||||
|
CanCan::ControllerResource.new(@controller, :ability, parent).find(123)
|
||||||
|
@controller.instance_variable_get(:@ability).should == :some_ability
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should build model through model class and assign it to the instance" do
|
||||||
|
stub(Ability).new(123) { :some_ability }
|
||||||
|
CanCan::ControllerResource.new(@controller, :ability).build(123)
|
||||||
|
@controller.instance_variable_get(:@ability).should == :some_ability
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should build model through parent and assign it to the instance" do
|
||||||
|
parent = Object.new
|
||||||
|
stub(parent).model_instance.stub!.abilities.stub!.build(123) { :some_ability }
|
||||||
|
CanCan::ControllerResource.new(@controller, :ability, parent).build(123)
|
||||||
|
@controller.instance_variable_get(:@ability).should == :some_ability
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should not load resource if instance variable is already provided" do
|
||||||
|
@controller.instance_variable_set(:@ability, :some_ability)
|
||||||
|
CanCan::ControllerResource.new(@controller, :ability).find(123)
|
||||||
|
@controller.instance_variable_get(:@ability).should == :some_ability
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should use the model class option if provided" do
|
||||||
|
stub(Person).find(123) { :some_resource }
|
||||||
|
CanCan::ControllerResource.new(@controller, :ability, nil, :resource => Person).find(123)
|
||||||
|
@controller.instance_variable_get(:@ability).should == :some_resource
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should convert string to constant for resource" do
|
||||||
|
CanCan::ControllerResource.new(@controller, :ability, nil, :resource => "Person").model_class.should == Person
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should raise an exception when specifying :class option since it is no longer used" do
|
||||||
|
lambda {
|
||||||
|
CanCan::ControllerResource.new(@controller, :ability, nil, :class => Person)
|
||||||
|
}.should raise_error(CanCan::ImplementationRemoved)
|
||||||
|
end
|
||||||
|
end
|
||||||
35
spec/cancan/exceptions_spec.rb
Normal file
35
spec/cancan/exceptions_spec.rb
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
require "spec_helper"
|
||||||
|
|
||||||
|
describe CanCan::AccessDenied do
|
||||||
|
describe "with action and subject" do
|
||||||
|
before(:each) do
|
||||||
|
@exception = CanCan::AccessDenied.new(nil, :some_action, :some_subject)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should have action and subject accessors" do
|
||||||
|
@exception.action.should == :some_action
|
||||||
|
@exception.subject.should == :some_subject
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should have a changable default message" do
|
||||||
|
@exception.message.should == "You are not authorized to access this page."
|
||||||
|
@exception.default_message = "Unauthorized!"
|
||||||
|
@exception.message.should == "Unauthorized!"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "with only a message" do
|
||||||
|
before(:each) do
|
||||||
|
@exception = CanCan::AccessDenied.new("Access denied!")
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should have nil action and subject" do
|
||||||
|
@exception.action.should be_nil
|
||||||
|
@exception.subject.should be_nil
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should have passed message" do
|
||||||
|
@exception.message.should == "Access denied!"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
33
spec/cancan/matchers_spec.rb
Normal file
33
spec/cancan/matchers_spec.rb
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
require "spec_helper"
|
||||||
|
|
||||||
|
describe "be_able_to" do
|
||||||
|
it "delegates to can?" do
|
||||||
|
object = Object.new
|
||||||
|
mock(object).can?(:read, 123) { true }
|
||||||
|
object.should be_able_to(:read, 123)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "reports a nice failure message for should" do
|
||||||
|
object = Object.new
|
||||||
|
mock(object).can?(:read, 123) { false }
|
||||||
|
expect do
|
||||||
|
object.should be_able_to(:read, 123)
|
||||||
|
end.should raise_error('expected to be able to :read 123')
|
||||||
|
end
|
||||||
|
|
||||||
|
it "reports a nice failure message for should not" do
|
||||||
|
object = Object.new
|
||||||
|
mock(object).can?(:read, 123) { true }
|
||||||
|
expect do
|
||||||
|
object.should_not be_able_to(:read, 123)
|
||||||
|
end.should raise_error('expected not to be able to :read 123')
|
||||||
|
end
|
||||||
|
|
||||||
|
it "delegates additional arguments to can? and reports in failure message" do
|
||||||
|
object = Object.new
|
||||||
|
mock(object).can?(:read, 123, 456) { false }
|
||||||
|
expect do
|
||||||
|
object.should be_able_to(:read, 123, 456)
|
||||||
|
end.should raise_error('expected to be able to :read 123 456')
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -1,9 +1,8 @@
|
|||||||
require File.dirname(__FILE__) + '/../spec_helper'
|
require "spec_helper"
|
||||||
|
|
||||||
describe CanCan::ResourceAuthorization do
|
describe CanCan::ResourceAuthorization do
|
||||||
before(:each) do
|
before(:each) do
|
||||||
@controller = Object.new # simple stub for now
|
@controller = Object.new # simple stub for now
|
||||||
stub(@controller).unauthorized! { raise CanCan::AccessDenied }
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should load the resource into an instance variable if params[:id] is specified" do
|
it "should load the resource into an instance variable if params[:id] is specified" do
|
||||||
@@ -20,6 +19,13 @@ describe CanCan::ResourceAuthorization do
|
|||||||
@controller.instance_variable_get(:@ability).should == :some_resource
|
@controller.instance_variable_get(:@ability).should == :some_resource
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "should properly load resource for namespaced controller when using '::' for namespace" do
|
||||||
|
stub(Ability).find(123) { :some_resource }
|
||||||
|
authorization = CanCan::ResourceAuthorization.new(@controller, :controller => "Admin::AbilitiesController", :action => "show", :id => 123)
|
||||||
|
authorization.load_resource
|
||||||
|
@controller.instance_variable_get(:@ability).should == :some_resource
|
||||||
|
end
|
||||||
|
|
||||||
it "should build a new resource with hash if params[:id] is not specified" do
|
it "should build a new resource with hash if params[:id] is not specified" do
|
||||||
stub(Ability).new(:foo => "bar") { :some_resource }
|
stub(Ability).new(:foo => "bar") { :some_resource }
|
||||||
authorization = CanCan::ResourceAuthorization.new(@controller, :controller => "abilities", :action => "create", :ability => {:foo => "bar"})
|
authorization = CanCan::ResourceAuthorization.new(@controller, :controller => "abilities", :action => "create", :ability => {:foo => "bar"})
|
||||||
@@ -42,18 +48,70 @@ describe CanCan::ResourceAuthorization do
|
|||||||
|
|
||||||
it "should perform authorization using controller action and loaded model" do
|
it "should perform authorization using controller action and loaded model" do
|
||||||
@controller.instance_variable_set(:@ability, :some_resource)
|
@controller.instance_variable_set(:@ability, :some_resource)
|
||||||
stub(@controller).cannot?(:show, :some_resource) { true }
|
stub(@controller).authorize!(:show, :some_resource) { raise CanCan::AccessDenied }
|
||||||
authorization = CanCan::ResourceAuthorization.new(@controller, :controller => "abilities", :action => "show")
|
authorization = CanCan::ResourceAuthorization.new(@controller, :controller => "abilities", :action => "show")
|
||||||
lambda {
|
lambda { authorization.authorize_resource }.should raise_error(CanCan::AccessDenied)
|
||||||
authorization.authorize_resource
|
|
||||||
}.should raise_error(CanCan::AccessDenied)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should perform authorization using controller action and non loaded model" do
|
it "should perform authorization using controller action and non loaded model" do
|
||||||
stub(@controller).cannot?(:show, Ability) { true }
|
stub(@controller).authorize!(:show, Ability) { raise CanCan::AccessDenied }
|
||||||
authorization = CanCan::ResourceAuthorization.new(@controller, :controller => "abilities", :action => "show")
|
authorization = CanCan::ResourceAuthorization.new(@controller, :controller => "abilities", :action => "show")
|
||||||
lambda {
|
lambda { authorization.authorize_resource }.should raise_error(CanCan::AccessDenied)
|
||||||
authorization.authorize_resource
|
end
|
||||||
}.should raise_error(CanCan::AccessDenied)
|
|
||||||
|
it "should call load_resource and authorize_resource for load_and_authorize_resource" do
|
||||||
|
authorization = CanCan::ResourceAuthorization.new(@controller, :controller => "abilities", :action => "show")
|
||||||
|
mock(authorization).load_resource
|
||||||
|
mock(authorization).authorize_resource
|
||||||
|
authorization.load_and_authorize_resource
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should not build a resource when on custom collection action" do
|
||||||
|
authorization = CanCan::ResourceAuthorization.new(@controller, {:controller => "abilities", :action => "sort"}, {:collection => [:sort, :list]})
|
||||||
|
authorization.load_resource
|
||||||
|
@controller.instance_variable_get(:@ability).should be_nil
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should build a resource when on custom new action even when params[:id] exists" do
|
||||||
|
stub(Ability).new(nil) { :some_resource }
|
||||||
|
authorization = CanCan::ResourceAuthorization.new(@controller, {:controller => "abilities", :action => "build", :id => 123}, {:new => :build})
|
||||||
|
authorization.load_resource
|
||||||
|
@controller.instance_variable_get(:@ability).should == :some_resource
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should not try to load resource for other action if params[:id] is undefined" do
|
||||||
|
authorization = CanCan::ResourceAuthorization.new(@controller, :controller => "abilities", :action => "list")
|
||||||
|
authorization.load_resource
|
||||||
|
@controller.instance_variable_get(:@ability).should be_nil
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should load nested resource and fetch other resource through the association" do
|
||||||
|
stub(Person).find(456).stub!.abilities.stub!.find(123) { :some_ability }
|
||||||
|
authorization = CanCan::ResourceAuthorization.new(@controller, {:controller => "abilities", :action => "show", :id => 123, :person_id => 456}, {:nested => :person})
|
||||||
|
authorization.load_resource
|
||||||
|
@controller.instance_variable_get(:@ability).should == :some_ability
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should load nested resource and build resource through a deep association" do
|
||||||
|
stub(Person).find(456).stub!.behaviors.stub!.find(789).stub!.abilities.stub!.build(nil) { :some_ability }
|
||||||
|
authorization = CanCan::ResourceAuthorization.new(@controller, {:controller => "abilities", :action => "new", :person_id => 456, :behavior_id => 789}, {:nested => [:person, :behavior]})
|
||||||
|
authorization.load_resource
|
||||||
|
@controller.instance_variable_get(:@ability).should == :some_ability
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should not load nested resource and build through this if *_id param isn't specified" do
|
||||||
|
stub(Person).find(456) { :some_person }
|
||||||
|
stub(Ability).new(nil) { :some_ability }
|
||||||
|
authorization = CanCan::ResourceAuthorization.new(@controller, {:controller => "abilities", :action => "new", :person_id => 456}, {:nested => [:person, :behavior]})
|
||||||
|
authorization.load_resource
|
||||||
|
@controller.instance_variable_get(:@person).should == :some_person
|
||||||
|
@controller.instance_variable_get(:@ability).should == :some_ability
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should load the model using a custom class" do
|
||||||
|
stub(Person).find(123) { :some_resource }
|
||||||
|
authorization = CanCan::ResourceAuthorization.new(@controller, {:controller => "abilities", :action => "show", :id => 123}, {:resource => Person})
|
||||||
|
authorization.load_resource
|
||||||
|
@controller.instance_variable_get(:@ability).should == :some_resource
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -4,7 +4,8 @@ require 'active_support'
|
|||||||
require 'active_record'
|
require 'active_record'
|
||||||
require 'action_controller'
|
require 'action_controller'
|
||||||
require 'action_view'
|
require 'action_view'
|
||||||
require File.dirname(__FILE__) + '/../lib/cancan.rb'
|
require 'cancan'
|
||||||
|
require 'cancan/matchers'
|
||||||
|
|
||||||
Spec::Runner.configure do |config|
|
Spec::Runner.configure do |config|
|
||||||
config.mock_with :rr
|
config.mock_with :rr
|
||||||
@@ -16,3 +17,7 @@ class Ability
|
|||||||
def initialize(user)
|
def initialize(user)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# this class helps out in testing nesting
|
||||||
|
class Person
|
||||||
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user