six 0.1.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +19 -0
  3. data/lib/six.rb +97 -55
  4. metadata +16 -17
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 7b040bab565e465f27226c607722a50a933afdd8063ff7adb199651357cf7e2e
4
+ data.tar.gz: 04d532eff125a67d1cbc0120c1ed8dd3d526ddc152fcd98defa4f314a6f3ba06
5
+ SHA512:
6
+ metadata.gz: 63340b25f2f0c0bd751a6de758c77a6858526431706c6d57de02906f6a31ca0da0290eb1940b7590a0cbd5225cfe012bbf1db29078a521de39f250e0f9b749a6
7
+ data.tar.gz: 5c83df574dd1e722bcdd6dc0ac4054a60af5cfe0fedcc5aa9e6777c2a2cdf7796da7f992546abd79cdabeb065f95e943a0ab98220cef0e79cbe6c1542cb13a14
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2011 Dmitriy Zaporozhets
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
data/lib/six.rb CHANGED
@@ -11,64 +11,96 @@ class Six
11
11
  end
12
12
  end
13
13
 
14
+ class InitializeArgumentError < StandardError
15
+ def message
16
+ "Six.new require hash as pack argument in format {:name_of_pack => PackRules.new}"
17
+ end
18
+ end
19
+
14
20
  attr_reader :rules_packs
15
21
  attr_reader :current_rule_pack
16
22
 
17
- def initialize()
23
+ # Initialize ability object
24
+ #
25
+ # == Parameters:
26
+ # packs::
27
+ # A Hash or rules to add with initializtion
28
+ #
29
+ # == Returns:
30
+ # self
31
+ #
32
+ def initialize(packs={})
33
+ raise InitializeArgumentError.new unless packs.kind_of?(Hash)
34
+
18
35
  @rules_packs = {}
19
36
  @current_rule_pack = nil
37
+
38
+ packs.each { |key, pack| add_pack!(key, pack) }
20
39
  end
21
40
 
22
- # Set current pack from stored packs by key
23
- #
41
+ # Set current pack from stored packs by key
42
+ #
24
43
  # == Parameters:
25
44
  # name::
26
- # A Symbol declaring the key name of stored pack
27
- #
45
+ # A Symbol declaring the key name of stored pack
46
+ #
28
47
  # == Returns:
29
- # self or false
30
- #
31
- def use(name)
48
+ # self or false
49
+ #
50
+ def use_pack(name)
32
51
  if pack_exist?(name)
33
52
  @current_rule_pack = name.to_sym
34
53
  self
35
54
  end
36
55
  end
37
56
 
38
- # Same as use but raise exception if no pack found
39
- def use!(name)
40
- use(name) ? self : raise_no_such_pack
57
+ # Same as use but raise exception if no pack found
58
+ def use_pack!(name)
59
+ use_pack(name) ? self : raise_no_such_pack
41
60
  end
42
61
 
43
- # Add pack to authorization class
44
- #
62
+ # Add pack to authorization class
63
+ #
45
64
  # == Parameters:
46
65
  # name::
47
- # A Symbol declaring the key name of stored pack
66
+ # A Symbol declaring the key name of stored pack
48
67
  # pack::
49
- # Any kind of object responding to allowed method
50
- #
68
+ # Any kind of object responding to allowed method
69
+ #
51
70
  # == Returns:
52
- # true or false
53
- #
71
+ # true or false
72
+ #
54
73
  def add_pack(name, pack)
55
74
  rules_packs[name.to_sym] = pack if valid_rules_object?(pack)
56
75
  end
57
76
 
58
- # Same as add_pack but raise exception if pack is invalid
77
+ # Same as add_pack but raise exception if pack is invalid
59
78
  def add_pack!(name, pack)
60
79
  add_pack(name, pack) || raise_incorrect_pack_object
61
80
  end
62
81
 
63
- # Remove pack from authorization class
64
- #
82
+ # Add pack to authorization class w/o key
83
+ #
84
+ # == Parameters:
85
+ # pack::
86
+ # Any kind of object responding to allowed method
87
+ #
88
+ # == Returns:
89
+ # true or raise exception
90
+ #
91
+ def <<(pack)
92
+ add_pack!(pack.object_id.to_s, pack)
93
+ end
94
+
95
+ # Remove pack from authorization class
96
+ #
65
97
  # == Parameters:
66
98
  # name::
67
- # A Symbol declaring the key name of stored pack
68
- #
99
+ # A Symbol declaring the key name of stored pack
100
+ #
69
101
  # == Returns:
70
- # true or false
71
- #
102
+ # true or false
103
+ #
72
104
  def remove_pack(name)
73
105
  if pack_exist?(name)
74
106
  @current_rule_pack = nil if rules_packs[name.to_sym] == @current_rule_pack
@@ -81,61 +113,61 @@ class Six
81
113
  remove_pack(name) || raise_no_such_pack
82
114
  end
83
115
 
84
- # Check if object for rule pack is valid
85
- #
116
+ # Check if object for rule pack is valid
117
+ #
86
118
  # == Parameters:
87
119
  # pack::
88
- # Any kind of object responding to allowed method
89
- #
120
+ # Any kind of object responding to allowed method
121
+ #
90
122
  # == Returns:
