Skip to content
This repository
Newer
Older
100644 93 lines (54 sloc) 4.35 kb
6c6a57be »
2009-11-16 adding documentation placeholder
1 = CanCan
2
39bffe91 »
2011-01-08 fixing github links in readme
3 Wiki[https://github.com/ryanb/cancan/wiki] | RDocs[http://rdoc.info/projects/ryanb/cancan] | Screencast[http://railscasts.com/episodes/192-authorization-with-cancan]
a13f78d6 »
2009-12-30 listing additional resources at top of readme page (including metrics…
4
6c3e87ee »
2010-10-05 updating readme and documentation
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.
b1d3d66e »
2009-11-16 filling readme
6
7
8 == Installation
9
6c3e87ee »
2010-10-05 updating readme and documentation
10 In <b>Rails 3</b>, add this to your Gemfile.
baeef0b9 »
2010-04-15 adding conditions behavior to Ability#can and fetch with Ability#cond…
11
6c3e87ee »
2010-10-05 updating readme and documentation
12 gem "cancan"
baeef0b9 »
2010-04-15 adding conditions behavior to Ability#can and fetch with Ability#cond…
13
6c3e87ee »
2010-10-05 updating readme and documentation
14 In <b>Rails 2</b>, add this to your environment.rb file.
18dcf2a1 »
2010-07-21 mention Rails 3 installation in README - closes #56
15
6c3e87ee »
2010-10-05 updating readme and documentation
16 config.gem "cancan"
18dcf2a1 »
2010-07-21 mention Rails 3 installation in README - closes #56
17
6c3e87ee »
2010-10-05 updating readme and documentation
18 Alternatively, you can install it as a plugin.
28eaf1bc »
2009-11-16 releasing gem v0.1.0
19
6c3e87ee »
2010-10-05 updating readme and documentation
20 rails plugin install git://github.com/ryanb/cancan.git
b1d3d66e »
2009-11-16 filling readme
21
22
f7480d1f »
2009-12-13 releasing gem v1.0.0 (backwards incompatible, see changelog)
23 == Getting Started
b1d3d66e »
2009-11-16 filling readme
24
120eafea »
2011-01-11 updating readme for 1.5
25 CanCan expects a +current_user+ method to exist in controllers. 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 to customize this behavior.
6c3e87ee »
2010-10-05 updating readme and documentation
26
120eafea »
2011-01-11 updating readme for 1.5
27 Next, make an +Ability+ class. CanCan 1.5 includes a generator for this.
b1d3d66e »
2009-11-16 filling readme
28
120eafea »
2011-01-11 updating readme for 1.5
29 rails g cancan:ability
25637bb3 »
2010-07-21 removing extra white space at end of lines
30
120eafea »
2011-01-11 updating readme for 1.5
31 This is where the user permission will be defined. See the comments in models/ability.rb and {Defining Abilities}[https://github.com/ryanb/cancan/wiki/defining-abilities] for details.
b1d3d66e »
2009-11-16 filling readme
32
120eafea »
2011-01-11 updating readme for 1.5
33 The current user's permissions can then be checked using the <tt>can?</tt> and <tt>cannot?</tt> methods in the view and controller.
b1d3d66e »
2009-11-16 filling readme
34
35 <% if can? :update, @article %>
36 <%= link_to "Edit", edit_article_path(@article) %>
37 <% end %>
38
39bffe91 »
2011-01-08 fixing github links in readme
39 See {Checking Abilities}[https://github.com/ryanb/cancan/wiki/checking-abilities] for more information
283f58ee »
2010-04-17 improving readme with links to wiki
40
41 The "authorize!" method in the controller will raise an exception if the user is not able to perform the given action.
b1d3d66e »
2009-11-16 filling readme
42
43 def show
44 @article = Article.find(params[:id])
8903feee »
2010-04-16 removing unauthorized! in favor of authorize! and including more info…
45 authorize! :read, @article
b1d3d66e »
2009-11-16 filling readme
46 end
47
b9995c61 »
2010-04-17 minor changes to readme
48 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 each action.
b1d3d66e »
2009-11-16 filling readme
49
50 class ArticlesController < ApplicationController
f7480d1f »
2009-12-13 releasing gem v1.0.0 (backwards incompatible, see changelog)
51 load_and_authorize_resource
25637bb3 »
2010-07-21 removing extra white space at end of lines
52
b1d3d66e »
2009-11-16 filling readme
53 def show
283f58ee »
2010-04-17 improving readme with links to wiki
54 # @article is already loaded and authorized
b1d3d66e »
2009-11-16 filling readme
55 end
56 end
57
120eafea »
2011-01-11 updating readme for 1.5
58 See {Authorizing Controller Actions}[https://github.com/ryanb/cancan/wiki/authorizing-controller-actions] for more information.
283f58ee »
2010-04-17 improving readme with links to wiki
59
6c3e87ee »
2010-10-05 updating readme and documentation
60 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+.
b1d3d66e »
2009-11-16 filling readme
61
62 class ApplicationController < ActionController::Base
f919ac53 »
2009-12-14 releasing gem v1.0.1
63 rescue_from CanCan::AccessDenied do |exception|
6c3e87ee »
2010-10-05 updating readme and documentation
64 flash[:alert] = exception.message
b1d3d66e »
2009-11-16 filling readme
65 redirect_to root_url
66 end
67 end
68
39bffe91 »
2011-01-08 fixing github links in readme
69 See {Exception Handling}[https://github.com/ryanb/cancan/wiki/exception-handling] for more information.
8903feee »
2010-04-16 removing unauthorized! in favor of authorize! and including more info…
70
b1d3d66e »
2009-11-16 filling readme
71
120eafea »
2011-01-11 updating readme for 1.5
72 == Wiki Docs
37f482e8 »
2010-04-15 default ActiveRecordAdditions#can method action to :read and use 'sco…
73
120eafea »
2011-01-11 updating readme for 1.5
74 * {Upgrading to 1.5}[https://github.com/ryanb/cancan/wiki/Upgrading-to-1.5]
75 * {Defining Abilities}[https://github.com/ryanb/cancan/wiki/Defining-Abilities]
76 * {Checking Abilities}[https://github.com/ryanb/cancan/wiki/Checking-Abilities]
77 * {Authorizing Controller Actions}[https://github.com/ryanb/cancan/wiki/Authorizing-Controller-Actions]
78 * {Exception Handling}[https://github.com/ryanb/cancan/wiki/Exception-Handling]
79 * {Changing Defaults}[https://github.com/ryanb/cancan/wiki/Changing-Defaults]
39bffe91 »
2011-01-08 fixing github links in readme
80 * {See more}[https://github.com/ryanb/cancan/wiki]
df276536 »
2009-11-17 adding documentation for testing abilities - closes #6
81
18b45d5c »
2010-10-05 adding Gemfile, to get specs running just bundle and rake - closes #163
82
83 == Questions or Problems?
84
39bffe91 »
2011-01-08 fixing github links in readme
85 If you have any issues with CanCan which you cannot find the solution to in the documentation, please add an {issue on GitHub}[https://github.com/ryanb/cancan/issues] or fork the project and send a pull request.
18b45d5c »
2010-10-05 adding Gemfile, to get specs running just bundle and rake - closes #163
86
e49190fc »
2011-01-08 fixing link in readme
87 To get the specs running you should call +bundle+ and then +rake+. Specs currently do not work in Ruby 1.9 due to the RR mocking framework. See the {spec/README}[https://github.com/ryanb/cancan/blob/master/spec/README.rdoc] for more information.
18b45d5c »
2010-10-05 adding Gemfile, to get specs running just bundle and rake - closes #163
88
89
9d582265 »
2009-11-16 couple fixes in readme
90 == Special Thanks
91
39bffe91 »
2011-01-08 fixing github links in readme
92 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.
Something went wrong with that request. Please try again.