lib/adauth/ad_object.rb in adauth-2.0.0 vs lib/adauth/ad_object.rb in adauth-2.0.1

- old
+ new

@@ -12,23 +12,23 @@ # Active Directory Interface Object # # Objects inherit from this class. # # Provides all the common functions for Active Directory. - class AdObject + class AdObject # Returns all objects which have the ObjectClass of the inherited class def self.all - Adauth.logger.info(self.inspect) { "Searching for all objects matching filter \"#{self::ObjectFilter}\"" } + Adauth.logger.info(self.class.inspect) { "Searching for all objects matching filter \"#{self::ObjectFilter}\"" } self.filter(self::ObjectFilter) end # Returns all the objects which match the supplied query # # Uses ObjectFilter to restrict to the current object def self.where(field, value) search_filter = Net::LDAP::Filter.eq(field, value) - Adauth.logger.info(self.inspect) { "Searching for all \"#{self::ObjectFilter}\" where #{field} = #{value}" } + Adauth.logger.info(self.class.inspect) { "Searching for all \"#{self::ObjectFilter}\" where #{field} = #{value}" } filter(add_object_filter(search_filter)) end # Returns all LDAP objects that match the given filter # @@ -60,32 +60,46 @@ # Allows direct access to @ldap_object def ldap_object @ldap_object end - # Over rides method_missing and interacts with @ldap_object + # Over ride method missing to see if the object has a field by that name def method_missing(method, *args) - if self.class::Fields.keys.include?(method) - field = self.class::Fields[method] - if field.is_a? Symbol - return (@ldap_object.send(field).to_s).gsub(/\"|\[|\]/, "") - elsif field.is_a? Array - @ldap_object.send(field.first).collect(&field.last) - end - else - super - end + field = self.class::Fields[method] + return handle_field(field) if field + return super end + # Handle the output for the given field + def handle_field(field) + case field + when Symbol then return return_symbol_value(field) + when Array then return @ldap_object.send(field.first).collect(&field.last) + end + end + # Returns all the groups the object is a member of def groups unless @groups @groups = convert_to_objects(cn_groups) end @groups end + # The same as cn_groups, but with the parent groups included + def cn_groups_nested + @cn_groups_nested = cn_groups + cn_groups.each do |group| + ado = Adauth::AdObjects::Group.where('name', group).first + groups = convert_to_objects ado.cn_groups + groups.each do |g| + @cn_groups_nested.push g if !(@cn_groups_nested.include?(g)) + end + end + return @cn_groups_nested + end + # Returns all the ous the object is in def ous unless @ous @ous = [] @ldap_object.dn.split(/,/).each do |entry| @@ -106,11 +120,15 @@ @dn_ous end # Runs a modify action on the current object, takes an aray of operations def modify(operations) - raise 'Modify Operation Failed' unless Adauth.connection.modify :dn => @ldap_object.dn, :operations => operations + Adauth.logger.info(self.class.inspect) { "Attempting modify operation" } + unless Adauth.connection.modify :dn => @ldap_object.dn, :operations => operations + Adauth.logger.fatal(self.class.inspect) { "Modify Operation Failed! Code: #{Adauth.connection.get_operation_result.code} Message: #{Adauth.connection.get_operation_result.message}" } + raise 'Modify Operation Failed (see log for details)' + end end # Returns an array of member objects for this object def members unless @members @@ -146,8 +164,16 @@ def convert_to_object(entity) user = Adauth::AdObjects::User.where('sAMAccountName', entity).first group = Adauth::AdObjects::Group.where('sAMAccountName', entity).first (user || group) + end + + def return_symbol_value(field) + value = @ldap_object.send(field) + case value + when String then return value + when Net::BER::BerIdentifiedArray then return value.first + end end end end \ No newline at end of file