91
- # true or false
92
- #
123
+ # true or false
124
+ #
93
125
  def valid_rules_object?(object)
94
- object.respond_to?(:allowed) &&
95
- object.send(:allowed, nil, nil).kind_of?(Array)
96
- rescue
97
- false
126
+ object.respond_to?(:allowed)
98
127
  end
99
128
 
100
- # Check if authorization class has pack with such name
101
- #
129
+ # Check if authorization class has pack with such name
130
+ #
102
131
  # == Parameters:
103
132
  # name::
104
- # A Symbol declaring the key name of stored pack
105
- #
133
+ # A Symbol declaring the key name of stored pack
134
+ #
106
135
  # == Returns:
107
- # true or false
108
- #
136
+ # true or false
137
+ #
109
138
  def pack_exist?(name)
110
139
  rules_packs.has_key?(name.to_sym)
111
140
  end
112
141
 
113
142
  # Check if authorization class allow access for object to subject
114
- # using selected pack or all stored.
115
- # Basically this method
143
+ # using selected pack or all stored.
144
+ # Basically this method
116
145
  # 1. send :allowed for every stored object in packs and pass object & subject
117
146
  # 2. check if any of results include allowed action
118
- #
147
+ #
119
148
  # == Parameters:
120
149
  # action::
121
- # Action name to check for access
150
+ # Action name to check for access
122
151
  # object::
123
- # object trying to access resource
152
+ # object trying to access resource
124
153
  # subject::
125
154
  # resource
126
- #
155
+ #
127
156
  # == Returns:
128
- # true or false
129
- #
130
- def allowed?(action, object, subject)
131
- if current_rule_pack
132
- rules_packs[current_rule_pack].allowed(object, subject).include?(action)
133
- else
134
- rules_packs.values.map { |rp| rp.allowed(object, subject) }.flatten.include?(action)
157
+ # true or false
158
+ #
159
+ def allowed?(object, actions, subject)
160
+ # if multiple actions passed
161
+ # check all actions to be allowed
162
+ if actions.respond_to?(:each)
163
+ actions.all? { |action| action_included?(object, action, subject) }
164
+ else
165
+ # single action check
166
+ action_included?(object, actions, subject)
135
167
  end
136
168
  end
137
169
 
138
- # Reset current used rule pack so auth class use
170
+ # Reset current used rule pack so auth class use
139
171
  # global allowed? for new request
140
172
  def reset_use
141
173
  @current_rule_pack = nil
@@ -143,6 +175,14 @@ class Six
143
175
 
144
176
  protected
145
177
 
178
+ def action_included?(object, action, subject)
179
+ if current_rule_pack
180
+ rules_packs[current_rule_pack].allowed(object, subject).include?(action)
181
+ else
182
+ rules_packs.values.map { |rp| rp.allowed(object, subject) }.flatten.include?(action)
183
+ end
184
+ end
185
+
146
186
  def raise_no_such_pack
147
187
  raise Six::NoPackError.new
148
188
  end
@@ -153,6 +193,8 @@ class Six
153
193
 
154
194
  # shotcuts for long methods
155
195
 
196
+ alias_method :use, :use_pack
197
+ alias_method :use!, :use_pack!
156
198
  alias_method :add, :add_pack
157
199
  alias_method :add!, :add_pack!
158
200
  alias_method :remove, :remove_pack
metadata CHANGED
@@ -1,45 +1,44 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: six
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
5
- prerelease:
4
+ version: 0.2.2
6
5
  platform: ruby
7
6
  authors:
8
7
  - Dmitriy Zaporozhets
9
- autorequire:
8
+ autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2011-08-24 00:00:00.000000000Z
11
+ date: 2021-03-04 00:00:00.000000000 Z
13
12
  dependencies: []
14
- description: Very simple authorization gem
13
+ description: A simple authorization gem
15
14
  email: dmitriy.zaporozhets@gmail.com
16
15
  executables: []
17
16
  extensions: []
18
17
  extra_rdoc_files: []
19
18
  files:
19
+ - LICENSE
20
20
  - lib/six.rb
21
- homepage: https://github.com/randx/six
22
- licenses: []
23
- post_install_message:
21
+ homepage: https://gitlab.com/dzaporozhets/six
22
+ licenses:
23
+ - MIT
24
+ metadata: {}
25
+ post_install_message:
24
26
  rdoc_options: []
25
27
  require_paths:
26
28
  - lib
27
29
  required_ruby_version: !ruby/object:Gem::Requirement
28
- none: false
29
30
  requirements:
30
- - - ! '>='
31
+ - - ">="
31
32
  - !ruby/object:Gem::Version
32
- version: '0'
33
+ version: 1.9.3
33
34
  required_rubygems_version: !ruby/object:Gem::Requirement
34
- none: false
35
35
  requirements:
36
- - - ! '>='
36
+ - - ">="
37
37
  - !ruby/object:Gem::Version
38
38
  version: '0'
39
39
  requirements: []
40
- rubyforge_project:
41
- rubygems_version: 1.8.6
42
- signing_key:
43
- specification_version: 3
40
+ rubygems_version: 3.2.1
41
+ signing_key:
42
+ specification_version: 4
44
43
  summary: six
45
44
  test_files: []