Skip to content

Commit 45e91af

Browse files
committed
Merge pull request #196 from doximity/rails4
Rails 4 (thanks to @divoxx, @zeiv, @gordonbisnor and @aepstein)
2 parents 549a8d3 + 963ae55 commit 45e91af

30 files changed

+706
-103
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,7 @@ garlic
33
nbproject
44
rdoc
55
gemfiles/*.lock
6+
log/*
7+
*.sublime*
8+
9+

.travis.yml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,19 @@ language: ruby
22
script: bundle exec rake test
33
rvm:
44
- 1.8.7
5-
- 1.9.2
65
- 1.9.3
76
gemfile:
87
- gemfiles/2.3.gemfile
98
- gemfiles/3.0.gemfile
109
- gemfiles/3.1.gemfile
1110
- gemfiles/3.2.gemfile
11+
- gemfiles/4.0.gemfile
12+
- gemfiles/4.1.gemfile
1213
matrix:
13-
allow_failures:
14-
- rvm: 1.9.2
15-
gemfile: gemfiles/2.3.gemfile
14+
exclude:
15+
- rvm: 1.8.7
16+
gemfile: gemfiles/4.0.gemfile
17+
- rvm: 1.8.7
18+
gemfile: gemfiles/4.1.gemfile
1619
- rvm: 1.9.3
1720
gemfile: gemfiles/2.3.gemfile

README.rdoc

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Plugin features
2121
* Authorize CRUD (Create, Read, Update, Delete) activities
2222
* Query rewriting to automatically only fetch authorized records
2323
* DSL for specifying Authorization rules in an authorization configuration
24+
* Support for Rails 4, with backwards compatibility through Rails 2
2425

2526

2627
Requirements
@@ -36,6 +37,99 @@ There is a decl_auth screencast by Ryan Bates, nicely introducing the main conce
3637
http://railscasts.com/episodes/188-declarative-authorization
3738

3839

40+
= Quick Start
41+
42+
=== Installer
43+
44+
Declarative Authorization comes with an installer to make setup easy.
45+
46+
First, include declarative_authorization in your gemfile.
47+
48+
#! Gemfile
49+
gem 'declarative_authorization'
50+
51+
Next, bundle and install.
52+
53+
$ bundle
54+
$ rails g authorization:install [UserModel=User] [field:type field:type ...] [--create-user --commit --user-belongs-to-role]
55+
56+
This installer will create a Role model, an admin and a user role, and set a
57+
has_and_belongs_to_many relationship between the User model and the Role model.
58+
It will also add a `role_symbols` method to the user model to meet
59+
declarative_authorization's requirements. The default User model is User. You can override this by simply typing the name of a model as above.
60+
61+
You can create the model with the fields provided by using the `--create-user` option.
62+
63+
The `--commit` option will run `rake db:migrate` and `rake db:seed`.
64+
65+
The `--user-belongs-to-role` option will set up a one-to-many relationship between Users and Roles.
66+
That is, each user has a role_id column and can only have one role. Role inheritance can be used
67+
in authorization rules.
68+
69+
Finally, the installer also copies default authorization rules, as below.
70+
71+
=== Generate Authorization Rules
72+
73+
To copy a default set of authorization rules which includes CRUD priveleges, run:
74+
75+
$ rails g authorization:rules
76+
77+
This command will copy the following to `config/authorization_rules.rb`. Remember
78+
to implement the requirements of this gem as described in the Installation section
79+
at the end of this README if you do not use the above installer.
80+
81+
authorization do
82+
role :guest do
83+
# add permissions for guests here, e.g.
84+
# has_permission_on :conferences, :to => :read
85+
end
86+
87+
# permissions on other roles, such as
88+
# role :admin do
89+
# has_permission_on :conferences, :to => :manage
90+
# end
91+
# role :user do
92+
# has_permission_on :conferences, :to => [:read, :create]
93+
# has_permission_on :conferences, :to => [:update, :delete] do
94+
# if_attribute :user_id => is {user.id}
95+
# end
96+
# end
97+
# See the readme or GitHub for more examples
98+
end
99+
100+
privileges do
101+
# default privilege hierarchies to facilitate RESTful Rails apps
102+
privilege :manage, :includes => [:create, :read, :update, :delete]
103+
privilege :create, :includes => :new
104+
privilege :read, :includes => [:index, :show]
105+
privilege :update, :includes => :edit
106+
privilege :delete, :includes => :destroy
107+
end
108+
109+
=== Controller Authorization
110+
111+
For RESTful controllers, add `filter_resource_access`:
112+
113+
class MyRestfulController < ApplicationController
114+
filter_resource_access
115+
...
116+
end
117+
118+
For a non-RESTful controller, you can use `filter_access_to`:
119+
120+
class MyOtherController < ApplicationController
121+
filter_access_to :all
122+
# or a group: filter_access_to [:action1, :action2]
123+
...
124+
end
125+
126+
=== View Authorization
127+
128+
Declarative Authorization will use `current_user` to check authorization.
129+
130+
<%= link_to 'Edit Post', edit_post_path(@post) if permitted_to? :update, @post %>
131+
132+
39133
= Authorization Data Model
40134

41135
----- App domain ----|-------- Authorization conf ---------|------- App domain ------
@@ -92,6 +186,15 @@ filter_access_to with the appropriate parameters to protect the CRUD methods.
92186
See Authorization::AuthorizationInController::ClassMethods for options on
93187
nested resources and custom member and collection actions.
94188

189+
By default, declarative_authorization will enable filter_resource_access compatibility with strong_parameters in Rails 4. If you want to disable this behavior, you can use the `:strong_parameters` option.
190+
191+
class EmployeesController < ApplicationController
192+
filter_resource_access :strong_parameters => false
193+
...
194+
end
195+
196+
Simalarly, you can use `:strong_parameters => true` if you are using strong_parameters in Rails 3.
197+
95198
If you prefer less magic or your controller has no resemblance with the resource
96199
controllers, directly calling filter_access_to may be the better option. Examples
97200
are given in the following. E.g. the privilege index users is required for

Rakefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
require 'rake'
22
require 'rake/testtask'
3-
require 'rake/rdoctask'
3+
require 'rdoc/task'
44

55
desc 'Default: run unit tests against all versions.'
66
task :default => 'bundles:test'

app/controllers/authorization_rules_controller.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
rescue LoadError; end
1313

1414
class AuthorizationRulesController < ApplicationController
15-
unloadable
1615

1716
filter_access_to :all, :require => :read
1817
def index
@@ -256,4 +255,4 @@ def find_all_users
256255

257256
else
258257
class AuthorizationRulesController < ApplicationController; end
259-
end # activate_authorization_rules_browser?
258+
end # activate_authorization_rules_browser?

app/controllers/authorization_usages_controller.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
require File.join(File.dirname(__FILE__), %w{.. .. lib declarative_authorization maintenance})
44

55
class AuthorizationUsagesController < ApplicationController
6-
unloadable
76

87
helper :authorization_rules
98
filter_access_to :all, :require => :read
@@ -20,4 +19,4 @@ def index
2019

2120
else
2221
class AuthorizationUsagesController < ApplicationController; end
23-
end # activate_authorization_rules_browser?
22+
end # activate_authorization_rules_browser?

app/views/authorization_rules/_change.erb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<%= select_tag :context, options_for_select(@contexts.map {|c| [human_context(c), c.to_s]}.sort, @context.to_s) %>
99
<br/>
1010
<label></label>
11-
<%= link_to_function "Show current permissions", "show_current_permissions()", :class => 'unimportant' %>
11+
<%= link_to "Show current permissions", '#', onclick: "show_current_permissions()", :class => 'unimportant' %>
1212
<br/><br/>
1313
How many users should be <strong>affected</strong>?
1414
<br/>
@@ -53,6 +53,6 @@
5353
<ul id="prohibited_actions"></ul>
5454

5555
<p class="submit">
56-
<%= button_to_function "Suggest Changes", "suggest_changes()" %>
56+
<%= button_to "Suggest Changes", '#', onclick: "suggest_changes()" %>
5757
</p>
5858
</form>

app/views/authorization_rules/_show_graph.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,6 @@
3939
}
4040
<% end %>
4141
<div id="graph-container" style="display:none">
42-
<%= link_to_function "Hide", "$('graph-container').hide()", :class => 'important' %><br/>
42+
<%= link_to '#', "Hide", onclick: "$('graph-container').hide()", :class => 'important' %><br/>
4343
<object id="graph" data="" type="image/svg+xml" style="max-width:100%;margin-top: 0.5em"/>
4444
</div>

app/views/authorization_rules/change.html.erb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<h1>Suggestions on Authorization Rules Change</h1>
22
<p><%= navigation %></p>
33
<div style="display:none" id="suggest-graph-container">
4-
<%= link_to_function "Hide", "$(this).up().hide()", :class => 'important' %>
5-
<%= link_to_function "Toggle stacked roles", "toggle_graph_params('suggest-graph', 'stacked_roles');" %>
6-
<%= link_to_function "Toggle only users' roles", "toggle_graph_params('suggest-graph', 'only_relevant_roles');" %><br/>
4+
<%= link_to "Hide", '#', onclick: "$(this).up().hide()", :class => 'important' %>
5+
<%= link_to "Toggle stacked roles", '#', onclick: "toggle_graph_params('suggest-graph', 'stacked_roles');" %>
6+
<%= link_to "Toggle only users' roles", '#', onclick: "toggle_graph_params('suggest-graph', 'only_relevant_roles');" %><br/>
77
<object id="suggest-graph" data="" type="image/svg+xml" style="max-width: 98%;max-height: 95%;margin-top: 0.5em"/>
88
</div>
99
<%= render 'show_graph' %>

app/views/authorization_rules/graph.html.erb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,5 @@
4343
<div style="margin: 1em;border:1px solid #ccc;max-width:95%">
4444
<object id="graph" data="<%= url_for :format => 'svg' %>" type="image/svg+xml" style="max-width:100%"/>
4545
</div>
46-
<%= button_to_function "Zoom in", '$("graph").style.maxWidth = "";$(this).toggle();$(this).next().toggle()' %>
47-
<%= button_to_function "Zoom out", '$("graph").style.maxWidth = "100%";$(this).toggle();$(this).previous().toggle()', :style => 'display:none' %>
46+
<%= button_to "Zoom in", '#', onclick: '$("graph").style.maxWidth = "";$(this).toggle();$(this).next().toggle()' %>
47+
<%= button_to "Zoom out", '#', onclick: '$("graph").style.maxWidth = "100%";$(this).toggle();$(this).previous().toggle()', :style => 'display:none' %>

0 commit comments

Comments
 (0)