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

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.

主站蜘蛛池模板: 晋中市| 乌恰县| 历史| 咸丰县| 蒙山县| 威远县| 彭山县| 长沙市| 观塘区| 略阳县| 惠州市| 邵武市| 永吉县| 洱源县| 松滋市| 哈尔滨市| 凯里市| 怀远县| 榆中县| 玉山县| 图木舒克市| 措美县| 齐齐哈尔市| 长海县| 南木林县| 仙居县| 普兰县| 五寨县| 临清市| 大足县| 澎湖县| 凌海市| 涟水县| 大渡口区| 巴马| 鲁甸县| 镇沅| 喀喇| 公主岭市| 宁津县| 辽阳市|