action-guard 0.1.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.rspec CHANGED
@@ -1 +1,2 @@
1
1
  --color
2
+ --format documentation
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 1.1.0
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "action-guard"
8
- s.version = "0.1.0"
8
+ s.version = "1.1.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Rob Westgeest"]
12
- s.date = "2012-03-01"
12
+ s.date = "2012-06-06"
13
13
  s.description = "authorisation module of actions based on url-paths for usage in Rails and possibly other ruby based web frameworks"
14
14
  s.email = "rob.westgeest@qwan.it"
15
15
  s.extra_rdoc_files = [
@@ -42,8 +42,8 @@ Gem::Specification.new do |s|
42
42
  s.licenses = ["MIT"]
43
43
  s.rdoc_options = ["--charset=UTF-8"]
44
44
  s.require_paths = ["lib"]
45
- s.rubygems_version = "1.8.10"
46
- s.summary = "Action guard-0.1.0"
45
+ s.rubygems_version = "1.8.24"
46
+ s.summary = "Action guard-1.1.0"
47
47
 
48
48
  if s.respond_to? :specification_version then
49
49
  s.specification_version = 3
@@ -45,10 +45,11 @@ module ActionGuard
45
45
  rules[path_matcher] = ExactRoleRule.new(role_value)
46
46
  end
47
47
 
48
- def authorized?(person, path)
48
+ def authorized?(person, request_params)
49
49
  raise Error.new("no configuration loaded") if rules.empty?
50
+ path = "#{request_params['controller']}##{request_params['action']}"
50
51
  rule_key = rules.keys.sort{|x,y| y <=> x }.select {|k| path =~ /^#{k}/}.first
51
- rules[rule_key].allows?(person)
52
+ rules[rule_key].allows?(person,request_params)
52
53
  end
53
54
 
54
55
  private
@@ -3,7 +3,7 @@ module ActionGuard
3
3
  def initialize(role)
4
4
  @allowed_role = role.to_s
5
5
  end
6
- def allows?(person)
6
+ def allows?(person, request_params)
7
7
  return false unless person
8
8
  return person.role.to_s == @allowed_role
9
9
  end
@@ -17,23 +17,23 @@ module ActionGuard
17
17
  @additional_rule = proc
18
18
  end
19
19
 
20
- def allows?(person)
20
+ def allows?(person, request_params)
21
21
  return false unless person
22
22
  return false unless @role_leveler.role(person.role) >= @role_leveler.role(@allowed_level)
23
23
  return false if @to_allowed_level && @role_leveler.role(@to_allowed_level) < @role_leveler.role(person.role)
24
24
  return true unless @additional_rule
25
- return @additional_rule.call(person)
25
+ return @additional_rule.call(person, request_params)
26
26
  end
27
27
  end
28
28
 
29
29
  class AllowRule
30
- def allows?(person)
30
+ def allows?(person, request_params)
31
31
  true
32
32
  end
33
33
  end
34
34
 
35
35
  class DisallowRule
36
- def allows?(person)
36
+ def allows?(person, request_params)
37
37
  false
38
38
  end
39
39
  end
@@ -1,11 +1,11 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  RSpec::Matchers.define :authorize do |account|
4
- chain :to_perform_action do |action|
5
- @action = action
4
+ chain :to_perform_action do |request|
5
+ @request = a_request_for(request)
6
6
  end
7
7
  match do |actual_guard|
8
- actual_guard.authorized?(account, @action)
8
+ actual_guard.authorized?(account, @request)
9
9
  end
10
10
  end
11
11
 
@@ -13,8 +13,21 @@ describe ActionGuard do
13
13
  let (:guard) { ActionGuard::Guard.new }
14
14
 
15
15
 
16
+ def a_request_for(path)
17
+ request_params_for(path)
18
+ end
19
+
20
+ def request_params_for(path)
21
+ path, parameters = path.split("?")
22
+ controller, action = path.split('#')
23
+ parameters_hash = Hash[ parameters && parameters.split("&").map {|key_value| key_value.split('=').map{|e| e.strip }} || [] ]
24
+ parameters_hash['controller'] = controller
25
+ parameters_hash['action'] = action || 'index'
26
+ parameters_hash
27
+ end
28
+
16
29
  def account_with_role(role)
17
- return stub(:account,:role => role)
30
+ return stub(:account,:role => role.to_s)
18
31
  end
19
32
 
20
33
  describe "valid_role" do
@@ -69,21 +82,21 @@ describe ActionGuard do
69
82
  describe "defining a rule" do
70
83
  it "fails when role not defined" do
71
84
  lambda {
72
- guard.leveled_rule '/some_controller/some_action', :biker
85
+ guard.leveled_rule 'some_controller#some_action', :biker
73
86
  }.should raise_error ActionGuard::Error
74
87
  end
75
88
 
76
89
  it "fails when role not defined" do
77
90
  guard.define_role(:god, 0)
78
91
  lambda {
79
- guard.leveled_rule '/some_controller/some_action', :god, :biker
92
+ guard.leveled_rule 'some_controller/some_action', :god, :biker
80
93
  }.should raise_error ActionGuard::Error
81
94
  end
82
95
 
83
96
  it "passes when role defined" do
84
97
  lambda {
85
98
  guard.define_role :biker, 0
86
- guard.leveled_rule '/some_controller/some_action', :biker
99
+ guard.leveled_rule 'some_controller#some_action', :biker
87
100
  }.should_not raise_error ActionGuard::Error
88
101
  end
89
102
  end
@@ -91,7 +104,7 @@ describe ActionGuard do
91
104
  describe "authorization when no rules defined" do
92
105
  it "raises error on trying to authorize" do
93
106
  lambda {
94
- guard.authorized?(account_with_role(:admin), '/some_controller/some_action')
107
+ guard.authorized?(account_with_role(:admin), 'some_controller#some_action')
95
108
  }.should raise_error ActionGuard::Error
96
109
  end
97
110
  end
@@ -106,61 +119,61 @@ describe ActionGuard do
106
119
 
107
120
  describe "on an allowance rule" do
108
121
  before do
109
- guard.allow_rule '/'
122
+ guard.allow_rule 'home'
110
123
  end
111
124
  it "allows" do
112
- guard.should authorize(account_with_role(:worker)).to_perform_action('/')
125
+ guard.should authorize(account_with_role(:worker)).to_perform_action('home')
113
126
  end
114
127
  it "allows regardless of account" do
115
- guard.should authorize(nil).to_perform_action( '/')
128
+ guard.should authorize(nil).to_perform_action('home')
116
129
  end
117
130
  end
118
131
 
119
132
  describe "on an exact rule" do
120
133
  before do
121
- guard.exact_role_rule '/', :admin
134
+ guard.exact_role_rule 'home', :admin
122
135
  end
123
136
  it "allows if role matches" do
124
- guard.should authorize(account_with_role(:admin)).to_perform_action( '/')
137
+ guard.should authorize(account_with_role(:admin)).to_perform_action( 'home')
125
138
  end
126
139
  it "allows if role is a string" do
127
- guard.should authorize(account_with_role('admin')).to_perform_action('/')
140
+ guard.should authorize(account_with_role('admin')).to_perform_action('home')
128
141
  end
129
142
  it "does not allow action if role does not match" do
130
- guard.should_not authorize(account_with_role(:worker)).to_perform_action('/')
131
- guard.should_not authorize(account_with_role(:god)).to_perform_action('/')
143
+ guard.should_not authorize(account_with_role(:worker)).to_perform_action('home')
144
+ guard.should_not authorize(account_with_role(:god)).to_perform_action('home')
132
145
  end
133
146
  it "does not allow action if person not passed" do
134
- guard.should_not authorize(nil).to_perform_action('/')
147
+ guard.should_not authorize(nil).to_perform_action('home')
135
148
  end
136
149
  end
137
150
 
138
151
  describe "on a leveled action rule" do
139
152
  before do
140
- guard.leveled_rule '/some_controller/some_action', :admin
141
- guard.leveled_rule '/some_controller/some_other_action', :admin, :king
153
+ guard.leveled_rule 'some_controller#some_action', :admin
154
+ guard.leveled_rule 'some_controller#some_other_action', :admin, :king
142
155
  end
143
156
 
144
157
  it "disallows action when no account available" do
145
- guard.should_not authorize(nil).to_perform_action('/some_controller/some_action')
146
- guard.should_not authorize(nil).to_perform_action('/some_controller/some_other')
158
+ guard.should_not authorize(nil).to_perform_action('some_controller#some_action')
159
+ guard.should_not authorize(nil).to_perform_action('some_controller#some_other')
147
160
  end
148
161
 
149
162
  it "allows action for that level and higher" do
150
- guard.should authorize(account_with_role(:god)).to_perform_action('/some_controller/some_action')
151
- guard.should authorize(account_with_role(:admin)).to_perform_action('/some_controller/some_action')
152
- guard.should_not authorize(account_with_role(:worker)).to_perform_action('/some_controller/some_action')
163
+ guard.should authorize(account_with_role(:god)).to_perform_action('some_controller#some_action')
164
+ guard.should authorize(account_with_role(:admin)).to_perform_action('some_controller#some_action')
165
+ guard.should_not authorize(account_with_role(:worker)).to_perform_action('some_controller#some_action')
153
166
  end
154
167
 
155
168
  it "allows action for that level and higher until second level" do
156
- guard.should authorize(account_with_role(:king)).to_perform_action('/some_controller/some_other_action')
157
- guard.should authorize(account_with_role(:admin)).to_perform_action('/some_controller/some_other_action')
158
- guard.should_not authorize(account_with_role(:god)).to_perform_action('/some_controller/some_other_action')
159
- guard.should_not authorize(account_with_role(:worker)).to_perform_action('/some_controller/some_other_action')
169
+ guard.should authorize(account_with_role(:king)).to_perform_action('some_controller#some_other_action')
170
+ guard.should authorize(account_with_role(:admin)).to_perform_action('some_controller#some_other_action')
171
+ guard.should_not authorize(account_with_role(:god)).to_perform_action('some_controller#some_other_action')
172
+ guard.should_not authorize(account_with_role(:worker)).to_perform_action('some_controller#some_other_action')
160
173
  end
161
174
 
162
175
  it "does not allow the action for a account with an illegal role value" do
163
- guard.should_not authorize(account_with_role(:biker)).to_perform_action('/some_controller/some_action')
176
+ guard.should_not authorize(account_with_role(:biker)).to_perform_action('some_controller#some_action')
164
177
  end
165
178
  end
166
179
 
@@ -168,58 +181,70 @@ describe ActionGuard do
168
181
  let(:mock_block_body) { mock }
169
182
 
170
183
  before do
171
- guard.leveled_rule('/some_controller/some_action', :admin) do |accnt|
172
- mock_block_body.block_called(accnt)
184
+ guard.leveled_rule('some_controller#some_action', :admin) do |*args|
185
+ mock_block_body.block_called(*args)
173
186
  end
174
187
  end
175
188
 
176
189
  it "does not authorize action if the rule disallows the action" do
177
190
  account = account_with_role(:worker)
178
- mock_block_body.should_receive(:block_called).with(account).never
179
- guard.should_not authorize(account).to_perform_action('/some_controller/some_action')
191
+ mock_block_body.should_receive(:block_called).never
192
+ guard.should_not authorize(account).to_perform_action('some_controller#some_action')
193
+ end
194
+
195
+ it "calls block if action is authorized" do
196
+ account = account_with_role(:admin)
197
+ mock_block_body.should_receive(:block_called).with(account, request_params_for('some_controller#some_action'))
198
+ guard.authorized?(account, a_request_for( 'some_controller#some_action'))
180
199
  end
181
200
 
182
201
  it "does not authorize action if role sufices and block returns false" do
183
202
  account = account_with_role(:admin)
184
- mock_block_body.should_receive(:block_called).with(account).and_return false
185
- guard.should_not be_authorized(account,'/some_controller/some_action')
203
+ mock_block_body.stub(:block_called).and_return false
204
+ guard.should_not be_authorized(account, a_request_for('some_controller#some_action'))
186
205
  end
187
206
 
188
207
  it "authorizes action is role sufices and block returns true" do
189
208
  account = account_with_role(:admin)
190
- mock_block_body.should_receive(:block_called).with(account).and_return true
191
- guard.should be_authorized(account,'/some_controller/some_action')
209
+ mock_block_body.stub(:block_called).and_return true
210
+ guard.should be_authorized(account, a_request_for('some_controller#some_action'))
192
211
  end
193
212
  end
194
213
 
195
214
  describe "matching rules" do
196
215
  before do
197
- guard.allow_rule('/home')
198
- guard.refuse_rule('/maintenance')
216
+ guard.allow_rule('home')
217
+ guard.refuse_rule('maintenance')
199
218
  end
219
+
200
220
  it "does not authorize if path does not match any rule" do
201
- guard.authorized?(nil, '/unmatched/path').should be_false
221
+ guard.authorized?(nil, a_request_for('unmatched/path')).should be_false
202
222
  end
223
+
203
224
  it "matches a rule on exact path" do
204
- guard.should authorize(nil).to_perform_action('/home')
225
+ guard.should authorize(nil).to_perform_action('home')
205
226
  end
227
+
206
228
  it "matches a rule on part of a path" do
207
- guard.should authorize(nil).to_perform_action('/home/contact')
229
+ guard.should authorize(nil).to_perform_action('home/contact')
208
230
  end
209
- it "preferres a longer path" do
210
- guard.allow_rule('/maintenance/show')
211
- guard.authorized?(nil, '/maintenance/edit/1').should be_false
212
- guard.should authorize(nil).to_perform_action('/maintenance/show/1')
231
+
232
+ it "preferres a longer path in matching" do
233
+ guard.allow_rule('maintenance/things')
234
+ guard.should_not authorize(nil).to_perform_action('maintenance#edit?id=1')
235
+ guard.should authorize(nil).to_perform_action('maintenance/things')
213
236
  end
237
+
214
238
  it "preferres a longer path regardless off order of appearance" do
215
- guard.allow_rule('/some_path/show')
216
- guard.refuse_rule('/some_path')
217
- guard.authorized?(nil, '/some_path/edit/1').should be_false
218
- guard.should authorize(nil).to_perform_action('/some_path/show/1')
239
+ guard.allow_rule('some_path#show')
240
+ guard.refuse_rule('some_path')
241
+ guard.should_not authorize(nil).to_perform_action('some_path#edit?id=1')
242
+ guard.should authorize(nil).to_perform_action('some_path#show?1')
219
243
  end
244
+
220
245
  it "matches all rules from the beginnning of the path" do
221
246
  # /home/maintenance is evaluated by /home, not by /maintenance
222
- guard.should authorize(nil).to_perform_action('/home/maintenance')
247
+ guard.should authorize(nil).to_perform_action('home/maintenance')
223
248
  end
224
249
  end
225
250
  end
@@ -229,20 +254,20 @@ describe ActionGuard do
229
254
  guard.load_from_string %q{
230
255
  role :worker, 1
231
256
  role :admin, 0
232
- allow '/some_controller', :at_least => :worker
233
- allow '/some_controller/some_action', :at_least => :admin
234
- allow '/some_controller/when_role_matches_exact', :only_by => :worker
235
- allow '/some_controller/when_matches_exact_by_implication', :at_least => :worker, :at_most => :worker
236
- allow '/'
257
+ allow 'some_controller', :at_least => :worker
258
+ allow 'some_controller#some_action', :at_least => :admin
259
+ allow 'some_controller#when_role_matches_exact', :only_by => :worker
260
+ allow 'some_controller#when_matches_exact_by_implication', :at_least => :worker, :at_most => :worker
261
+ allow '' # wildcard for other controllers
237
262
  }
238
- guard.should authorize(account_with_role(:admin)).to_perform_action('/some_controller/some_action')
239
- guard.should_not authorize(account_with_role(:worker)).to_perform_action('/some_controller/some_action')
240
- guard.should authorize(account_with_role(:worker)).to_perform_action('/some_controller/some_other_action')
241
- guard.should authorize(account_with_role(:worker)).to_perform_action('/some_other_controller/some_other_action')
242
- guard.should authorize(nil).to_perform_action('/some_other_controller/some_other_action')
243
- guard.should_not authorize(account_with_role(:admin)).to_perform_action('/some_controller/when_role_matches_exact')
244
- guard.should authorize(account_with_role(:worker)).to_perform_action('/some_controller/when_matches_exact_by_implication')
245
- guard.should_not authorize(account_with_role(:admin)).to_perform_action('/some_controller/when_matches_exact_by_implication')
263
+ guard.should authorize(account_with_role(:admin)).to_perform_action('some_controller#some_action')
264
+ guard.should_not authorize(account_with_role(:worker)).to_perform_action('some_controller#some_action')
265
+ guard.should authorize(account_with_role(:worker)).to_perform_action('some_controller#some_other_action')
266
+ guard.should authorize(account_with_role(:worker)).to_perform_action('some_other_controller#some_other_action')
267
+ guard.should authorize(nil).to_perform_action('some_other_controller#some_other_action')
268
+ guard.should_not authorize(account_with_role(:admin)).to_perform_action('some_controller#when_role_matches_exact')
269
+ guard.should authorize(account_with_role(:worker)).to_perform_action('some_controller#when_matches_exact_by_implication')
270
+ guard.should_not authorize(account_with_role(:admin)).to_perform_action('some_controller#when_matches_exact_by_implication')
246
271
  end
247
272
  end
248
273
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: action-guard
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 19
5
5
  prerelease:
6
6
  segments:
7
- - 0
7
+ - 1
8
8
  - 1
9
9
  - 0
10
- version: 0.1.0
10
+ version: 1.1.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Rob Westgeest
@@ -15,10 +15,12 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-03-01 00:00:00 Z
18
+ date: 2012-06-06 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
- requirement: &id001 !ruby/object:Gem::Requirement
21
+ prerelease: false
22
+ name: rspec
23
+ version_requirements: &id001 !ruby/object:Gem::Requirement
22
24
  none: false
23
25
  requirements:
24
26
  - - ~>
@@ -29,12 +31,12 @@ dependencies:
29
31
  - 5
30
32
  - 0
31
33
  version: 2.5.0
32
- version_requirements: *id001
33
- name: rspec
34
- prerelease: false
34
+ requirement: *id001
35
35
  type: :development
36
36
  - !ruby/object:Gem::Dependency
37
- requirement: &id002 !ruby/object:Gem::Requirement
37
+ prerelease: false
38
+ name: bundler
39
+ version_requirements: &id002 !ruby/object:Gem::Requirement
38
40
  none: false
39
41
  requirements:
40
42
  - - ~>
@@ -45,12 +47,12 @@ dependencies:
45
47
  - 0
46
48
  - 0
47
49
  version: 1.0.0
48
- version_requirements: *id002
49
- name: bundler
50
- prerelease: false
50
+ requirement: *id002
51
51
  type: :development
52
52
  - !ruby/object:Gem::Dependency
53
- requirement: &id003 !ruby/object:Gem::Requirement
53
+ prerelease: false
54
+ name: jeweler
55
+ version_requirements: &id003 !ruby/object:Gem::Requirement
54
56
  none: false
55
57
  requirements:
56
58
  - - ~>
@@ -61,12 +63,12 @@ dependencies:
61
63
  - 5
62
64
  - 2
63
65
  version: 1.5.2
64
- version_requirements: *id003
65
- name: jeweler
66
- prerelease: false
66
+ requirement: *id003
67
67
  type: :development
68
68
  - !ruby/object:Gem::Dependency
69
- requirement: &id004 !ruby/object:Gem::Requirement
69
+ prerelease: false
70
+ name: rcov
71
+ version_requirements: &id004 !ruby/object:Gem::Requirement
70
72
  none: false
71
73
  requirements:
72
74
  - - ">="
@@ -75,12 +77,12 @@ dependencies:
75
77
  segments:
76
78
  - 0
77
79
  version: "0"
78
- version_requirements: *id004
79
- name: rcov
80
- prerelease: false
80
+ requirement: *id004
81
81
  type: :development
82
82
  - !ruby/object:Gem::Dependency
83
- requirement: &id005 !ruby/object:Gem::Requirement
83
+ prerelease: false
84
+ name: ZenTest
85
+ version_requirements: &id005 !ruby/object:Gem::Requirement
84
86
  none: false
85
87
  requirements:
86
88
  - - ">="
@@ -91,12 +93,12 @@ dependencies:
91
93
  - 2
92
94
  - 0
93
95
  version: 4.2.0
94
- version_requirements: *id005
95
- name: ZenTest
96
- prerelease: false
96
+ requirement: *id005
97
97
  type: :development
98
98
  - !ruby/object:Gem::Dependency
99
- requirement: &id006 !ruby/object:Gem::Requirement
99
+ prerelease: false
100
+ name: rspec
101
+ version_requirements: &id006 !ruby/object:Gem::Requirement
100
102
  none: false
101
103
  requirements:
102
104
  - - ">"
@@ -107,9 +109,7 @@ dependencies:
107
109
  - 5
108
110
  - 0
109
111
  version: 2.5.0
110
- version_requirements: *id006
111
- name: rspec
112
- prerelease: false
112
+ requirement: *id006
113
113
  type: :development
114
114
  description: authorisation module of actions based on url-paths for usage in Rails and possibly other ruby based web frameworks
115
115
  email: rob.westgeest@qwan.it
@@ -170,9 +170,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
170
170
  requirements: []
171
171
 
172
172
  rubyforge_project:
173
- rubygems_version: 1.8.10
173
+ rubygems_version: 1.8.24
174
174
  signing_key:
175
175
  specification_version: 3
176
- summary: Action guard-0.1.0
176
+ summary: Action guard-1.1.0
177
177
  test_files: []
178
178