authorization-san 2.0.1 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -48,7 +48,11 @@ module Authorization
48
48
  # end
49
49
  def allow_access(*args, &block)
50
50
  unless self.respond_to?(:access_allowed_for)
51
- self.class_inheritable_accessor(:access_allowed_for)
51
+ if respond_to?(:class_attribute)
52
+ class_attribute :access_allowed_for
53
+ else
54
+ class_inheritable_accessor(:access_allowed_for)
55
+ end
52
56
  self.access_allowed_for = {}.with_indifferent_access
53
57
  send(:protected, :access_allowed_for, :access_allowed_for=)
54
58
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: authorization-san
3
3
  version: !ruby/object:Gem::Version
4
- hash: 13
4
+ hash: 11
5
5
  prerelease:
6
6
  segments:
7
7
  - 2
8
- - 0
9
8
  - 1
10
- version: 2.0.1
9
+ - 0
10
+ version: 2.1.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Manfred Stienstra
@@ -15,8 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-03-02 00:00:00 +01:00
19
- default_executable:
18
+ date: 2011-11-17 00:00:00 Z
20
19
  dependencies: []
21
20
 
22
21
  description: A plugin for authorization in a ReSTful application.
@@ -36,32 +35,6 @@ files:
36
35
  - lib/authorization/block_access.rb
37
36
  - lib/authorization/deprecated.rb
38
37
  - rails/init.rb
39
- - examples/administrations_controller.rb
40
- - examples/application.rb
41
- - examples/application_with_multiple_auth_methods.rb
42
- - examples/authenticated_controller.rb
43
- - examples/page_controller_with_full_policy.rb
44
- - examples/pages_controller.rb
45
- - examples/public_controller.rb
46
- - examples/users_controller.rb
47
- - test/cases/behaviour_test.rb
48
- - test/cases/deprecated_test.rb
49
- - test/cases/internals_test.rb
50
- - test/cases/structural_test.rb
51
- - test/controllers/all.rb
52
- - test/controllers/application_controller.rb
53
- - test/controllers/authenticated_controller.rb
54
- - test/controllers/broken_block_controller.rb
55
- - test/controllers/complicated_controller.rb
56
- - test/controllers/multiple_roles_controller.rb
57
- - test/controllers/public_controller.rb
58
- - test/controllers/users_controller.rb
59
- - test/helpers/methods.rb
60
- - test/models/resource.rb
61
- - test/test_helper/rails2/test_helper.rb
62
- - test/test_helper/rails3/test_helper.rb
63
- - test/test_helper/shared.rb
64
- has_rdoc: true
65
38
  homepage: http://fingertips.github.com
66
39
  licenses: []
67
40
 
@@ -91,33 +64,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
91
64
  requirements: []
92
65
 
93
66
  rubyforge_project:
94
- rubygems_version: 1.5.2
67
+ rubygems_version: 1.8.11
95
68
  signing_key:
96
69
  specification_version: 3
97
70
  summary: A plugin for authorization in a ReSTful application.
