Patch IssueQuery to add new QueryColumn on user rights

Added by Quentin Champeau 11 months ago

I want to add a new column to the issue table. It's working fine BUT I now want to add that column only if the user has specifics rights (let's say for now, only check if the user is admin). But, the user is always anonymous therefore not admin even if I am logged. I tried many different ways to path `issue` and `issue_query`.

init.rb

require 'redmine'

Issue.send(:include, IssuePatch)
IssueQuery.send(:include, IssueQueryPatch)

Redmine::Plugin.register :test do
  blablabla
end

issue_patch.rb

require_dependency 'issue'

module IssuePatch
  def self.included(base)
    base.class_eval do
      def test
        return 1
      end
    end
  end
end

issue_query_patch.rb

require_dependency "issue_query" 

module IssueQueryPatch
  def self.included(base)
    base.class_eval do
      # TODO
      if User.current.admin? # How to access current user status ??
        self.available_columns << QueryColumn.new(:test,
                                                  :caption => :Test)
      end
    end
  end
end

Another way, is like this https://stackoverflow.com/questions/64896217/redmine-add-new-querycolumn-on-condition (this is my question).

If you see in the Redmine code, the IssueQuery uses the user current status, but I can't figure out how I can do so myself.

(https://www.redmine.org/boards/3/topics/35615 is very interesting but can't be reproduce because `before_filter` doesn't exist anymore)

Answer
I found a way to do that by forcing the model to hook after by using before_validation

Like :
issue_query_patch.rb

require_dependency "issue_query" 

module IssueQueryPatch
  def self.included(base)
    base.class_eval do
      before_validation :display_test

      def display_test
         # view_test is a permission in init.rb
        if User.current.admin? || User.current.allowed_to?(:view_test, nil, :global => true)

          self.available_columns << QueryColumn.new(:test,
                                                   :caption => :Test)
        end
      end
    end
  end
end