Commit f67035139f05c753d1f71973c0f8c1d45608694f

Authored by Aurélio A. Heckert
Committed by Daniela Feitosa
1 parent 2f381962

LDAP Plugin: Convert non utf-8 chars

plugins/ldap/lib/ldap_authentication.rb
@@ -19,6 +19,7 @@ require 'rubygems' @@ -19,6 +19,7 @@ require 'rubygems'
19 require 'iconv' 19 require 'iconv'
20 require 'net/ldap' 20 require 'net/ldap'
21 require 'net/ldap/dn' 21 require 'net/ldap/dn'
  22 +require 'magic'
22 23
23 class LdapAuthentication 24 class LdapAuthentication
24 25
@@ -134,7 +135,10 @@ class LdapAuthentication @@ -134,7 +135,10 @@ class LdapAuthentication
134 135
135 def self.get_attr(entry, attr_name) 136 def self.get_attr(entry, attr_name)
136 if !attr_name.blank? 137 if !attr_name.blank?
137 - entry[attr_name].is_a?(Array) ? entry[attr_name].first : entry[attr_name] 138 + val = entry[attr_name].is_a?(Array) ? entry[attr_name].first : entry[attr_name]
  139 + charset = Magic.guess_string_mime(val).match(/charset=([^\s]+)/)[1]
  140 + val.encode 'utf-8', charset, invalid: :replace, undef: :replace
138 end 141 end
139 end 142 end
  143 +
140 end 144 end
plugins/ldap/test/unit/ldap_authentication_test.rb
  1 +# encoding: UTF-8
