Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Redmine 5/Rails 6.1 compatibility #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
coverage
tmp
Gemfile
.svn
.directory
19 changes: 19 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

source 'https://rubygems.org' do
gem 'sorted_set'
end
8 changes: 5 additions & 3 deletions init.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@

#RedmineApp::Application.config.after_initialize do
Rails.application.config.to_prepare do
require_dependency 'ldap_sync/core_ext'
require_dependency 'ldap_sync/infectors'
require "#{File.dirname(__FILE__)}/lib/ldap_sync/core_ext"
require "#{File.dirname(__FILE__)}/lib/ldap_sync/infectors"
end

# hooks
require_dependency 'ldap_sync/hooks'
require "#{File.dirname(__FILE__)}/lib/ldap_sync/hooks"

require "#{File.dirname(__FILE__)}/lib/ldap_sync/infectors"
6 changes: 5 additions & 1 deletion lib/ldap_sync/core_ext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,8 @@
#
# You should have received a copy of the GNU General Public License
# along with Redmine LDAP Sync. If not, see <http://www.gnu.org/licenses/>.
Dir[File.dirname(__FILE__) + "/core_ext/*.rb"].each { |file| require(file) }
module LdapSync
class CoreExt
Dir[File.dirname(__FILE__) + "/core_ext/*.rb"].each { |file| require(file) }
end
end
86 changes: 46 additions & 40 deletions lib/ldap_sync/core_ext/ber.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,48 +15,54 @@
#
# You should have received a copy of the GNU General Public License
# along with Redmine LDAP Sync. If not, see <http://www.gnu.org/licenses/>.
if ('0.12.0'..'0.13.0') === Gem.loaded_specs['net-ldap'].version.to_s
require 'net/ber'
module LdapSync
class CoreExt
class Ber
if ('0.12.0'..'0.13.0') === Gem.loaded_specs['net-ldap'].version.to_s
require 'net/ber'

##
# A String object with a BER identifier attached.
#
class Net::BER::BerIdentifiedString < String
attr_accessor :ber_identifier
##
# A String object with a BER identifier attached.
#
class Net::BER::BerIdentifiedString < String
attr_accessor :ber_identifier

# The binary data provided when parsing the result of the LDAP search
# has the encoding 'ASCII-8BIT' (which is basically 'BINARY', or 'unknown').
#
# This is the kind of a backtrace showing how the binary `data` comes to
# BerIdentifiedString.new(data):
#
# @conn.read_ber(syntax)
# -> StringIO.new(self).read_ber(syntax), i.e. included from module
# -> Net::BER::BERParser.read_ber(syntax)
# -> (private)Net::BER::BERParser.parse_ber_object(syntax, id, data)
#
# In the `#parse_ber_object` method `data`, according to its OID, is being
# 'casted' to one of the Net::BER:BerIdentifiedXXX classes.
#
# As we are using LDAP v3 we can safely assume that the data is encoded
# in UTF-8 and therefore the only thing to be done when instantiating is to
# switch the encoding from 'ASCII-8BIT' to 'UTF-8'.
#
# Unfortunately, there are some ActiveDirectory specific attributes
# (like `objectguid`) that should remain binary (do they really?).
# Using the `#valid_encoding?` we can trap this cases. Special cases like
# Japanese, Korean, etc. encodings might also profit from this. However
# I have no clue how this encodings function.
def initialize args
super
#
# Check the encoding of the newly created String and set the encoding
# to 'UTF-8' (NOTE: we do NOT change the bytes, but only set the
# encoding to 'UTF-8').
current_encoding = encoding
if current_encoding == Encoding::BINARY
force_encoding('UTF-8')
force_encoding(current_encoding) unless valid_encoding?
# The binary data provided when parsing the result of the LDAP search
# has the encoding 'ASCII-8BIT' (which is basically 'BINARY', or 'unknown').
#
# This is the kind of a backtrace showing how the binary `data` comes to
# BerIdentifiedString.new(data):
#
# @conn.read_ber(syntax)
# -> StringIO.new(self).read_ber(syntax), i.e. included from module
# -> Net::BER::BERParser.read_ber(syntax)
# -> (private)Net::BER::BERParser.parse_ber_object(syntax, id, data)
#
# In the `#parse_ber_object` method `data`, according to its OID, is being
# 'casted' to one of the Net::BER:BerIdentifiedXXX classes.
#
# As we are using LDAP v3 we can safely assume that the data is encoded
# in UTF-8 and therefore the only thing to be done when instantiating is to
# switch the encoding from 'ASCII-8BIT' to 'UTF-8'.
#
# Unfortunately, there are some ActiveDirectory specific attributes
# (like `objectguid`) that should remain binary (do they really?).
# Using the `#valid_encoding?` we can trap this cases. Special cases like
# Japanese, Korean, etc. encodings might also profit from this. However
# I have no clue how this encodings function.
def initialize args
super
#
# Check the encoding of the newly created String and set the encoding
# to 'UTF-8' (NOTE: we do NOT change the bytes, but only set the
# encoding to 'UTF-8').
current_encoding = encoding
if current_encoding == Encoding::BINARY
force_encoding('UTF-8')
force_encoding(current_encoding) unless valid_encoding?
end
end
end
end
end
end
Expand Down
18 changes: 12 additions & 6 deletions lib/ldap_sync/core_ext/file_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,18 @@
#
# You should have received a copy of the GNU General Public License
# along with Redmine LDAP Sync. If not, see <http://www.gnu.org/licenses/>.
class ActiveSupport::Cache::FileStore
def delete_unless
options = merged_options(options)
search_dir(cache_path) do |path|
key = file_path_key(path)
delete_entry(key, options) unless yield(key)
module LdapSync
class CoreExt
class FileStore
class ActiveSupport::Cache::FileStore
def delete_unless
options = merged_options(options)
search_dir(cache_path) do |path|
key = file_path_key(path)
delete_entry(key, options) unless yield(key)
end
end
end
end
end
end
13 changes: 10 additions & 3 deletions lib/ldap_sync/core_ext/ldap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,15 @@
# along with Redmine LDAP Sync. If not, see <http://www.gnu.org/licenses/>.
require 'net/ldap'

