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.