Class: RubyACL::ACLHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/rubyacl.rb

Overview

The ACL handler serves as a repository of ACL’s. Each ACL registers with the ACLHandler which provides a centralized storage mechanism for outside callers.

Instance Method Summary collapse

Constructor Details

#initializeACLHandler

Returns a new instance of ACLHandler.



124
125
126
127
# File 'lib/rubyacl.rb', line 124

def initialize()
	@acl_types = Hash.new()
	@acls = Hash.new()
end

Instance Method Details

#load_acls(filename) ⇒ Object

loads a YAML file containing acls into the handler



131
132
133
134
135
136
# File 'lib/rubyacl.rb', line 131

def load_acls(filename)
		acllist = YAML.load_file(filename)
		acllist['acls'].each() { |acl|
			register_acl(acl['name'], acl['acltype'], acl['data'])
		}
end

#process(aclstr, context = {}) ⇒ Object

processes an acl str and returns true or false



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/rubyacl.rb', line 170

def process(aclstr, context = {})
	# if aclstr contains any parenthasized patterns, call process recursively on them
	while(aclstr =~ /\(/)
		aclstr.sub!(/\(([^\)]+)\)/) {
			process($1, context)
		}
	end
	
	# process each operator in order of precedence
	#!
	while(aclstr =~ /!/)
		aclstr.sub!(/!([^ &|]+)/) { 
			(!@acls[aclstr[$1]].check(context)).to_s
		}
	end
	
	#&
	if(aclstr =~ /&/)
		return(process(aclstr[/^[^&]+/], context) and process(aclstr[/^[^&]+&(.*)$/,1], context))
	end
	
	#|
	if(aclstr =~ /\|/)
		return(process(aclstr[/^[^\|]+/], context) or process(aclstr[/^[^\|]+\|(.*)$/,1], context))
	end
	
	# constants
	if(aclstr =~ /^\s*true\s*$/i)
		return(true)
	elsif(aclstr =~ /^\s*false\s*$/i)
		return(false)
	end
	
	# single list items
	return(@acls[aclstr.strip()].check(context))
end

#register_acl(name, type, data) ⇒ Object

Registers an actual acl by name, type and data



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/rubyacl.rb', line 152

def register_acl(name, type, data)
	name.downcase!()
	type.downcase!()
	
	if(@acls.has_key?(name))
		raise(DuplicateACLError, "An ACL named '#{name}' is already registered")
	end
	
	aclclass = @acl_types[type]
	if(aclclass == nil)
		raise(ACLError, "The ACL type '#{type}' is not registered")
	end
	
	@acls[name] = aclclass.new(data)
end

#register_acl_type(aclclass) ⇒ Object

Registers a new ACL type with the ACLHandler. aclclass must conform to the ACL interface.



141
142
143
144
145
146
147
148
# File 'lib/rubyacl.rb', line 141

def register_acl_type(aclclass)
	if(@acl_types.has_key?(aclclass.acltype))
		raise(DuplicateACLTypeError, "An ACL of type '#{aclclass.acltype}' has already been registered")
	end
	
	puts(aclclass.to_s)
	@acl_types[aclclass.acltype.downcase()] = aclclass
end