From f67035139f05c753d1f71973c0f8c1d45608694f Mon Sep 17 00:00:00 2001 From: Aurélio A. Heckert Date: Fri, 4 Sep 2015 12:57:12 -0300 Subject: [PATCH] LDAP Plugin: Convert non utf-8 chars --- plugins/ldap/lib/ldap_authentication.rb | 6 +++++- plugins/ldap/test/unit/ldap_authentication_test.rb | 55 +++++++++++++++++++++++++++++++------------------------ 2 files changed, 36 insertions(+), 25 deletions(-) diff --git a/plugins/ldap/lib/ldap_authentication.rb b/plugins/ldap/lib/ldap_authentication.rb index 1918272..eaa7a18 100644 --- a/plugins/ldap/lib/ldap_authentication.rb +++ b/plugins/ldap/lib/ldap_authentication.rb @@ -19,6 +19,7 @@ require 'rubygems' require 'iconv' require 'net/ldap' require 'net/ldap/dn' +require 'magic' class LdapAuthentication @@ -134,7 +135,10 @@ class LdapAuthentication def self.get_attr(entry, attr_name) if !attr_name.blank? - entry[attr_name].is_a?(Array) ? entry[attr_name].first : entry[attr_name] + val = entry[attr_name].is_a?(Array) ? entry[attr_name].first : entry[attr_name] + charset = Magic.guess_string_mime(val).match(/charset=([^\s]+)/)[1] + val.encode 'utf-8', charset, invalid: :replace, undef: :replace end end + end diff --git a/plugins/ldap/test/unit/ldap_authentication_test.rb b/plugins/ldap/test/unit/ldap_authentication_test.rb index 783f0e7..b57632a 100644 --- a/plugins/ldap/test/unit/ldap_authentication_test.rb +++ b/plugins/ldap/test/unit/ldap_authentication_test.rb @@ -1,3 +1,4 @@ +# encoding: UTF-8 require File.dirname(__FILE__) + '/../test_helper' class LdapAuthenticationTest < ActiveSupport::TestCase @@ -6,137 +7,143 @@ class LdapAuthenticationTest < ActiveSupport::TestCase @ldap_config = load_ldap_config end - should "host be nil as default" do + should 'host be nil as default' do ldap = LdapAuthentication.new assert_nil ldap.host end - should "create with host passed as parameter" do + should 'create with host passed as parameter' do value = 'http://myhost.com' ldap = LdapAuthentication.new('host' => value) assert_equal value, ldap.host end - should "port be 389 as default" do + should 'port be 389 as default' do ldap = LdapAuthentication.new assert_equal 389, ldap.port end - should "create with port passed as parameter" do + should 'create with port passed as parameter' do value = 555 ldap = LdapAuthentication.new('port' => value) assert_equal value, ldap.port end - should "account be nil as default" do + should 'account be nil as default' do ldap = LdapAuthentication.new assert_nil ldap.account end - should "create with account passed as parameter" do + should 'create with account passed as parameter' do value = 'uid=sector,ou=Service,ou=corp,dc=company,dc=com,dc=br' ldap = LdapAuthentication.new('account' => value) assert_equal value, ldap.account end - should "account_password be nil as default" do + should 'account_password be nil as default' do ldap = LdapAuthentication.new assert_nil ldap.account_password end - should "create with account_password passed as parameter" do + should 'create with account_password passed as parameter' do value = 'password' ldap = LdapAuthentication.new('account_password' => value) assert_equal value, ldap.account_password end - should "base_dn be nil as default" do + should 'base_dn be nil as default' do ldap = LdapAuthentication.new assert_nil ldap.base_dn end - should "create with base_dn passed as parameter" do + should 'create with base_dn passed as parameter' do value = 'dc=company,dc=com,dc=br' ldap = LdapAuthentication.new('base_dn' => value) assert_equal value, ldap.base_dn end - should "attr_login be nil as default" do + should 'attr_login be nil as default' do ldap = LdapAuthentication.new assert_nil ldap.attr_login end - should "create with attr_login passed as parameter" do + should 'create with attr_login passed as parameter' do value = 'uid' ldap = LdapAuthentication.new('attr_login' => value) assert_equal value, ldap.attr_login end - should "attr_fullname be nil as default" do + should 'attr_fullname be nil as default' do ldap = LdapAuthentication.new assert_nil ldap.attr_fullname end - should "create with attr_fullname passed as parameter" do + should 'create with attr_fullname passed as parameter' do value = 'Noosfero System' ldap = LdapAuthentication.new('attr_fullname' => value) assert_equal value, ldap.attr_fullname end - should "attr_mail be nil as default" do + should 'attr_mail be nil as default' do ldap = LdapAuthentication.new assert_nil ldap.attr_mail end - should "create with attr_mail passed as parameter" do + should 'create with attr_mail passed as parameter' do value = 'test@noosfero.com' ldap = LdapAuthentication.new('attr_mail' => value) assert_equal value, ldap.attr_mail end - should "onthefly_register be false as default" do + should 'onthefly_register be false as default' do ldap = LdapAuthentication.new refute ldap.onthefly_register end - should "create with onthefly_register passed as parameter" do + should 'create with onthefly_register passed as parameter' do value = true ldap = LdapAuthentication.new('onthefly_register' => value) assert_equal value, ldap.onthefly_register end - should "filter be nil as default" do + should 'filter be nil as default' do ldap = LdapAuthentication.new assert_nil ldap.filter end - should "create with filter passed as parameter" do + should 'create with filter passed as parameter' do value = 'test' ldap = LdapAuthentication.new('filter' => value) assert_equal value, ldap.filter end - should "tls be false as default" do + should 'tls be false as default' do ldap = LdapAuthentication.new refute ldap.tls end - should "create with tls passed as parameter" do + should 'create with tls passed as parameter' do value = true ldap = LdapAuthentication.new('tls' => value) assert_equal value, ldap.tls end - should "onthefly_register? return true if onthefly_register is true" do + should 'onthefly_register? return true if onthefly_register is true' do ldap = LdapAuthentication.new('onthefly_register' => true) assert ldap.onthefly_register? end - should "onthefly_register? return false if onthefly_register is false" do + should 'onthefly_register? return false if onthefly_register is false' do ldap = LdapAuthentication.new('onthefly_register' => false) refute ldap.onthefly_register? end + should 'detect and convert non utf-8 charset from ldap' do + entry = { 'name' => "Jos\xE9 Jo\xE3o" } + name = LdapAuthentication.get_attr entry, 'name' + assert_equal name, 'José João' + end + if ldap_configured? should 'return the user attributes' do auth = LdapAuthentication.new(@ldap_config['server']) -- libgit2 0.21.2