Skip to content
This repository has been archived by the owner on Nov 26, 2021. It is now read-only.

Commit

Permalink
Reworked a lot of code for efficiency purposes
Browse files Browse the repository at this point in the history
  • Loading branch information
ajrkerr committed Mar 3, 2011
1 parent a503d2b commit 9b92d1b
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 56 deletions.
3 changes: 2 additions & 1 deletion Rakefile
@@ -1,3 +1,4 @@
require 'psych'

begin
require 'jeweler'
Expand All @@ -16,4 +17,4 @@ begin
Jeweler::GemcutterTasks.new
rescue LoadError
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
end
end
132 changes: 84 additions & 48 deletions lib/active_directory/base.rb
Expand Up @@ -239,7 +239,7 @@ def self.find(*args)
}

cached_results = find_cached_results(args[1])
return cached_results unless cached_results.nil?
return cached_results if cached_results or cached_results.nil?

options[:in] = [ options[:in].to_s, @@settings[:base] ].delete_if { |part| part.empty? }.join(",")

Expand All @@ -258,55 +258,56 @@ def self.find(*args)
end
end

##
# Filters the cache result by the object type we're looking for
#
def self.filter_cache_result(result)
result.delete_if { |entry| !entry.kind_of? self }
end

##
# Searches the cache and returns the result
# Returns false on failure, nil on wrong object type
#
def self.find_cached_results(filters)
return nil unless cache?
return false unless cache?

#Check to see if we're only looking for :distinguishedname
if filters.is_a? Hash and filters.keys == [:distinguishedname]
#Find keys we're looking up, convert to array
dns = filters[:distinguishedname]
return false unless filters.is_a? Hash and filters.keys == [:distinguishedname]

if dns.kind_of? Array
#Check to see if all of the results are in teh cache
return nil unless (dns & @@cache.keys == dns)
#Find keys we're looking up
dns = filters[:distinguishedname]

result = []
if dns.kind_of? Array
result = []

dns.each { |dn| result << @@cache[dn] }
dns.each do |dn|
entry = @@cache[dn]

return filter_cache_result(result) if result.size == dns.size
else
return @@cache[dns] if @@cache.key? dns and @@cache[dns].is_a? self.class
#If the object isn't in the cache just run the query
return false if entry.nil?

#Only permit objects of the type we're looking for
result << entry if entry.kind_of? self
end

return result
else
return false unless @@cache.key? dns
return @@cache[dns] if @@cache[dns].is_a? self
end
end

def self.find_all(options)
results = []
@@ldap.search(:filter => options[:filter], :base => options[:in], :return_result => false) do |entry|
ad_entry = new(entry)
@@cache[ad_entry.dn] = ad_entry
results << ad_entry
ldap_objs = @@ldap.search(:filter => options[:filter], :base => options[:in])

ldap_objs.each do |entry|
ad_obj = new(entry)
@@cache[entry.dn] = ad_obj unless ad_obj.instance_of? Base
results << ad_obj
end

results
end

def self.find_first(options)
@@ldap.search(:filter => options[:filter], :base => options[:in], :return_result => false) do |entry|
ad_entry = new(entry)
@@cache[ad_entry.dn] = ad_entry
return ad_entry
end
ad_obj = new(@@ldap.search(:filter => options[:filter], :base => options[:in]))
@@cache[ad_obj.dn] = ad_obj unless ad_obj.instance_of? Base
return ad_obj
end

def self.method_missing(name, *args) # :nodoc:
Expand Down Expand Up @@ -336,7 +337,7 @@ def self.parse_finder_spec(method_name) # :nodoc:

def ==(other) # :nodoc:
return false if other.nil?
other.objectGUID == objectGUID
other[:objectguid] == get_attr(:objectguid)
end

#
Expand Down Expand Up @@ -386,7 +387,7 @@ def update_attributes(attributes_to_update)
values = values.collect { |v| v.to_s }

current_value = begin
@entry.send(attribute)
@entry[attribute]
rescue NoMethodError
nil
end
Expand Down Expand Up @@ -494,29 +495,73 @@ def initialize(attributes = {}) # :nodoc:
end
end

##
# Pull the class we're in
# This isn't quite right, as extending the object does funny things to how we
# lookup objects
def self.class_name
@klass ||= (self.name.include?('::') ? self.name[/.*::(.*)/, 1] : self.name)
end

