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

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.

主站蜘蛛池模板: 时尚| 托克逊县| 三原县| 隆林| 潍坊市| 呈贡县| 洛浦县| 辽宁省| 田阳县| 边坝县| 阿巴嘎旗| 鄱阳县| 沈丘县| 广德县| 临沂市| 兴山县| 沙河市| 青田县| 望奎县| 通州市| 云和县| 西贡区| 出国| 淮阳县| 芜湖县| 聂拉木县| 白河县| 习水县| 普陀区| 陆川县| 吉安市| 荔浦县| 友谊县| 清镇市| 五原县| 英德市| 赣榆县| 揭东县| 丹棱县| 珲春市| 思南县|