access_schema 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,7 @@
1
1
  module AccessSchema
2
2
  class NamespaceBuilder < BasicBuilder
3
3
 
4
- def privilege(name, roles, &block)
4
+ def privilege(name, roles = [], &block)
5
5
  element = Element.new(name.to_sym, roles.map(&:to_sym))
6
6
  if block_given?
7
7
  builder = ElementBuilder.new(element)
@@ -13,9 +13,9 @@ module AccessSchema
13
13
  @expectations << expectation
14
14
  end
15
15
 
16
- def allow?(role)
17
- @roles.include?(role) || begin
18
- checklist = @expectations.select { |exp| exp.for?(role) }
16
+ def allow?(roles)
17
+ (@roles & roles).size > 0 || begin
18
+ checklist = @expectations.select { |exp| exp.for?(roles) }
19
19
  checklist.length > 0 && checklist.all? { |exp| yield(exp) }
20
20
  end
21
21
  end
@@ -10,8 +10,8 @@ module AccessSchema
10
10
  @options = options
11
11
  end
12
12
 
13
- def for?(role)
14
- @roles.include?(role)
13
+ def for?(roles)
14
+ (@roles & roles).size > 0
15
15
  end
16
16
  end
17
17
  end
@@ -15,25 +15,35 @@ module AccessSchema
15
15
  end
16
16
 
17
17
  def allow?(*args)
18
+ @schema.allow?(*normalize_args(args))
19
+ end
20
+
21
+ def require!(*args)
22
+ @schema.require!(*normalize_args(args))
23
+ end
24
+
25
+ def with_options(options)
26
+ Proxy.new(self, options)
27
+ end
28
+
29
+ private
30
+
31
+ def normalize_args(args)
18
32
  namespace = args[0]
19
33
  feature = args[1]
20
34
 
21
- role, options = case args[2]
22
- when Symbol, String
23
- [args[2], args[3]]
35
+ roles, options = case args[2]
36
+ when Hash, nil
37
+ [@options[:role] || @options[:plan], args[2] || {}]
24
38
  else
25
- [@options[:role] || @options[:plan], args[2]]
39
+ [args[2], args[3] || {}]
26
40
  end
27
41
 
28
- @schema.allow?(namespace, feature, role, options)
29
- end
42
+ options_to_pass = @options.dup
43
+ options_to_pass.delete :plan
44
+ options_to_pass.delete :role
30
45
 
31
- def require!(*args)
32
- @schema.require!(*args)
33
- end
34
-
35
- def with_options(options)
36
- Proxy.new(@schema, options)
46
+ [namespace, feature, roles, options_to_pass.merge(options)]
37
47
  end
38
48
 
39
49
  end
@@ -31,21 +31,34 @@ module AccessSchema
31
31
  end
32
32
 
33
33
  def require!(*args)
34
+ check!(*normalize_args(args))
35
+ end
36
+
37
+ private
38
+
39
+ def normalize_args(args)
40
+ #Rails.logger.debug("schema normalize args: #{args.inspect}")
41
+
34
42
  options = args.last.is_a?(Hash) ? args.pop : {}
43
+ roles = args[2]
44
+ roles = roles.respond_to?(:map) ? roles.map(&:to_sym) : [roles.to_sym]
45
+ privilege = args[1].to_sym
46
+
35
47
  case args[0]
36
48
  when String, Symbol
37
- check!(args[0].to_sym, args[1].to_sym, args[2].to_sym, options)
49
+ namespace = args[0].to_sym
50
+ [namespace, privilege, roles, options]
38
51
  else
39
- check!(args[0].class.name.to_sym, args[1].to_sym, args[2].to_sym, options.merge(:subject => args[0]) )
52
+ namespace = args[0].class.name.to_sym
53
+ [namespace, privilege, roles, options.merge(:subject => args[0])]
40
54
  end
41
- end
42
55
 
43
- private
44
-
45
- def check!(namespace_name, element_name, role, options)
56
+ end
46
57
 
58
+ def check!(namespace_name, element_name, roles, options)
59
+ #Rails.logger.debug [namespace_name, element_name, roles, options].inspect
47
60
  allowed = for_element(namespace_name, element_name) do |element|
48
- element.allow?(role) do |expectation|
61
+ element.allow?(roles) do |expectation|
49
62
  check_assert(expectation, options)
50
63
  end
51
64
  end
@@ -1,3 +1,3 @@
1
1
  module AccessSchema
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -13,7 +13,7 @@ describe AccessSchema::Proxy do
13
13
  describe "#with_options" do
14
14
 
15
15
  before do
16
- @schema = @proxy.with_options(:plan => :flower)
16
+ @schema = @proxy.with_options(:plan => [:flower], :user_id => 1)
17
17
  end
18
18
 
19
19
  it "allows to not specify plan for schema calls" do
@@ -25,6 +25,11 @@ describe AccessSchema::Proxy do
25
25
  @schema.allow?("Review", :mark_featured, :none).should be_false
26
26
  end
27
27
 
28
+ it "passes options to schema" do
29
+ @proxy.should_receive(:allow?).with("Review", :mark_featured, [:flower], {:user_id => 1})
30
+ @schema.allow?("Review", :mark_featured)
31
+ end
32
+
28
33
  end
29
34
 
30
35
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: access_schema
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-02-29 00:00:00.000000000 Z
12
+ date: 2012-03-04 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: AccessSchema is a tool for ACL or tariff plans schema definition and
15
15
  checks
@@ -44,11 +44,11 @@ files:
44
44
  - lib/access_schema/proxy.rb
45
45
  - lib/access_schema/schema.rb
46
46
  - lib/access_schema/version.rb
47
+ - spec/access_schema/config_builder_spec.rb
48
+ - spec/access_schema/proxy_spec.rb
49
+ - spec/access_schema/schema_builder_spec.rb
50
+ - spec/access_schema/schema_spec.rb
47
51
  - spec/access_schema_spec.rb
48
- - spec/assess_schema/config_builder_spec.rb
49
- - spec/assess_schema/proxy_spec.rb
50
- - spec/assess_schema/schema_builder_spec.rb
51
- - spec/assess_schema/schema_spec.rb
52
52
  - spec/schema_example.rb
53
53
  - spec/spec_helper.rb
54
54
  homepage: ''
@@ -76,10 +76,10 @@ signing_key:
76
76
  specification_version: 3
77
77
  summary: AccessSchema is an ACL tool
78
78
  test_files:
79
+ - spec/access_schema/config_builder_spec.rb
80
+ - spec/access_schema/proxy_spec.rb
81
+ - spec/access_schema/schema_builder_spec.rb
82
+ - spec/access_schema/schema_spec.rb
79
83
  - spec/access_schema_spec.rb
80
- - spec/assess_schema/config_builder_spec.rb
81
- - spec/assess_schema/proxy_spec.rb
82
- - spec/assess_schema/schema_builder_spec.rb
83
- - spec/assess_schema/schema_spec.rb
84
84
  - spec/schema_example.rb
85
85
  - spec/spec_helper.rb