Wrapping class method via alias_method_chain in the plugin for Redmine

I'm not sure if this issue is a general Rails issue or Redmine specific.

There is a User class that has a try_to_login class method. I wrote a module containing the method_alias_chain method to wrap this method and provide additional functionality. This works fine if I log into the console and call try_to_login. My wrapper will be done and everything will be fine. However, when I run this on the server, only the vanilla method is called. The wrapper never starts. I added the logger command to the vanilla method to make sure it is 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 loading the plugin.

+3
1

? , , alias_method_chain.

, ( ):

require 'dispatcher'

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

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

+3

Source: https://habr.com/ru/post/1733112/


All Articles