1 require File.dirname(__FILE__) + '/../test_helper' 2 require File.dirname(__FILE__) + '/../test_helper'
2 3
3 class LdapAuthenticationTest < ActiveSupport::TestCase 4 class LdapAuthenticationTest < ActiveSupport::TestCase
@@ -6,137 +7,143 @@ class LdapAuthenticationTest &lt; ActiveSupport::TestCase @@ -6,137 +7,143 @@ class LdapAuthenticationTest &lt; ActiveSupport::TestCase
6 @ldap_config = load_ldap_config 7 @ldap_config = load_ldap_config
7 end 8 end
8 9
9 - should "host be nil as default" do 10 + should 'host be nil as default' do
10 ldap = LdapAuthentication.new 11 ldap = LdapAuthentication.new
11 assert_nil ldap.host 12 assert_nil ldap.host
12 end 13 end
13 14
14 - should "create with host passed as parameter" do 15 + should 'create with host passed as parameter' do
15 value = 'http://myhost.com' 16 value = 'http://myhost.com'
16 ldap = LdapAuthentication.new('host' => value) 17 ldap = LdapAuthentication.new('host' => value)
17 assert_equal value, ldap.host 18 assert_equal value, ldap.host
18 end 19 end
19 20
20 - should "port be 389 as default" do 21 + should 'port be 389 as default' do
21 ldap = LdapAuthentication.new 22 ldap = LdapAuthentication.new
22 assert_equal 389, ldap.port 23 assert_equal 389, ldap.port
23 end 24 end
24 25
25 - should "create with port passed as parameter" do 26 + should 'create with port passed as parameter' do
26 value = 555 27 value = 555
27 ldap = LdapAuthentication.new('port' => value) 28 ldap = LdapAuthentication.new('port' => value)
28 assert_equal value, ldap.port 29 assert_equal value, ldap.port
29 end 30 end
30 31
31 - should "account be nil as default" do 32 + should 'account be nil as default' do
32 ldap = LdapAuthentication.new 33 ldap = LdapAuthentication.new
33 assert_nil ldap.account 34 assert_nil ldap.account
34 end 35 end
35 36
36 - should "create with account passed as parameter" do 37 + should 'create with account passed as parameter' do
37 value = 'uid=sector,ou=Service,ou=corp,dc=company,dc=com,dc=br' 38 value = 'uid=sector,ou=Service,ou=corp,dc=company,dc=com,dc=br'
38 ldap = LdapAuthentication.new('account' => value) 39 ldap = LdapAuthentication.new('account' => value)
39 assert_equal value, ldap.account 40 assert_equal value, ldap.account
40 end 41 end
41 42
42 - should "account_password be nil as default" do 43 + should 'account_password be nil as default' do
43 ldap = LdapAuthentication.new 44 ldap = LdapAuthentication.new
44 assert_nil ldap.account_password 45 assert_nil ldap.account_password
45 end 46 end
46 47
47 - should "create with account_password passed as parameter" do 48 + should 'create with account_password passed as parameter' do
48 value = 'password' 49 value = 'password'
49 ldap = LdapAuthentication.new('account_password' => value) 50 ldap = LdapAuthentication.new('account_password' => value)
50 assert_equal value, ldap.account_password 51 assert_equal value, ldap.account_password
51 end 52 end
52 53
53 - should "base_dn be nil as default" do 54 + should 'base_dn be nil as default' do
54 ldap = LdapAuthentication.new 55 ldap = LdapAuthentication.new
55 assert_nil ldap.base_dn 56 assert_nil ldap.base_dn
56 end 57 end
57 58
58 - should "create with base_dn passed as parameter" do 59 + should 'create with base_dn passed as parameter' do
59 value = 'dc=company,dc=com,dc=br' 60 value = 'dc=company,dc=com,dc=br'
60 ldap = LdapAuthentication.new('base_dn' => value) 61 ldap = LdapAuthentication.new('base_dn' => value)
61 assert_equal value, ldap.base_dn 62 assert_equal value, ldap.base_dn
62 end 63 end
63 64
64 - should "attr_login be nil as default" do 65 + should 'attr_login be nil as default' do
65 ldap = LdapAuthentication.new 66 ldap = LdapAuthentication.new
66 assert_nil ldap.attr_login 67 assert_nil ldap.attr_login
67 end 68 end
68 69
69 - should "create with attr_login passed as parameter" do 70 + should 'create with attr_login passed as parameter' do
70 value = 'uid' 71 value = 'uid'
71 ldap = LdapAuthentication.new('attr_login' => value) 72 ldap = LdapAuthentication.new('attr_login' => value)
72 assert_equal value, ldap.attr_login 73 assert_equal value, ldap.attr_login
73 end 74 end
74 75
75 - should "attr_fullname be nil as default" do 76 + should 'attr_fullname be nil as default' do
76 ldap = LdapAuthentication.new 77 ldap = LdapAuthentication.new
77 assert_nil ldap.attr_fullname 78 assert_nil ldap.attr_fullname
78 end 79 end
79 80
80 - should "create with attr_fullname passed as parameter" do 81 + should 'create with attr_fullname passed as parameter' do
81 value = 'Noosfero System' 82 value = 'Noosfero System'
82 ldap = LdapAuthentication.new('attr_fullname' => value) 83 ldap = LdapAuthentication.new('attr_fullname' => value)
83 assert_equal value, ldap.attr_fullname 84 assert_equal value, ldap.attr_fullname
84 end 85 end
85 86
86 - should "attr_mail be nil as default" do 87 + should 'attr_mail be nil as default' do
87 ldap = LdapAuthentication.new 88 ldap = LdapAuthentication.new
88 assert_nil ldap.attr_mail 89 assert_nil ldap.attr_mail
89 end 90 end
90 91
91 - should "create with attr_mail passed as parameter" do 92 + should 'create with attr_mail passed as parameter' do
92 value = 'test@noosfero.com' 93 value = 'test@noosfero.com'
93 ldap = LdapAuthentication.new('attr_mail' => value) 94 ldap = LdapAuthentication.new('attr_mail' => value)
94 assert_equal value, ldap.attr_mail 95 assert_equal value, ldap.attr_mail
95 end 96 end
96 97
97 - should "onthefly_register be false as default" do 98 + should 'onthefly_register be false as default' do
98 ldap = LdapAuthentication.new 99 ldap = LdapAuthentication.new
99 refute ldap.onthefly_register 100 refute ldap.onthefly_register
100 end 101 end
101 102
102 - should "create with onthefly_register passed as parameter" do 103 + should 'create with onthefly_register passed as parameter' do
103 value = true 104 value = true
104 ldap = LdapAuthentication.new('onthefly_register' => value) 105 ldap = LdapAuthentication.new('onthefly_register' => value)
105 assert_equal value, ldap.onthefly_register 106 assert_equal value, ldap.onthefly_register
106 end 107 end
107 108
108 - should "filter be nil as default" do 109 + should 'filter be nil as default' do
109 ldap = LdapAuthentication.new 110 ldap = LdapAuthentication.new
110 assert_nil ldap.filter 111 assert_nil ldap.filter
111 end 112 end
112 113
113 - should "create with filter passed as parameter" do 114 + should 'create with filter passed as parameter' do
114 value = 'test' 115 value = 'test'
115 ldap = LdapAuthentication.new('filter' => value) 116 ldap = LdapAuthentication.new('filter' => value)
116 assert_equal value, ldap.filter 117 assert_equal value, ldap.filter
117 end 118 end
118 119
119 - should "tls be false as default" do 120 + should 'tls be false as default' do
120 ldap = LdapAuthentication.new 121 ldap = LdapAuthentication.new
121 refute ldap.tls 122 refute ldap.tls
122 end 123 end
123 124
124 - should "create with tls passed as parameter" do 125 + should 'create with tls passed as parameter' do
125 value = true 126 value = true
126 ldap = LdapAuthentication.new('tls' => value) 127 ldap = LdapAuthentication.new('tls' => value)
127 assert_equal value, ldap.tls 128 assert_equal value, ldap.tls
128 end 129 end
129 130
130 - should "onthefly_register? return true if onthefly_register is true" do 131 + should 'onthefly_register? return true if onthefly_register is true' do
131 ldap = LdapAuthentication.new('onthefly_register' => true) 132 ldap = LdapAuthentication.new('onthefly_register' => true)
132 assert ldap.onthefly_register? 133 assert ldap.onthefly_register?
133 end 134 end
134 135
135 - should "onthefly_register? return false if onthefly_register is false" do 136 + should 'onthefly_register? return false if onthefly_register is false' do
136 ldap = LdapAuthentication.new('onthefly_register' => false) 137 ldap = LdapAuthentication.new('onthefly_register' => false)
137 refute ldap.onthefly_register? 138 refute ldap.onthefly_register?
138 end 139 end
139 140
  141 + should 'detect and convert non utf-8 charset from ldap' do
  142 + entry = { 'name' => "Jos\xE9 Jo\xE3o" }
  143 + name = LdapAuthentication.get_attr entry, 'name'
  144 + assert_equal name, 'José João'
  145 + end
  146 +
140 if ldap_configured? 147 if ldap_configured?
141 should 'return the user attributes' do 148 should 'return the user attributes' do
142 auth = LdapAuthentication.new(@ldap_config['server']) 149 auth = LdapAuthentication.new(@ldap_config['server'])