iron_hide 0.2.1 → 0.3.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: 43f6efb8400709edf09c958d29466af574d7bbeb
4
- data.tar.gz: 122df1373619876ef2cd2c5e1d7c109839b59373
3
+ metadata.gz: 0a326eeca3af2614b0635b9528484b4d5100bd24
4
+ data.tar.gz: bd4d3a1b41405a646b1ab4b32d02d4a1de2a0b12
5
5
  SHA512:
6
- metadata.gz: 50020dee2524090274d7191e43bfb24a4271e8da4dfa6ddb4a73df560badfc4fc42f1090536171d9221ff352d0d72694cf3fda2a29b5b60249b0c68badc23574
7
- data.tar.gz: 03112c2cee64e74ed11962aa6fcc2308b13686654726c617978df4700478c7bd15dc2cd67a907e2504afb11531be4dbae036582608880dac31fea47829c8ff26
6
+ metadata.gz: 18b5b3bec2a8ba8dbe751466559cb74b693546fa1eb7b85d9877c5378e966e2cf73be8f795d76f9548d70c6fd29de69d93c77cfb58280ab9a68e68c813420753
7
+ data.tar.gz: d1275f59f86dc7d2ca4e0f6626908d8617c43089c30eb30b0bba987048adbcfaadd69c50fbfffde9438edfb314f318e7d48f1b7b3bc6ebb5c139159e21a2d9d9
data/lib/iron_hide.rb CHANGED
@@ -1,86 +1,51 @@
1
1
  module IronHide
2
- # @raise [IronHide::AuthorizationError] if authorization fails
3
- # @return [true] if authorization succeeds
4
- #
5
- def self.authorize!(user, action, resource)
6
- unless can?(user, action, resource)
7
- raise AuthorizationError
2
+ class << self
3
+
4
+ # @raise [IronHide::AuthorizationError] if authorization fails
5
+ # @return [true] if authorization succeeds
6
+ #
7
+ def authorize!(user, action, resource)
8
+ unless can?(user, action, resource)
9
+ raise AuthorizationError
10
+ end
11
+ true
8
12
  end
9
- true
10
- end
11
-
12
- # @return [Boolean]
13
- # @param user [Object]
14
- # @param action [Symbol, String]
15
- # @param resource [Object]
16
- # @see IronHide::Rule::allow?
17
- #
18
- def self.can?(user, action, resource)
19
- Rule.allow?(user, action.to_s, resource)
20
- end
21
-
22
- # Specify where to load rules from. This is specified in a config file
23
- # @param type [:file] Specify the adapter type. Only json is supported
24
- # for now
25
- def self.adapter=(type)
26
- @adapter_type = type
27
- end
28
-
29
- def self.adapter
30
- @adapter_type
31
- end
32
13
 
33
- # Set the top-level namespace for the application's Rules
34
- #
35
- # @param val [String]
36
- # @example
37
- # 'com::myCompany::myProject'
38
- def self.namespace=(val)
39
- @namespace = val
40
- end
14
+ # @return [Boolean]
15
+ # @param user [Object]
16
+ # @param action [Symbol, String]
17
+ # @param resource [Object]
18
+ # @see IronHide::Rule::allow?
19
+ #
20
+ def can?(user, action, resource)
21
+ Rule.allow?(user, action.to_s, resource)
22
+ end
41
23
 
42
- # Default namespace is com::IronHide
43
- #
44
- # @return [String]
45
- def self.namespace
46
- @namespace || 'com::IronHide'
47
- end
24
+ # @return [IronHide::Storage]
25
+ def storage
26
+ @storage ||= IronHide::Storage.new(configuration.adapter)
27
+ end
48
28
 
49
- # Specify the file path for the JSON flat-file for rules
50
- # Only applicable if using the JSON adapter
51
- # @param files [String, Array<String>]
52
- #
53
- def self.json=(*files)
54
- @json_files = files
55
- end
29
+ attr_reader :configuration
56
30
 
57
- # @return [Array<String>]
58
- def self.json
59
- @json_files
60
- end
31
+ # @yield [IronHide::Configuration]
32
+ def config
33
+ yield configuration
34
+ end
61
35
 
62
- # @return [IronHide::Storage]
63
- def self.storage
64
- @storage ||= begin
65
- if @adapter_type.nil?
66
- raise IronHideError, "Storage adapter not defined"
67
- end
68
- IronHide::Storage.new(@adapter_type)
36
+ def configuration
37
+ @configuration ||= IronHide::Configuration.new
69
38
  end
70
- end
71
39
 
72
- # Allow the module to be configurable from a config file
73
- # See: {file:README.md}
74
- # @yield [IronHide]
75
- def self.config
76
- yield self
77
- end
40
+ alias_method :configure, :config
78
41
 
