Ruby-ACL 1.0.0

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.
@@ -0,0 +1,91 @@
1
+ class Ace < ACL_Object
2
+
3
+ def initialize(connector, col_path, report = false)
4
+ super(connector, col_path, report)
5
+ @doc = "doc(\"#{@col_path}acl.xml\")"
6
+ end
7
+
8
+ private
9
+ def generate_expr(id, prin_id, acc_type, priv_id, res_ob_id)
10
+ expr = <<END
11
+ <Ace id="#{id}">
12
+ <Principal idref="#{prin_id}"/>
13
+ <accessType>#{acc_type}</accessType>
14
+ <Privilege idref="#{priv_id}"/>
15
+ <ResourceObject idref="#{res_ob_id}"/>
16
+ </Ace>
17
+ END
18
+ return expr
19
+ end
20
+
21
+ #finds ace's id by principal id, acc_type, privilege id, resource object id
22
+ def find_ace(prin_id, acc_type, priv_id, res_ob_id)
23
+ query = "#{@doc}//#{self.class.name}s/descendant::*
24
+ [Principal/@idref=\"#{prin_id}\" and accessType=\"#{acc_type}\" and
25
+ Privilege/@idref=\"#{priv_id}\" and ResourceObject/@idref=\"#{res_ob_id}\"]
26
+ /string(@id)"
27
+ handle = @connector.execute_query(query)
28
+ hits = @connector.get_hits(handle)
29
+ case hits
30
+ when 1
31
+ ace_id = @connector.retrieve(handle, 0)
32
+ if(ace_id == "") #eXist returns empty result => should return no result
33
+ return nil
34
+ else
35
+ return ace_id
36
+ end
37
+
38
+ when 0
39
+ return nil
40
+ else
41
+ raise RubyACLExceptionRubyACLException.new(self.class.name, __method__,
42
+ "#{self.class.name}
43
+ Principal=\"#{prin_id}\" and accessType=\"#{acc_type}\" and
44
+ Privilege=\"#{priv_id}\" and ResourceObject=\"#{res_ob_id}\"
45
+ exists more then once. (#{hits}x)", 220), caller
46
+ end
47
+ rescue => e
48
+ raise e
49
+ end #def find_ace
50
+
51
+ protected
52
+
53
+ public
54
+ def create_new(prin_id, acc_type, priv_id, res_ob_id)
55
+ if(acc_type == "deny" || acc_type == "allow")
56
+ id = find_ace(prin_id, acc_type, priv_id, res_ob_id)
57
+ if(id == nil) #this ace doesnt exist
58
+ id = "a" + Random.rand(1000000000).to_s
59
+ while(exists?(id))
60
+ id = "a" + Random.rand(1000000000).to_s
61
+ end
62
+ expr = generate_expr(id, prin_id, acc_type, priv_id, res_ob_id)
63
+ expr_loc = "#{@doc}//#{self.class.name}s/#{self.class.name}[last()]"
64
+ #puts expr_loc
65
+ @connector.update_insert(expr, "following", expr_loc)
66
+ if(exists?(id))
67
+ #puts "New #{self.class.name} \"#{name}\" created."
68
+ return id
69
+ else
70
+ puts "#{self.class.name} \"#{id}\" was not able to create."
71
+ raise RubyACLException.new(self.class.name, __method__,
72
+ "#{self.class.name} \"#{id}\" was not able to create.", 221), caller
73
+ return nil
74
+ end
75
+ else #already exists
76
+ return id
77
+ end
78
+ else
79
+ raise RubyACLException.new(self.class.name, __method__,
80
+ "Access type \"#{acc_type}\" is not allowed. Only allowed type is \"deny\" or \"allow\".", 222), caller
81
+ return nil
82
+ end
83
+ rescue => e
84
+ raise e
85
+ end #def create_new
86
+
87
+ def rename()
88
+ raise RubyACLException.new(self.class.name, __method__,
89
+ "Rename method is not supported for ACE.", 223), caller
90
+ end
91
+ end #class Ace
@@ -0,0 +1,36 @@
1
+ class AceRule
2
+
3
+ attr_reader :prin
4
+ attr_reader :priv
5
+ attr_reader :res_obj
6
+ attr_reader :acc_type
7
+
8
+ def initialize(ace_id, ace, connector)
9
+ @ace = ace
10
+ @connector = connector
11
+ reload!(ace_id)
12
+ rescue => e
13
+ raise e
14
+ end
15
+
16
+ def reload!(ace_id)
17
+ query = "#{@ace.doc}//Ace[@id=\"#{ace_id}\"]/Principal/string(@idref)"
18
+ handle = @connector.execute_query(query)
19
+ @prin = @connector.retrieve(handle, 0)
20
+
21
+ query = "#{@ace.doc}//Ace[@id=\"#{ace_id}\"]/Privilege/string(@idref)"
22
+ handle = @connector.execute_query(query)
23
+ @priv = @connector.retrieve(handle, 0)
24
+
25
+ query = "#{@ace.doc}//Ace[@id=\"#{ace_id}\"]/ResourceObject/string(@idref)"
26
+ handle = @connector.execute_query(query)
27
+ @res_obj = @connector.retrieve(handle, 0)
28
+
29
+ query = "#{@ace.doc}//Ace[@id=\"#{ace_id}\"]/accessType/text()"
30
+ #puts query
31
+ handle = @connector.execute_query(query)
32
+ @acc_type = @connector.retrieve(handle, 0)
33
+ rescue => e
34
+ raise e
35
+ end #def reload!
36
+ end #class AceRule
@@ -0,0 +1,32 @@
1
+ class Group < Principal
2
+
3
+ def initialize(connector, col_path, report = false)
4
+ super(connector, col_path, report)
5
+ end
6
+
7
+ private
8
+
9
+ public
10
+
11
+ def create_new(name, groups, members)
12
+ super(name, groups)
13
+ if(members.length > 0) #add members into group
14
+ for member in members
15
+ add_membership(member, [name])
16
+ end
17
+ end
18
+ rescue => e
19
+ raise e
20
+ end
21
+
22
+ def delete(name)
23
+ super(name)
24
+
25
+ expr = "#{@doc}//node()[@idref=\"#{name}\"]"
26
+ @connector.update_delete(expr)
27
+ return name
28
+ rescue => e
29
+ raise e
30
+ end
31
+
32
+ end
@@ -0,0 +1,14 @@
1
+ class Individual < Principal
2
+
3
+ def initialize(connector, col_path, report = false)
4
+ super(connector, col_path, report)
5
+ end
6
+
7
+ public
8
+ def create_new(name, groups)
9
+ super(name, groups)
10
+ rescue => e
11
+ raise e
12
+ end
13
+ end
14
+
@@ -0,0 +1,44 @@
1
+ class Principal < ACL_Object
2
+ def initialize(connector, col_path, report = false)
3
+ super(connector, col_path, report)
4
+ @doc = "doc(\"#{@col_path}Principals.xml\")"
5
+ end
6
+
7
+ def eq (temp_ace, final_ace)
8
+ if(temp_ace.prin == final_ace.prin)
9
+ return true
10
+ else
11
+ return false
12
+ end
13
+ end
14
+
15
+ def ne (temp_ace, final_ace)
16
+ return !eq(temp_ace, final_ace)
17
+ end
18
+
19
+ def delete(name)
20
+ super(name)
21
+
22
+ expr = "#{@doc}//node()[@idref=\"#{name}\"]/parent::node()"
23
+ @connector.update_delete(expr)
24
+ return name
25
+ rescue => e
26
+ raise e
27
+ end
28
+
29
+ def add_membership(name, groups, ob_exists = false)
30
+ ok = true
31
+ #Make sure that group is really group and not individual
32
+ for group in groups
33
+ if(!exists?(group, "#{@doc}//Group[@id=\"#{group}\"]"))
34
+ ok = false;
35
+ end
36
+ end
37
+ if(ok)
38
+ super(name, groups, ob_exists)
39
+ else
40
+ raise RubyACLException.new(self.class.name, __method__,
41
+ "Failed to add membership. Group \"#{group}\" does not exist.", 113), caller
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,13 @@
1
+ class Privilege < ACL_Object
2
+
3
+ def initialize(connector, col_path, report = false)
4
+ super(connector, col_path, report)
5
+ @doc = "doc(\"#{@col_path}Privileges.xml\")"
6
+ end
7
+
8
+ def ge(temp_ace, final_ace, grid)
9
+ temp = grid.find_index(temp_ace.priv)
10
+ final = grid.find_index(final_ace.priv)
11
+ return super(temp, final)
12
+ end
13
+ end
@@ -0,0 +1,208 @@
1
+ class ResourceObject < ACL_Object
2
+ def initialize(connector, col_path, report = false)
3
+ super(connector, col_path, report)
4
+ @doc = "doc(\"#{@col_path}ResourceObjects.xml\")"
5
+ end
6
+
7
+ private
8
+ def generate_expr(id, type, address, owner)
9
+ expr = <<END
10
+ <#{self.class.name} id="#{id}">
11
+ <type>#{type}</type>
12
+ <address>#{address}</address>
13
+ <owner idref="#{owner}" />
14
+ </#{self.class.name}>
15
+ END
16
+ return expr
17
+ end
18
+
19
+ def parent(adr)
20
+ if(adr[-1] == "/") #if last is "/" then delete it
21
+ adr = adr[0..-2]
22
+ end
23
+ pos = adr.rindex("/")
24
+ adr = adr[0..pos-1]
25
+ return adr
26
+ end
27
+
28
+ def get_adr(res_ob_id)
29
+ query = "#{@doc}//node()[@id=\"#{res_ob_id}\"]/address/text()"
30
+ #puts query
31
+ handle = @connector.execute_query(query)
32
+ hits = @connector.get_hits(handle)
33
+ if(hits == 1)
34
+ adr = @connector.retrieve(handle, 0)
35
+ else
36
+ raise RubyACLException.new(self.class.name, __method__,
37
+ "#{self.class.name}(id=\"#{res_ob_id}\") exists more then once. (#{hits}x)", 31), caller
38
+ end
39
+ return adr
40
+ rescue => e
41
+ raise e
42
+ end
43
+
44
+ def get_type(res_ob_id)
45
+ query = "#{@doc}//node()[@id=\"#{res_ob_id}\"]/type/text()"
46
+ #puts query
47
+ handle = @connector.execute_query(query)
48
+ hits = @connector.get_hits(handle)
49
+ if(hits == 1)
50
+ type = @connector.retrieve(handle, 0)
51
+ else
52
+ raise RubyACLException.new(self.class.name, __method__,
53
+ "#{self.class.name}(id=\"#{res_ob_id}\") exists more then once. (#{hits}x)", 32), caller
54
+ end
55
+ return type
56
+ rescue => e
57
+ raise e
58
+ end
59
+
60
+ def change(type, address, what_is_changed, with_what)
61
+ address.delete!('(")')
62
+ # puts "type ,#{type},"
63
+ # puts "adr ,#{address},"
64
+ res_ob_id = find_res_ob(type, address)
65
+ if(res_ob_id.nil?)
66
+ raise RubyACLException.new(self.class.name, __method__,
67
+ "Failed to change #{what_is_changed}. Resource objects doesn't exist.", 36), caller
68
+ end
69
+ if(what_is_changed == "owner")
70
+ expr = "#{@doc}//node()[@id=\"#{res_ob_id}\"]/#{what_is_changed}/@idref"
71
+ else
72
+ expr = "#{@doc}//node()[@id=\"#{res_ob_id}\"]/#{what_is_changed}/text()"
73
+ end
74
+ expr_single = "\"#{with_what}\""
75
+ @connector.update_value(expr, expr_single)
76
+ if(what_is_changed == "type")
77
+ res_ob_id = find_res_ob(with_what, address)
78
+ end
79
+ if (what_is_changed == "address")
80
+ res_ob_id = find_res_ob(type, with_what)
81
+ end
82
+ if(what_is_changed == "owner")
83
+ expr = "#{@doc}//node()[@id=\"#{res_ob_id}\"]/#{what_is_changed}/string(@idref)"
84
+ end
85
+ handle = @connector.execute_query(expr)
86
+ hits = @connector.get_hits(handle)
87
+ if(hits == 1)
88
+ res = @connector.retrieve(handle, 0)
89
+ if(with_what == res)
90
+ puts "Change #{what_is_changed} succeeded." if @report
91
+ else
92
+ raise RubyACLException.new(self.class.name, __method__,
93
+ "Failed to change #{what_is_changed}.", 34), caller
94
+ end
95
+ else
96
+ raise RubyACLException.new(self.class.name, __method__,
97
+ "Failed to change #{what_is_changed}.", 34), caller
98
+ end
99
+ return type
100
+ rescue => e
101
+ raise e
102
+ end
103
+
104
+ public
105
+ def create_new(type, address, owner)
106
+ address.delete!('(")')
107
+ id = find_res_ob(type, address)
108
+ if(id == nil) #this resOb doesnt exist
109
+ id = "r" + Random.rand(1000000000).to_s
110
+ while(exists?(id))
111
+ id = "r" + Random.rand(1000000000).to_s
112
+ end
113
+ expr = generate_expr(id, type, address, owner)
114
+ expr_loc = "#{@doc}//#{self.class.name}s/#{self.class.name}[last()]"
115
+ #puts expr_loc
116
+ @connector.update_insert(expr, "following", expr_loc)
117
+ if(exists?(id))
118
+ puts "New #{self.class.name} \"#{id}\" created." if @report
119
+ return id
120
+ else
121
+ raise RubyACLException.new(self.class.name, __method__,
122
+ "#{self.class.name} type=\"#{type}\", address=\"#{address}\" was not able to create.", 33), caller
123
+ end
124
+ else #already exists
125
+ puts "#{self.class.name} \"#{id}\" was already created created." if @report
126
+ return id
127
+ end
128
+ end
129
+
130
+ def find_res_ob(type, address) #finds resource object's id by type and address
131
+ address.delete!('(")')
132
+ query = "#{@doc}//#{self.class.name}s/descendant::*[type=\"#{type}\" and address=\"#{address}\"]/string(@id)"
133
+ #puts query
134
+ handle = @connector.execute_query(query)
135
+ hits = @connector.get_hits(handle)
136
+ #puts hits
137
+ case hits
138
+ when 1
139
+ res_ob_id = @connector.retrieve(handle, 0)
140
+ if(res_ob_id == "") #eXist returns empty result => should return no result
141
+ return nil
142
+ else
143
+ return res_ob_id
144
+ end
145
+
146
+ when 0
147
+ return nil
148
+ else
149
+ raise RubyACLException.new(self.class.name, __method__,
150
+ "#{self.class.name}(type=\"#{type}\", address=\"#{address}\") exists more then once. (#{hits}x)", 30), caller
151
+ end
152
+ end
153
+
154
+ def ge(temp_ace, final_ace, grid)
155
+ temp = grid.find_index(temp_ace.res_obj)
156
+ final = grid.find_index(final_ace.res_obj)
157
+ return super(temp, final)
158
+ end
159
+
160
+ #finds membership parrent, e.g. dog's parrent is mammal
161
+ def find_res_ob_parents(res_ob_type, res_ob_adr)
162
+ ids = Array.new
163
+ while(res_ob_adr.rindex("/") != 0)
164
+ res_ob_adr = parent(res_ob_adr)
165
+ #puts res_ob_adr
166
+ ids.push(find_res_ob(res_ob_type, res_ob_adr))
167
+ end
168
+ #puts "ids #{ids.to_s}"
169
+ ids.compact!
170
+ #puts "ids #{ids.to_s}"
171
+ return ids
172
+ rescue => e
173
+ raise e
174
+ end
175
+
176
+ #finds resOb, which ends with /*
177
+ def res_obs_grand2children(res_ob_ids)
178
+ ids = Array.new
179
+ for res_ob_id in res_ob_ids
180
+ adr = get_adr(res_ob_id)
181
+ type = get_type(res_ob_id)
182
+ adr += "/*"
183
+ ids.push(find_res_ob(type, adr))
184
+ end
185
+ ids.compact!
186
+ return ids
187
+ rescue => e
188
+ raise e
189
+ end
190
+
191
+ def rename()
192
+ raise RubyACLException.new(self.class.name, __method__,
193
+ "Rename method is not supported for resource object", 35), caller
194
+ end
195
+
196
+ def change_type(type, address, new_type)
197
+ change(type, address, "type", new_type)
198
+ end
199
+
200
+ def change_address(type, address, new_address)
201
+ change(type, address, "address", new_address)
202
+ end
203
+
204
+ def change_owner(type, address, new_owner)
205
+ change(type, address, "owner", new_owner)
206
+ end
207
+
208
+ end #class ResourceObject
@@ -0,0 +1,69 @@
1
+ # To change this template, choose Tools | Templates
2
+ # and open the template in the editor.
3
+
4
+ class RubyACLException < RuntimeError
5
+
6
+ @description
7
+ @number
8
+
9
+ def initialize(called_class, called_method, _description = "Epic fail \n", _number = nil)
10
+ @clas = called_class
11
+ @method = called_method
12
+ @description = _description + "\n"
13
+ @number = _number
14
+ end
15
+ def inspect
16
+ self.tostring
17
+ end
18
+
19
+ def to_s
20
+ self.tostring
21
+ end
22
+
23
+ def tostring
24
+ @number.to_s + ": " + method + "\n" + @description
25
+ end
26
+
27
+ def code
28
+ @number
29
+ end
30
+
31
+ def method
32
+ return "#{@clas}.#{@method}"
33
+ end
34
+ end
35
+
36
+ #List of all exception. In brackets is method, that raise mentioned exception.
37
+
38
+ #0: Name is empty (RubyACL.initialize)
39
+ #1: Failed to create ACL in database (RubyACL.create_acl_in_db)
40
+ #2: Failed to set new name (RubyACL.setname)
41
+
42
+ #110: Name is empty (ACL_Object.create_new)
43
+ #111: ... already exist(s) (ACL_Object.create_new)
44
+ #112: ... was not able to create (ACL_Object.create_new)
45
+ #113: Failed to add membership. Group ... does not exist. (ACL_Object.add_membership)
46
+ #114: Failed to add membership. ... does not exist. (ACL_Object.add_membership)
47
+ #115: Failed to delete membership. Group ... does not exist. (ACL_Object.del_membership)
48
+ #116: Failed to delete membership. ... does not exist. (ACL_Object.del_membership)
49
+ #117: Failed to delete ... ... does not exist. (ACL_Object.delete)
50
+ #118: Failed to add membership. Membership is in cycle. (ACL_Object.add_membership)
51
+ #119: Failed to rename ... ... already exists (ACL_Object.rename)
52
+ #120: Failed to rename. (ACL_Object.rename)
53
+ #121:
54
+
55
+ #220: #{self.class.name} (Ace.find_ace)
56
+ #Principal=\"#{prin_id}\" and accessType=\"#{acc_type}\" and
57
+ #Privilege=\"#{priv_id}\" and ResourceObject=\"#{res_ob_id}\"
58
+ #exists more then once. (#{hits}x)
59
+ #221: #{self.class.name} \"#{id}\" was not able to create. (Ace.create_new)
60
+ #222: Access type #{acc_type} is not allowed. Only allowed type is \"deny\" and \"allow\". (Ace.create_new)
61
+ #223: Rename method is not supported for ACE. (Ace.rename)
62
+
63
+ #30: "#{self.class.name}(type=\"#{type}\", address=\"#{address}\") exists more then once. (#{hits}x)" (ResourceObject.find_res_ob)
64
+ #31: "#{self.class.name}(id=\"#{res_ob_id}\") exists more then once. (#{hits}x)" (ResourceObject.get_adr)
65
+ #32: "#{self.class.name}(id=\"#{res_ob_id}\") exists more then once. (#{hits}x)" (ResourceObject.get_type)
66
+ #33: #{self.class.name} type=\"#{type}\", address=\"#{address}\" was not able to create. (ResourceObject.create_new)
67
+ #34: Failed to change owner. (ResourceObject.change)
68
+ #35: Rename method is not supported for resource object (ResourceObject.rename)
69
+ #36: Failed to change #{what_is_changed}. Resource objects doesn't exist. (ResourceObject.change)