iron_hide 0.2.1 → 0.3.1
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.
- checksums.yaml +4 -4
- data/lib/iron_hide.rb +39 -73
- data/lib/iron_hide/configuration.rb +30 -0
- data/lib/iron_hide/rule.rb +1 -1
- data/lib/iron_hide/storage.rb +1 -11
- data/lib/iron_hide/storage/file_adapter.rb +3 -5
- data/lib/iron_hide/version.rb +1 -1
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0a326eeca3af2614b0635b9528484b4d5100bd24
|
4
|
+
data.tar.gz: bd4d3a1b41405a646b1ab4b32d02d4a1de2a0b12
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 18b5b3bec2a8ba8dbe751466559cb74b693546fa1eb7b85d9877c5378e966e2cf73be8f795d76f9548d70c6fd29de69d93c77cfb58280ab9a68e68c813420753
|
7
|
+
data.tar.gz: d1275f59f86dc7d2ca4e0f6626908d8617c43089c30eb30b0bba987048adbcfaadd69c50fbfffde9438edfb314f318e7d48f1b7b3bc6ebb5c139159e21a2d9d9
|
data/lib/iron_hide.rb
CHANGED
@@ -1,86 +1,51 @@
|
|
1
1
|
module IronHide
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
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
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
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
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
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
|
-
|
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
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
31
|
+
# @yield [IronHide::Configuration]
|
32
|
+
def config
|
33
|
+
yield configuration
|
34
|
+
end
|
61
35
|
|
62
|
-
|
63
|
-
|
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
|
-
|
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
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
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
|
data/lib/iron_hide/rule.rb
CHANGED
@@ -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
|
data/lib/iron_hide/storage.rb
CHANGED
@@ -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
|
4
|
+
class FileAdapter
|
5
5
|
attr_reader :rules
|
6
6
|
|
7
7
|
def initialize
|
8
|
-
json = IronHide.json.each_with_object([]) do |
|
9
|
-
|
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
|
data/lib/iron_hide/version.rb
CHANGED
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.
|
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-
|
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.
|
146
|
+
rubygems_version: 2.2.1
|
146
147
|
signing_key:
|
147
148
|
specification_version: 4
|
148
149
|
summary: Describe your authorization rules with JSON
|