From codesite-noreply at google.com Tue Jul 8 07:20:58 2008 From: codesite-noreply at google.com (codesite-noreply at google.com) Date: Tue, 08 Jul 2008 04:20:58 -0700 Subject: [Ruby-activeldap-commit] [ruby-activeldap commit] r773 - in trunk: lib/active_ldap test Message-ID: <001636164179e176280451816276@google.com> Author: koutou Date: Tue Jul 8 04:20:00 2008 New Revision: 773 Modified: trunk/lib/active_ldap/base.rb trunk/test/test_base.rb Log: * to_xml support :except option. [Issue 5] * escape XML text. Modified: trunk/lib/active_ldap/base.rb ============================================================================== --- trunk/lib/active_ldap/base.rb (original) +++ trunk/lib/active_ldap/base.rb Tue Jul 8 04:20:00 2008 @@ -30,6 +30,7 @@ require 'English' require 'thread' +require 'erb' module ActiveLdap # OO-interface to LDAP assuming pam/nss_ldap-style organization with @@ -792,7 +793,7 @@ # Also be sure to only pass in key-value pairs of your choosing. # Do not let URL/form hackers supply the keys. def attributes=(new_attributes) - return if new_attributes.nil? + return if new_attributes.blank? _schema = _local_entry_attribute = nil targets = remove_attributes_protected_from_mass_assignment(new_attributes) targets.each do |key, value| @@ -819,8 +820,7 @@ def to_xml(options={}) root = options[:root] || self.class.name.underscore result = "<#{root}>\n" - result << " #{dn}\n" - normalize_data(@data).sort_by {|key, values| key}.each do |key, values| + to_xml_data(options).each do |key, values| targets = [] values.each do |value| if value.is_a?(Hash) @@ -832,11 +832,26 @@ end end targets.sort_by {|value, attr| value}.each do |value, attr| - result << " <#{key}#{attr}>#{value}\n" + result << " <#{key}#{attr}>#{ERB::Util.h(value)}\n" end end result << "\n" result + end + + def to_xml_data(options={}) + except_dn = false + data = normalize_data(@data) + (options[:except] || []).each do |name| + real_name = to_real_attribute_name(name) + data.delete(real_name) if real_name + if (real_name || name).to_s.downcase == "dn" + except_dn = true + end + end + data = data.sort_by {|key, values| key} + data.unshift(["dn", [dn]]) unless except_dn + data end def have_attribute?(name, except=[]) Modified: trunk/test/test_base.rb ============================================================================== --- trunk/test/test_base.rb (original) +++ trunk/test/test_base.rb Tue Jul 8 04:20:00 2008 @@ -544,7 +544,7 @@ EOX make_temporary_user do |user, password| - assert_equal(<<-EOX, user.to_xml(:root => "user")) + assert_equal(<<-EOX, user.to_xml(:root => "user")) #{user.dn} #{user.cn} @@ -560,6 +560,48 @@ #{user.uid} #{user.uid_number} #{certificate} + #{user.user_password} + +EOX + end + end + + def test_to_xml_except + ou = ou_class.new("Sample") + assert_equal(<<-EOX, ou.to_xml(:root => "sample", :except => [:objectClass])) + + #{ou.dn} + Sample + +EOX + + except = [:dn, :object_class] + assert_equal(<<-EOX, ou.to_xml(:root => "sample", :except => except)) + + Sample + +EOX + end + + def test_to_xml_escape + make_temporary_user do |user, password| + sn = user.sn + user.sn = "<#{sn}>" + except = [:jpeg_photo, :user_certificate] + assert_equal(<<-EOX, user.to_xml(:root => "user", :except => except)) + + #{user.dn} + #{user.cn} + #{user.gid_number} + #{user.home_directory} + inetOrgPerson + organizationalPerson + person + posixAccount + shadowAccount + <#{sn}> + #{user.uid} + #{user.uid_number} #{user.user_password} EOX From codesite-noreply at google.com Tue Jul 8 08:18:23 2008 From: codesite-noreply at google.com (codesite-noreply at google.com) Date: Tue, 08 Jul 2008 05:18:23 -0700 Subject: [Ruby-activeldap-commit] [ruby-activeldap commit] r774 - in trunk: lib lib/active_ldap test Message-ID: <0016361e7c9a391baf0451823028@google.com> Author: koutou Date: Tue Jul 8 05:18:04 2008 New Revision: 774 Added: trunk/lib/active_ldap/xml.rb Modified: trunk/lib/active_ldap.rb trunk/lib/active_ldap/base.rb trunk/test/test_base.rb Log: * split XML serialization to lib/active_ldap/xml.rb. * support Base64-ed binary XML serialization. Modified: trunk/lib/active_ldap.rb ============================================================================== --- trunk/lib/active_ldap.rb (original) +++ trunk/lib/active_ldap.rb Tue Jul 8 05:18:04 2008 @@ -948,6 +948,7 @@ require 'active_ldap/distinguished_name' require 'active_ldap/ldif' +require 'active_ldap/xml' require 'active_ldap/associations' require 'active_ldap/attributes' Modified: trunk/lib/active_ldap/base.rb ============================================================================== --- trunk/lib/active_ldap/base.rb (original) +++ trunk/lib/active_ldap/base.rb Tue Jul 8 05:18:04 2008 @@ -818,40 +818,19 @@ end def to_xml(options={}) - root = options[:root] || self.class.name.underscore - result = "<#{root}>\n" - to_xml_data(options).each do |key, values| - targets = [] - values.each do |value| - if value.is_a?(Hash) - value.each do |option, real_value| - targets << [real_value, " #{option}=\"true\""] - end + options = options.dup + options[:root] ||= self.class.name.underscore + except = options[:except] + if except + options[:except] = except.collect do |name| + if name.to_s.downcase == "dn" + "dn" else - targets << [value] + to_real_attribute_name(name) end - end - targets.sort_by {|value, attr| value}.each do |value, attr| - result << " <#{key}#{attr}>#{ERB::Util.h(value)}\n" - end + end.compact end - result << "\n" - result - end - - def to_xml_data(options={}) - except_dn = false - data = normalize_data(@data) - (options[:except] || []).each do |name| - real_name = to_real_attribute_name(name) - data.delete(real_name) if real_name - if (real_name || name).to_s.downcase == "dn" - except_dn = true - end - end - data = data.sort_by {|key, values| key} - data.unshift(["dn", [dn]]) unless except_dn - data + XML.new(dn, normalize_data(@data)).to_s(options) end def have_attribute?(name, except=[]) Added: trunk/lib/active_ldap/xml.rb ============================================================================== --- (empty file) +++ trunk/lib/active_ldap/xml.rb Tue Jul 8 05:18:04 2008 @@ -0,0 +1,89 @@ +require 'erb' + +require 'active_ldap/ldif' + +module ActiveLdap + class Xml + class Serializer + PRINTABLE_STRING = /[\x20-\x7e\w\s]*/um + + def initialize(dn, attributes, options={}) + @dn = dn + @attributes = attributes + @options = options + end + + def to_s + root = @options[:root] + result = "<#{root}>\n" + target_attributes.each do |key, values| + values = normalize_values(values).sort_by {|value, attr| value} + values.each do |value, attr| + attr = " #{attr}" unless attr.blank? + result << " <#{key}#{attr}>#{ERB::Util.h(value)}\n" + end + end + result << "\n" + result + end + + private + def target_attributes + except_dn = false + attributes = @attributes.dup + (@options[:except] || []).each do |name| + if name == "dn" + except_dn = true + else + attributes.delete(name) + end + end + attributes = attributes.sort_by {|key, values| key} + attributes.unshift(["dn", [@dn]]) unless except_dn + attributes + end + + def normalize_values(values) + targets = [] + values.each do |value| + targets.concat(normalize_value(value)) + end + targets + end + + def normalize_value(value, options=[]) + targets = [] + if value.is_a?(Hash) + value.each do |real_option, real_value| + targets.concat(normalize_value(real_value, options + [real_option])) + end + elsif value.is_a?(Array) + value.each do |real_value| + targets.concat(normalize_value(real_value, options)) + end + else + if /\A#{PRINTABLE_STRING}\z/u !~ value + value = [value].pack("m").gsub(/\n/u, '') + options += ["base64"] + end + xml_attributes = options.collect do |name, val| + "#{ERB::Util.h(name)}=\"#{ERB::Util.h(val || 'true')}\"" + end.join(" ") + targets << [value, xml_attributes] + end + targets + end + end + + def initialize(dn, attributes) + @dn = dn + @attributes = attributes + end + + def to_s(options={}) + Serializer.new(@dn, @attributes, options).to_s + end + end + + XML = Xml +end Modified: trunk/test/test_base.rb ============================================================================== --- trunk/test/test_base.rb (original) +++ trunk/test/test_base.rb Tue Jul 8 05:18:04 2008 @@ -550,7 +550,7 @@ #{user.cn} #{user.gid_number} #{user.home_directory} - #{jpeg_photo} + #{base64(jpeg_photo)} inetOrgPerson organizationalPerson person @@ -559,7 +559,7 @@ #{user.sn} #{user.uid} #{user.uid_number} - #{certificate} + #{base64(certificate)} #{user.user_password} EOX @@ -721,5 +721,9 @@ entry.class.send(:instantiate, [record.dn, record.attributes]) end assert_equal([entry], parsed_entries) + end + + def base64(string) + [string].pack("m").gsub(/\n/u, "") end end From codesite-noreply at google.com Thu Jul 10 07:47:34 2008 From: codesite-noreply at google.com (codesite-noreply at google.com) Date: Thu, 10 Jul 2008 04:47:34 -0700 Subject: [Ruby-activeldap-commit] [ruby-activeldap commit] r775 - in trunk: . lib/active_ldap test Message-ID: <001636283adab9e8b70451a9fdce@google.com> Author: koutou Date: Thu Jul 10 04:46:55 2008 New Revision: 775 Modified: trunk/README trunk/lib/active_ldap/xml.rb trunk/test/test_base.rb Log: * use Activeresource XML format. Suggested by Baptiste Grenier. Thanks!!! Modified: trunk/README ============================================================================== --- trunk/README (original) +++ trunk/README Thu Jul 10 04:46:55 2008 @@ -110,3 +110,4 @@ * Matt Mencel: A bug report. * CultureSpy: A bug report. * gwarf12: A bug report. +* Baptiste Grenier: API improvement ideas. Modified: trunk/lib/active_ldap/xml.rb ============================================================================== --- trunk/lib/active_ldap/xml.rb (original) +++ trunk/lib/active_ldap/xml.rb Thu Jul 10 04:46:55 2008 @@ -17,11 +17,8 @@ root = @options[:root] result = "<#{root}>\n" target_attributes.each do |key, values| - values = normalize_values(values).sort_by {|value, attr| value} - values.each do |value, attr| - attr = " #{attr}" unless attr.blank? - result << " <#{key}#{attr}>#{ERB::Util.h(value)}\n" - end + values = normalize_values(values).sort_by {|value, _| value} + result << serialize_attribute_values(key, values) end result << "\n" result @@ -66,12 +63,45 @@ value = [value].pack("m").gsub(/\n/u, '') options += ["base64"] end - xml_attributes = options.collect do |name, val| - "#{ERB::Util.h(name)}=\"#{ERB::Util.h(val || 'true')}\"" - end.join(" ") + xml_attributes = {} + options.each do |name, val| + xml_attributes[name] = val || "true" + end targets << [value, xml_attributes] end targets + end + + def serialize_attribute_values(name, values) + return "" if values.blank? + + result = "" + if name == "dn" or @options[:type].to_s.downcase == "ldif" + values.collect do |value, xml_attributes| + xml = serialize_attribute_value(name, value, xml_attributes) + result << " #{xml}\n" + end + else + plural_name = name.pluralize + result << " <#{plural_name} type=\"array\">\n" + values.each do |value, xml_attributes| + xml = serialize_attribute_value(name, value, xml_attributes) + result << " #{xml}\n" + end + result << " \n" + end + result + end + + def serialize_attribute_value(name, value, xml_attributes) + if xml_attributes.blank? + xml_attributes = "" + else + xml_attributes = " " + xml_attributes.collect do |n, v| + "#{ERB::Util.h(n)}=\"#{ERB::Util.h(v)}\"" + end.join(" ") + end + "<#{name}#{xml_attributes}>#{ERB::Util.h(value)}" end end Modified: trunk/test/test_base.rb ============================================================================== --- trunk/test/test_base.rb (original) +++ trunk/test/test_base.rb Thu Jul 10 04:46:55 2008 @@ -528,18 +528,26 @@ assert_equal(<<-EOX, ou.to_xml(:root => "ou")) #{ou.dn} - organizationalUnit - top - Sample + + organizationalUnit + top + + + Sample + EOX assert_equal(<<-EOX, ou.to_xml) <> #{ou.dn} - organizationalUnit - top - Sample + + organizationalUnit + top + + + Sample + EOX @@ -547,20 +555,40 @@ assert_equal(<<-EOX, user.to_xml(:root => "user")) #{user.dn} - #{user.cn} - #{user.gid_number} - #{user.home_directory} - #{base64(jpeg_photo)} - inetOrgPerson - organizationalPerson - person - posixAccount - shadowAccount - #{user.sn} - #{user.uid} - #{user.uid_number} - #{base64(certificate)} - #{user.user_password} + + #{user.cn} + + + #{user.gid_number} + + + #{user.home_directory} + + + #{base64(jpeg_photo)} + + + inetOrgPerson + organizationalPerson + person + posixAccount + shadowAccount + + + #{user.sn} + + + #{user.uid} + + + #{user.uid_number} + + + #{base64(certificate)} + + + #{user.user_password} + EOX end @@ -571,14 +599,18 @@ assert_equal(<<-EOX, ou.to_xml(:root => "sample", :except => [:objectClass])) #{ou.dn} - Sample + + Sample + EOX except = [:dn, :object_class] assert_equal(<<-EOX, ou.to_xml(:root => "sample", :except => except)) - Sample + + Sample + EOX end @@ -589,6 +621,48 @@ user.sn = "<#{sn}>" except = [:jpeg_photo, :user_certificate] assert_equal(<<-EOX, user.to_xml(:root => "user", :except => except)) + + #{user.dn} + + #{user.cn} + + + #{user.gid_number} + + + #{user.home_directory} + + + inetOrgPerson + organizationalPerson + person + posixAccount + shadowAccount + + + <#{sn}> + + + #{user.uid} + + + #{user.uid_number} + + + #{user.user_password} + + +EOX + end + end + + def test_to_xml_type_ldif + make_temporary_user do |user, password| + sn = user.sn + user.sn = "<#{sn}>" + except = [:jpeg_photo, :user_certificate] + options = {:root => "user", :except => except, :type => :ldif} + assert_equal(<<-EOX, user.to_xml(options)) #{user.dn} #{user.cn} From codesite-noreply at google.com Sat Jul 12 02:08:10 2008 From: codesite-noreply at google.com (codesite-noreply at google.com) Date: Fri, 11 Jul 2008 23:08:10 -0700 Subject: [Ruby-activeldap-commit] [ruby-activeldap commit] r776 - in trunk: lib/active_ldap test Message-ID: <00163628393891df780451cd7bec@google.com> Author: koutou Date: Fri Jul 11 23:07:07 2008 New Revision: 776 Modified: trunk/lib/active_ldap/base.rb trunk/lib/active_ldap/xml.rb trunk/test/test_base.rb Log: * apply SINGLE-VALUE information for to_xml. Modified: trunk/lib/active_ldap/base.rb ============================================================================== --- trunk/lib/active_ldap/base.rb (original) +++ trunk/lib/active_ldap/base.rb Fri Jul 11 23:07:07 2008 @@ -830,7 +830,7 @@ end end.compact end - XML.new(dn, normalize_data(@data)).to_s(options) + XML.new(dn, normalize_data(@data), schema).to_s(options) end def have_attribute?(name, except=[]) Modified: trunk/lib/active_ldap/xml.rb ============================================================================== --- trunk/lib/active_ldap/xml.rb (original) +++ trunk/lib/active_ldap/xml.rb Fri Jul 11 23:07:07 2008 @@ -7,9 +7,10 @@ class Serializer PRINTABLE_STRING = /[\x20-\x7e\w\s]*/um - def initialize(dn, attributes, options={}) + def initialize(dn, attributes, schema, options={}) @dn = dn @attributes = attributes + @schema = schema @options = options end @@ -18,7 +19,11 @@ result = "<#{root}>\n" target_attributes.each do |key, values| values = normalize_values(values).sort_by {|value, _| value} - result << serialize_attribute_values(key, values) + if @schema.attribute(key).single_value? + result << " #{serialize_attribute_value(key, *values[0])}\n" + else + result << serialize_attribute_values(key, values) + end end result << "\n" result @@ -105,13 +110,14 @@ end end - def initialize(dn, attributes) + def initialize(dn, attributes, schema) @dn = dn @attributes = attributes + @schema = schema end def to_s(options={}) - Serializer.new(@dn, @attributes, options).to_s + Serializer.new(@dn, @attributes, @schema, options).to_s end end Modified: trunk/test/test_base.rb ============================================================================== --- trunk/test/test_base.rb (original) +++ trunk/test/test_base.rb Fri Jul 11 23:07:07 2008 @@ -558,12 +558,8 @@ #{user.cn} - - #{user.gid_number} - - - #{user.home_directory} - + #{user.gid_number} + #{user.home_directory} #{base64(jpeg_photo)} @@ -580,9 +576,7 @@ #{user.uid} - - #{user.uid_number} - + #{user.uid_number} #{base64(certificate)} @@ -626,12 +620,8 @@ #{user.cn} - - #{user.gid_number} - - - #{user.home_directory} - + #{user.gid_number} + #{user.home_directory} inetOrgPerson organizationalPerson @@ -645,9 +635,7 @@ #{user.uid} - - #{user.uid_number} - + #{user.uid_number} #{user.user_password} From codesite-noreply at google.com Wed Jul 16 08:12:01 2008 From: codesite-noreply at google.com (codesite-noreply at google.com) Date: Wed, 16 Jul 2008 05:12:01 -0700 Subject: [Ruby-activeldap-commit] [ruby-activeldap commit] r777 - in trunk: . lib/active_ldap/schema test Message-ID: <0016e640817a348d9404522308c5@google.com> Author: koutou Date: Wed Jul 16 05:11:13 2008 New Revision: 777 Modified: trunk/README trunk/lib/active_ldap/schema/syntaxes.rb trunk/test/test_syntax.rb Log: * use fallback value as Time.at(0) if GeneralizedTime. Suggested by Richard 3 Nicholas. Thanks!!! Modified: trunk/README ============================================================================== --- trunk/README (original) +++ trunk/README Wed Jul 16 05:11:13 2008 @@ -111,3 +111,4 @@ * CultureSpy: A bug report. * gwarf12: A bug report. * Baptiste Grenier: API improvement ideas. +* Richard 3 Nicholas: API improvement ideas. Modified: trunk/lib/active_ldap/schema/syntaxes.rb ============================================================================== --- trunk/lib/active_ldap/schema/syntaxes.rb (original) +++ trunk/lib/active_ldap/schema/syntaxes.rb Wed Jul 16 05:11:13 2008 @@ -187,9 +187,19 @@ fraction = match_data[-2] fraction = fraction.to_f if fraction time_zone = match_data[-1] - Time.send(:make_time, - year, month, day, hour, minute, second, fraction, - time_zone, Time.now) + begin + Time.send(:make_time, + year, month, day, hour, minute, second, fraction, + time_zone, Time.now) + rescue ArgumentError + raise if year >= 1700 + raise if $!.message != "argument out of range" + Time.at(0) + rescue RangeError + raise if year >= 1700 + raise if $!.message != "bignum too big to convert into `long'" + Time.at(0) + end else value end Modified: trunk/test/test_syntax.rb ============================================================================== --- trunk/test/test_syntax.rb (original) +++ trunk/test/test_syntax.rb Wed Jul 16 05:11:13 2008 @@ -93,6 +93,18 @@ assert_type_cast(Time.parse("1994/12/16 10:32:12.345 +09:00"), "19941216103212.345+0900", "Generalized Time") + assert_type_cast(Time.parse("1970/01/01 09:00:00 +09:00"), + "19700101090000+0900", + "Generalized Time") + begin + Time.at(-1) + rescue ArgumentError + if $!.message == "argument out of range" + assert_type_cast(Time.parse("1969/12/31 23:59:59 +00:00"), + "19691231235959+0000", + "Generalized Time") + end + end end def test_integer_type_cast