access_schema 0.3.2 → 0.3.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/access_schema/exceptions.rb +10 -3
 - data/lib/access_schema/schema.rb +20 -6
 - data/lib/access_schema/version.rb +1 -1
 - data/spec/access_schema/schema_spec.rb +16 -1
 - metadata +4 -4
 
| 
         @@ -1,9 +1,16 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            module AccessSchema
         
     | 
| 
       2 
2 
     | 
    
         | 
| 
       3 
     | 
    
         
            -
              class Error < RuntimeError
         
     | 
| 
       4 
     | 
    
         
            -
              end
         
     | 
| 
      
 3 
     | 
    
         
            +
              class Error < RuntimeError; end
         
     | 
| 
       5 
4 
     | 
    
         | 
| 
       6 
     | 
    
         
            -
              class  
     | 
| 
      
 5 
     | 
    
         
            +
              class DefinitionError < Error; end
         
     | 
| 
      
 6 
     | 
    
         
            +
              class CheckError < Error; end
         
     | 
| 
      
 7 
     | 
    
         
            +
              class AccessError < CheckError; end
         
     | 
| 
       7 
8 
     | 
    
         | 
| 
       8 
9 
     | 
    
         | 
| 
      
 10 
     | 
    
         
            +
              class NotAlowedError < AccessError; end
         
     | 
| 
      
 11 
     | 
    
         
            +
             
     | 
| 
      
 12 
     | 
    
         
            +
              class NoRoleError < CheckError; end
         
     | 
| 
      
 13 
     | 
    
         
            +
              class NoResourceError < CheckError; end
         
     | 
| 
      
 14 
     | 
    
         
            +
              class NoPrivilegeError < CheckError; end
         
     | 
| 
      
 15 
     | 
    
         
            +
             
     | 
| 
       9 
16 
     | 
    
         
             
            end
         
     | 
    
        data/lib/access_schema/schema.rb
    CHANGED
    
    | 
         @@ -39,10 +39,14 @@ module AccessSchema 
     | 
|
| 
       39 
39 
     | 
    
         
             
                def normalize_args(args)
         
     | 
| 
       40 
40 
     | 
    
         | 
| 
       41 
41 
     | 
    
         
             
                  options = args.last.is_a?(Hash) ? args.pop : {}
         
     | 
| 
       42 
     | 
    
         
            -
                  roles = args[2]
         
     | 
| 
       43 
     | 
    
         
            -
                  roles = roles.respond_to?(:map) ? roles.map(&:to_sym) : [roles.to_sym]
         
     | 
| 
       44 
42 
     | 
    
         
             
                  privilege =  args[1].to_sym
         
     | 
| 
       45 
43 
     | 
    
         | 
| 
      
 44 
     | 
    
         
            +
                  roles = args[2]
         
     | 
| 
      
 45 
     | 
    
         
            +
                  roles = roles.respond_to?(:map) ? roles.map(&:to_sym) : [roles && roles.to_sym]
         
     | 
| 
      
 46 
     | 
    
         
            +
             
     | 
| 
      
 47 
     | 
    
         
            +
                  raise NoRoleError.new if (self.roles & roles).empty?
         
     | 
| 
      
 48 
     | 
    
         
            +
             
     | 
| 
      
 49 
     | 
    
         
            +
             
     | 
| 
       46 
50 
     | 
    
         
             
                  case args[0]
         
     | 
| 
       47 
51 
     | 
    
         
             
                  when String, Symbol
         
     | 
| 
       48 
52 
     | 
    
         
             
                    namespace = args[0].to_sym
         
     | 
| 
         @@ -56,13 +60,17 @@ module AccessSchema 
     | 
|
| 
       56 
60 
     | 
    
         | 
| 
       57 
61 
     | 
    
         
             
                def check!(namespace_name, element_name, roles, options)
         
     | 
| 
       58 
62 
     | 
    
         | 
| 
      
 63 
     | 
    
         
            +
                  existent_element = false
         
     | 
| 
       59 
64 
     | 
    
         | 
| 
       60 
65 
     | 
    
         
             
                  allowed = for_element(namespace_name, element_name) do |element|
         
     | 
| 
      
 66 
     | 
    
         
            +
                    existent_element = true
         
     | 
| 
       61 
67 
     | 
    
         
             
                    element.allow?(roles) do |expectation|
         
     | 
| 
       62 
68 
     | 
    
         
             
                      check_assert(expectation, options)
         
     | 
| 
       63 
69 
     | 
    
         
             
                    end
         
     | 
| 
       64 
70 
     | 
    
         
             
                  end
         
     | 
| 
       65 
71 
     | 
    
         | 
| 
      
 72 
     | 
    
         
            +
                  raise NoPrivilegeError.new(:privilege => element_name.to_sym) unless existent_element
         
     | 
| 
      
 73 
     | 
    
         
            +
             
     | 
| 
       66 
