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 19 require 'iconv'
20 20 require 'net/ldap'
21 21 require 'net/ldap/dn'
  22 +require 'magic'
22 23  
23 24 class LdapAuthentication
24 25  
... ... @@ -134,7 +135,10 @@ class LdapAuthentication
134 135  
135 136 def self.get_attr(entry, attr_name)
136 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 141 end
139 142 end
  143 +
140 144 end
... ...
plugins/ldap/test/unit/ldap_authentication_test.rb
  1 +# encoding: UTF-8
1 2 require File.dirname(__FILE__) + '/../test_helper'
2 3  
3 4 class LdapAuthenticationTest < ActiveSupport::TestCase
... ... @@ -6,137 +7,143 @@ class LdapAuthenticationTest &lt; ActiveSupport::TestCase
6 7 @ldap_config = load_ldap_config
7 8 end
8 9  
9   - should "host be nil as default" do
  10 + should 'host be nil as default' do
10 11 ldap = LdapAuthentication.new
11 12 assert_nil ldap.host
12 13 end
13 14  
14   - should "create with host passed as parameter" do
  15 + should 'create with host passed as parameter' do
15 16 value = 'http://myhost.com'
16 17 ldap = LdapAuthentication.new('host' => value)
17 18 assert_equal value, ldap.host
18 19 end
19 20  
20   - should "port be 389 as default" do
  21 + should 'port be 389 as default' do
21 22 ldap = LdapAuthentication.new
22 23 assert_equal 389, ldap.port
23 24 end
24 25  
25   - should "create with port passed as parameter" do
  26 + should 'create with port passed as parameter' do
26 27 value = 555
27 28 ldap = LdapAuthentication.new('port' => value)
28 29 assert_equal value, ldap.port
29 30 end
30 31  
31   - should "account be nil as default" do
  32 + should 'account be nil as default' do
32 33 ldap = LdapAuthentication.new
33 34 assert_nil ldap.account
34 35 end
35 36  
36   - should "create with account passed as parameter" do
  37 + should 'create with account passed as parameter' do
37 38 value = 'uid=sector,ou=Service,ou=corp,dc=company,dc=com,dc=br'
38 39 ldap = LdapAuthentication.new('account' => value)
39 40 assert_equal value, ldap.account
40 41 end
41 42  
42   - should "account_password be nil as default" do
  43 + should 'account_password be nil as default' do
43 44 ldap = LdapAuthentication.new
44 45 assert_nil ldap.account_password
45 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 49 value = 'password'
49 50 ldap = LdapAuthentication.new('account_password' => value)
50 51 assert_equal value, ldap.account_password
51 52 end
52 53  
53   - should "base_dn be nil as default" do
  54 + should 'base_dn be nil as default' do
54 55 ldap = LdapAuthentication.new
55 56 assert_nil ldap.base_dn
56 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 60 value = 'dc=company,dc=com,dc=br'
60 61 ldap = LdapAuthentication.new('base_dn' => value)
61 62 assert_equal value, ldap.base_dn
62 63 end
63 64  
64   - should "attr_login be nil as default" do
  65 + should 'attr_login be nil as default' do
65 66 ldap = LdapAuthentication.new
66 67 assert_nil ldap.attr_login
67 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 71 value = 'uid'
71 72 ldap = LdapAuthentication.new('attr_login' => value)
72 73 assert_equal value, ldap.attr_login
73 74 end
74 75  
75   - should "attr_fullname be nil as default" do
  76 + should 'attr_fullname be nil as default' do
76 77 ldap = LdapAuthentication.new
77 78 assert_nil ldap.attr_fullname
78 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 82 value = 'Noosfero System'
82 83 ldap = LdapAuthentication.new('attr_fullname' => value)
83 84 assert_equal value, ldap.attr_fullname
84 85 end
85 86  
86   - should "attr_mail be nil as default" do
  87 + should 'attr_mail be nil as default' do
87 88 ldap = LdapAuthentication.new
88 89 assert_nil ldap.attr_mail
89 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 93 value = 'test@noosfero.com'
93 94 ldap = LdapAuthentication.new('attr_mail' => value)
94 95 assert_equal value, ldap.attr_mail
95 96 end
96 97  
97   - should "onthefly_register be false as default" do
  98 + should 'onthefly_register be false as default' do
98 99 ldap = LdapAuthentication.new
99 100 refute ldap.onthefly_register
100 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 104 value = true
104 105 ldap = LdapAuthentication.new('onthefly_register' => value)
105 106 assert_equal value, ldap.onthefly_register
106 107 end
107 108  
108   - should "filter be nil as default" do
  109 + should 'filter be nil as default' do
109 110 ldap = LdapAuthentication.new
110 111 assert_nil ldap.filter
111 112 end
112 113  
113   - should "create with filter passed as parameter" do
  114 + should 'create with filter passed as parameter' do
114 115 value = 'test'
115 116 ldap = LdapAuthentication.new('filter' => value)
116 117 assert_equal value, ldap.filter
117 118 end
118 119  
119   - should "tls be false as default" do
  120 + should 'tls be false as default' do
120 121 ldap = LdapAuthentication.new
121 122 refute ldap.tls
122 123 end
123 124  
124   - should "create with tls passed as parameter" do
  125 + should 'create with tls passed as parameter' do
125 126 value = true
126 127 ldap = LdapAuthentication.new('tls' => value)
127 128 assert_equal value, ldap.tls
128 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 132 ldap = LdapAuthentication.new('onthefly_register' => true)
132 133 assert ldap.onthefly_register?
133 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 137 ldap = LdapAuthentication.new('onthefly_register' => false)
137 138 refute ldap.onthefly_register?
138 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 147 if ldap_configured?
141 148 should 'return the user attributes' do
142 149 auth = LdapAuthentication.new(@ldap_config['server'])
... ...