##
# Grabs the field type depending on the class it is called from
# Takes the field name as a parameter
def self.get_field_type(name)
#Extract class name
throw "Invalid field name" if name.nil?
klass = self.name.include?('::') ? self.name[/.*::(.*)/, 1] : self.name
type = ::ActiveDirectory.special_fields[klass.to_sym][name.to_s.downcase.to_sym]
type = ::ActiveDirectory.special_fields[class_name.to_sym][name.to_s.downcase.to_sym]
type.to_s unless type.nil?
end

@types = {}

def self.decode_field(name, value) # :nodoc:
type = get_field_type name
return ::ActiveDirectory::FieldType::const_get(type).decode(value) if !type.nil? and ::ActiveDirectory::FieldType::const_defined? type
if !type.nil? and ::ActiveDirectory::FieldType::const_defined? type
return ::ActiveDirectory::FieldType::const_get(type).decode(value)
end
return value
end

def self.encode_field(name, value) # :nodoc:
type = get_field_type name
return ::ActiveDirectory::FieldType::const_get(type).encode(value) if !type.nil? and ::ActiveDirectory::FieldType::const_defined? type
if !type.nil? and ::ActiveDirectory::FieldType::const_defined? type
return ::ActiveDirectory::FieldType::const_get(type).encode(value)
end
return value
end

##
# Reads the array of values for the provided attribute. The attribute name
# is canonicalized prior to reading. Returns an empty array if the
# attribute does not exist.
def [](name)
get_attr(name)
end

##
# Ensure that flattening works correctly
def to_ary
end

def valid_attribute? name
@attributes.has_key?(name) || @entry.attribute_names.include?(name)
end

def get_attr(name)
name = name.to_s.downcase

return decode_field(name, @attributes[name.to_sym]) if @attributes.has_key?(name.to_sym)

if @entry.attribute_names.include? name.to_sym
value = @entry[name.to_sym]
value = value.first if value.kind_of?(Array) && value.size == 1
value = value.to_s if value.nil? || value.size == 1
value = nil.to_s if value.empty?
return self.class.decode_field(name, value)
end
end

def method_missing(name, args = []) # :nodoc:
name = name.to_s.downcase

Expand All @@ -525,20 +570,11 @@ def method_missing(name, args = []) # :nodoc:
@attributes[name.to_sym] = encode_field(name, args)
end

return decode_field(name, @attributes[name.to_sym]) if @attributes.has_key?(name.to_sym)

if @entry
begin
value = @entry.send(name.to_sym)
value = value.first if value.kind_of?(Array) && value.size == 1
value = value.to_s if value.nil? || value.size == 1
return self.class.decode_field(name, value)
rescue NoMethodError => e
return nil
end
if valid_attribute? name.to_sym
get_attr(name)
else
super
end

super
end

end
Expand Down
2 changes: 1 addition & 1 deletion lib/active_directory/field_type/binary.rb
Expand Up @@ -32,7 +32,7 @@ def self.encode(hex_string)
# Decodes a binary GUID as a hex string
#
def self.decode(guid)
guid.unpack("H*").to_s
guid.unpack("H*").first.to_s
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/active_directory/field_type/dn_array.rb
Expand Up @@ -33,7 +33,7 @@ def self.encode(obj_array)
#
def self.decode(dn_array)
# How to do user or group?
Base.find( :all, :distinguishedname => dn_array)
Base.find(:all, :distinguishedname => dn_array)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/active_directory/field_type/group_dn_array.rb
Expand Up @@ -33,7 +33,7 @@ def self.encode(obj_array)
#
def self.decode(dn_array)
# How to do user or group?
Group.find( :all, :distinguishedname => dn_array)
Group.find(:all, :distinguishedname => dn_array)
end
end
end
Expand Down
10 changes: 7 additions & 3 deletions lib/active_directory/field_type/member_dn_array.rb
Expand Up @@ -33,10 +33,14 @@ def self.encode(obj_array)
#
def self.decode(dn_array)
# Ensures that the objects are cast correctly
users = [User.find( :all, :distinguishedname => dn_array)].flatten
groups = [Group.find(:all, :distinguishedname => dn_array)].flatten
users = User.find(:all, :distinguishedname => dn_array)
groups = Group.find(:all, :distinguishedname => dn_array)

users + groups
arr = Array.new
arr << users unless users.nil?
arr << groups unless groups.nil?

return arr.flatten
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/active_directory/field_type/user_dn_array.rb
Expand Up @@ -33,7 +33,7 @@ def self.encode(obj_array)
#
def self.decode(dn_array)
# How to do user or group?
User.find( :all, :distinguishedname => dn_array)
User.find(:all, :distinguishedname => dn_array)
end
end
end
Expand Down

0 comments on commit 9b92d1b

Please sign in to comment.