iron_hide 0.3.1 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0a326eeca3af2614b0635b9528484b4d5100bd24
4
- data.tar.gz: bd4d3a1b41405a646b1ab4b32d02d4a1de2a0b12
3
+ metadata.gz: f96504f984094bdbc0cd59532575d48ea63690cd
4
+ data.tar.gz: f61e250b8bc7cfed8bc65e6f80891f9c28ddd0e5
5
5
  SHA512:
6
- metadata.gz: 18b5b3bec2a8ba8dbe751466559cb74b693546fa1eb7b85d9877c5378e966e2cf73be8f795d76f9548d70c6fd29de69d93c77cfb58280ab9a68e68c813420753
7
- data.tar.gz: d1275f59f86dc7d2ca4e0f6626908d8617c43089c30eb30b0bba987048adbcfaadd69c50fbfffde9438edfb314f318e7d48f1b7b3bc6ebb5c139159e21a2d9d9
6
+ metadata.gz: d8c0a108b078135cf5c069f456b250faac2d63d8b997e6988df46aa243512a786c6fae5671c8d41c9cd375d206da2afb52852394c537a4332dd08df98269d11a
7
+ data.tar.gz: 5e20a1309529d85d4b7615b8e5ed415ad10fce0b7176545890b3f014c57fe7b5b0e9a081e9a34d970a44cf5c4770968e18be74e168dce3c66e7ae993b42d40c4
@@ -20,7 +20,7 @@ module IronHide
20
20
  # @return [EqualCondition, NotEqualCondition]
21
21
  # @raise [IronHide::InvalidConditional] for too many keys
22
22
  #
23
- def self.new(params)
23
+ def self.new(params, cache = NullCache.new)
24
24
  if params.length > 1
25
25
  raise InvalidConditional, "Expected #{params} to have one key"
26
26
  end
@@ -30,7 +30,7 @@ module IronHide
30
30
  # See: http://ruby-doc.org/core-1.9.3/Class.html#method-i-allocate
31
31
  klass = VALID_TYPES.fetch(type){ raise InvalidConditional, "#{type} is not valid"}
32
32
  cond = IronHide.const_get(klass).allocate
33
- cond.send(:initialize, (conditionals))
33
+ cond.send(:initialize, conditionals, cache)
34
34
  cond
35
35
  end
36
36
 
@@ -41,11 +41,14 @@ module IronHide
41
41
  # 'user::user_role_ids' => ['8']
42
42
  # }
43
43
  #
44
- def initialize(conditionals)
44
+ # @param [IronHide::SimpleCache, IronHide::NullCache] cache
45
+ #
46
+ def initialize(conditionals, cache)
45
47
  @conditionals = conditionals
48
+ @cache = cache
46
49
  end
47
50
 
48
- attr_reader :conditionals
51
+ attr_reader :conditionals, :cache
49
52
 
50
53
  # @param user [Object]
51
54
  # @param resource [Object]
@@ -93,18 +96,20 @@ module IronHide
93
96
  def evaluate(expression, user, resource)
94
97
  Array(expression).flat_map do |el|
95
98
  if expression?(el)
96
- type, *ary = el.split('::')
97
- if type == 'user'
98
- Array(ary.inject(user) do |rval, attr|
99
- rval.freeze.public_send(attr)
100
- end)
101
- elsif type == 'resource'
102
- Array(ary.inject(resource) do |rval, attr|
103
- rval.freeze.public_send(attr)
104
- end)
105
- else
106
- raise "Expected #{type} to be 'resource' or 'user'"
107
- end
99
+ cache.fetch(el) {
100
+ type, *ary = el.split('::')
101
+ if type == 'user'
102
+ Array(ary.inject(user) do |rval, attr|
103
+ rval.freeze.public_send(attr)
104
+ end)
105
+ elsif type == 'resource'
106
+ Array(ary.inject(resource) do |rval, attr|
107
+ rval.freeze.public_send(attr)
108
+ end)
109
+ else
110
+ raise "Expected #{type} to be 'resource' or 'user'"
111
+ end
112
+ }
108
113
  else
109
114
  el
110
115
  end
@@ -1,11 +1,16 @@
1
1
  module IronHide
2
2
  class Configuration
3
3
 
4
- attr_accessor :adapter, :namespace, :json
4
+ attr_accessor :adapter, :namespace, :json, :memoize
5
5
 
6
6
  def initialize
7
7
  @adapter = :file
8
8
  @namespace = 'com::IronHide'
9
+ @memoize = true
10
+ end
11
+
12
+ def memoizer
13
+ memoize ? SimpleCache : NullCache
9
14
  end
10
15
 
11
16
  # Extend configuration variables
@@ -0,0 +1,26 @@
1
+ module IronHide
2
+
3
+ # The SimpleCache does not expire cache entries
4
+ # It is used only to memoize method calls during a single authorization
5
+ # decision.
6
+ #
7
+ class SimpleCache
8
+ attr_accessor :cache
9
+
10
+ def initialize
11
+ @cache = {}
12
+ end
13
+
14
+ def fetch(expression)
15
+ cache.fetch(expression) {
16
+ cache[expression] = yield
17
+ }
18
+ end
19
+ end
20
+
21
+ class NullCache
22
+ def fetch(_)
23
+ yield
24
+ end
25
+ end
26
+ end
@@ -1,16 +1,18 @@
1
+ require 'iron_hide/memoize'
2
+
1
3
  module IronHide
2
4
  class Rule
3
5
  ALLOW = 'allow'.freeze
4
6
  DENY = 'deny'.freeze
5
7
 
6
- attr_reader :description, :effect, :conditions, :user, :resource
8
+ attr_reader :description, :effect, :conditions, :user, :resource, :cache
7
9
 
8
- def initialize(user, resource, params = {})
10
+ def initialize(user, resource, params = {}, cache = NullCache.new)
9
11
  @user = user
10
12
  @resource = resource
11
13
  @description = params['description']
12
14
  @effect = params.fetch('effect', DENY) # Default DENY
13
- @conditions = Array(params['conditions']).map { |c| Condition.new(c) }
15
+ @conditions = Array(params['conditions']).map { |c| Condition.new(c, cache) }
14
16
  end
15
17
 
16
18
  # Returns all applicable rules matching on resource and action
@@ -20,9 +22,10 @@ module IronHide
20
22
  # @param resource [Object]
21
23
  # @return [Array<IronHide::Rule>]
22
24
  def self.find(user, action, resource)
25
+ cache = IronHide.configuration.memoizer.new
23
26
  ns_resource = "#{IronHide.configuration.namespace}::#{resource.class.name}"
24
27
  storage.where(resource: ns_resource, action: action).map do |json|
25
- new(user, resource, json)
28
+ new(user, resource, json, cache)
26
29
  end
27
30
  end
28
31
 
@@ -1,6 +1,6 @@
1
1
  module IronHide
2
2
  MAJOR = "0"
3
- MINOR = "3"
3
+ MINOR = "4"
4
4
  BUILD = "1"
5
5
  VERSION = [MAJOR, MINOR, BUILD].join(".")
6
6
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: iron_hide
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alan Cohen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-04-14 00:00:00.000000000 Z
11
+ date: 2014-04-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: multi_json
@@ -119,6 +119,7 @@ files:
119
119
  - lib/iron_hide/condition.rb
120
120
  - lib/iron_hide/configuration.rb
121
121
  - lib/iron_hide/errors.rb
122
+ - lib/iron_hide/memoize.rb
122
123
  - lib/iron_hide/rule.rb
123
124
  - lib/iron_hide/storage.rb
124
125
  - lib/iron_hide/storage/file_adapter.rb