platanus 0.0.26 → 0.0.27
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/platanus/canned2.rb +15 -8
- data/lib/platanus/version.rb +1 -1
- data/spec/canned2_spec.rb +9 -5
- metadata +1 -1
data/lib/platanus/canned2.rb
CHANGED
@@ -203,11 +203,11 @@ module Platanus
|
|
203
203
|
# @param [Symbol] _what parameter name.
|
204
204
|
# @param [Symbol] :using matcher (:equals|:equals_int|:higher_than|:lower_than),
|
205
205
|
# uses profile default matcher if not provided.
|
206
|
-
# @param [Symbol|String] :
|
206
|
+
# @param [Symbol|String] :key key or expression used to retrieve
|
207
207
|
# the matching value for current resource, if not given then _what is used.
|
208
208
|
# @param [Mixed] :value if given, this value is matched against parameter instead of resource's.
|
209
209
|
#
|
210
|
-
def
|
210
|
+
def same(_what, _options={})
|
211
211
|
matcher = _options.fetch(:using, @def_matcher)
|
212
212
|
|
213
213
|
param = @ctx.params[_what]
|
@@ -216,7 +216,7 @@ module Platanus
|
|
216
216
|
if _options.has_key? :value
|
217
217
|
user_value = _options[:value]
|
218
218
|
else
|
219
|
-
user_value = self.class.load_value_for(@res, _options.fetch(:
|
219
|
+
user_value = self.class.load_value_for(@res, _options.fetch(:key, _what))
|
220
220
|
return false if user_value.nil?
|
221
221
|
return true if user_value == :wildcard
|
222
222
|
end
|
@@ -231,7 +231,6 @@ module Platanus
|
|
231
231
|
false
|
232
232
|
end
|
233
233
|
end
|
234
|
-
alias :match :matches
|
235
234
|
|
236
235
|
## Test whether the current resource passes a given test.
|
237
236
|
#
|
@@ -240,16 +239,24 @@ module Platanus
|
|
240
239
|
# @param [Symbol] _test test identifier.
|
241
240
|
# @param [Symbol|String] :on optional key or expression used to retrieve
|
242
241
|
# from the resource the value to be passed to the test instead of the resource.
|
242
|
+
# @param [Block] _block block to be executed (if test identifier is not given)
|
243
243
|
#
|
244
|
-
def
|
245
|
-
|
246
|
-
|
244
|
+
def passes(_test=nil, _options={}, &_block)
|
245
|
+
|
246
|
+
if !_test.nil?
|
247
|
+
test = @tests[_test]
|
248
|
+
raise SetupError.new "Invalid test identifier '#{_test}'" if test.nil?
|
249
|
+
elsif !_block.nil?
|
250
|
+
test = _block
|
251
|
+
raise SetupError.new "Invalid block arity" if _block.arity > 1
|
252
|
+
else raise SetupError.new "Must provide a test name or a block" end
|
253
|
+
|
247
254
|
if test.arity == 1
|
248
255
|
user_value = self.class.load_value_for(@res, _options[:on])
|
249
256
|
@ctx.instance_exec(user_value, &test)
|
250
257
|
else @ctx.instance_eval &test end
|
251
258
|
end
|
252
|
-
alias :checks :
|
259
|
+
alias :checks :passes
|
253
260
|
|
254
261
|
## Tests whether a given expression evaluated in the resource context returns true.
|
255
262
|
#
|
data/lib/platanus/version.rb
CHANGED
data/spec/canned2_spec.rb
CHANGED
@@ -36,14 +36,15 @@ describe Platanus::Canned2 do
|
|
36
36
|
|
37
37
|
# Simple allows
|
38
38
|
allow 'rute1#action1'
|
39
|
-
allow 'rute1#action2', upon(:current_user) {
|
40
|
-
allow 'rute1#action3', upon {
|
41
|
-
allow 'rute1#action4', upon(:current_user) {
|
39
|
+
allow 'rute1#action2', upon(:current_user) { same(:char1) }
|
40
|
+
allow 'rute1#action3', upon { same(:char1, key: "current_user.char1") }
|
41
|
+
allow 'rute1#action4', upon(:current_user) { same(:param2, key: "char2") and checks(:test1) }
|
42
|
+
allow 'rute1#action5', upon(:current_user) { passes { current_user.char2 == params[:param2] } }
|
42
43
|
|
43
44
|
# Complex routes
|
44
45
|
allow 'rute1#action5' do
|
45
|
-
upon(:current_user) {
|
46
|
-
upon(:current_user) {
|
46
|
+
upon(:current_user) { same(:char1) }
|
47
|
+
upon(:current_user) { same(:param2, value: 55) or checks(:test1) }
|
47
48
|
end
|
48
49
|
end
|
49
50
|
end
|
@@ -69,6 +70,9 @@ describe Platanus::Canned2 do
|
|
69
70
|
it "does not authorize on rute with context, match and test with bad credentials" do
|
70
71
|
Roles.can?(bad_ctx, :user, 'rute1#action4').should be_false
|
71
72
|
end
|
73
|
+
it "does authorize on rute with context and inline test" do
|
74
|
+
Roles.can?(good_ctx, :user, 'rute1#action5').should be_true
|
75
|
+
end
|
72
76
|
end
|
73
77
|
|
74
78
|
context 'when using multiple context rules' do
|