class Net::LDAP
if Gem.loaded_specs['net-ldap'].version < Gem::Version.new('0.12.0')
Error = LdapError
module LdapSync
class CoreExt
class Ldap

class Net::LDAP
if Gem.loaded_specs['net-ldap'].version < Gem::Version.new('0.12.0')
Error = LdapError
end
end
end
end
end
12 changes: 9 additions & 3 deletions lib/ldap_sync/core_ext/ldap_entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,14 @@
# along with Redmine LDAP Sync. If not, see <http://www.gnu.org/licenses/>.
require 'net/ldap'

class Net::LDAP
class Entry
include Enumerable
module LdapSync
class CoreExt
class LdapEntry
class Net::LDAP
class Entry
include Enumerable
end
end
end
end
end
20 changes: 13 additions & 7 deletions lib/ldap_sync/core_ext/migration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,18 @@
#
# You should have received a copy of the GNU General Public License
# along with Redmine LDAP Sync. If not, see <http://www.gnu.org/licenses/>.
class ActiveRecord::Migration
unless defined? self.[]

# Enables the use of versioned migrations on rails < 5
def self.[](version)
self
end
module LdapSync
class CoreExt
class Migration
class ActiveRecord::Migration
unless defined? self.[]

# Enables the use of versioned migrations on rails < 5
def self.[](version)
self
end
end
end
end
end
end
20 changes: 13 additions & 7 deletions lib/ldap_sync/core_ext/string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,19 @@
# along with Redmine LDAP Sync. If not, see <http://www.gnu.org/licenses/>.
require 'net/ldap'

module Net::BER::Extensions::String
if Gem.loaded_specs['net-ldap'].version < Gem::Version.new('0.12.0')
def raw_utf8_encoded
if self.respond_to?(:encode) && self.encoding.name != 'ASCII-8BIT'
self.encode('UTF-8').force_encoding('ASCII-8BIT')
else
self
module LdapSync
class CoreExt
class String
module Net::BER::Extensions::String
if Gem.loaded_specs['net-ldap'].version < Gem::Version.new('0.12.0')
def raw_utf8_encoded
if self.respond_to?(:encode) && self.encoding.name != 'ASCII-8BIT'
self.encode('UTF-8').force_encoding('ASCII-8BIT')
else
self
end
end
end
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions lib/ldap_sync/entity_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def get_user_fields(username, user_data=nil, options={})
end
return {} if user_data.nil?

user_fields = user_data.inject({}) do |fields, (attr, value)|
user_fields = user_data.to_h.inject({}) do |fields, (attr, value)|
f = setting.user_field(attr)
if f && fields_to_sync.include?(f)
fields[f] = value.first unless value.nil? || value.first.blank?
Expand Down Expand Up @@ -103,8 +103,8 @@ def ldap_users
end
end

changes[:enabled].delete(nil)
changes[:locked].delete(nil)
changes[:enabled].delete("")
changes[:locked].delete("")

users_on_local = self.users.active.map {|u| u.login.downcase }
users_on_ldap = changes.values.sum.map(&:downcase)
Expand Down