six 0.0.3 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/six.rb +120 -122
- metadata +2 -2
data/lib/six.rb
CHANGED
@@ -11,144 +11,142 @@ class Six
|
|
11
11
|
end
|
12
12
|
end
|
13
13
|
|
14
|
-
|
15
|
-
|
16
|
-
attr_accessor :current_rule_pack
|
14
|
+
attr_accessor :rules_packs
|
15
|
+
attr_accessor :current_rule_pack
|
17
16
|
|
18
|
-
|
19
|
-
|
20
|
-
|
17
|
+
def rules_packs
|
18
|
+
@rules_packs ||= {}
|
19
|
+
end
|
21
20
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
end
|
21
|
+
# Set current pack from stored packs by key
|
22
|
+
#
|
23
|
+
# == Parameters:
|
24
|
+
# name::
|
25
|
+
# A Symbol declaring the key name of stored pack
|
26
|
+
#
|
27
|
+
# == Returns:
|
28
|
+
# self or false
|
29
|
+
#
|
30
|
+
def use(name)
|
31
|
+
if pack_exist?(name)
|
32
|
+
@current_rule_pack = name.to_sym
|
33
|
+
self
|
36
34
|
end
|
35
|
+
end
|
37
36
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
37
|
+
# Same as use but raise exception if no pack found
|
38
|
+
def use!(name)
|
39
|
+
use(name) ? self : raise_no_such_pack
|
40
|
+
end
|
42
41
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
42
|
+
# Add pack to authorization class
|
43
|
+
#
|
44
|
+
# == Parameters:
|
45
|
+
# name::
|
46
|
+
# A Symbol declaring the key name of stored pack
|
47
|
+
# pack::
|
48
|
+
# Any kind of object responding to allowed method
|
49
|
+
#
|
50
|
+
# == Returns:
|
51
|
+
# true or false
|
52
|
+
#
|
53
|
+
def add_pack(name, pack)
|
54
|
+
rules_packs[name.to_sym] = pack if valid_rules_object?(pack)
|
55
|
+
end
|
57
56
|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
57
|
+
# Same as add_pack but raise exception if pack is invalid
|
58
|
+
def add_pack!(name, pack)
|
59
|
+
add_pack(name, pack) || raise_incorrect_pack_object
|
60
|
+
end
|
62
61
|
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
end
|
62
|
+
# Remove pack from authorization class
|
63
|
+
#
|
64
|
+
# == Parameters:
|
65
|
+
# name::
|
66
|
+
# A Symbol declaring the key name of stored pack
|
67
|
+
#
|
68
|
+
# == Returns:
|
69
|
+
# true or false
|
70
|
+
#
|
71
|
+
def remove_pack(name)
|
72
|
+
if pack_exist?(name)
|
73
|
+
@current_rule_pack = nil if rules_packs[name.to_sym] == @current_rule_pack
|
74
|
+
rules_packs.delete(name.to_sym)
|
77
75
|
end
|
76
|
+
end
|
78
77
|
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
78
|
+
# Same as remove_pack but raise exception if pack wasnt found
|
79
|
+
def remove_pack!(name)
|
80
|
+
remove_pack(name) || raise_no_such_pack
|
81
|
+
end
|
83
82
|
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
83
|
+
# Check if object for rule pack is valid
|
84
|
+
#
|
85
|
+
# == Parameters:
|
86
|
+
# pack::
|
87
|
+
# Any kind of object responding to allowed method
|
88
|
+
#
|
89
|
+
# == Returns:
|
90
|
+
# true or false
|
91
|
+
#
|
92
|
+
def valid_rules_object?(object)
|
93
|
+
object.respond_to?(:allowed) &&
|
94
|
+
object.send(:allowed, nil, nil).kind_of?(Array)
|
95
|
+
rescue
|
96
|
+
false
|
97
|
+
end
|
99
98
|
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
99
|
+
# Check if authorization class has pack with such name
|
100
|
+
#
|
101
|
+
# == Parameters:
|
102
|
+
# name::
|
103
|
+
# A Symbol declaring the key name of stored pack
|
104
|
+
#
|
105
|
+
# == Returns:
|
106
|
+
# true or false
|
107
|
+
#
|
108
|
+
def pack_exist?(name)
|
109
|
+
rules_packs.has_key?(name.to_sym)
|
110
|
+
end
|
112
111
|
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
end
|
112
|
+
# Check if authorization class allow access for object to subject
|
113
|
+
# using selected pack or all stored.
|
114
|
+
# Basically this method
|
115
|
+
# 1. send :allowed for every stored object in packs and pass object & subject
|
116
|
+
# 2. check if any of results include allowed action
|
117
|
+
#
|
118
|
+
# == Parameters:
|
119
|
+
# action::
|
120
|
+
# Action name to check for access
|
121
|
+
# object::
|
122
|
+
# object trying to access resource
|
123
|
+
# subject::
|
124
|
+
# resource
|
125
|
+
#
|
126
|
+
# == Returns:
|
127
|
+
# true or false
|
128
|
+
#
|
129
|
+
def allowed?(action, object, subject)
|
130
|
+
if current_rule_pack
|
131
|
+
rules_packs[current_rule_pack].allowed(object, subject).include?(action)
|
132
|
+
else
|
133
|
+
rules_packs.values.map { |rp| rp.allowed(object, subject) }.flatten.include?(action)
|
136
134
|
end
|
135
|
+
end
|
137
136
|
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
137
|
+
# Reset current used rule pack so auth class use
|
138
|
+
# global allowed? for new request
|
139
|
+
def reset_use
|
140
|
+
@current_rule_pack = nil
|
141
|
+
end
|
143
142
|
|
144
|
-
|
143
|
+
protected
|
145
144
|
|
146
|
-
|
147
|
-
|
148
|
-
|
145
|
+
def raise_no_such_pack
|
146
|
+
raise Six::NoPackError.new
|
147
|
+
end
|
149
148
|
|
150
|
-
|
151
|
-
|
152
|
-
end
|
149
|
+
def raise_incorrect_pack_object
|
150
|
+
raise Six::InvalidPackPassed.new
|
153
151
|
end
|
154
152
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: six
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
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: 2011-08-
|
12
|
+
date: 2011-08-20 00:00:00.000000000Z
|
13
13
|
dependencies: []
|
14
14
|
description: Very simple authorization gem
|
15
15
|
email: dmitriy.zaporozhets@gmail.com
|