views:

111

answers:

1

I am not sure if this problem is a general Rails problem or Redmine specific.

There is a class User which has a class method try_to_login. I wrote a module containing a method_alias_chain to wrap that method and provide additional functionality. This works fine if I go into the console and call try_to_login. My wrapper will be executed and everything is fine. However, when I run this on the server just the vanilla method is called. The wrapper is never touched. I added a logger command to the vanilla method to be sure and it is in deed being called.

Here is a simplified version of the code:

require_dependency 'principal'
require_dependency 'user'
require 'login_attempt_count'

module UserLoginAttemptLimiterPatch

  def self.included(base)
    base.extend ClassMethods
    base.class_eval do
      class << self
        alias_method_chain :try_to_login, :attempt_limit
      end
    end
  end

  module ClassMethods
    def try_to_login_with_attempt_limit(login, password)

      user = try_to_login_without_attempt_limit login, password      

      #stuff here gets called via console but not via browser

      user
    end


    def authentication_failed(login)     
      #important code here
    end     

  end
end

User.send(:include, UserLoginAttemptLimiterPatch)

In addition this module is required when the plugin is loaded.

+1  A: 

How are you requiring the module? If you are running in development mode, the User class could be reloaded after the first request which would clear out your patch and alias_method_chain.

You can get around it by doing the patch inside of a Dispatcher (which runs with every code reload):

require 'dispatcher'

Dispatcher.to_prepare do
  Issue.send(:include, MyMooPatch)
end

Reference: http://theadmin.org/articles/2009/04/13/how-to-modify-core-redmine-classes-from-a-plugin/

Eric Davis
Thank you very much this was very helpful!Looking at your code athttp://github.com/edavis10/redmine_extra_ldap/blob/707df49063c0ffe67c9a8035920a7e2b75b04dd0/init.rband http://github.com/edavis10/redmine_extra_ldap/blob/707df49063c0ffe67c9a8035920a7e2b75b04dd0/lib/redmine_extra_ldap/patches/auth_source_ldap_patch.rbmade solving the problem easy!You rock man!
ajmurmann