74 
     | 
    
         
             
                  format_log_payload = lambda {
         
     | 
| 
       67 
75 
     | 
    
         
             
                    [
         
     | 
| 
       68 
76 
     | 
    
         
             
                      "namespace = '#{namespace_name}'",
         
     | 
| 
         @@ -88,16 +96,22 @@ module AccessSchema 
     | 
|
| 
       88 
96 
     | 
    
         | 
| 
       89 
97 
     | 
    
         
             
                def for_element(namespace, element)
         
     | 
| 
       90 
98 
     | 
    
         
             
                  ns = namespace.to_sym
         
     | 
| 
       91 
     | 
    
         
            -
                   
     | 
| 
       92 
     | 
    
         
            -
             
     | 
| 
       93 
     | 
    
         
            -
             
     | 
| 
      
 99 
     | 
    
         
            +
                  en = element.to_sym
         
     | 
| 
      
 100 
     | 
    
         
            +
             
     | 
| 
      
 101 
     | 
    
         
            +
                  elements_for(ns).any? do |element|
         
     | 
| 
      
 102 
     | 
    
         
            +
                    if element.name == en
         
     | 
| 
       94 
103 
     | 
    
         
             
                      yield(element)
         
     | 
| 
       95 
104 
     | 
    
         
             
                    end
         
     | 
| 
       96 
105 
     | 
    
         
             
                  end
         
     | 
| 
      
 106 
     | 
    
         
            +
             
     | 
| 
       97 
107 
     | 
    
         
             
                end
         
     | 
| 
       98 
108 
     | 
    
         | 
| 
       99 
109 
     | 
    
         
             
                def elements_for(namespace)
         
     | 
| 
       100 
     | 
    
         
            -
                  @namespaces 
     | 
| 
      
 110 
     | 
    
         
            +
                  if @namespaces.has_key?(namespace)
         
     | 
| 
      
 111 
     | 
    
         
            +
                    @namespaces[namespace].elements
         
     | 
| 
      
 112 
     | 
    
         
            +
                  else
         
     | 
| 
      
 113 
     | 
    
         
            +
                    raise NoResourceError.new(:resource => namespace)
         
     | 
| 
      
 114 
     | 
    
         
            +
                  end
         
     | 
| 
       101 
115 
     | 
    
         
             
                end
         
     | 
| 
       102 
116 
     | 
    
         | 
| 
       103 
117 
     | 
    
         
             
                private
         
     | 
| 
         @@ -28,7 +28,22 @@ describe AccessSchema::Schema, "errors rising" do 
     | 
|
| 
       28 
28 
     | 
    
         | 
| 
       29 
29 
     | 
    
         
             
              describe "#allow?" do
         
     | 
| 
       30 
30 
     | 
    
         | 
| 
       31 
     | 
    
         
            -
                it "raises exception on invalid namespace"
         
     | 
| 
      
 31 
     | 
    
         
            +
                it "raises exception on invalid namespace" do
         
     | 
| 
      
 32 
     | 
    
         
            +
                  lambda {
         
     | 
| 
      
 33 
     | 
    
         
            +
                    @schema.allow? "Invalid", :mark_featured, :none
         
     | 
| 
      
 34 
     | 
    
         
            +
                  }.should raise_error(AccessSchema::NoResourceError)
         
     | 
| 
      
 35 
     | 
    
         
            +
                end
         
     | 
| 
      
 36 
     | 
    
         
            +
             
     | 
| 
      
 37 
     | 
    
         
            +
                it "raises exception on invalid role" do
         
     | 
| 
      
 38 
     | 
    
         
            +
                  lambda {
         
     | 
| 
      
 39 
     | 
    
         
            +
                    @schema.allow? "Review", :mark_featured, :invalid
         
     | 
| 
      
 40 
     | 
    
         
            +
                  }.should raise_error(AccessSchema::NoRoleError)
         
     | 
| 
      
 41 
     | 
    
         
            +
             
     | 
| 
      
 42 
     | 
    
         
            +
                  lambda {
         
     | 
| 
      
 43 
     | 
    
         
            +
                    @schema.allow? "Review", :mark_featured
         
     | 
| 
      
 44 
     | 
    
         
            +
                  }.should raise_error(AccessSchema::NoRoleError)
         
     | 
| 
      
 45 
     | 
    
         
            +
                end
         
     | 
| 
      
 46 
     | 
    
         
            +
             
     | 
| 
       32 
47 
     | 
    
         
             
                it "raises exception on invalid feature"
         
     | 
| 
       33 
48 
     | 
    
         | 
| 
       34 
49 
     | 
    
         
             
              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.3. 
     | 
| 
      
 4 
     | 
    
         
            +
              version: 0.3.3
         
     | 
| 
       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-03- 
     | 
| 
      
 12 
     | 
    
         
            +
            date: 2012-03-21 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
         
     | 
| 
         @@ -69,7 +69,7 @@ required_ruby_version: !ruby/object:Gem::Requirement 
     | 
|
| 
       69 
69 
     | 
    
         
             
                  version: '0'
         
     | 
| 
       70 
70 
     | 
    
         
             
                  segments:
         
     | 
| 
       71 
71 
     | 
    
         
             
                  - 0
         
     | 
| 
       72 
     | 
    
         
            -
                  hash: - 
     | 
| 
      
 72 
     | 
    
         
            +
                  hash: -3043700016320745078
         
     | 
| 
       73 
73 
     | 
    
         
             
            required_rubygems_version: !ruby/object:Gem::Requirement
         
     | 
| 
       74 
74 
     | 
    
         
             
              none: false
         
     | 
| 
       75 
75 
     | 
    
         
             
              requirements:
         
     | 
| 
         @@ -78,7 +78,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement 
     | 
|
| 
       78 
78 
     | 
    
         
             
                  version: '0'
         
     | 
| 
       79 
79 
     | 
    
         
             
                  segments:
         
     | 
| 
       80 
80 
     | 
    
         
             
                  - 0
         
     | 
| 
       81 
     | 
    
         
            -
                  hash: - 
     | 
| 
      
 81 
     | 
    
         
            +
                  hash: -3043700016320745078
         
     | 
| 
       82 
82 
     | 
    
         
             
            requirements: []
         
     | 
| 
       83 
83 
     | 
    
         
             
            rubyforge_project: access_schema
         
     | 
| 
       84 
84 
     | 
    
         
             
            rubygems_version: 1.8.16
         
     |