98
- test_files:
99
- - examples/administrations_controller.rb
100
- - examples/application.rb
101
- - examples/application_with_multiple_auth_methods.rb
102
- - examples/authenticated_controller.rb
103
- - examples/page_controller_with_full_policy.rb
104
- - examples/pages_controller.rb
105
- - examples/public_controller.rb
106
- - examples/users_controller.rb
107
- - test/cases/behaviour_test.rb
108
- - test/cases/deprecated_test.rb
109
- - test/cases/internals_test.rb
110
- - test/cases/structural_test.rb
111
- - test/controllers/all.rb
112
- - test/controllers/application_controller.rb
113
- - test/controllers/authenticated_controller.rb
114
- - test/controllers/broken_block_controller.rb
115
- - test/controllers/complicated_controller.rb
116
- - test/controllers/multiple_roles_controller.rb
117
- - test/controllers/public_controller.rb
118
- - test/controllers/users_controller.rb
119
- - test/helpers/methods.rb
120
- - test/models/resource.rb
121
- - test/test_helper/rails2/test_helper.rb
122
- - test/test_helper/rails3/test_helper.rb
123
- - test/test_helper/shared.rb
71
+ test_files: []
72
+
@@ -1,11 +0,0 @@
1
- # The administrations controller is nested under organizations (ie. /organizations/3214/administrations)
2
- class PagesController < ApplicationController
3
- # The following rule only allows @authenticated if @authenticated.organization.id == params[:organization_id].
4
- # Roughly translated this means that the authenticated user can only access resources belonging to its own
5
- # organization.
6
- allow_access :authenticated, :scope => :organization
7
-
8
- def index
9
- @administrations = @authenticated.organization.administrations
10
- end
11
- end
@@ -1,22 +0,0 @@
1
- class ApplicationController < ActionController::Base
2
- # You have to specify where you want these actions to appear in your filter chain. Make sure you :block_access
3
- # before any sensitive processing occurs.
4
- before_filter :find_authenticated, :block_access
5
-
6
- private
7
-
8
- # Find the authenticated user
9
- def find_authenticated
10
- @authenticated = authenticate_with_http_basic { |username, password| User.authenticate(username, password) }
11
- end
12
-
13
- # Access was forbidden to client requesting the resource. React to that appropriately. Note that this reply is very
14
- # bare bones and you might want to return more elaborate responses in a real application.
15
- def access_forbidden
16
- if @authenticated.nil?
17
- request_http_basic_authentication "Accounting"
18
- else
19
- head :forbidden
20
- end
21
- end
22
- end
@@ -1,33 +0,0 @@
1
- class ApplicationController < ActionController::Base
2
- before_filter :find_authenticated, :block_access
3
-
4
- private
5
-
6
- # Find the authenticated user, cookie based authentication for browser users and HTTP Basic Authentication for
7
- # API users. Note that this does not allow you to get HTML resources when logged in through Basic Auth.
8
- def find_authenticated
9
- respond_to do |format|
10
- format.html do
11
- @authenticated = Person.find_by_id session[:authenticated_id] unless session[:authenticated_id].nil?
12
- end
13
- format.xml do
14
- @authenticated = authenticate_with_http_basic { |username, password| User.authenticate(username, password) }
15
- end
16
- end
17
- end
18
-
19
- # Access was forbidden to client requesting the resource. React to that appropriately. Note that this reply is very
20
- # bare bones and you might want to return more elaborate responses in a real application.
21
- def access_forbidden
22
- unless @authenticated
23
- # The user is not authenticated; ask for credentials
24
- respond_to do |format|
25
- format.html { redirect_to login_url }
26
- format.xml { request_http_basic_authentication "Accounting" }
27
- end
28
- else
29
- # The user is authentication but unauthorized for this resource
30
- head :forbidden
31
- end
32
- end
33
- end
@@ -1,6 +0,0 @@
1
- class AuthenticatedController < ApplicationController
2
- # Authenticated users can access all actions
3
- allow_access :authenticated
4
-
5
- def index; end
6
- end
@@ -1,28 +0,0 @@
1
- # The pages controller is a nest resource under users (ie. /users/12/pages)
2
- class PagesController < ApplicationController
3
- # A user may only access her own index
4
- allow_access(:authenticated, :only => :index) { @authenticated == @user }
5
- # A user may only access her own pages
6
- allow_access(:authenticated, :only => :show) { @authenticated == @page.user}
7
-
8
- # Always find the user the pages are nested under before applying the rules
9
- prepend_before_filter :find_user
10
- # Find the page before applying the rules when the show action is called
11
- prepend_before_filter :find_page, :only => :show
12
-
13
- def index
14
- @pages = @user.pages
15
- end
16
-
17
- def show; end
18
-
19
- private
20
-
21
- def find_user
22
- @user = User.find params[:user_id]
23
- end
24
-
25
- def find_page
26
- @page = Page.find params[:id]
27
- end
28
- end
@@ -1,25 +0,0 @@
1
- # The pages controller is nested under users (ie. /users/12/pages)
2
- class PagesController < ApplicationController
3
- # Users can only reach pages nested under their user_id. Note that this doesn't define the complete access policy,
4
- # some of the authorization is still done in the actions. See pages_controller_with_full_policy.rb for an example
5
- # of specifying everything in access rules.
6
- allow_access(:authenticated) { @authenticated.to_param == params[:user_id].to_param }
7
-
8
- before_filter :find_user
9
-
10
- def index
11
- @pages = @user.pages
12
- end
13
-
14
- def show
15
- @page = @user.pages.find params[:id]
16
- rescue ActiveRecord::RecordNotFound
17
- head :forbidden
18
- end
19
-
20
- private
21
-
22
- def find_user
23
- @user = User.find params[:user_id]
24
- end
25
- end
@@ -1,6 +0,0 @@
1
- class PublicController < ApplicationController
2
- # Everyone can access all actions
3
- allow_access
4
-
5
- def index; end
6
- end
@@ -1,27 +0,0 @@
1
- class UsersController < ApplicationController
2
- # The default is to deny all access. Every rule creates a 'hole' in this policy. You can specify multiple rules
3
- # per role if you want.
4
-
5
- # The 'admin' role (@authenticated.role) has access to all the actions.
6
- allow_access :admin
7
- # The 'editor' role has access to the index and show action.
8
- allow_access :editor, :only => [:index, :show]
9
- # The 'user' role has access to the index, show, edit and update role only if the resource he's editing is the same
10
- # as the user resource.
11
- allow_access :user, :only => [:index, :show, :edit, :update], :user_resource => true
12
- # The 'guest' role has access to the index and show action if the Proc returns true.
13
- allow_access(:guest, :only => [:index, :show]) { @authenticated.valid_email? }
14
- # Everyone can access the listing and the index action, the other actions can be accessed when it's not sunday.
15
- allow_access :only => :listing
16
- allow_access :only => :index
17
- allow_access() { Time.now.strftime('%A') != 'Sunday' }
18
-
19
- def index; end
20
- def listing; end
21
- def new; end
22
- def create; end
23
- def show; end
24
- def edit; end
25
- def update; end
26
- def destroy; end
27
- end
@@ -1,183 +0,0 @@
1
- require 'test_helper'
2
-
3
- require 'controllers/all'
4
- require 'models/resource'
5
-
6
- class BehaviourTest < ActionController::TestCase
7
- test "access is denied for nonexistant actions without an access rule" do
8
- begin
9
- tests UsersController, :authenticated => Resource.new(:role => :tester, :id => 1)
10
- get :unknown, :id => 1
11
- assert_response :forbidden
12
- rescue AbstractController::ActionNotFound # Rails 3 behaves diffently to missing methods
13
- assert true
14
- end
15
- end
16
-
17
- test "roles are properly checked" do
18
- tests UsersController, :authenticated => Resource.new
19
- {
20
- [:admin, :index] => :ok,
21
- [:admin, :show] => :ok,
22
- [:admin, :guest] => :ok,
23
- [:admin, :listing] => :ok,
24
- [:admin, :react] => :ok,
25
- [:editor, :index] => :ok,
26
- [:editor, :guest] => :forbidden,
27
- [:editor, :listing] => :ok,
28
- [:editor, :react] => :ok,
29
- [:guest, :index] => :forbidden,
30
- [:guest, :guest] => :ok,
31
- [:guest, :listing] => :ok,
32
- [:guest, :react] => :ok,
33
- [:user, :listing] => :ok,
34
- [:user, :react] => :ok,
35
- [:user, :index] => :forbidden,
36
- }.each do |(role, action), status|
37
- @controller.authenticated.role = role
38
- get action
39
- assert_response status
40
- end
41
- end
42
-
43
- test "authenticated is allowed to access its own resource" do
44
- tests UsersController, :authenticated => Resource.new(:role => :tester, :id => 1)
45
- get :show, :id => 1
46
- assert_response :ok
47
- end
48
-
49
- test "authenticated is not allowed to access other users" do
50
- tests UsersController, :authenticated => Resource.new(:role => :tester, :id => 1)
51
- get :show, :id => 2
52
- assert_response :forbidden
53
- end
54
-
55
- test "authenticated is allowed to access within the defined scope" do
56
- tests UsersController, :authenticated => Resource.new(:role => :reader, :organization => Resource.new(:id => 1))
57
- get :show, :organization_id => 1
58
- assert_response :success
59
- end
60
-
61
- test "authenticated is not allowed to access outside of the defined scope" do
62
- tests UsersController, :authenticated => Resource.new(:role => :tester, :id => 1)
63
- get :show, :organization_id => 2
64
- assert_response :forbidden
65
- end
66
- test "rule without restrictions opens up the whole controller" do
67
- tests PublicController
68
- get :index
69
- assert_response :ok
70
- end
71
-
72
- test "rule with special role :authenticated allows when @authenticated is truthy" do
73
- tests AuthenticatedController, :authenticated => true
74
- get :index
75
- assert_response :ok
76
- end
77
-
78
- test "rule with special role :authenticated disallows when @authenticated is not truthy" do
79
- tests AuthenticatedController, :authenticated => false
80
- get :index
81
- assert_response :forbidden
82
- end
83
-
84
- test "rule with broken block should raise an exception when evaluated" do
85
- tests BrokenBlockController
86
- assert_raises(NoMethodError) do
87
- get :index
88
- end
89
- end
90
-
91
- test "rule with block should only be evaluated when the action matches" do
92
- tests BrokenBlockController
93
- assert_nothing_raised do
94
- get :show
95
- end
96
- end
97
-
98
- test "rule with block should only be evaluated when the role matches" do
99
- tests BrokenBlockController, :authenticated => Resource.new(:role => :admin)
100
- assert_nothing_raised do
101
- get :show
102
- end
103
- end
104
-
105
- test "rule with block should only be evaluated when the special role matches" do
106
- tests BrokenBlockController, :authenticated => true
107
- assert_nothing_raised do
108
- get :show
109
- end
110
- end
111
-
112
- test "rule with multiple roles" do
113
- tests MultipleRolesController, :authenticated => Resource.new
114
- {
115
- [:a, :index] => :ok,
116
- [:b, :index] => :ok,
117
- [:c, :index] => :ok,
118
- [:d, :index] => :ok,
119
- [:e, :index] => :ok,
120
- [:f, :index] => :ok,
121
- [:e, :show] => :forbidden,
122
- [:f, :show] => :forbidden,
123
- [:g, :index] => :forbidden,
124
- [:h, :index] => :forbidden,
125
- [:g, :show] => :ok,
126
- [:h, :show] => :ok,
127
- }.each do |(role, action), status|
128
- @controller.authenticated.role = role
129
- get action
130
- assert_response status
131
- end
132
- end
133
-
134
- test "rule with special role, user resource and action restriction, should disallow unauthenticated" do
135
- tests ComplicatedController
136
- get :show, :id => 1
137
- assert_response :forbidden
138
- end
139
-
140
- test "rule with special role, user resource and action restriction, should disallow incorrect user" do
141
- tests ComplicatedController, :authenticated => Resource.new(:id => 2)
142
- get :show, :id => 1
143
- assert_response :forbidden
144
- end
145
-
146
- test "rule with special role, user resource and action restriction, should allow correct user" do
147
- tests ComplicatedController, :authenticated => Resource.new(:id => 1)
148
- get :show, :id => 1
149
- assert_response :ok
150
- end
151
-
152
- test "controller with rule about special role, user resource and action restriction, should allow open actions" do
153
- tests ComplicatedController
154
- get :index
155
- assert_response :ok
156
- end
157
-
158
- class ActionController::Base
159
- class << self
160
- attr_accessor :_routes
161
- end
162
- end
163
-
164
- private
165
-
166
- def tests(controller, options={})
167
- @request = ActionController::TestRequest.new
168
- @response = ActionController::TestResponse.new
169
- @controller ||= controller.new rescue nil
170
-
171
- if defined?(ActionDispatch)
172
- @routes = ActionDispatch::Routing::RouteSet.new
173
- @routes.draw { match ':controller(/:action(/:id(.:format)))' }
174
- @routes.finalize!
175
- controller._routes = @routes
176
- end
177
-
178
- @controller.request = @request
179
- @controller.params = {}
180
-
181
- @controller.authenticated = options[:authenticated]
182
- end
183
- end
@@ -1,127 +0,0 @@
1
- require 'test_helper'
2
-
3
- require 'models/resource'
4
- require 'helpers/methods'
5
-
6
- class DeprecatedTest < ActiveSupport::TestCase
7
- include Authorization::BlockAccess
8
- include MethodsHelpers
9
-
10
- test "action_allowed? sanity" do
11
- @access_allowed_for = {
12
- :admin => [{
13
- :directives => {}
14
- }],
15
- :editor => [{
16
- :directives => {:only => :index}
17
- }],
18
- :complex => [
19
- {:directives => {:only => :index}},
20
- {:directives => {:only => :show}}
21
- ],
22
- :all => [{
23
- :directives => {:only => :listing}
24
- }]
25
- }
26
- assert_action_allowed({
27
- [:admin, :index] => true,
28
- [:admin, :show] => true,
29
- [:admin, :unknown] => true,
30
- [:editor, :unknown] => false,
31
- [:editor, :index] => true,
32
- [:all, :index] => false,
33
- [:all, :unknown] => false,
34
- [:all, :listing] => true,
35
- [:complex, :index] => true,
36
- [:complex, :show] => true,
37
- [:complex, :unknown] => false
38
- })
39
- end
40
-
41
- test "action_allowed? sanity with directives" do
42
- @access_allowed_for = {:all => [{:directives => {}}] }
43
- assert_action_allowed({
44
- [:admin, :index] => false,
45
- [:all, :show] => true,
46
- [:unknown, :show] => false
47
- })
48
- end
49
-
50
- test "action_allowed? sanity without directives" do
51
- @access_allowed_for = {}
52
- assert_action_allowed({
53
- [:admin, :index] => false,
54
- [:all, :show] => false,
55
- [:show, :unknown] => false
56
- })
57
- end
58
-
59
- test "action_allowed? breaks when no rules are defined" do
60
- @access_allowed_for = nil
61
- params = HashWithIndifferentAccess.new :action => :something
62
- assert_raises(ArgumentError) { action_allowed?(params, :something) }
63
- end
64
-
65
- test "resource_allowed? sanity with :authenticated directive" do
66
- @access_allowed_for = {
67
- :all => [{
68
- :directives => {:authenticated => true}
69
- }]
70
- }
71
- assert !resource_allowed?({}, :admin, nil)
72
- assert !resource_allowed?({}, :admin, true)
73
- assert resource_allowed?({}, :all, true)
74
- assert resource_allowed?({:action => :edit}, :all, true)
75
- end
76
-
77
- test "resource_allowed? sanity with :user_resource directive" do
78
- @access_allowed_for = {
79
- :user => [{
80
- :directives => {:only => [:index, :show], :user_resource => true}
81
- }]
82
- }
83
- assert_resource_allowed({
84
- [{}, :admin, {}] => false,
85
- [{:id => 1}, :admin, {:id => 1}] => false,
86
- [{}, :admin, {:id => 1}] => false,
87
- [{:id => 1}, :admin, {}] => false,
88
- [{}, :user, {}] => false,
89
- [{:id => 1}, :user, {:id => 1}] => true,
90
- [{:id => 2}, :user, {:id => 1}] => false,
91
- [{:id => 1}, :user, {:id => 2}] => false,
92
- [{}, :user, {:id => 1}] => false,
93
- [{:id => 1}, :user, {}] => false,
94
- })
95
- end
96
-
97
- test "resource_allowed? sanity with :scope directive" do
98
- @access_allowed_for = {
99
- :user => [{
100
- :directives => {:only => [:index, :show], :scope => :organization}
101
- }]
102
- }
103
- assert_resource_allowed({
104
- [{}, :admin, {}] => false,
105
- [{:organization_id => 1}, :admin, {:organization => Resource.new({:id => 1})}] => false,
106
- [{}, :admin, {:organization => Resource.new({:id => 1})}] => false,
107
- [{:organization_id => 1}, :admin, {}] => false,
108
- [{}, :user, {}] => false,
109
- [{:organization_id => 1}, :user, {:organization => Resource.new({:id => 1})}] => true,
110
- [{}, :user, {:organization => Resource.new({:id => 1})}] => false,
111
- [{:organization_id => 1}, :user, {}] => false,
112
- [{:organization_id => 2}, :user, {:organization => Resource.new({:id => 1})}] => false,
113
- [{:organization_id => 1}, :user, {:organization => Resource.new({:id => 2})}] => false,
114
- })
115
- end
116
-
117
- test "block_allowed? sanity" do
118
- @access_allowed_for = {
119
- :admin => [{:block => self.class.instance_method(:do_true)}],
120
- :all => [{:block => self.class.instance_method(:do_false)}]
121
- }
122
- assert_block_allowed({
123
- :admin => true,
124
- :all => false
125
- })
126
- end
127
- end
@@ -1,223 +0,0 @@
1
- require 'test_helper'
2
-
3
- require 'models/resource'
4
- require 'helpers/methods'
5
-
6
- class BlockAccessTest < ActiveSupport::TestCase
7
- include Authorization::BlockAccess
8
- include MethodsHelpers
9
-
10
- test "block_access sanity" do
11
- @access_allowed_for = {
12
- :admin => [{
13
- :directives => {}
14
- }],
15
- :editor => [{
16
- :directives => {:only => :index}
17
- }],
18
- :blocked_guest => [{
19
- :directives => {:only => :index},
20
- :block => self.class.instance_method(:do_false)
21
- }],
22
- :open_guest => [{
23
- :directives => {:only => :index},
24
- :block => self.class.instance_method(:do_true)
25
- }],
26
- :complex => [
27
- {:directives => {:only => :index}},
28
- {:directives => {:only => :show}}
29
- ],
30
- :all => [{
31
- :directives => {:only => :listing}
32
- }]
33
- }
34
- assert_block_access({
35
- [:admin, :index] => true,
36
- [:admin, :show] => true,
37
- [:admin, :unknown] => true,
38
- [:editor, :unknown] => false,
39
- [:editor, :index] => true,
40
- [:blocked_guest, :index] => false,
41
- [:blocked_guest, :unknown] => false,
42
- [:open_guest, :index] => true,
43
- [:open_guest, :unknown] => false,
44
- [:all, :index] => false,
45
- [:all, :unknown] => false,
46
- [:all, :listing] => true,
47
- [:complex, :index] => true,
48
- [:complex, :show] => true,
49
- [:complex, :unknown] => false
50
- })
51
- end
52
-
53
- test "block_access breaks when no rules are defined" do
54
- @access_allowed_for = nil
55
- assert_raises(ArgumentError) { block_access }
56
- end
57
-
58
- test "access is denied when there are no rules" do
59
- @access_allowed_for = {}
60
- assert !block_access
61
- end
62
-
63
- test "access is granted when authenticated has role and accessor and a rule matches accessor" do
64
- @authenticated = Resource.new(:role => 'user', :'special?' => true)
65
- set_rules(:special => [{:directives => {}}])
66
- set_params(:action => :new)
67
- assert block_access
68
- end
69
-
70
- test "access is granted when authenticated has role and accessor and a rule matches role" do
71
- @authenticated = Resource.new(:role => 'user', :'special?' => true)
72
- set_rules(:user => [{:directives => {}}])
73
- set_params(:action => :new)
74
- assert block_access
75
- end
76
-
77
- test "access is denied when authenticated has role and accessor and NO rule matches" do
78
- @authenticated = Resource.new(:role => 'user', :'special?' => true)
79
- set_rules(:admin => [{:directives => {}}])
80
- set_params(:action => :new)
81
- assert !block_access
82
- end
83
-
84
- test "access is granted when authenticated has multiple accessors and a rule matches" do
85
- @access_allowed_for = {:special => [{
86
- :directives => {}
87
- }]}
88
- @authenticated = Resource.new(:'special?' => true, :'admin?' => true)
89
- @params = { :action => :new }.with_indifferent_access
90
- assert block_access
91
- end
92
- end
93
-
94
- class AccessByRuleTest < ActiveSupport::TestCase
95
- include Authorization::BlockAccess
96
- include MethodsHelpers
97
-
98
- test "matches action when there are no restrictions on action" do
99
- assert _matches_action?({}, :new)
100
- end
101
-
102
- test "matches action when there are no restrictions on action and no action" do
103
- assert _matches_action?({}, nil)
104
- end
105
-
106
- test "matches action when there are inclusive restrictions on action (array)" do
107
- assert _matches_action?({:only => [:index, :new, :create]}, :index)
108
- end
109
-
110
- test "matches action when there are inclusive restrictions on action (symbol)" do
111
- assert _matches_action?({:only => :index}, :index)
112
- end
113
-
114
- test "matches action when there are exclusive restrictions on action (array)" do
115
- assert _matches_action?({:except => [:update, :create, :delete]}, :index)
116
- end
117
-
118
- test "matches action when there are exclusive restrictions on action (symbol)" do
119
- assert _matches_action?({:except => :update}, :index)
120
- end
121
-
122
- test "does not match action when there are inclusive restrictions on action (array)" do
123
- assert !_matches_action?({:only => [:index, :new, :create]}, :update)
124
- end
125
-
126
- test "does not match action when there are inclusive restrictions on action (symbol)" do
127
- assert !_matches_action?({:only => :index}, :update)
128
- end
129
-
130
- test "does not match action when there are exclusive restrictions on action (array)" do
131
- assert !_matches_action?({:except => [:update, :create, :delete]}, :update)
132
- end
133
-
134
- test "does not match action when there are exclusive restrictions on action (symbol)" do
135
- assert !_matches_action?({:except => :update}, :update)
136
- end
137
-
138
- test "accepts a block when it's not there" do
139
- assert _block_is_successful?(nil)
140
- end
141
-
142
- test "accepts a block when it returns true" do
143
- assert _block_is_successful?(lambda { true })
144
- end
145
-
146
- test "refuses a block when it returns false" do
147
- assert !_block_is_successful?(lambda { false })
148
- end
149
-
150
- test "matches scope when there is no scope" do
151
- assert _matches_scope?(nil, {}, nil)
152
- end
153
-
154
- test "matches scope when the object ID matches the ID in the params" do
155
- assert _matches_scope?(:organization,
156
- {:organization_id => 12}.with_indifferent_access,
157
- Resource.new(:organization => Resource.new(:id => 12)))
158
- end
159
-
160
- test "does not match scope when the ID in the params is blank" do
161
- assert !_matches_scope?(:organization,
162
- {}.with_indifferent_access,
163
- Resource.new(:organization => Resource.new(:id => 12)))
164
- end
165
-
166
- test "does not match scope when the object ID is nil" do
167
- assert !_matches_scope?(:organization,
168
- {:organization_id => 12}.with_indifferent_access,
169
- Resource.new(:organization => Resource.new(:id => nil)))
170
- end
171
-
172
- test "does not match scope when both params are blank and the object ID is nil" do
173
- assert !_matches_scope?(:organization,
174
- {}.with_indifferent_access,
175
- Resource.new(:organization => Resource.new(:id => nil)))
176
- end
177
-
178
- test "does not match scope when the object ID does not match the ID in the params" do
179
- assert !_matches_scope?(:organization,
180
- {:organization_id => 32 }.with_indifferent_access,
181
- Resource.new(:organization => Resource.new(:id => 65)))
182
- end
183
-
184
- test "matches user resource when it doesn't have to run" do
185
- assert _matches_user_resource?(false, {}, nil)
186
- end
187
-
188
- test "matches user resource when it matches the params" do
189
- assert _matches_user_resource?(true, {:id => 12}.with_indifferent_access, Resource.new(:id => 12))
190
- end
191
-
192
- test "does not match user resource when the params are empty" do
193
- assert !_matches_user_resource?(true, {}.with_indifferent_access, Resource.new(:id => 12))
194
- end
195
-
196
- test "does not match user resource when the params are wrong" do
197
- assert !_matches_user_resource?(true, {:id => 32}.with_indifferent_access, Resource.new(:id => 12))
198
- end
199
-
200
- test "does not match user resource when the resource has no ID" do
201
- assert !_matches_user_resource?(true, {:id => 12}.with_indifferent_access, Resource.new(:id => nil))
202
- end
203
-
204
- test "matches authenticated requirement when it doesn't have to run (boolean)" do
205
- assert _matches_authenticated_requirement?(false, nil)
206
- end
207
-
208
- test "matches authenticated requirement when it doesn't have to run (nil)" do
209
- assert _matches_authenticated_requirement?(nil, nil)
210
- end
211
-
212
- test "matches authenticated requirement when authenticated is thruthy" do
213
- assert _matches_authenticated_requirement?(true, Resource.new)
214
- end
215
-
216
- test "does not match authenticated requirement when authenticated is not thruthy (boolean)" do
217
- assert !_matches_authenticated_requirement?(true, false)
218
- end
219
-
220
- test "does not match authenticated requirement when authenticated is not thruthy (nil)" do
221
- assert !_matches_authenticated_requirement?(true, nil)
222
- end
223
- end
@@ -1,21 +0,0 @@
1
- require 'test_helper'
2
-
3
- require 'controllers/application_controller'
4
- require 'controllers/users_controller'
5
- require 'models/resource'
6
-
7
- class StructuralTest < ActionController::TestCase
8
- tests UsersController
9
-
10
- def setup
11
- @controller.authenticated = Resource.new(:role => :admin)
12
- end
13
-
14
- test "rules should be in place" do
15
- assert @controller.__send__(:access_allowed_for)
16
- end
17
-
18
- test "role accessors should not be public" do
19
- assert @acontroller.public_methods.grep(/access_allowed_for/).empty?
20
- end
21
- end
@@ -1,7 +0,0 @@
1
- require 'controllers/application_controller'
2
- require 'controllers/authenticated_controller'
3
- require 'controllers/broken_block_controller'
4
- require 'controllers/complicated_controller'
5
- require 'controllers/public_controller'
6
- require 'controllers/multiple_roles_controller'
7
- require 'controllers/users_controller'
@@ -1,16 +0,0 @@
1
- class ApplicationController < ActionController::Base
2
- attr_accessor :authenticated
3
-
4
- before_filter :block_access
5
-
6
- def access_forbidden
7
- head :forbidden
8
- false
9
- end
10
-
11
- def logger
12
- @logger ||= Logger.new('/dev/null')
13
- end
14
-
15
- def rescue_action(e) raise e end;
16
- end
@@ -1,7 +0,0 @@
1
- class AuthenticatedController < ApplicationController
2
- allow_access :authenticated
3
-
4
- def index
5
- head :ok
6
- end
7
- end
@@ -1,10 +0,0 @@
1
- class BrokenBlockController < ApplicationController
2
- allow_access(:only => :index) { nil.unknown_method }
3
- allow_access(:only => :show) { true }
4
- allow_access(:authenticated, :only => :edit) { @authenticated.unknown_method }
5
- allow_access(:admin, :only => :edit) { @authenticated.unknown_method }
6
-
7
- %w(index show edit).each do |name|
8
- define_method(name) { head :ok }
9
- end
10
- end
@@ -1,8 +0,0 @@
1
- class ComplicatedController < ApplicationController
2
- allow_access :all, :only => :index
3
- allow_access :authenticated, :only => [:show, :edit, :update], :user_resource => true
4
-
5
- %w(index show edit update).each do |name|
6
- define_method(name) { head :ok }
7
- end
8
- end
@@ -1,10 +0,0 @@
1
- class MultipleRolesController < ApplicationController
2
- allow_access :a, :b
3
- allow_access [:c, :d]
4
- allow_access [:e, :f], :only => :index
5
- allow_access :g, :h, :only => :show
6
-
7
- %w(index show).each do |name|
8
- define_method(name) { head 200 }
9
- end
10
- end
@@ -1,7 +0,0 @@
1
- class PublicController < ApplicationController
2
- allow_access
3
-
4
- def index
5
- head :ok
6
- end
7
- end
@@ -1,13 +0,0 @@
1
- class UsersController < ApplicationController
2
- allow_access :admin
3
- allow_access :editor, :only => [:index, :show]
4
- allow_access(:guest, :only => :guest) { params[:action] == 'guest' }
5
- allow_access :tester, :only => :show, :user_resource => true
6
- allow_access :reader, :only => :show, :scope => :organization
7
- allow_access :only => :listing
8
- allow_access :only => :react
9
-
10
- %w(index show guest listing react).each do |name|
11
- define_method(name) { head :ok }
12
- end
13
- end
@@ -1,52 +0,0 @@
1
- module MethodsHelpers
2
- attr_reader :access_allowed_for, :params
3
-
4
- def logger
5
- @logger ||= Logger.new('/dev/null')
6
- end
7
-
8
- def do_false
9
- false
10
- end
11
-
12
- def do_true
13
- true
14
- end
15
-
16
- def set_rules(rules)
17
- @access_allowed_for = rules.with_indifferent_access
18
- end
19
-
20
- def set_params(params)
21
- @params = params.with_indifferent_access
22
- end
23
-
24
- def assert_action_allowed(h)
25
- h.each do |(role, action), value|
26
- params = {:action => action}.with_indifferent_access
27
- assert_equal(value, action_allowed?(params, role), "Expected #{role} to access #{action} with params #{params.inspect}")
28
- end
29
- end
30
-
31
- def assert_resource_allowed(h)
32
- h.each do |(params, role, authenticated), value|
33
- params = params.with_indifferent_access
34
- authenticated = authenticated ? Resource.new(authenticated) : nil
35
- assert_equal(value, resource_allowed?(params, role, authenticated), "Expected #{role} #{authenticated} to access #{params.inspect}")
36
- end
37
- end
38
-
39
- def assert_block_allowed(h)
40
- h.each do |role, value|
41
- assert_equal value, block_allowed?(role)
42
- end
43
- end
44
-
45
- def assert_block_access(h)
46
- h.each do |(role, action), expected|
47
- @authenticated = Resource.new(:role => role)
48
- @params = {:action => action}.with_indifferent_access
49
- assert_equal(expected, block_access, "Expected #{role} #{@authenticated} #{expected ? '' : 'NOT '}to access #{action}")
50
- end
51
- end
52
- end
@@ -1,37 +0,0 @@
1
- class Resource
2
- def initialize(hash={})
3
- @attributes = {}
4
- hash.each do |k,v|
5
- self.send("#{k}=", v)
6
- end
7
- end
8
-
9
- def id
10
- @attributes['id']
11
- end
12
-
13
- def id=(value)
14
- @attributes['id'] = value
15
- end
16
-
17
- def to_s
18
- "#<Resource:#{object_id} #{@attributes.inspect}>"
19
- end
20
-
21
- def method_missing(m, v=nil)
22
- if m.to_s =~ /(.*)=$/
23
- @attributes[$1] = v
24
- else
25
- if @attributes.has_key?(m.to_s)
26
- @attributes[m.to_s]
27
- else
28
- raise NoMethodError, "We don't know anything about #{m}"
29
- end
30
- end
31
- end
32
-
33
- alias_method :old_respond_to?, :respond_to?
34
- def respond_to?(m)
35
- old_respond_to?(m) or @attributes.has_key?(m.to_s)
36
- end
37
- end
@@ -1,29 +0,0 @@
1
- require File.expand_path('../../shared', __FILE__)
2
-
3
- module AuthorizationSanTest
4
- module Initializer
5
- def self.load_dependencies
6
- if rails_directory
7
- $:.unshift(File.join(rails_directory, 'activesupport', 'lib'))
8
- $:.unshift(File.join(rails_directory, 'activerecord', 'lib'))
9
- else
10
- require 'rubygems'
11
- gem 'rails', '< 3.0'
12
- end
13
-
14
- require 'test/unit'
15
-
16
- require 'active_support'
17
- require 'active_support/test_case'
18
- require 'active_record'
19
- require 'active_record/test_case'
20
- require 'active_record/base' # this is needed because of dependency hell
21
- require 'action_controller'
22
-
23
- $:.unshift File.expand_path('../../lib', __FILE__)
24
- require File.join(PLUGIN_ROOT, 'rails', 'init')
25
- end
26
- end
27
- end
28
-
29
- AuthorizationSanTest::Initializer.start
@@ -1,29 +0,0 @@
1
- require File.expand_path('../../shared', __FILE__)
2
-
3
- module AuthorizationSanTest
4
- module Initializer
5
- def self.load_dependencies
6
- if rails_directory
7
- $:.unshift(File.join(rails_directory, 'activesupport', 'lib'))
8
- $:.unshift(File.join(rails_directory, 'activerecord', 'lib'))
9
- else
10
- require 'rubygems'
11
- gem 'rails', '> 3.0'
12
- end
13
-
14
- require 'test/unit'
15
-
16
- require 'active_support'
17
- require 'active_support/test_case'
18
- require 'active_record'
19
- require 'active_record/test_case'
20
- require 'active_record/base' # this is needed because of dependency hell
21
- require 'action_controller'
22
-
23
- $:.unshift File.expand_path('../../lib', __FILE__)
24
- require File.join(PLUGIN_ROOT, 'rails', 'init')
25
- end
26
- end
27
- end
28
-
29
- AuthorizationSanTest::Initializer.start
@@ -1,17 +0,0 @@
1
- module AuthorizationSanTest
2
- module Initializer
3
- VENDOR_RAILS = File.expand_path('../../../../../rails', __FILE__)
4
- PLUGIN_ROOT = File.expand_path('../../../', __FILE__)
5
-
6
- def self.rails_directory
7
- if File.exist?(VENDOR_RAILS)
8
- VENDOR_RAILS
9
- end
10
- end
11
-
12
- def self.start
13
- load_dependencies
14
- ActionController::Routing::Routes.reload rescue nil
15
- end
16
- end
17
- end