savoirfairelinux / redmine-add-ldap-user-to-group

Automatically add newly logged-in LDAP user to a specific group.
GNU General Public License v3.0
7 stars 2 forks source link

4.x compatibility #3

Open znac049 opened 5 years ago

znac049 commented 5 years ago

Hello, are there any plans to make this really useful plugin compatible with Redmine 4.x.x?

Thanks

ghost commented 5 years ago

I do not work anymore at SFL so im no longer maintainer of that plugin.

I think that it could be migrated to latest version of Redmine in one or 2 hours only, should not be complicated at all. Feel free to try it.

Have great day !

hlogmans commented 2 years ago

Well, it is a bit more of work... I changed the registration code, that works but the try_to_login_with_add_ldap_user_to_group is never called. If I take a quick look at other plugins, this whole monkey patching thing is nowadays done in another way for the User class. So I switched to another plugin that can auto assign groups based on regex rules. Does not support the authentication source but fits my needs.

def self.included(base)
    base.extend(ClassMethods)
    base.class_eval do
        Rails.logger.info 'Starting ADD LDAP group for Redmine'
        #unloadable not functional anymore on Rails 5
        class << self
            if Rails::VERSION::MAJOR >= 5
              alias_method :try_to_login_without_add_ldap_user_to_group, :try_to_login
              alias_method :try_to_login, :try_to_login_with_add_ldap_user_to_group

            else
              alias_method_chain :try_to_login, :add_ldap_user_to_group
            end  
      end
    end
end
tgoeg commented 2 years ago

Indeed, rails 5 does not support alias_method_chain anymore. However, monkey patching is possible with the super method now, which is much more readable.

I already fixed https://github.com/keeps/redmine_email_notification_content_filter like this.

But somehow it does not work with this plugin.

It should basically look like this:

require_dependency 'user'

module UserPatch
  def try_to_login(*args)
    login, = *args
    already_exists = User.exists?(:login => login)
    user = super(*args)
    return user if (
        already_exists or user.nil? or
        user.auth_source.type != 'AuthSourceLdap'
    )
    settings = Setting.plugin_redmine_add_ldap_user_to_group
    begin
        auth_source = AuthSourceLdap.find(
            settings['ldapsource_id'].to_i
        )
        group = Group.find(settings['group_id'].to_i)
        group.users << user
        user.reload
    rescue ActiveRecord::RecordNotFound
    end
    return user
  end
end

User.send(:prepend, UserPatch)

But somehow I have the same problem, this function never gets called. I am a total ruby noob, so this might be a PEBKAC as well :-)

The mentioned regex matching plugin is the following: https://www.redmine.org/plugins/redmine_auto_assign_group

I'll see whether this works as a replacement for me as well. It basically does it the same way, so I wonder why this does not work here..