官术网_书友最值得收藏!

Controller hooks

Controller hooks allow custom functionality to be injected into an existing process. A normal use-case for this type of hook is to perform some custom validation on the context object provided to the callback.

In /path/to/redmine/app/models/issue.rb, there is a hook registered for controller_issues_edit_before_save. In order to take advantage of this hook, we would have to provide our own callback function. This can be done as follows:

module Knowledgebase
  module Hooks
    class ControllerIssuesEditBeforeSaveHook < Redmine::Hook::ViewListener
      def controller_issues_edit_before_save(context={})
        if context[:params] && context[:params][:issue]
          if User.current.allowed_to?(:assign_article_to_issue, context[:issue].project)
            if context[:params][:issue][:article_id].present?
              article = KbArticle.find_by_id(context[:params][:issue][:article_id])
              if article.category.project == context[:issue].project
                context[:issue].article = article
              end
            else
              context[:issue].article = nil
            end
          end
        end
        return ''
      end
    end
  end
end

Once registered, this hook will check to see whether the current user has permission to attach a knowledgebase article to an issue before saving the issue.

主站蜘蛛池模板: 贵南县| 溧水县| 鱼台县| 蕲春县| 当涂县| 长泰县| 伊春市| 河曲县| 呼伦贝尔市| 聊城市| 肥东县| 河南省| 赞皇县| 石阡县| 崇阳县| 北票市| 永济市| 大竹县| 永济市| 眉山市| 正阳县| 木兰县| 恩平市| 泾川县| 元氏县| 丰县| 西乡县| 东源县| 黎平县| 信丰县| 横峰县| 得荣县| 志丹县| 广东省| 丘北县| 天长市| 曲沃县| 孝昌县| 禄丰县| 乐安县| 蒲江县|