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

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.

主站蜘蛛池模板: 商水县| 招远市| 阆中市| 新营市| 长沙市| 卓资县| 中江县| 河津市| 济宁市| 策勒县| 盱眙县| 屏南县| 明溪县| 益阳市| 南木林县| 城口县| 五家渠市| 桐庐县| 甘洛县| 宝清县| 沙河市| 文化| 宁蒗| 横山县| 积石山| 天全县| 白朗县| 柳林县| 维西| 内乡县| 大庆市| 晋城| 思南县| 宁陵县| 育儿| 景泰县| 中山市| 沽源县| 宜州市| 萍乡市| 宜章县|