79
- # Resets internal state
80
- #
81
- # @return [void]
82
- def self.reset
83
- instance_variables.each { |i| instance_variable_set(i,nil) }
42
+ # Resets storage
43
+ # Useful primarily for testing
44
+ #
45
+ # @return [void]
46
+ def reset
47
+ @storage = nil
48
+ end
84
49
  end
85
50
  end
86
51
 
@@ -89,3 +54,4 @@ require 'iron_hide/errors'
89
54
  require 'iron_hide/rule'
90
55
  require 'iron_hide/condition'
91
56
  require 'iron_hide/storage'
57
+ require 'iron_hide/configuration'
@@ -0,0 +1,30 @@
1
+ module IronHide
2
+ class Configuration
3
+
4
+ attr_accessor :adapter, :namespace, :json
5
+
6
+ def initialize
7
+ @adapter = :file
8
+ @namespace = 'com::IronHide'
9
+ end
10
+
11
+ # Extend configuration variables
12
+ #
13
+ # @param config_hash [Hash]
14
+ #
15
+ # @example
16
+ # IronHide.configuration.add_configuration(couchdb_server: 'http://127.0.0.1:5984')
17
+ # IronHide.configuration.couchdb_server)
18
+ # #=> 'http://127.0.0.1:5984'
19
+ #
20
+ # IronHide.configuration.couchdb_server = 'other'
21
+ # #=> 'other'
22
+ #
23
+ def add_configuration(config_hash)
24
+ config_hash.each do |key, val|
25
+ instance_eval { instance_variable_set("@#{key}",val) }
26
+ self.class.instance_eval { attr_accessor key }
27
+ end
28
+ end
29
+ end
30
+ end
@@ -20,7 +20,7 @@ module IronHide
20
20
  # @param resource [Object]
21
21
  # @return [Array<IronHide::Rule>]
22
22
  def self.find(user, action, resource)
23
- ns_resource = "#{IronHide.namespace}::#{resource.class.name}"
23
+ ns_resource = "#{IronHide.configuration.namespace}::#{resource.class.name}"
24
24
  storage.where(resource: ns_resource, action: action).map do |json|
25
25
  new(user, resource, json)
26
26
  end
@@ -1,5 +1,4 @@
1
1
  # IronHide::Storage provides a common interface regardless of storage type
2
- # by implementing the Adapter pattern to decouple _how_ JSON
3
2
  #
4
3
  require 'multi_json'
5
4
 
@@ -22,16 +21,7 @@ module IronHide
22
21
  adapter.where(opts)
23
22
  end
24
23
  end
25
-
26
- # @abstract Subclass and override {#where} to implement an Adapter class
27
- class AbstractAdapter
28
-
29
- # @option opts [String] :resource *required*
30
- # @option opts [String] :action *required*
31
- def where(opts = {})
32
- raise NotImplementedError
33
- end
34
- end
35
24
  end
36
25
 
26
+
37
27
  require 'iron_hide/storage/file_adapter'
@@ -1,14 +1,12 @@
1
1
  module IronHide
2
2
  class Storage
3
3
  # @api private
4
- class FileAdapter < AbstractAdapter
4
+ class FileAdapter
5
5
  attr_reader :rules
6
6
 
7
7
  def initialize
8
- json = IronHide.json.each_with_object([]) do |files, ary|
9
- Array(files).map do |file|
10
- ary.concat(MultiJson.load(File.open(file).read, minify: true))
11
- end
8
+ json = Array(IronHide.configuration.json).each_with_object([]) do |file, ary|
9
+ ary.concat(MultiJson.load(File.open(file).read, minify: true))
12
10
  end
13
11
  @rules = unfold(json)
14
12
  rescue MultiJson::ParseError => e
@@ -1,6 +1,6 @@
1
1
  module IronHide
2
2
  MAJOR = "0"
3
- MINOR = "2"
3
+ MINOR = "3"
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.2.1
4
+ version: 0.3.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-05 00:00:00.000000000 Z
11
+ date: 2014-04-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: multi_json
@@ -117,6 +117,7 @@ extra_rdoc_files: []
117
117
  files:
118
118
  - lib/iron_hide.rb
119
119
  - lib/iron_hide/condition.rb
120
+ - lib/iron_hide/configuration.rb
120
121
  - lib/iron_hide/errors.rb
121
122
  - lib/iron_hide/rule.rb
122
123
  - lib/iron_hide/storage.rb
@@ -142,7 +143,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
142
143
  version: '0'
143
144
  requirements: []
144
145
  rubyforge_project:
145
- rubygems_version: 2.2.2
146
+ rubygems_version: 2.2.1
146
147
  signing_key:
147
148
  specification_version: 4
148
149
  summary: Describe your authorization rules with JSON