Skip to content

Commit 5078d82

Browse files
committed
Fix compatibility with redmine 3.2
Refs: thorin#166
1 parent ba84cac commit 5078d82

File tree

6 files changed

+66
-11
lines changed

6 files changed

+66
-11
lines changed

.travis.yml

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
---
22
language: ruby
3+
sudo: false
4+
addons:
5+
apt:
6+
packages:
7+
- ldap-utils
8+
- slapd
39
timeouts:
410
bundle: 600
511
rvm:
@@ -41,7 +47,6 @@ matrix:
4147
- env: REDMINE=v3.8.0
4248
gemfile: workspace/redmine/Gemfile
4349
before_install:
44-
- sudo apt-get update -qq && sudo apt-get install -qq slapd ldap-utils
4550
- ./script/ci.sh clone_redmine --target $REDMINE_DIR
4651
- cp ./config/database.yml.travis $REDMINE_DIR/config/database.yml
4752
- ./script/ci.sh install_plugin_gemfile

app/models/ldap_setting.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ class LdapSetting
6868

6969
[:login, *User::STANDARD_FIELDS].each {|f| module_eval("def #{f}; auth_source_ldap.attr_#{f}; end") }
7070

71+
LdapError = (Gem.loaded_specs['net-ldap'].version.to_s >= '0.12.0' ? Net::LDAP::Error : Net::LDAP::LdapError)
72+
7173
def id
7274
@auth_source_ldap_id
7375
end
@@ -286,7 +288,7 @@ def validate_groupname_pattern
286288

287289
def validate_group_filter
288290
Net::LDAP::Filter.construct(group_search_filter) if group_search_filter.present?
289-
rescue Net::LDAP::LdapError
291+
rescue LdapError
290292
errors.add :group_search_filter, :invalid
291293
end
292294

lib/ldap_sync/core_ext/ber.rb

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
if ('0.12.0'..'0.13.0') === Gem.loaded_specs['net-ldap'].version.to_s
2+
##
3+
# A String object with a BER identifier attached.
4+
#
5+
class Net::BER::BerIdentifiedString < String
6+
attr_accessor :ber_identifier
7+
8+
# The binary data provided when parsing the result of the LDAP search
9+
# has the encoding 'ASCII-8BIT' (which is basically 'BINARY', or 'unknown').
10+
#
11+
# This is the kind of a backtrace showing how the binary `data` comes to
12+
# BerIdentifiedString.new(data):
13+
#
14+
# @conn.read_ber(syntax)
15+
# -> StringIO.new(self).read_ber(syntax), i.e. included from module
16+
# -> Net::BER::BERParser.read_ber(syntax)
17+
# -> (private)Net::BER::BERParser.parse_ber_object(syntax, id, data)
18+
#
19+
# In the `#parse_ber_object` method `data`, according to its OID, is being
20+
# 'casted' to one of the Net::BER:BerIdentifiedXXX classes.
21+
#
22+
# As we are using LDAP v3 we can safely assume that the data is encoded
23+
# in UTF-8 and therefore the only thing to be done when instantiating is to
24+
# switch the encoding from 'ASCII-8BIT' to 'UTF-8'.
25+
#
26+
# Unfortunately, there are some ActiveDirectory specific attributes
27+
# (like `objectguid`) that should remain binary (do they really?).
28+
# Using the `#valid_encoding?` we can trap this cases. Special cases like
29+
# Japanese, Korean, etc. encodings might also profit from this. However
30+
# I have no clue how this encodings function.
31+
def initialize args
32+
super
33+
#
34+
# Check the encoding of the newly created String and set the encoding
35+
# to 'UTF-8' (NOTE: we do NOT change the bytes, but only set the
36+
# encoding to 'UTF-8').
37+
current_encoding = encoding
38+
if current_encoding == Encoding::BINARY
39+
force_encoding('UTF-8')
40+
force_encoding(current_encoding) unless valid_encoding?
41+
end
42+
end
43+
end
44+
end

lib/ldap_sync/core_ext/string.rb

+7-5
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@
1818
require 'net/ldap'
1919

2020
module Net::BER::Extensions::String
21-
def raw_utf8_encoded
22-
if self.respond_to?(:encode) && self.encoding.name != 'ASCII-8BIT'
23-
self.encode('UTF-8').force_encoding('ASCII-8BIT')
24-
else
25-
self
21+
if Gem.loaded_specs['net-ldap'].version.to_s < '0.12.0'
22+
def raw_utf8_encoded
23+
if self.respond_to?(:encode) && self.encoding.name != 'ASCII-8BIT'
24+
self.encode('UTF-8').force_encoding('ASCII-8BIT')
25+
else
26+
self
27+
end
2628
end
2729
end
2830
end

lib/ldap_sync/entity_manager.rb

+4-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ module LdapSync::EntityManager
2121
def connect_as_user?; setting.account.include?('$login'); end
2222

2323
private
24+
LdapError = (Gem.loaded_specs['net-ldap'].version.to_s >= '0.12.0' ? Net::LDAP::Error : Net::LDAP::LdapError)
25+
2426
def get_user_fields(username, user_data=nil, options={})
2527
fields_to_sync = setting.user_fields_to_sync
2628
if options.try(:fetch, :include_required, false)
@@ -314,9 +316,9 @@ def ldap_search(ldap, options, &block)
314316
block = Proc.new {|e| yield e[attrs] } if block_given?
315317
result = ldap.search(options, &block) or fail
316318
result.map {|e| e[attrs] } unless block_given? || result.nil?
317-
rescue
319+
rescue => exception
318320
os = ldap.get_operation_result
319-
raise Net::LDAP::LdapError, "LDAP Error(#{os.code}): #{os.message}"
321+
raise LdapError, "LDAP Error(#{os.code}): #{os.message}"
320322
end
321323

322324
def n(field)

test/unit/auth_source_ldap_test.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -458,8 +458,8 @@ class AuthSourceLdapTest < ActiveSupport::TestCase
458458
assert_equal '[email protected]', @user.mail
459459
assert_equal 'User', @user.firstname
460460
assert_equal 'Misc', @user.lastname
461-
assert_equal nil, @user.custom_field_values[0].value
462-
assert_equal nil, @user.custom_field_values[1].value
461+
assert_equal 'en', @user.custom_field_values[0].value
462+
assert_equal '0', @user.custom_field_values[1].value
463463
end
464464

465465
test "#sync_user should delete groups" do

0 commit comments

Comments
 (0)